integer_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package integer
  14. import "testing"
  15. func TestIntMax(t *testing.T) {
  16. tests := []struct {
  17. nums []int
  18. expectedMax int
  19. }{
  20. {
  21. nums: []int{-1, 0},
  22. expectedMax: 0,
  23. },
  24. {
  25. nums: []int{-1, -2},
  26. expectedMax: -1,
  27. },
  28. {
  29. nums: []int{0, 1},
  30. expectedMax: 1,
  31. },
  32. {
  33. nums: []int{1, 2},
  34. expectedMax: 2,
  35. },
  36. }
  37. for i, test := range tests {
  38. t.Logf("executing scenario %d", i)
  39. if max := IntMax(test.nums[0], test.nums[1]); max != test.expectedMax {
  40. t.Errorf("expected %v, got %v", test.expectedMax, max)
  41. }
  42. }
  43. }
  44. func TestIntMin(t *testing.T) {
  45. tests := []struct {
  46. nums []int
  47. expectedMin int
  48. }{
  49. {
  50. nums: []int{-1, 0},
  51. expectedMin: -1,
  52. },
  53. {
  54. nums: []int{-1, -2},
  55. expectedMin: -2,
  56. },
  57. {
  58. nums: []int{0, 1},
  59. expectedMin: 0,
  60. },
  61. {
  62. nums: []int{1, 2},
  63. expectedMin: 1,
  64. },
  65. }
  66. for i, test := range tests {
  67. t.Logf("executing scenario %d", i)
  68. if min := IntMin(test.nums[0], test.nums[1]); min != test.expectedMin {
  69. t.Errorf("expected %v, got %v", test.expectedMin, min)
  70. }
  71. }
  72. }
  73. func TestInt32Max(t *testing.T) {
  74. tests := []struct {
  75. nums []int32
  76. expectedMax int32
  77. }{
  78. {
  79. nums: []int32{-1, 0},
  80. expectedMax: 0,
  81. },
  82. {
  83. nums: []int32{-1, -2},
  84. expectedMax: -1,
  85. },
  86. {
  87. nums: []int32{0, 1},
  88. expectedMax: 1,
  89. },
  90. {
  91. nums: []int32{1, 2},
  92. expectedMax: 2,
  93. },
  94. }
  95. for i, test := range tests {
  96. t.Logf("executing scenario %d", i)
  97. if max := Int32Max(test.nums[0], test.nums[1]); max != test.expectedMax {
  98. t.Errorf("expected %v, got %v", test.expectedMax, max)
  99. }
  100. }
  101. }
  102. func TestInt32Min(t *testing.T) {
  103. tests := []struct {
  104. nums []int32
  105. expectedMin int32
  106. }{
  107. {
  108. nums: []int32{-1, 0},
  109. expectedMin: -1,
  110. },
  111. {
  112. nums: []int32{-1, -2},
  113. expectedMin: -2,
  114. },
  115. {
  116. nums: []int32{0, 1},
  117. expectedMin: 0,
  118. },
  119. {
  120. nums: []int32{1, 2},
  121. expectedMin: 1,
  122. },
  123. }
  124. for i, test := range tests {
  125. t.Logf("executing scenario %d", i)
  126. if min := Int32Min(test.nums[0], test.nums[1]); min != test.expectedMin {
  127. t.Errorf("expected %v, got %v", test.expectedMin, min)
  128. }
  129. }
  130. }
  131. func TestInt64Max(t *testing.T) {
  132. tests := []struct {
  133. nums []int64
  134. expectedMax int64
  135. }{
  136. {
  137. nums: []int64{-1, 0},
  138. expectedMax: 0,
  139. },
  140. {
  141. nums: []int64{-1, -2},
  142. expectedMax: -1,
  143. },
  144. {
  145. nums: []int64{0, 1},
  146. expectedMax: 1,
  147. },
  148. {
  149. nums: []int64{1, 2},
  150. expectedMax: 2,
  151. },
  152. }
  153. for i, test := range tests {
  154. t.Logf("executing scenario %d", i)
  155. if max := Int64Max(test.nums[0], test.nums[1]); max != test.expectedMax {
  156. t.Errorf("expected %v, got %v", test.expectedMax, max)
  157. }
  158. }
  159. }
  160. func TestInt64Min(t *testing.T) {
  161. tests := []struct {
  162. nums []int64
  163. expectedMin int64
  164. }{
  165. {
  166. nums: []int64{-1, 0},
  167. expectedMin: -1,
  168. },
  169. {
  170. nums: []int64{-1, -2},
  171. expectedMin: -2,
  172. },
  173. {
  174. nums: []int64{0, 1},
  175. expectedMin: 0,
  176. },
  177. {
  178. nums: []int64{1, 2},
  179. expectedMin: 1,
  180. },
  181. }
  182. for i, test := range tests {
  183. t.Logf("executing scenario %d", i)
  184. if min := Int64Min(test.nums[0], test.nums[1]); min != test.expectedMin {
  185. t.Errorf("expected %v, got %v", test.expectedMin, min)
  186. }
  187. }
  188. }
  189. func TestRoundToInt32(t *testing.T) {
  190. tests := []struct {
  191. num float64
  192. exp int32
  193. }{
  194. {
  195. num: 5.5,
  196. exp: 6,
  197. },
  198. {
  199. num: -3.7,
  200. exp: -4,
  201. },
  202. {
  203. num: 3.49,
  204. exp: 3,
  205. },
  206. {
  207. num: -7.9,
  208. exp: -8,
  209. },
  210. {
  211. num: -4.499999,
  212. exp: -4,
  213. },
  214. {
  215. num: 0,
  216. exp: 0,
  217. },
  218. }
  219. for i, test := range tests {
  220. t.Logf("executing scenario %d", i)
  221. if got := RoundToInt32(test.num); got != test.exp {
  222. t.Errorf("expected %d, got %d", test.exp, got)
  223. }
  224. }
  225. }