github.com/ethersphere/bee/v2@v2.2.0/pkg/shed/vector_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 // TestUint64Vector validates put and get operations 26 // of the Uint64Vector. 27 func TestUint64Vector(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 bins, err := db.NewUint64Vector("bins") 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 got, err := bins.Get(0) 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 bins, err := db.NewUint64Vector("bins") 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 for _, index := range []uint64{0, 1, 2, 5, 100} { 59 var want uint64 = 42 + index 60 err = bins.Put(index, want) 61 if err != nil { 62 t.Fatal(err) 63 } 64 got, err := bins.Get(index) 65 if err != nil { 66 t.Fatal(err) 67 } 68 if got != want { 69 t.Errorf("got %v uint64 %v, want %v", index, got, want) 70 } 71 72 t.Run("overwrite", func(t *testing.T) { 73 var want uint64 = 84 + index 74 err = bins.Put(index, want) 75 if err != nil { 76 t.Fatal(err) 77 } 78 got, err := bins.Get(index) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if got != want { 83 t.Errorf("got %v uint64 %v, want %v", index, got, want) 84 } 85 }) 86 } 87 }) 88 89 t.Run("put in batch", func(t *testing.T) { 90 t.Parallel() 91 92 db := newTestDB(t) 93 bins, err := db.NewUint64Vector("bins") 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 for _, index := range []uint64{0, 1, 2, 3, 5, 10} { 99 batch := new(leveldb.Batch) 100 var want uint64 = 43 + index 101 bins.PutInBatch(batch, index, want) 102 err = db.WriteBatch(batch) 103 if err != nil { 104 t.Fatal(err) 105 } 106 got, err := bins.Get(index) 107 if err != nil { 108 t.Fatal(err) 109 } 110 if got != want { 111 t.Errorf("got %v uint64 %v, want %v", index, got, want) 112 } 113 114 t.Run("overwrite", func(t *testing.T) { 115 batch := new(leveldb.Batch) 116 var want uint64 = 85 + index 117 bins.PutInBatch(batch, index, want) 118 err = db.WriteBatch(batch) 119 if err != nil { 120 t.Fatal(err) 121 } 122 got, err := bins.Get(index) 123 if err != nil { 124 t.Fatal(err) 125 } 126 if got != want { 127 t.Errorf("got %v uint64 %v, want %v", index, got, want) 128 } 129 }) 130 } 131 }) 132 } 133 134 // TestUint64Vector_Inc validates Inc operation 135 // of the Uint64Vector. 136 func TestUint64Vector_Inc(t *testing.T) { 137 t.Parallel() 138 139 db := newTestDB(t) 140 141 bins, err := db.NewUint64Vector("bins") 142 if err != nil { 143 t.Fatal(err) 144 } 145 146 for _, index := range []uint64{0, 1, 2, 3, 5, 10} { 147 var want uint64 = 1 148 got, err := bins.Inc(index) 149 if err != nil { 150 t.Fatal(err) 151 } 152 if got != want { 153 t.Errorf("got %v uint64 %v, want %v", index, got, want) 154 } 155 156 want = 2 157 got, err = bins.Inc(index) 158 if err != nil { 159 t.Fatal(err) 160 } 161 if got != want { 162 t.Errorf("got %v uint64 %v, want %v", index, got, want) 163 } 164 } 165 } 166 167 // TestUint64Vector_IncInBatch validates IncInBatch operation 168 // of the Uint64Vector. 169 func TestUint64Vector_IncInBatch(t *testing.T) { 170 t.Parallel() 171 172 db := newTestDB(t) 173 174 bins, err := db.NewUint64Vector("bins") 175 if err != nil { 176 t.Fatal(err) 177 } 178 179 for _, index := range []uint64{0, 1, 2, 3, 5, 10} { 180 batch := new(leveldb.Batch) 181 var want uint64 = 1 182 got, err := bins.IncInBatch(batch, index) 183 if err != nil { 184 t.Fatal(err) 185 } 186 if got != want { 187 t.Errorf("got %v uint64 %v, want %v", index, got, want) 188 } 189 err = db.WriteBatch(batch) 190 if err != nil { 191 t.Fatal(err) 192 } 193 got, err = bins.Get(index) 194 if err != nil { 195 t.Fatal(err) 196 } 197 if got != want { 198 t.Errorf("got %v uint64 %v, want %v", index, got, want) 199 } 200 201 batch2 := new(leveldb.Batch) 202 want = 2 203 got, err = bins.IncInBatch(batch2, index) 204 if err != nil { 205 t.Fatal(err) 206 } 207 if got != want { 208 t.Errorf("got %v uint64 %v, want %v", index, got, want) 209 } 210 err = db.WriteBatch(batch2) 211 if err != nil { 212 t.Fatal(err) 213 } 214 got, err = bins.Get(index) 215 if err != nil { 216 t.Fatal(err) 217 } 218 if got != want { 219 t.Errorf("got %v uint64 %v, want %v", index, got, want) 220 } 221 } 222 } 223 224 // TestUint64Vector_Dec validates Dec operation 225 // of the Uint64Vector. 226 func TestUint64Vector_Dec(t *testing.T) { 227 t.Parallel() 228 229 db := newTestDB(t) 230 231 bins, err := db.NewUint64Vector("bins") 232 if err != nil { 233 t.Fatal(err) 234 } 235 236 for _, index := range []uint64{0, 1, 2, 3, 5, 10} { 237 // test overflow protection 238 var want uint64 239 got, err := bins.Dec(index) 240 if err != nil { 241 t.Fatal(err) 242 } 243 if got != want { 244 t.Errorf("got %v uint64 %v, want %v", index, got, want) 245 } 246 247 want = 32 + index 248 err = bins.Put(index, want) 249 if err != nil { 250 t.Fatal(err) 251 } 252 253 want = 31 + index 254 got, err = bins.Dec(index) 255 if err != nil { 256 t.Fatal(err) 257 } 258 if got != want { 259 t.Errorf("got %v uint64 %v, want %v", index, got, want) 260 } 261 } 262 } 263 264 // TestUint64Vector_DecInBatch validates DecInBatch operation 265 // of the Uint64Vector. 266 func TestUint64Vector_DecInBatch(t *testing.T) { 267 t.Parallel() 268 269 db := newTestDB(t) 270 271 bins, err := db.NewUint64Vector("bins") 272 if err != nil { 273 t.Fatal(err) 274 } 275 276 for _, index := range []uint64{0, 1, 2, 3, 5, 10} { 277 batch := new(leveldb.Batch) 278 var want uint64 279 got, err := bins.DecInBatch(batch, index) 280 if err != nil { 281 t.Fatal(err) 282 } 283 if got != want { 284 t.Errorf("got %v uint64 %v, want %v", index, got, want) 285 } 286 err = db.WriteBatch(batch) 287 if err != nil { 288 t.Fatal(err) 289 } 290 got, err = bins.Get(index) 291 if err != nil { 292 t.Fatal(err) 293 } 294 if got != want { 295 t.Errorf("got %v uint64 %v, want %v", index, got, want) 296 } 297 298 batch2 := new(leveldb.Batch) 299 want = 42 + index 300 bins.PutInBatch(batch2, index, want) 301 err = db.WriteBatch(batch2) 302 if err != nil { 303 t.Fatal(err) 304 } 305 got, err = bins.Get(index) 306 if err != nil { 307 t.Fatal(err) 308 } 309 if got != want { 310 t.Errorf("got %v uint64 %v, want %v", index, got, want) 311 } 312 313 batch3 := new(leveldb.Batch) 314 want = 41 + index 315 got, err = bins.DecInBatch(batch3, index) 316 if err != nil { 317 t.Fatal(err) 318 } 319 if got != want { 320 t.Errorf("got %v uint64 %v, want %v", index, got, want) 321 } 322 err = db.WriteBatch(batch3) 323 if err != nil { 324 t.Fatal(err) 325 } 326 got, err = bins.Get(index) 327 if err != nil { 328 t.Fatal(err) 329 } 330 if got != want { 331 t.Errorf("got %v uint64 %v, want %v", index, got, want) 332 } 333 } 334 }