memorydb_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package memorydb
  17. import (
  18. "bytes"
  19. "testing"
  20. )
  21. // Tests that key-value iteration on top of a memory database works.
  22. func TestMemoryDBIterator(t *testing.T) {
  23. tests := []struct {
  24. content map[string]string
  25. prefix string
  26. order []string
  27. }{
  28. // Empty databases should be iterable
  29. {map[string]string{}, "", nil},
  30. {map[string]string{}, "non-existent-prefix", nil},
  31. // Single-item databases should be iterable
  32. {map[string]string{"key": "val"}, "", []string{"key"}},
  33. {map[string]string{"key": "val"}, "k", []string{"key"}},
  34. {map[string]string{"key": "val"}, "l", nil},
  35. // Multi-item databases should be fully iterable
  36. {
  37. map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
  38. "",
  39. []string{"k1", "k2", "k3", "k4", "k5"},
  40. },
  41. {
  42. map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
  43. "k",
  44. []string{"k1", "k2", "k3", "k4", "k5"},
  45. },
  46. {
  47. map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
  48. "l",
  49. nil,
  50. },
  51. // Multi-item databases should be prefix-iterable
  52. {
  53. map[string]string{
  54. "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
  55. "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
  56. },
  57. "ka",
  58. []string{"ka1", "ka2", "ka3", "ka4", "ka5"},
  59. },
  60. {
  61. map[string]string{
  62. "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
  63. "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
  64. },
  65. "kc",
  66. nil,
  67. },
  68. }
  69. for i, tt := range tests {
  70. // Create the key-value data store
  71. db := New()
  72. for key, val := range tt.content {
  73. if err := db.Put([]byte(key), []byte(val)); err != nil {
  74. t.Fatalf("test %d: failed to insert item %s:%s into database: %v", i, key, val, err)
  75. }
  76. }
  77. // Iterate over the database with the given configs and verify the results
  78. it, idx := db.NewIteratorWithPrefix([]byte(tt.prefix)), 0
  79. for it.Next() {
  80. if !bytes.Equal(it.Key(), []byte(tt.order[idx])) {
  81. t.Errorf("test %d: item %d: key mismatch: have %s, want %s", i, idx, string(it.Key()), tt.order[idx])
  82. }
  83. if !bytes.Equal(it.Value(), []byte(tt.content[tt.order[idx]])) {
  84. t.Errorf("test %d: item %d: value mismatch: have %s, want %s", i, idx, string(it.Value()), tt.content[tt.order[idx]])
  85. }
  86. idx++
  87. }
  88. if err := it.Error(); err != nil {
  89. t.Errorf("test %d: iteration failed: %v", i, err)
  90. }
  91. if idx != len(tt.order) {
  92. t.Errorf("test %d: iteration terminated prematurely: have %d, want %d", i, idx, len(tt.order))
  93. }
  94. }
  95. }