util_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package hashmap
  2. import (
  3. "math"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestLog2(t *testing.T) {
  8. var fixtures = map[uintptr]uintptr{
  9. 0: 0,
  10. 1: 0,
  11. 2: 1,
  12. 3: 2,
  13. 4: 2,
  14. 5: 3,
  15. }
  16. for input, result := range fixtures {
  17. output := log2(input)
  18. if output != result {
  19. t.Errorf("Log2 of %d should have been %d but was %d", input, result, output)
  20. }
  21. }
  22. }
  23. func TestKeyHash(t *testing.T) {
  24. type testFixture struct {
  25. input interface{}
  26. output uintptr
  27. }
  28. var fixtures = []testFixture{
  29. {input: "123", output: 9076048966884696828},
  30. {input: []byte("123"), output: 9076048966884696828},
  31. {input: int(1), output: 1754102016959854353},
  32. {input: int8(1), output: 1754102016959854353},
  33. {input: int16(-1), output: 6588593453755867710},
  34. {input: int32(math.MaxInt32), output: 15638166383137924496},
  35. {input: int64(math.MaxInt64), output: 4889285460913276945},
  36. {input: uint(0), output: 9257401834698437112},
  37. {input: uint8(123), output: 2662021623061770816},
  38. {input: uint16(1234), output: 1663804089773015140},
  39. {input: uint32(12345), output: 11667327197262824396},
  40. {input: uint64(123456), output: 9063688366117729139},
  41. {input: uintptr(1234567), output: 14770111569646361914},
  42. }
  43. for _, f := range fixtures {
  44. output := getKeyHash(f.input)
  45. if output != f.output {
  46. t.Errorf("Key hash of %v and type %v should have been %d but was %d", f.input, reflect.TypeOf(f.input), f.output, output)
  47. }
  48. }
  49. }
  50. func TestKeyHashPanic(t *testing.T) {
  51. defer func() {
  52. if r := recover(); r == nil {
  53. t.Error("Invalid type did not panic")
  54. }
  55. }()
  56. _ = getKeyHash(true)
  57. }