vector_uint64_test.go 6.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package shed

import (
	"testing"
21 22

	"github.com/syndtr/goleveldb/leveldb"
23 24 25 26 27
)

// TestUint64Vector validates put and get operations
// of the Uint64Vector.
func TestUint64Vector(t *testing.T) {
28
	db := newTestDB(t)
29 30

	bins, err := db.NewUint64Vector("bins")
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	if err != nil {
		t.Fatal(err)
	}

	t.Run("get empty", func(t *testing.T) {
		got, err := bins.Get(0)
		if err != nil {
			t.Fatal(err)
		}
		var want uint64
		if got != want {
			t.Errorf("got uint64 %v, want %v", got, want)
		}
	})

	t.Run("put", func(t *testing.T) {
		for _, index := range []uint64{0, 1, 2, 5, 100} {
			var want uint64 = 42 + index
			err = bins.Put(index, want)
			if err != nil {
				t.Fatal(err)
			}
			got, err := bins.Get(index)
			if err != nil {
				t.Fatal(err)
			}
			if got != want {
				t.Errorf("got %v uint64 %v, want %v", index, got, want)
			}

			t.Run("overwrite", func(t *testing.T) {
				var want uint64 = 84 + index
				err = bins.Put(index, want)
				if err != nil {
					t.Fatal(err)
				}
				got, err := bins.Get(index)
				if err != nil {
					t.Fatal(err)
				}
				if got != want {
					t.Errorf("got %v uint64 %v, want %v", index, got, want)
				}
			})
		}
	})

	t.Run("put in batch", func(t *testing.T) {
		for _, index := range []uint64{0, 1, 2, 3, 5, 10} {
80
			batch := new(leveldb.Batch)
81
			var want uint64 = 43 + index
82
			bins.PutInBatch(batch, index, want)
83 84 85 86 87 88 89 90 91 92 93 94 95
			err = db.WriteBatch(batch)
			if err != nil {
				t.Fatal(err)
			}
			got, err := bins.Get(index)
			if err != nil {
				t.Fatal(err)
			}
			if got != want {
				t.Errorf("got %v uint64 %v, want %v", index, got, want)
			}

			t.Run("overwrite", func(t *testing.T) {
96
				batch := new(leveldb.Batch)
97
				var want uint64 = 85 + index
98
				bins.PutInBatch(batch, index, want)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
				err = db.WriteBatch(batch)
				if err != nil {
					t.Fatal(err)
				}
				got, err := bins.Get(index)
				if err != nil {
					t.Fatal(err)
				}
				if got != want {
					t.Errorf("got %v uint64 %v, want %v", index, got, want)
				}
			})
		}
	})
}

// TestUint64Vector_Inc validates Inc operation
// of the Uint64Vector.
func TestUint64Vector_Inc(t *testing.T) {
118
	db := newTestDB(t)
119 120

	bins, err := db.NewUint64Vector("bins")
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	if err != nil {
		t.Fatal(err)
	}

	for _, index := range []uint64{0, 1, 2, 3, 5, 10} {
		var want uint64 = 1
		got, err := bins.Inc(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}

		want = 2
		got, err = bins.Inc(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
	}
}

// TestUint64Vector_IncInBatch validates IncInBatch operation
// of the Uint64Vector.
func TestUint64Vector_IncInBatch(t *testing.T) {
149
	db := newTestDB(t)
150 151

	bins, err := db.NewUint64Vector("bins")
152 153 154 155 156
	if err != nil {
		t.Fatal(err)
	}

	for _, index := range []uint64{0, 1, 2, 3, 5, 10} {
157
		batch := new(leveldb.Batch)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		var want uint64 = 1
		got, err := bins.IncInBatch(batch, index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
		err = db.WriteBatch(batch)
		if err != nil {
			t.Fatal(err)
		}
		got, err = bins.Get(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}

178
		batch2 := new(leveldb.Batch)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		want = 2
		got, err = bins.IncInBatch(batch2, index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
		err = db.WriteBatch(batch2)
		if err != nil {
			t.Fatal(err)
		}
		got, err = bins.Get(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
	}
}

// TestUint64Vector_Dec validates Dec operation
// of the Uint64Vector.
func TestUint64Vector_Dec(t *testing.T) {
204
	db := newTestDB(t)
205 206

	bins, err := db.NewUint64Vector("bins")
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	if err != nil {
		t.Fatal(err)
	}

	for _, index := range []uint64{0, 1, 2, 3, 5, 10} {
		// test overflow protection
		var want uint64
		got, err := bins.Dec(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}

		want = 32 + index
		err = bins.Put(index, want)
		if err != nil {
			t.Fatal(err)
		}

		want = 31 + index
		got, err = bins.Dec(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
	}
}

// TestUint64Vector_DecInBatch validates DecInBatch operation
// of the Uint64Vector.
func TestUint64Vector_DecInBatch(t *testing.T) {
242
	db := newTestDB(t)
243 244

	bins, err := db.NewUint64Vector("bins")
245 246 247 248 249
	if err != nil {
		t.Fatal(err)
	}

	for _, index := range []uint64{0, 1, 2, 3, 5, 10} {
250
		batch := new(leveldb.Batch)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		var want uint64
		got, err := bins.DecInBatch(batch, index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
		err = db.WriteBatch(batch)
		if err != nil {
			t.Fatal(err)
		}
		got, err = bins.Get(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}

271
		batch2 := new(leveldb.Batch)
272
		want = 42 + index
273
		bins.PutInBatch(batch2, index, want)
274 275 276 277 278 279 280 281 282 283 284 285
		err = db.WriteBatch(batch2)
		if err != nil {
			t.Fatal(err)
		}
		got, err = bins.Get(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}

286
		batch3 := new(leveldb.Batch)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		want = 41 + index
		got, err = bins.DecInBatch(batch3, index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
		err = db.WriteBatch(batch3)
		if err != nil {
			t.Fatal(err)
		}
		got, err = bins.Get(index)
		if err != nil {
			t.Fatal(err)
		}
		if got != want {
			t.Errorf("got %v uint64 %v, want %v", index, got, want)
		}
	}
}