github.com/ethersphere/bee/v2@v2.2.0/pkg/shed/field_uint64_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package shed 18 19 import ( 20 "testing" 21 22 "github.com/syndtr/goleveldb/leveldb" 23 ) 24 25 // TestUint64Field validates put and get operations 26 // of the Uint64Field. 27 func TestUint64Field(t *testing.T) { 28 t.Parallel() 29 30 t.Run("get empty", func(t *testing.T) { 31 t.Parallel() 32 33 db := newTestDB(t) 34 counter, err := db.NewUint64Field("counter") 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 got, err := counter.Get() 40 if err != nil { 41 t.Fatal(err) 42 } 43 var want uint64 44 if got != want { 45 t.Errorf("got uint64 %v, want %v", got, want) 46 } 47 }) 48 49 t.Run("put", func(t *testing.T) { 50 t.Parallel() 51 52 db := newTestDB(t) 53 counter, err := db.NewUint64Field("counter") 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 var want uint64 = 42 59 err = counter.Put(want) 60 if err != nil { 61 t.Fatal(err) 62 } 63 got, err := counter.Get() 64 if err != nil { 65 t.Fatal(err) 66 } 67 if got != want { 68 t.Errorf("got uint64 %v, want %v", got, want) 69 } 70 71 t.Run("overwrite", func(t *testing.T) { 72 var want uint64 = 84 73 err = counter.Put(want) 74 if err != nil { 75 t.Fatal(err) 76 } 77 got, err := counter.Get() 78 if err != nil { 79 t.Fatal(err) 80 } 81 if got != want { 82 t.Errorf("got uint64 %v, want %v", got, want) 83 } 84 }) 85 }) 86 87 t.Run("put in batch", func(t *testing.T) { 88 t.Parallel() 89 90 db := newTestDB(t) 91 counter, err := db.NewUint64Field("counter") 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 batch := new(leveldb.Batch) 97 var want uint64 = 42 98 counter.PutInBatch(batch, want) 99 err = db.WriteBatch(batch) 100 if err != nil { 101 t.Fatal(err) 102 } 103 got, err := counter.Get() 104 if err != nil { 105 t.Fatal(err) 106 } 107 if got != want { 108 t.Errorf("got uint64 %v, want %v", got, want) 109 } 110 111 t.Run("overwrite", func(t *testing.T) { 112 batch := new(leveldb.Batch) 113 var want uint64 = 84 114 counter.PutInBatch(batch, want) 115 err = db.WriteBatch(batch) 116 if err != nil { 117 t.Fatal(err) 118 } 119 got, err := counter.Get() 120 if err != nil { 121 t.Fatal(err) 122 } 123 if got != want { 124 t.Errorf("got uint64 %v, want %v", got, want) 125 } 126 }) 127 }) 128 } 129 130 // TestUint64Field_Inc validates Inc operation 131 // of the Uint64Field. 132 func TestUint64Field_Inc(t *testing.T) { 133 t.Parallel() 134 135 db := newTestDB(t) 136 137 counter, err := db.NewUint64Field("counter") 138 if err != nil { 139 t.Fatal(err) 140 } 141 142 var want uint64 = 1 143 got, err := counter.Inc() 144 if err != nil { 145 t.Fatal(err) 146 } 147 if got != want { 148 t.Errorf("got uint64 %v, want %v", got, want) 149 } 150 151 want = 2 152 got, err = counter.Inc() 153 if err != nil { 154 t.Fatal(err) 155 } 156 if got != want { 157 t.Errorf("got uint64 %v, want %v", got, want) 158 } 159 } 160 161 // TestUint64Field_IncInBatch validates IncInBatch operation 162 // of the Uint64Field. 163 func TestUint64Field_IncInBatch(t *testing.T) { 164 t.Parallel() 165 166 db := newTestDB(t) 167 168 counter, err := db.NewUint64Field("counter") 169 if err != nil { 170 t.Fatal(err) 171 } 172 173 batch := new(leveldb.Batch) 174 var want uint64 = 1 175 got, err := counter.IncInBatch(batch) 176 if err != nil { 177 t.Fatal(err) 178 } 179 if got != want { 180 t.Errorf("got uint64 %v, want %v", got, want) 181 } 182 err = db.WriteBatch(batch) 183 if err != nil { 184 t.Fatal(err) 185 } 186 got, err = counter.Get() 187 if err != nil { 188 t.Fatal(err) 189 } 190 if got != want { 191 t.Errorf("got uint64 %v, want %v", got, want) 192 } 193 194 batch2 := new(leveldb.Batch) 195 want = 2 196 got, err = counter.IncInBatch(batch2) 197 if err != nil { 198 t.Fatal(err) 199 } 200 if got != want { 201 t.Errorf("got uint64 %v, want %v", got, want) 202 } 203 err = db.WriteBatch(batch2) 204 if err != nil { 205 t.Fatal(err) 206 } 207 got, err = counter.Get() 208 if err != nil { 209 t.Fatal(err) 210 } 211 if got != want { 212 t.Errorf("got uint64 %v, want %v", got, want) 213 } 214 } 215 216 // TestUint64Field_Dec validates Dec operation 217 // of the Uint64Field. 218 func TestUint64Field_Dec(t *testing.T) { 219 t.Parallel() 220 221 db := newTestDB(t) 222 223 counter, err := db.NewUint64Field("counter") 224 if err != nil { 225 t.Fatal(err) 226 } 227 228 // test overflow protection 229 var want uint64 230 got, err := counter.Dec() 231 if err != nil { 232 t.Fatal(err) 233 } 234 if got != want { 235 t.Errorf("got uint64 %v, want %v", got, want) 236 } 237 238 want = 32 239 err = counter.Put(want) 240 if err != nil { 241 t.Fatal(err) 242 } 243 244 want = 31 245 got, err = counter.Dec() 246 if err != nil { 247 t.Fatal(err) 248 } 249 if got != want { 250 t.Errorf("got uint64 %v, want %v", got, want) 251 } 252 } 253 254 // TestUint64Field_DecInBatch validates DecInBatch operation 255 // of the Uint64Field. 256 func TestUint64Field_DecInBatch(t *testing.T) { 257 t.Parallel() 258 259 db := newTestDB(t) 260 261 counter, err := db.NewUint64Field("counter") 262 if err != nil { 263 t.Fatal(err) 264 } 265 266 batch := new(leveldb.Batch) 267 var want uint64 268 got, err := counter.DecInBatch(batch) 269 if err != nil { 270 t.Fatal(err) 271 } 272 if got != want { 273 t.Errorf("got uint64 %v, want %v", got, want) 274 } 275 err = db.WriteBatch(batch) 276 if err != nil { 277 t.Fatal(err) 278 } 279 got, err = counter.Get() 280 if err != nil { 281 t.Fatal(err) 282 } 283 if got != want { 284 t.Errorf("got uint64 %v, want %v", got, want) 285 } 286 287 batch2 := new(leveldb.Batch) 288 want = 42 289 counter.PutInBatch(batch2, want) 290 err = db.WriteBatch(batch2) 291 if err != nil { 292 t.Fatal(err) 293 } 294 got, err = counter.Get() 295 if err != nil { 296 t.Fatal(err) 297 } 298 if got != want { 299 t.Errorf("got uint64 %v, want %v", got, want) 300 } 301 302 batch3 := new(leveldb.Batch) 303 want = 41 304 got, err = counter.DecInBatch(batch3) 305 if err != nil { 306 t.Fatal(err) 307 } 308 if got != want { 309 t.Errorf("got uint64 %v, want %v", got, want) 310 } 311 err = db.WriteBatch(batch3) 312 if err != nil { 313 t.Fatal(err) 314 } 315 got, err = counter.Get() 316 if err != nil { 317 t.Fatal(err) 318 } 319 if got != want { 320 t.Errorf("got uint64 %v, want %v", got, want) 321 } 322 }