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.

54 lines
1.6 KiB

  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. return tmhash.Sum([]byte(str))
  11. }
  12. func TestSimpleMap(t *testing.T) {
  13. {
  14. db := newSimpleMap()
  15. db.Set("key1", strHasher("value1"))
  16. assert.Equal(t, "fa9bc106ffd932d919bee935ceb6cf2b3dd72d8f", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  17. }
  18. {
  19. db := newSimpleMap()
  20. db.Set("key1", strHasher("value2"))
  21. assert.Equal(t, "e00e7dcfe54e9fafef5111e813a587f01ba9c3e8", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  22. }
  23. {
  24. db := newSimpleMap()
  25. db.Set("key1", strHasher("value1"))
  26. db.Set("key2", strHasher("value2"))
  27. assert.Equal(t, "eff12d1c703a1022ab509287c0f196130123d786", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  28. }
  29. {
  30. db := newSimpleMap()
  31. db.Set("key2", strHasher("value2")) // NOTE: out of order
  32. db.Set("key1", strHasher("value1"))
  33. assert.Equal(t, "eff12d1c703a1022ab509287c0f196130123d786", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  34. }
  35. {
  36. db := newSimpleMap()
  37. db.Set("key1", strHasher("value1"))
  38. db.Set("key2", strHasher("value2"))
  39. db.Set("key3", strHasher("value3"))
  40. assert.Equal(t, "b2c62a277c08dbd2ad73ca53cd1d6bfdf5830d26", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  41. }
  42. {
  43. db := newSimpleMap()
  44. db.Set("key2", strHasher("value2")) // NOTE: out of order
  45. db.Set("key1", strHasher("value1"))
  46. db.Set("key3", strHasher("value3"))
  47. assert.Equal(t, "b2c62a277c08dbd2ad73ca53cd1d6bfdf5830d26", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  48. }
  49. }