convert_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package help
  2. import "testing"
  3. func TestIntConverter(t *testing.T) {
  4. tests := []struct {
  5. input interface{}
  6. want int64
  7. }{
  8. {2, 2},
  9. {int8(-1), -1},
  10. {int16(0), 0},
  11. {int32(10), 10},
  12. {int64(5), 5},
  13. }
  14. for _, test := range tests {
  15. if conv := getIntConverter(test.input); conv(test.input) != test.want {
  16. t.Errorf("IntConverter for %v failed", test.input)
  17. }
  18. }
  19. }
  20. func TestUIntConverter(t *testing.T) {
  21. tests := []struct {
  22. input interface{}
  23. want uint64
  24. }{
  25. {uint(2), 2},
  26. {uint8(1), 1},
  27. {uint16(0), 0},
  28. {uint32(10), 10},
  29. {uint64(5), 5},
  30. }
  31. for _, test := range tests {
  32. if conv := getUIntConverter(test.input); conv(test.input) != test.want {
  33. t.Errorf("UIntConverter for %v failed", test.input)
  34. }
  35. }
  36. }
  37. func TestFloatConverter(t *testing.T) {
  38. tests := []struct {
  39. input interface{}
  40. want float64
  41. }{
  42. {float32(-1), -1},
  43. {float64(0), 0},
  44. }
  45. for _, test := range tests {
  46. if conv := getFloatConverter(test.input); conv(test.input) != test.want {
  47. t.Errorf("FloatConverter for %v failed", test.input)
  48. }
  49. }
  50. }