main_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestMorton(t *testing.T) {
  6. var x, y uint16 = 0x0000, 0xffff
  7. var expected uint32 = 0xaaaaaaaa
  8. actual := morton(x, y)
  9. if actual != expected {
  10. t.Errorf("expected %b; got M(%b, %b) = %b", expected, x, y, actual)
  11. }
  12. a, b := demorton(expected)
  13. if a != x || b != y {
  14. t.Errorf("expected (%b, %b); got (%b, %b)", x, y, a, b)
  15. }
  16. }
  17. func TestReverse(t *testing.T) {
  18. var x byte = 0b1101
  19. var expected byte = 0b10110000
  20. actual := reverse(uint32(x))
  21. if actual != expected {
  22. t.Errorf("expected %b; got %b", expected, actual)
  23. }
  24. }
  25. func TestReverse32(t *testing.T) {
  26. var x uint32 = 0xaaaaaaaa
  27. var expected uint32 = 0x55555555
  28. actual := reverse32(x)
  29. if actual != expected {
  30. t.Errorf("expected %b; got %b", expected, actual)
  31. }
  32. x = 0x00000001
  33. expected = 0x80000000
  34. actual = reverse32(x)
  35. if actual != expected {
  36. t.Errorf("expected %b; got %b", expected, actual)
  37. }
  38. }
  39. func TestLog2(t *testing.T) {
  40. var x uint32 = 0x80000000
  41. var expected uint32 = 31
  42. actual := log2(x)
  43. if actual != expected {
  44. t.Errorf("expected %d; got %d", expected, actual)
  45. }
  46. x = 3
  47. expected = 1
  48. actual = log2(x)
  49. if actual != expected {
  50. t.Errorf("expected %d; got %d", expected, actual)
  51. }
  52. }
  53. func TestOnes(t *testing.T) {
  54. var x uint32 = 0xaaaaaaaa
  55. var expected uint32 = 16
  56. actual := ones(x)
  57. if actual != expected {
  58. t.Errorf("expected %d; got %d", expected, actual)
  59. }
  60. }
  61. func TestDistance(t *testing.T) {
  62. a := uint32(0b0000000_00000101_01000101_01111001)
  63. b := uint32(0b0000000_00000010_01000111_11011001)
  64. expected := uint32(14)
  65. actual := distance(a, b)
  66. if actual != expected {
  67. t.Errorf("expected %d; got %d", expected, actual)
  68. }
  69. actual = distance(b, a)
  70. if actual != expected {
  71. t.Errorf("expected %d; got %d", expected, actual)
  72. }
  73. }