atom_test.go (6844B)
1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package atom 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestInt32(t *testing.T) { 31 atom := NewInt32(42) 32 33 assert.Equal(t, int32(42), atom.Load(), "Load didn't work.") 34 assert.Equal(t, int32(46), atom.Add(4), "Add didn't work.") 35 assert.Equal(t, int32(44), atom.Sub(2), "Sub didn't work.") 36 assert.Equal(t, int32(45), atom.Inc(), "Inc didn't work.") 37 assert.Equal(t, int32(44), atom.Dec(), "Dec didn't work.") 38 39 assert.True(t, atom.CAS(44, 0), "CAS didn't report a swap.") 40 assert.Equal(t, int32(0), atom.Load(), "CAS didn't set the correct value.") 41 42 assert.Equal(t, int32(0), atom.Swap(1), "Swap didn't return the old value.") 43 assert.Equal(t, int32(1), atom.Load(), "Swap didn't set the correct value.") 44 45 atom.Store(42) 46 assert.Equal(t, int32(42), atom.Load(), "Store didn't set the correct value.") 47 } 48 49 func TestInt64(t *testing.T) { 50 atom := NewInt64(42) 51 52 assert.Equal(t, int64(42), atom.Load(), "Load didn't work.") 53 assert.Equal(t, int64(46), atom.Add(4), "Add didn't work.") 54 assert.Equal(t, int64(44), atom.Sub(2), "Sub didn't work.") 55 assert.Equal(t, int64(45), atom.Inc(), "Inc didn't work.") 56 assert.Equal(t, int64(44), atom.Dec(), "Dec didn't work.") 57 58 assert.True(t, atom.CAS(44, 0), "CAS didn't report a swap.") 59 assert.Equal(t, int64(0), atom.Load(), "CAS didn't set the correct value.") 60 61 assert.Equal(t, int64(0), atom.Swap(1), "Swap didn't return the old value.") 62 assert.Equal(t, int64(1), atom.Load(), "Swap didn't set the correct value.") 63 64 atom.Store(42) 65 assert.Equal(t, int64(42), atom.Load(), "Store didn't set the correct value.") 66 } 67 68 func TestUint32(t *testing.T) { 69 atom := NewUint32(42) 70 71 assert.Equal(t, uint32(42), atom.Load(), "Load didn't work.") 72 assert.Equal(t, uint32(46), atom.Add(4), "Add didn't work.") 73 assert.Equal(t, uint32(44), atom.Sub(2), "Sub didn't work.") 74 assert.Equal(t, uint32(45), atom.Inc(), "Inc didn't work.") 75 assert.Equal(t, uint32(44), atom.Dec(), "Dec didn't work.") 76 77 assert.True(t, atom.CAS(44, 0), "CAS didn't report a swap.") 78 assert.Equal(t, uint32(0), atom.Load(), "CAS didn't set the correct value.") 79 80 assert.Equal(t, uint32(0), atom.Swap(1), "Swap didn't return the old value.") 81 assert.Equal(t, uint32(1), atom.Load(), "Swap didn't set the correct value.") 82 83 atom.Store(42) 84 assert.Equal(t, uint32(42), atom.Load(), "Store didn't set the correct value.") 85 } 86 87 func TestUint64(t *testing.T) { 88 atom := NewUint64(42) 89 90 assert.Equal(t, uint64(42), atom.Load(), "Load didn't work.") 91 assert.Equal(t, uint64(46), atom.Add(4), "Add didn't work.") 92 assert.Equal(t, uint64(44), atom.Sub(2), "Sub didn't work.") 93 assert.Equal(t, uint64(45), atom.Inc(), "Inc didn't work.") 94 assert.Equal(t, uint64(44), atom.Dec(), "Dec didn't work.") 95 96 assert.True(t, atom.CAS(44, 0), "CAS didn't report a swap.") 97 assert.Equal(t, uint64(0), atom.Load(), "CAS didn't set the correct value.") 98 99 assert.Equal(t, uint64(0), atom.Swap(1), "Swap didn't return the old value.") 100 assert.Equal(t, uint64(1), atom.Load(), "Swap didn't set the correct value.") 101 102 atom.Store(42) 103 assert.Equal(t, uint64(42), atom.Load(), "Store didn't set the correct value.") 104 } 105 106 func TestBool(t *testing.T) { 107 atom := NewBool(false) 108 assert.False(t, atom.Toggle(), "Expected Toggle to return previous value.") 109 assert.True(t, atom.Toggle(), "Expected Toggle to return previous value.") 110 assert.False(t, atom.Toggle(), "Expected Toggle to return previous value.") 111 assert.True(t, atom.Load(), "Unexpected state after swap.") 112 113 assert.True(t, atom.CAS(true, true), "CAS should swap when old matches") 114 assert.True(t, atom.Load(), "CAS should have no effect") 115 assert.True(t, atom.CAS(true, false), "CAS should swap when old matches") 116 assert.False(t, atom.Load(), "CAS should have modified the value") 117 assert.False(t, atom.CAS(true, false), "CAS should fail on old mismatch") 118 assert.False(t, atom.Load(), "CAS should not have modified the value") 119 120 atom.Store(false) 121 assert.False(t, atom.Load(), "Unexpected state after store.") 122 123 prev := atom.Swap(false) 124 assert.False(t, prev, "Expected Swap to return previous value.") 125 126 prev = atom.Swap(true) 127 assert.False(t, prev, "Expected Swap to return previous value.") 128 } 129 130 func TestFloat64(t *testing.T) { 131 atom := NewFloat64(4.2) 132 133 assert.Equal(t, float64(4.2), atom.Load(), "Load didn't work.") 134 135 assert.True(t, atom.CAS(4.2, 0.5), "CAS didn't report a swap.") 136 assert.Equal(t, float64(0.5), atom.Load(), "CAS didn't set the correct value.") 137 assert.False(t, atom.CAS(0.0, 1.5), "CAS reported a swap.") 138 139 atom.Store(42.0) 140 assert.Equal(t, float64(42.0), atom.Load(), "Store didn't set the correct value.") 141 assert.Equal(t, float64(42.5), atom.Add(0.5), "Add didn't work.") 142 assert.Equal(t, float64(42.0), atom.Sub(0.5), "Sub didn't work.") 143 } 144 145 func TestDuration(t *testing.T) { 146 atom := NewDuration(5 * time.Minute) 147 148 assert.Equal(t, 5*time.Minute, atom.Load(), "Load didn't work.") 149 assert.Equal(t, 6*time.Minute, atom.Add(time.Minute), "Add didn't work.") 150 assert.Equal(t, 4*time.Minute, atom.Sub(2*time.Minute), "Sub didn't work.") 151 152 assert.True(t, atom.CAS(4*time.Minute, time.Minute), "CAS didn't report a swap.") 153 assert.Equal(t, time.Minute, atom.Load(), "CAS didn't set the correct value.") 154 155 assert.Equal(t, time.Minute, atom.Swap(2*time.Minute), "Swap didn't return the old value.") 156 assert.Equal(t, 2*time.Minute, atom.Load(), "Swap didn't set the correct value.") 157 158 atom.Store(10 * time.Minute) 159 assert.Equal(t, 10*time.Minute, atom.Load(), "Store didn't set the correct value.") 160 } 161 162 func TestValue(t *testing.T) { 163 var v Value 164 assert.Nil(t, v.Load(), "initial Value is not nil") 165 166 v.Store(42) 167 assert.Equal(t, 42, v.Load()) 168 169 v.Store(84) 170 assert.Equal(t, 84, v.Load()) 171 172 assert.Panics(t, func() { v.Store("foo") }) 173 }