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