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.

49 lines
1.3 KiB

  1. package merkle
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestSimpleMap(t *testing.T) {
  8. tests := []struct {
  9. keys []string
  10. values []string // each string gets converted to []byte in test
  11. want string
  12. }{
  13. {[]string{"key1"}, []string{"value1"}, "a44d3cc7daba1a4600b00a2434b30f8b970652169810d6dfa9fb1793a2189324"},
  14. {[]string{"key1"}, []string{"value2"}, "0638e99b3445caec9d95c05e1a3fc1487b4ddec6a952ff337080360b0dcc078c"},
  15. // swap order with 2 keys
  16. {
  17. []string{"key1", "key2"},
  18. []string{"value1", "value2"},
  19. "8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
  20. },
  21. {
  22. []string{"key2", "key1"},
  23. []string{"value2", "value1"},
  24. "8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
  25. },
  26. // swap order with 3 keys
  27. {
  28. []string{"key1", "key2", "key3"},
  29. []string{"value1", "value2", "value3"},
  30. "1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
  31. },
  32. {
  33. []string{"key1", "key3", "key2"},
  34. []string{"value1", "value3", "value2"},
  35. "1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
  36. },
  37. }
  38. for i, tc := range tests {
  39. db := newSimpleMap()
  40. for i := 0; i < len(tc.keys); i++ {
  41. db.Set(tc.keys[i], []byte(tc.values[i]))
  42. }
  43. got := db.Hash()
  44. assert.Equal(t, tc.want, fmt.Sprintf("%x", got), "Hash didn't match on tc %d", i)
  45. }
  46. }