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.

86 lines
1.1 KiB

  1. package math
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestParseFraction(t *testing.T) {
  7. testCases := []struct {
  8. f string
  9. exp Fraction
  10. err bool
  11. }{
  12. {
  13. f: "2/3",
  14. exp: Fraction{2, 3},
  15. err: false,
  16. },
  17. {
  18. f: "15/5",
  19. exp: Fraction{15, 5},
  20. err: false,
  21. },
  22. // test divide by zero error
  23. {
  24. f: "2/0",
  25. exp: Fraction{},
  26. err: true,
  27. },
  28. // test negative
  29. {
  30. f: "-1/2",
  31. exp: Fraction{},
  32. err: true,
  33. },
  34. {
  35. f: "1/-2",
  36. exp: Fraction{},
  37. err: true,
  38. },
  39. // test overflow
  40. {
  41. f: "9223372036854775808/2",
  42. exp: Fraction{},
  43. err: true,
  44. },
  45. {
  46. f: "2/9223372036854775808",
  47. exp: Fraction{},
  48. err: true,
  49. },
  50. {
  51. f: "2/3/4",
  52. exp: Fraction{},
  53. err: true,
  54. },
  55. {
  56. f: "123",
  57. exp: Fraction{},
  58. err: true,
  59. },
  60. {
  61. f: "1a2/4",
  62. exp: Fraction{},
  63. err: true,
  64. },
  65. {
  66. f: "1/3bc4",
  67. exp: Fraction{},
  68. err: true,
  69. },
  70. }
  71. for idx, tc := range testCases {
  72. output, err := ParseFraction(tc.f)
  73. if tc.err {
  74. assert.Error(t, err, idx)
  75. } else {
  76. assert.NoError(t, err, idx)
  77. }
  78. assert.Equal(t, tc.exp, output, idx)
  79. }
  80. }