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.

282 lines
7.2 KiB

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. "testing"
  4. "fmt"
  5. "os"
  6. "bytes"
  7. "math/rand"
  8. "encoding/binary"
  9. "github.com/tendermint/tendermint/db"
  10. "crypto/sha256"
  11. "runtime"
  12. )
  13. func init() {
  14. if urandom, err := os.Open("/dev/urandom"); err != nil {
  15. return
  16. } else {
  17. buf := make([]byte, 8)
  18. if _, err := urandom.Read(buf); err == nil {
  19. buf_reader := bytes.NewReader(buf)
  20. if seed, err := binary.ReadVarint(buf_reader); err == nil {
  21. rand.Seed(seed)
  22. }
  23. }
  24. urandom.Close()
  25. }
  26. }
  27. func TestUnit(t *testing.T) {
  28. // Convenience for a new node
  29. N := func(l, r interface{}) *IAVLNode {
  30. var left, right *IAVLNode
  31. if _, ok := l.(*IAVLNode); ok {
  32. left = l.(*IAVLNode)
  33. } else {
  34. left = NewIAVLNode(Int32(l.(int)), nil)
  35. }
  36. if _, ok := r.(*IAVLNode); ok {
  37. right = r.(*IAVLNode)
  38. } else {
  39. right = NewIAVLNode(Int32(r.(int)), nil)
  40. }
  41. n := &IAVLNode{
  42. key: right.lmd(nil).key,
  43. left: left,
  44. right: right,
  45. }
  46. n.calcHeightAndSize(nil)
  47. n.Hash()
  48. return n
  49. }
  50. // Convenience for simple printing of keys & tree structure
  51. var P func(*IAVLNode) string
  52. P = func(n *IAVLNode) string {
  53. if n.height == 0 {
  54. return fmt.Sprintf("%v", n.key)
  55. } else {
  56. return fmt.Sprintf("(%v %v)", P(n.left), P(n.right))
  57. }
  58. }
  59. expectHash := func(n2 *IAVLNode, hashCount uint64) {
  60. // ensure number of new hash calculations is as expected.
  61. hash, count := n2.Hash()
  62. if count != hashCount {
  63. t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
  64. }
  65. // nuke hashes and reconstruct hash, ensure it's the same.
  66. (&IAVLTree{root:n2}).Traverse(func(node Node) bool {
  67. node.(*IAVLNode).hash = nil
  68. return false
  69. })
  70. // ensure that the new hash after nuking is the same as the old.
  71. newHash, _ := n2.Hash()
  72. if bytes.Compare(hash, newHash) != 0 {
  73. t.Fatalf("Expected hash %v but got %v after nuking", hash, newHash)
  74. }
  75. }
  76. expectPut := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  77. n2, updated := n.put(nil, Int32(i), nil)
  78. // ensure node was added & structure is as expected.
  79. if updated == true || P(n2) != repr {
  80. t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
  81. i, P(n), repr, P(n2), updated)
  82. }
  83. // ensure hash calculation requirements
  84. expectHash(n2, hashCount)
  85. }
  86. expectRemove := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  87. n2, _, value, err := n.remove(nil, Int32(i))
  88. // ensure node was added & structure is as expected.
  89. if value != nil || err != nil || P(n2) != repr {
  90. t.Fatalf("Removing %v from %v:\nExpected %v\nUnexpectedly got %v value:%v err:%v",
  91. i, P(n), repr, P(n2), value, err)
  92. }
  93. // ensure hash calculation requirements
  94. expectHash(n2, hashCount)
  95. }
  96. //////// Test Put cases:
  97. // Case 1:
  98. n1 := N(4, 20)
  99. expectPut(n1, 8, "((4 8) 20)", 3)
  100. expectPut(n1, 25, "(4 (20 25))", 3)
  101. n2 := N(4, N(20, 25))
  102. expectPut(n2, 8, "((4 8) (20 25))", 3)
  103. expectPut(n2, 30, "((4 20) (25 30))", 4)
  104. n3 := N(N(1, 2), 6)
  105. expectPut(n3, 4, "((1 2) (4 6))", 4)
  106. expectPut(n3, 8, "((1 2) (6 8))", 3)
  107. n4 := N(N(1, 2), N(N(5, 6), N(7, 9)))
  108. expectPut(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
  109. expectPut(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
  110. //////// Test Remove cases:
  111. n10 := N(N(1, 2), 3)
  112. expectRemove(n10, 2, "(1 3)", 1)
  113. expectRemove(n10, 3, "(1 2)", 0)
  114. n11 := N(N(N(1, 2), 3), N(4, 5))
  115. expectRemove(n11, 4, "((1 2) (3 5))", 2)
  116. expectRemove(n11, 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(nil)
  125. var err error
  126. var val Value
  127. var updated bool
  128. randomRecord := func() *record {
  129. return &record{ randstr(20), randstr(20) }
  130. }
  131. for i := range records {
  132. r := randomRecord()
  133. records[i] = r
  134. //t.Log("New record", r)
  135. //PrintIAVLNode(tree.root)
  136. updated = tree.Put(r.key, String(""))
  137. if updated {
  138. t.Error("should have not been updated")
  139. }
  140. updated = tree.Put(r.key, r.value)
  141. if !updated {
  142. t.Error("should have been updated")
  143. }
  144. if tree.Size() != uint64(i+1) {
  145. t.Error("size was wrong", tree.Size(), i+1)
  146. }
  147. }
  148. for _, r := range records {
  149. if has := tree.Has(r.key); !has {
  150. t.Error("Missing key", r.key)
  151. }
  152. if has := tree.Has(randstr(12)); has {
  153. t.Error("Table has extra key")
  154. }
  155. if val := tree.Get(r.key); !(val.(String)).Equals(r.value) {
  156. t.Error("wrong value")
  157. }
  158. }
  159. for i, x := range records {
  160. if val, err = tree.Remove(x.key); err != nil {
  161. t.Error(err)
  162. } else if !(val.(String)).Equals(x.value) {
  163. t.Error("wrong value")
  164. }
  165. for _, r := range records[i+1:] {
  166. if has := tree.Has(r.key); !has {
  167. t.Error("Missing key", r.key)
  168. }
  169. if has := tree.Has(randstr(12)); has {
  170. t.Error("Table has extra key")
  171. }
  172. if val := tree.Get(r.key); !(val.(String)).Equals(r.value) {
  173. t.Error("wrong value")
  174. }
  175. }
  176. if tree.Size() != uint64(len(records) - (i+1)) {
  177. t.Error("size was wrong", tree.Size(), (len(records) - (i+1)))
  178. }
  179. }
  180. }
  181. func TestPersistence(t *testing.T) {
  182. db := db.NewMemDB()
  183. // Create some random key value pairs
  184. records := make(map[String]String)
  185. for i:=0; i<10000; i++ {
  186. records[String(randstr(20))] = String(randstr(20))
  187. }
  188. // Construct some tree and save it
  189. t1 := NewIAVLTree(db)
  190. for key, value := range records {
  191. t1.Put(key, value)
  192. }
  193. t1.Save()
  194. hash, _ := t1.Hash()
  195. // Load a tree
  196. t2 := NewIAVLTreeFromHash(db, hash)
  197. for key, value := range records {
  198. t2value := t2.Get(key)
  199. if !t2value.Equals(value) {
  200. t.Fatalf("Invalid value. Expected %v, got %v", value, t2value)
  201. }
  202. }
  203. }
  204. func BenchmarkHash(b *testing.B) {
  205. b.StopTimer()
  206. s := randstr(128)
  207. b.StartTimer()
  208. for i := 0; i < b.N; i++ {
  209. hasher := sha256.New()
  210. hasher.Write([]byte(s))
  211. hasher.Sum(nil)
  212. }
  213. }
  214. func BenchmarkImmutableAvlTree(b *testing.B) {
  215. b.StopTimer()
  216. type record struct {
  217. key String
  218. value String
  219. }
  220. randomRecord := func() *record {
  221. return &record{ randstr(32), randstr(32) }
  222. }
  223. t := NewIAVLTree(nil)
  224. for i:=0; i<1000000; i++ {
  225. r := randomRecord()
  226. t.Put(r.key, r.value)
  227. }
  228. fmt.Println("ok, starting")
  229. runtime.GC()
  230. b.StartTimer()
  231. for i := 0; i < b.N; i++ {
  232. r := randomRecord()
  233. t.Put(r.key, r.value)
  234. t.Remove(r.key)
  235. }
  236. }