compare_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package help
  2. import "testing"
  3. func TestGetComparer(t *testing.T) {
  4. tests := []struct {
  5. x interface{}
  6. y interface{}
  7. want int
  8. }{
  9. {100, 500, -1},
  10. {-100, -500, 1},
  11. {256, 256, 0},
  12. {int8(100), int8(-100), 1},
  13. {int8(-100), int8(100), -1},
  14. {int8(100), int8(100), 0},
  15. {int16(100), int16(-100), 1},
  16. {int16(-100), int16(100), -1},
  17. {int16(100), int16(100), 0},
  18. {int32(100), int32(-100), 1},
  19. {int32(-100), int32(100), -1},
  20. {int32(100), int32(100), 0},
  21. {int64(100), int64(-100), 1},
  22. {int64(-100), int64(100), -1},
  23. {int64(100), int64(100), 0},
  24. {uint(100), uint(0), 1},
  25. {uint(0), uint(100), -1},
  26. {uint(100), uint(100), 0},
  27. {uint8(100), uint8(0), 1},
  28. {uint8(0), uint8(100), -1},
  29. {uint8(100), uint8(100), 0},
  30. {uint16(100), uint16(0), 1},
  31. {uint16(0), uint16(100), -1},
  32. {uint16(100), uint16(100), 0},
  33. {uint32(100), uint32(0), 1},
  34. {uint32(0), uint32(100), -1},
  35. {uint32(100), uint32(100), 0},
  36. {uint64(100), uint64(0), 1},
  37. {uint64(0), uint64(100), -1},
  38. {uint64(100), uint64(100), 0},
  39. {float32(5.), float32(1.), 1},
  40. {float32(1.), float32(5.), -1},
  41. {float32(0), float32(0), 0},
  42. {float64(5.), float64(1.), 1},
  43. {float64(1.), float64(5.), -1},
  44. {float64(0), float64(0), 0},
  45. {true, true, 0},
  46. {false, false, 0},
  47. {true, false, 1},
  48. {false, true, -1},
  49. {"foo", "foo", 0},
  50. {"foo", "bar", 1},
  51. {"bar", "foo", -1},
  52. {"FOO", "bar", -1},
  53. {foo{f1: 1}, foo{f1: 5}, -1},
  54. {foo{f1: 5}, foo{f1: 1}, 1},
  55. {foo{f1: 1}, foo{f1: 1}, 0},
  56. }
  57. for _, test := range tests {
  58. if r := getComparer(test.x)(test.x, test.y); r != test.want {
  59. t.Errorf("getComparer(%v)(%v,%v)=%v expected %v", test.x, test.x, test.y, r, test.want)
  60. }
  61. }
  62. }