github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/atomic_test.go (about) 1 package zutil_test 2 3 import ( 4 "math/rand" 5 "sync" 6 "testing" 7 8 "github.com/sohaha/zlsgo" 9 "github.com/sohaha/zlsgo/zutil" 10 ) 11 12 func TestBool(t *testing.T) { 13 tt := zlsgo.NewTest(t) 14 15 isOk := zutil.NewBool(false) 16 tt.EqualTrue(!isOk.Load()) 17 18 isOk.Store(true) 19 tt.EqualTrue(isOk.Load()) 20 21 isOk.Store(false) 22 tt.EqualTrue(!isOk.Load()) 23 24 tt.EqualTrue(isOk.CAS(false, true)) 25 26 tt.EqualTrue(isOk.Load()) 27 28 // the current is true 29 tt.EqualTrue(!isOk.CAS(false, true)) 30 31 tt.EqualTrue(isOk.CAS(true, true)) 32 33 var wg sync.WaitGroup 34 for i := 0; i < 100; i++ { 35 wg.Add(1) 36 go func() { 37 isOk.Toggle() 38 wg.Done() 39 }() 40 } 41 wg.Wait() 42 } 43 44 func TestInt32(t *testing.T) { 45 tt := zlsgo.NewTest(t) 46 47 count := zutil.NewInt32(0) 48 tt.EqualTrue(count.Load() == 0) 49 50 var wg sync.WaitGroup 51 l := rand.Intn(10000) + 10000 52 53 count.Store(100) 54 55 for i := 0; i < l; i++ { 56 wg.Add(1) 57 go func() { 58 count.Add(1) 59 wg.Done() 60 }() 61 62 wg.Add(1) 63 go func() { 64 count.Sub(1) 65 wg.Done() 66 }() 67 } 68 wg.Wait() 69 70 tt.Equal(int32(100), count.Load()) 71 count.Swap(200) 72 tt.Equal(int32(200), count.Load()) 73 count.CAS(200, 300) 74 tt.Equal(int32(300), count.Load()) 75 } 76 77 func TestInt64(t *testing.T) { 78 tt := zlsgo.NewTest(t) 79 80 count := zutil.NewInt64(0) 81 tt.EqualTrue(count.Load() == 0) 82 83 var wg sync.WaitGroup 84 l := rand.Intn(10000) + 10000 85 86 count.Store(100) 87 88 for i := 0; i < l; i++ { 89 wg.Add(1) 90 go func() { 91 count.Add(1) 92 wg.Done() 93 }() 94 95 wg.Add(1) 96 go func() { 97 count.Sub(1) 98 wg.Done() 99 }() 100 } 101 wg.Wait() 102 103 tt.Equal(int64(100), count.Load()) 104 count.Swap(200) 105 tt.Equal(int64(200), count.Load()) 106 count.CAS(200, 300) 107 tt.Equal(int64(300), count.Load()) 108 } 109 110 func TestUint64(t *testing.T) { 111 tt := zlsgo.NewTest(t) 112 113 count := zutil.NewUint64(0) 114 tt.EqualTrue(count.Load() == 0) 115 116 var wg sync.WaitGroup 117 l := rand.Intn(10000) + 10000 118 119 count.Store(100) 120 121 for i := 0; i < l; i++ { 122 wg.Add(1) 123 go func() { 124 count.Add(1) 125 wg.Done() 126 }() 127 128 wg.Add(1) 129 go func() { 130 count.Sub(1) 131 wg.Done() 132 }() 133 } 134 wg.Wait() 135 136 tt.Equal(uint64(100), count.Load()) 137 count.Swap(200) 138 tt.Equal(uint64(200), count.Load()) 139 count.CAS(200, 300) 140 tt.Equal(uint64(300), count.Load()) 141 } 142 143 func TestUint32(t *testing.T) { 144 tt := zlsgo.NewTest(t) 145 146 count := zutil.NewUint32(0) 147 tt.EqualTrue(count.Load() == 0) 148 149 var wg sync.WaitGroup 150 l := rand.Intn(10000) + 10000 151 152 count.Store(100) 153 154 for i := 0; i < l; i++ { 155 wg.Add(1) 156 go func() { 157 count.Add(1) 158 wg.Done() 159 }() 160 161 wg.Add(1) 162 go func() { 163 count.Sub(1) 164 wg.Done() 165 }() 166 } 167 wg.Wait() 168 169 tt.Equal(uint32(100), count.Load()) 170 count.Swap(200) 171 tt.Equal(uint32(200), count.Load()) 172 count.CAS(200, 300) 173 tt.Equal(uint32(300), count.Load()) 174 }