gitee.com/liu-zhao234568/cntest@v1.0.0/core/rawdb/table_test.go (about) 1 // Copyright 2020 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 rawdb 18 19 import ( 20 "bytes" 21 "testing" 22 23 "gitee.com/liu-zhao234568/cntest/ethdb" 24 ) 25 26 func TestTableDatabase(t *testing.T) { testTableDatabase(t, "prefix") } 27 func TestEmptyPrefixTableDatabase(t *testing.T) { testTableDatabase(t, "") } 28 29 type testReplayer struct { 30 puts [][]byte 31 dels [][]byte 32 } 33 34 func (r *testReplayer) Put(key []byte, value []byte) error { 35 r.puts = append(r.puts, key) 36 return nil 37 } 38 39 func (r *testReplayer) Delete(key []byte) error { 40 r.dels = append(r.dels, key) 41 return nil 42 } 43 44 func testTableDatabase(t *testing.T, prefix string) { 45 db := NewTable(NewMemoryDatabase(), prefix) 46 47 var entries = []struct { 48 key []byte 49 value []byte 50 }{ 51 {[]byte{0x01, 0x02}, []byte{0x0a, 0x0b}}, 52 {[]byte{0x03, 0x04}, []byte{0x0c, 0x0d}}, 53 {[]byte{0x05, 0x06}, []byte{0x0e, 0x0f}}, 54 55 {[]byte{0xff, 0xff, 0x01}, []byte{0x1a, 0x1b}}, 56 {[]byte{0xff, 0xff, 0x02}, []byte{0x1c, 0x1d}}, 57 {[]byte{0xff, 0xff, 0x03}, []byte{0x1e, 0x1f}}, 58 } 59 60 // Test Put/Get operation 61 for _, entry := range entries { 62 db.Put(entry.key, entry.value) 63 } 64 for _, entry := range entries { 65 got, err := db.Get(entry.key) 66 if err != nil { 67 t.Fatalf("Failed to get value: %v", err) 68 } 69 if !bytes.Equal(got, entry.value) { 70 t.Fatalf("Value mismatch: want=%v, got=%v", entry.value, got) 71 } 72 } 73 74 // Test batch operation 75 db = NewTable(NewMemoryDatabase(), prefix) 76 batch := db.NewBatch() 77 for _, entry := range entries { 78 batch.Put(entry.key, entry.value) 79 } 80 batch.Write() 81 for _, entry := range entries { 82 got, err := db.Get(entry.key) 83 if err != nil { 84 t.Fatalf("Failed to get value: %v", err) 85 } 86 if !bytes.Equal(got, entry.value) { 87 t.Fatalf("Value mismatch: want=%v, got=%v", entry.value, got) 88 } 89 } 90 91 // Test batch replayer 92 r := &testReplayer{} 93 batch.Replay(r) 94 for index, entry := range entries { 95 got := r.puts[index] 96 if !bytes.Equal(got, entry.key) { 97 t.Fatalf("Key mismatch: want=%v, got=%v", entry.key, got) 98 } 99 } 100 101 check := func(iter ethdb.Iterator, expCount, index int) { 102 count := 0 103 for iter.Next() { 104 key, value := iter.Key(), iter.Value() 105 if !bytes.Equal(key, entries[index].key) { 106 t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key) 107 } 108 if !bytes.Equal(value, entries[index].value) { 109 t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value) 110 } 111 index += 1 112 count++ 113 } 114 if count != expCount { 115 t.Fatalf("Wrong number of elems, exp %d got %d", expCount, count) 116 } 117 iter.Release() 118 } 119 // Test iterators 120 check(db.NewIterator(nil, nil), 6, 0) 121 // Test iterators with prefix 122 check(db.NewIterator([]byte{0xff, 0xff}, nil), 3, 3) 123 // Test iterators with start point 124 check(db.NewIterator(nil, []byte{0xff, 0xff, 0x02}), 2, 4) 125 // Test iterators with prefix and start point 126 check(db.NewIterator([]byte{0xee}, nil), 0, 0) 127 check(db.NewIterator(nil, []byte{0x00}), 6, 0) 128 }