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.

53 lines
1.5 KiB

  1. package merkle
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. type strHasher string
  8. func (str strHasher) Hash() []byte {
  9. return SimpleHashFromBytes([]byte(str))
  10. }
  11. func TestSimpleMap(t *testing.T) {
  12. {
  13. db := NewSimpleMap()
  14. db.Set("key1", strHasher("value1"))
  15. assert.Equal(t, "19618304d1ad2635c4238bce87f72331b22a11a1", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  16. }
  17. {
  18. db := NewSimpleMap()
  19. db.Set("key1", strHasher("value2"))
  20. assert.Equal(t, "51cb96d3d41e1714def72eb4bacc211de9ddf284", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  21. }
  22. {
  23. db := NewSimpleMap()
  24. db.Set("key1", strHasher("value1"))
  25. db.Set("key2", strHasher("value2"))
  26. assert.Equal(t, "58a0a99d5019fdcad4bcf55942e833b2dfab9421", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  27. }
  28. {
  29. db := NewSimpleMap()
  30. db.Set("key2", strHasher("value2")) // NOTE: out of order
  31. db.Set("key1", strHasher("value1"))
  32. assert.Equal(t, "58a0a99d5019fdcad4bcf55942e833b2dfab9421", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  33. }
  34. {
  35. db := NewSimpleMap()
  36. db.Set("key1", strHasher("value1"))
  37. db.Set("key2", strHasher("value2"))
  38. db.Set("key3", strHasher("value3"))
  39. assert.Equal(t, "cb56db3c7993e977f4c2789559ae3e5e468a6e9b", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  40. }
  41. {
  42. db := NewSimpleMap()
  43. db.Set("key2", strHasher("value2")) // NOTE: out of order
  44. db.Set("key1", strHasher("value1"))
  45. db.Set("key3", strHasher("value3"))
  46. assert.Equal(t, "cb56db3c7993e977f4c2789559ae3e5e468a6e9b", fmt.Sprintf("%x", db.Hash()), "Hash didn't match")
  47. }
  48. }