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