github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/atomic/atomic_test.go (about) 1 package atomic 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestBool(t *testing.T) { 10 atom := NewBool(false) 11 assert.False(t, atom.Toggle(), "Expected swap to return False.") 12 assert.True(t, atom.Load(), "Unexpected state after swap. Expected True") 13 14 assert.True(t, atom.CompareAndSwap(true, true), "CAS should swap when old matches") 15 assert.True(t, atom.Load(), "previous swap should have no effect") 16 assert.True(t, atom.CompareAndSwap(true, false), "CAS should swap when old matches") 17 assert.False(t, atom.Load(), "Post swap the value should be true") 18 assert.False(t, atom.CompareAndSwap(true, false), "CAS should fail on old mismatch") 19 assert.False(t, atom.Load(), "CAS should not have modified the value") 20 21 atom.Store(false) 22 assert.False(t, atom.Load(), "Unexpected state after store.") 23 24 prev := atom.Swap(false) 25 assert.False(t, prev, "Expected Swap to return previous value.") 26 27 prev = atom.Swap(true) 28 assert.False(t, prev, "Expected Swap to return previous value.") 29 } 30 31 func TestInt32(t *testing.T) { 32 atom := NewInt32(2) 33 assert.False(t, atom.CompareAndSwap(3, 4), "Expected swap to return False.") 34 assert.Equal(t, int32(2), atom.Load(), "Unexpected state after swap. Expected True") 35 36 assert.True(t, atom.CompareAndSwap(2, 2), "CAS should swap when old matches") 37 assert.Equal(t, int32(2), atom.Load(), "previous swap should have no effect") 38 assert.True(t, atom.CompareAndSwap(2, 4), "CAS should swap when old matches") 39 assert.Equal(t, int32(4), atom.Load(), "Post swap the value should be true") 40 assert.False(t, atom.CompareAndSwap(2, 3), "CAS should fail on old mismatch") 41 assert.Equal(t, int32(4), atom.Load(), "CAS should not have modified the value") 42 43 atom.Store(5) 44 assert.Equal(t, int32(5), atom.Load(), "Unexpected state after store.") 45 } 46 47 func TestUint32(t *testing.T) { 48 atom := NewUint32(2) 49 assert.False(t, atom.CompareAndSwap(3, 4), "Expected swap to return False.") 50 assert.Equal(t, uint32(2), atom.Load(), "Unexpected state after swap. Expected True") 51 52 assert.True(t, atom.CompareAndSwap(2, 2), "CAS should swap when old matches") 53 assert.Equal(t, uint32(2), atom.Load(), "previous swap should have no effect") 54 assert.True(t, atom.CompareAndSwap(2, 4), "CAS should swap when old matches") 55 assert.Equal(t, uint32(4), atom.Load(), "Post swap the value should be true") 56 assert.False(t, atom.CompareAndSwap(2, 3), "CAS should fail on old mismatch") 57 assert.Equal(t, uint32(4), atom.Load(), "CAS should not have modified the value") 58 59 atom.Store(5) 60 assert.Equal(t, uint32(5), atom.Load(), "Unexpected state after store.") 61 }