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.

58 lines
1.5 KiB

  1. package kademlia
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestNewID(t *testing.T) {
  9. // get some IDs
  10. // for i := 0; i < 10; i++ {
  11. // id, err := NewID()
  12. // assert.Nil(t, err)
  13. // fmt.Println(id)
  14. // }
  15. idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  16. assert.Nil(t, err)
  17. assert.Equal(t, "0fd85ddddf15aeec2d5d8b01b013dbca030a18d7", idA.String())
  18. }
  19. func TestIDMarshalers(t *testing.T) {
  20. id, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  21. assert.Nil(t, err)
  22. idStr, err := json.Marshal(id)
  23. assert.Nil(t, err)
  24. assert.Equal(t, "\"0fd85ddddf15aeec2d5d8b01b013dbca030a18d7\"", string(idStr))
  25. fmt.Println("idStr", string(idStr))
  26. var idParsed ID
  27. err = json.Unmarshal(idStr, &idParsed)
  28. assert.Nil(t, err)
  29. assert.Equal(t, id, idParsed)
  30. var idParsed2 ID
  31. err = json.Unmarshal([]byte("\"0fd85ddddf15aeec2d5d8b01b013dbca030a18d7\""), &idParsed2)
  32. assert.Nil(t, err)
  33. assert.Equal(t, id, idParsed2)
  34. }
  35. func TestIDCmp(t *testing.T) {
  36. idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  37. assert.Nil(t, err)
  38. idB, err := IDFromString("c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b")
  39. assert.Nil(t, err)
  40. assert.True(t, !idA.Cmp(idB))
  41. }
  42. func TestIDDistance(t *testing.T) {
  43. idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
  44. assert.Nil(t, err)
  45. idB, err := IDFromString("c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b")
  46. assert.Nil(t, err)
  47. assert.Equal(t, "cb55d68e04fa18e5c0131fd236ce80e8ecc1348c", idA.Distance(idB).String())
  48. }