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.

68 lines
890 B

  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. {
  23. f: "-1/2",
  24. exp: Fraction{-1, 2},
  25. err: false,
  26. },
  27. {
  28. f: "1/-2",
  29. exp: Fraction{1, -2},
  30. err: false,
  31. },
  32. {
  33. f: "2/3/4",
  34. exp: Fraction{},
  35. err: true,
  36. },
  37. {
  38. f: "123",
  39. exp: Fraction{},
  40. err: true,
  41. },
  42. {
  43. f: "1a2/4",
  44. exp: Fraction{},
  45. err: true,
  46. },
  47. {
  48. f: "1/3bc4",
  49. exp: Fraction{},
  50. err: true,
  51. },
  52. }
  53. for idx, tc := range testCases {
  54. output, err := ParseFraction(tc.f)
  55. if tc.err {
  56. assert.Error(t, err, idx)
  57. } else {
  58. assert.NoError(t, err, idx)
  59. }
  60. assert.Equal(t, tc.exp, output, idx)
  61. }
  62. }