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.

56 lines
1.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package merkle
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/go-crypto/tmhash"
  7. )
  8. type strHasher string
  9. func (str strHasher) Hash() []byte {
  10. h := tmhash.New()
  11. h.Write([]byte(str))
  12. return h.Sum(nil)
  13. }
  14. func TestSimpleMap(t *testing.T) {
  15. {
  16. db := newSimpleMap()
  17. db.Set("key1", strHasher("value1"))
  18. assert.Equal(t, "3dafc06a52039d029be57c75c9d16356a4256ef4", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  19. }
  20. {
  21. db := newSimpleMap()
  22. db.Set("key1", strHasher("value2"))
  23. assert.Equal(t, "03eb5cfdff646bc4e80fec844e72fd248a1c6b2c", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  24. }
  25. {
  26. db := newSimpleMap()
  27. db.Set("key1", strHasher("value1"))
  28. db.Set("key2", strHasher("value2"))
  29. assert.Equal(t, "acc3971eab8513171cc90ce8b74f368c38f9657d", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  30. }
  31. {
  32. db := newSimpleMap()
  33. db.Set("key2", strHasher("value2")) // NOTE: out of order
  34. db.Set("key1", strHasher("value1"))
  35. assert.Equal(t, "acc3971eab8513171cc90ce8b74f368c38f9657d", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  36. }
  37. {
  38. db := newSimpleMap()
  39. db.Set("key1", strHasher("value1"))
  40. db.Set("key2", strHasher("value2"))
  41. db.Set("key3", strHasher("value3"))
  42. assert.Equal(t, "0cd117ad14e6cd22edcd9aa0d84d7063b54b862f", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  43. }
  44. {
  45. db := newSimpleMap()
  46. db.Set("key2", strHasher("value2")) // NOTE: out of order
  47. db.Set("key1", strHasher("value1"))
  48. db.Set("key3", strHasher("value3"))
  49. assert.Equal(t, "0cd117ad14e6cd22edcd9aa0d84d7063b54b862f", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  50. }
  51. }