number_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package validate
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestMax(t *testing.T) {
  7. type TestCase struct {
  8. Input int
  9. N int
  10. Excl bool
  11. Err error
  12. }
  13. testCases := []TestCase{
  14. {Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
  15. {Input: 10, N: 10},
  16. {Input: 10, N: 15},
  17. {Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
  18. }
  19. for n, tc := range testCases {
  20. t.Logf("(%d) Testing %d against maximum of %d", n, tc.Input, tc.N)
  21. f := Max(tc.N, tc.Excl)
  22. err := f(tc.Input)
  23. if !errors.Is(err, tc.Err) {
  24. t.Errorf("Expected error %v, got %v", tc.Err, err)
  25. }
  26. }
  27. }
  28. func TestMaxFloat32(t *testing.T) {
  29. type TestCase struct {
  30. Input float32
  31. N float32
  32. Excl bool
  33. Err error
  34. }
  35. testCases := []TestCase{
  36. {Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
  37. {Input: 10, N: 10},
  38. {Input: 10, N: 15},
  39. {Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
  40. }
  41. for n, tc := range testCases {
  42. t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
  43. f := MaxFloat32(tc.N, tc.Excl)
  44. err := f(tc.Input)
  45. if !errors.Is(err, tc.Err) {
  46. t.Errorf("Expected error %v, got %v", tc.Err, err)
  47. }
  48. }
  49. }
  50. func TestMaxFloat64(t *testing.T) {
  51. type TestCase struct {
  52. Input float64
  53. N float64
  54. Excl bool
  55. Err error
  56. }
  57. testCases := []TestCase{
  58. {Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
  59. {Input: 10, N: 10},
  60. {Input: 10, N: 15},
  61. {Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
  62. }
  63. for n, tc := range testCases {
  64. t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
  65. f := MaxFloat64(tc.N, tc.Excl)
  66. err := f(tc.Input)
  67. if !errors.Is(err, tc.Err) {
  68. t.Errorf("Expected error %v, got %v", tc.Err, err)
  69. }
  70. }
  71. }
  72. func TestMin(t *testing.T) {
  73. type TestCase struct {
  74. Input int
  75. N int
  76. Excl bool
  77. Err error
  78. }
  79. testCases := []TestCase{
  80. {Input: 10, N: 0},
  81. {Input: 10, N: 10},
  82. {Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
  83. {Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
  84. }
  85. for n, tc := range testCases {
  86. t.Logf("(%d) Testing %d against minimum of %d", n, tc.Input, tc.N)
  87. f := Min(tc.N, tc.Excl)
  88. err := f(tc.Input)
  89. if !errors.Is(err, tc.Err) {
  90. t.Errorf("Expected error %v, got %v", tc.Err, err)
  91. }
  92. }
  93. }
  94. func TestMinFloat32(t *testing.T) {
  95. type TestCase struct {
  96. Input float32
  97. N float32
  98. Excl bool
  99. Err error
  100. }
  101. testCases := []TestCase{
  102. {Input: 10, N: 0},
  103. {Input: 10, N: 10},
  104. {Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
  105. {Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
  106. }
  107. for n, tc := range testCases {
  108. t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
  109. f := MinFloat32(tc.N, tc.Excl)
  110. err := f(tc.Input)
  111. if !errors.Is(err, tc.Err) {
  112. t.Errorf("Expected error %v, got %v", tc.Err, err)
  113. }
  114. }
  115. }
  116. func TestMinFloat64(t *testing.T) {
  117. type TestCase struct {
  118. Input float64
  119. N float64
  120. Excl bool
  121. Err error
  122. }
  123. testCases := []TestCase{
  124. {Input: 10, N: 0},
  125. {Input: 10, N: 10},
  126. {Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
  127. {Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
  128. }
  129. for n, tc := range testCases {
  130. t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
  131. f := MinFloat64(tc.N, tc.Excl)
  132. err := f(tc.Input)
  133. if !errors.Is(err, tc.Err) {
  134. t.Errorf("Expected error %v, got %v", tc.Err, err)
  135. }
  136. }
  137. }