table_test.go 3.6 KB

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