You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
5.8 KiB

11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package merkle
  2. import (
  3. "bytes"
  4. "fmt"
  5. . "github.com/tendermint/tendermint/binary"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/db"
  8. "runtime"
  9. "testing"
  10. )
  11. func init() {
  12. // TODO: seed rand?
  13. }
  14. func randstr(length int) string {
  15. return RandStr(length)
  16. }
  17. // Convenience for a new node
  18. func N(l, r interface{}) *IAVLNode {
  19. var left, right *IAVLNode
  20. if _, ok := l.(*IAVLNode); ok {
  21. left = l.(*IAVLNode)
  22. } else {
  23. left = NewIAVLNode(l, "")
  24. }
  25. if _, ok := r.(*IAVLNode); ok {
  26. right = r.(*IAVLNode)
  27. } else {
  28. right = NewIAVLNode(r, "")
  29. }
  30. n := &IAVLNode{
  31. key: right.lmd(nil).key,
  32. value: "",
  33. leftNode: left,
  34. rightNode: right,
  35. }
  36. n.calcHeightAndSize(nil)
  37. return n
  38. }
  39. // Setup a deep node
  40. func T(n *IAVLNode) *IAVLTree {
  41. t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
  42. n.hashWithCount(t)
  43. t.root = n
  44. return t
  45. }
  46. // Convenience for simple printing of keys & tree structure
  47. func P(n *IAVLNode) string {
  48. if n.height == 0 {
  49. return fmt.Sprintf("%v", n.key)
  50. } else {
  51. return fmt.Sprintf("(%v %v)", P(n.leftNode), P(n.rightNode))
  52. }
  53. }
  54. func TestUnit(t *testing.T) {
  55. expectHash := func(tree *IAVLTree, hashCount uint64) {
  56. // ensure number of new hash calculations is as expected.
  57. hash, count := tree.HashWithCount()
  58. if count != hashCount {
  59. t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
  60. }
  61. // nuke hashes and reconstruct hash, ensure it's the same.
  62. tree.root.traverse(tree, func(node *IAVLNode) bool {
  63. node.hash = nil
  64. return false
  65. })
  66. // ensure that the new hash after nuking is the same as the old.
  67. newHash, _ := tree.HashWithCount()
  68. if bytes.Compare(hash, newHash) != 0 {
  69. t.Fatalf("Expected hash %v but got %v after nuking", hash, newHash)
  70. }
  71. }
  72. expectSet := func(tree *IAVLTree, i int, repr string, hashCount uint64) {
  73. origNode := tree.root
  74. updated := tree.Set(i, "")
  75. // ensure node was added & structure is as expected.
  76. if updated == true || P(tree.root) != repr {
  77. t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
  78. i, P(origNode), repr, P(tree.root), updated)
  79. }
  80. // ensure hash calculation requirements
  81. expectHash(tree, hashCount)
  82. tree.root = origNode
  83. }
  84. expectRemove := func(tree *IAVLTree, i int, repr string, hashCount uint64) {
  85. origNode := tree.root
  86. value, removed := tree.Remove(i)
  87. // ensure node was added & structure is as expected.
  88. if value != "" || !removed || P(tree.root) != repr {
  89. t.Fatalf("Removing %v from %v:\nExpected %v\nUnexpectedly got %v value:%v removed:%v",
  90. i, P(origNode), repr, P(tree.root), value, removed)
  91. }
  92. // ensure hash calculation requirements
  93. expectHash(tree, hashCount)
  94. tree.root = origNode
  95. }
  96. //////// Test Set cases:
  97. // Case 1:
  98. t1 := T(N(4, 20))
  99. expectSet(t1, 8, "((4 8) 20)", 3)
  100. expectSet(t1, 25, "(4 (20 25))", 3)
  101. t2 := T(N(4, N(20, 25)))
  102. expectSet(t2, 8, "((4 8) (20 25))", 3)
  103. expectSet(t2, 30, "((4 20) (25 30))", 4)
  104. t3 := T(N(N(1, 2), 6))
  105. expectSet(t3, 4, "((1 2) (4 6))", 4)
  106. expectSet(t3, 8, "((1 2) (6 8))", 3)
  107. t4 := T(N(N(1, 2), N(N(5, 6), N(7, 9))))
  108. expectSet(t4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
  109. expectSet(t4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
  110. //////// Test Remove cases:
  111. t10 := T(N(N(1, 2), 3))
  112. expectRemove(t10, 2, "(1 3)", 1)
  113. expectRemove(t10, 3, "(1 2)", 0)
  114. t11 := T(N(N(N(1, 2), 3), N(4, 5)))
  115. expectRemove(t11, 4, "((1 2) (3 5))", 2)
  116. expectRemove(t11, 3, "((1 2) (4 5))", 1)
  117. }
  118. func TestIntegration(t *testing.T) {
  119. type record struct {
  120. key string
  121. value string
  122. }
  123. records := make([]*record, 400)
  124. var tree *IAVLTree = NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
  125. randomRecord := func() *record {
  126. return &record{randstr(20), randstr(20)}
  127. }
  128. for i := range records {
  129. r := randomRecord()
  130. records[i] = r
  131. //t.Log("New record", r)
  132. //PrintIAVLNode(tree.root)
  133. updated := tree.Set(r.key, "")
  134. if updated {
  135. t.Error("should have not been updated")
  136. }
  137. updated = tree.Set(r.key, r.value)
  138. if !updated {
  139. t.Error("should have been updated")
  140. }
  141. if tree.Size() != uint64(i+1) {
  142. t.Error("size was wrong", tree.Size(), i+1)
  143. }
  144. }
  145. for _, r := range records {
  146. if has := tree.Has(r.key); !has {
  147. t.Error("Missing key", r.key)
  148. }
  149. if has := tree.Has(randstr(12)); has {
  150. t.Error("Table has extra key")
  151. }
  152. if _, val := tree.Get(r.key); val.(string) != r.value {
  153. t.Error("wrong value")
  154. }
  155. }
  156. for i, x := range records {
  157. if val, removed := tree.Remove(x.key); !removed {
  158. t.Error("Wasn't removed")
  159. } else if val != x.value {
  160. t.Error("Wrong value")
  161. }
  162. for _, r := range records[i+1:] {
  163. if has := tree.Has(r.key); !has {
  164. t.Error("Missing key", r.key)
  165. }
  166. if has := tree.Has(randstr(12)); has {
  167. t.Error("Table has extra key")
  168. }
  169. _, val := tree.Get(r.key)
  170. if val != r.value {
  171. t.Error("wrong value")
  172. }
  173. }
  174. if tree.Size() != uint64(len(records)-(i+1)) {
  175. t.Error("size was wrong", tree.Size(), (len(records) - (i + 1)))
  176. }
  177. }
  178. }
  179. func TestPersistence(t *testing.T) {
  180. db := db.NewMemDB()
  181. // Create some random key value pairs
  182. records := make(map[string]string)
  183. for i := 0; i < 10000; i++ {
  184. records[randstr(20)] = randstr(20)
  185. }
  186. // Construct some tree and save it
  187. t1 := NewIAVLTree(BasicCodec, BasicCodec, 0, db)
  188. for key, value := range records {
  189. t1.Set(key, value)
  190. }
  191. t1.Save()
  192. hash, _ := t1.HashWithCount()
  193. // Load a tree
  194. t2 := NewIAVLTree(BasicCodec, BasicCodec, 0, db)
  195. t2.Load(hash)
  196. for key, value := range records {
  197. _, t2value := t2.Get(key)
  198. if t2value != value {
  199. t.Fatalf("Invalid value. Expected %v, got %v", value, t2value)
  200. }
  201. }
  202. }
  203. func BenchmarkImmutableAvlTree(b *testing.B) {
  204. b.StopTimer()
  205. t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
  206. for i := 0; i < 1000000; i++ {
  207. t.Set(RandUInt64(), "")
  208. }
  209. fmt.Println("ok, starting")
  210. runtime.GC()
  211. b.StartTimer()
  212. for i := 0; i < b.N; i++ {
  213. ri := RandUInt64()
  214. t.Set(ri, "")
  215. t.Remove(ri)
  216. }
  217. }