math_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright 2014 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 resource
  14. import (
  15. "testing"
  16. )
  17. func TestDetectOverflowAdd(t *testing.T) {
  18. for _, test := range []struct {
  19. a, b int64
  20. c int64
  21. ok bool
  22. }{
  23. {0, 0, 0, true},
  24. {-1, 1, 0, true},
  25. {0, 1, 1, true},
  26. {2, 2, 4, true},
  27. {2, -2, 0, true},
  28. {-2, -2, -4, true},
  29. {mostNegative, -1, 0, false},
  30. {mostNegative, 1, mostNegative + 1, true},
  31. {mostPositive, -1, mostPositive - 1, true},
  32. {mostPositive, 1, 0, false},
  33. {mostNegative, mostPositive, -1, true},
  34. {mostPositive, mostNegative, -1, true},
  35. {mostPositive, mostPositive, 0, false},
  36. {mostNegative, mostNegative, 0, false},
  37. {-mostPositive, mostNegative, 0, false},
  38. {mostNegative, -mostPositive, 0, false},
  39. {-mostPositive, -mostPositive, 0, false},
  40. } {
  41. c, ok := int64Add(test.a, test.b)
  42. if c != test.c {
  43. t.Errorf("%v: unexpected result: %d", test, c)
  44. }
  45. if ok != test.ok {
  46. t.Errorf("%v: unexpected overflow: %t", test, ok)
  47. }
  48. // addition is commutative
  49. d, ok2 := int64Add(test.b, test.a)
  50. if c != d || ok != ok2 {
  51. t.Errorf("%v: not commutative: %d %t", test, d, ok2)
  52. }
  53. }
  54. }
  55. func TestDetectOverflowMultiply(t *testing.T) {
  56. for _, test := range []struct {
  57. a, b int64
  58. c int64
  59. ok bool
  60. }{
  61. {0, 0, 0, true},
  62. {-1, 1, -1, true},
  63. {-1, -1, 1, true},
  64. {1, 1, 1, true},
  65. {0, 1, 0, true},
  66. {1, 0, 0, true},
  67. {2, 2, 4, true},
  68. {2, -2, -4, true},
  69. {-2, -2, 4, true},
  70. {mostNegative, -1, 0, false},
  71. {mostNegative, 1, mostNegative, true},
  72. {mostPositive, -1, -mostPositive, true},
  73. {mostPositive, 1, mostPositive, true},
  74. {mostNegative, mostPositive, 0, false},
  75. {mostPositive, mostNegative, 0, false},
  76. {mostPositive, mostPositive, 1, false},
  77. {mostNegative, mostNegative, 0, false},
  78. {-mostPositive, mostNegative, 0, false},
  79. {mostNegative, -mostPositive, 0, false},
  80. {-mostPositive, -mostPositive, 1, false},
  81. } {
  82. c, ok := int64Multiply(test.a, test.b)
  83. if c != test.c {
  84. t.Errorf("%v: unexpected result: %d", test, c)
  85. }
  86. if ok != test.ok {
  87. t.Errorf("%v: unexpected overflow: %t", test, ok)
  88. }
  89. // multiplication is commutative
  90. d, ok2 := int64Multiply(test.b, test.a)
  91. if c != d || ok != ok2 {
  92. t.Errorf("%v: not commutative: %d %t", test, d, ok2)
  93. }
  94. }
  95. }
  96. func TestDetectOverflowScale(t *testing.T) {
  97. for _, a := range []int64{0, -1, 1, 10, -10, mostPositive, mostNegative, -mostPositive} {
  98. for _, b := range []int64{1, 2, 10, 100, 1000, mostPositive} {
  99. expect, expectOk := int64Multiply(a, b)
  100. c, ok := int64MultiplyScale(a, b)
  101. if c != expect {
  102. t.Errorf("%d*%d: unexpected result: %d", a, b, c)
  103. }
  104. if ok != expectOk {
  105. t.Errorf("%d*%d: unexpected overflow: %t", a, b, ok)
  106. }
  107. }
  108. for _, test := range []struct {
  109. base int64
  110. fn func(a int64) (int64, bool)
  111. }{
  112. {10, int64MultiplyScale10},
  113. {100, int64MultiplyScale100},
  114. {1000, int64MultiplyScale1000},
  115. } {
  116. expect, expectOk := int64Multiply(a, test.base)
  117. c, ok := test.fn(a)
  118. if c != expect {
  119. t.Errorf("%d*%d: unexpected result: %d", a, test.base, c)
  120. }
  121. if ok != expectOk {
  122. t.Errorf("%d*%d: unexpected overflow: %t", a, test.base, ok)
  123. }
  124. }
  125. }
  126. }
  127. func TestRemoveInt64Factors(t *testing.T) {
  128. for _, test := range []struct {
  129. value int64
  130. max int64
  131. result int64
  132. scale int32
  133. }{
  134. {100, 10, 1, 2},
  135. {100, 10, 1, 2},
  136. {100, 100, 1, 1},
  137. {1, 10, 1, 0},
  138. } {
  139. r, s := removeInt64Factors(test.value, test.max)
  140. if r != test.result {
  141. t.Errorf("%v: unexpected result: %d", test, r)
  142. }
  143. if s != test.scale {
  144. t.Errorf("%v: unexpected scale: %d", test, s)
  145. }
  146. }
  147. }
  148. func TestNegativeScaleInt64(t *testing.T) {
  149. for _, test := range []struct {
  150. base int64
  151. scale Scale
  152. result int64
  153. exact bool
  154. }{
  155. {1234567, 0, 1234567, true},
  156. {1234567, 1, 123457, false},
  157. {1234567, 2, 12346, false},
  158. {1234567, 3, 1235, false},
  159. {1234567, 4, 124, false},
  160. {-1234567, 0, -1234567, true},
  161. {-1234567, 1, -123457, false},
  162. {-1234567, 2, -12346, false},
  163. {-1234567, 3, -1235, false},
  164. {-1234567, 4, -124, false},
  165. {1000, 0, 1000, true},
  166. {1000, 1, 100, true},
  167. {1000, 2, 10, true},
  168. {1000, 3, 1, true},
  169. {1000, 4, 1, false},
  170. {-1000, 0, -1000, true},
  171. {-1000, 1, -100, true},
  172. {-1000, 2, -10, true},
  173. {-1000, 3, -1, true},
  174. {-1000, 4, -1, false},
  175. {0, 0, 0, true},
  176. {0, 1, 0, true},
  177. {0, 2, 0, true},
  178. // negative scale is undefined behavior
  179. {1000, -1, 1000, true},
  180. } {
  181. result, exact := negativeScaleInt64(test.base, test.scale)
  182. if result != test.result {
  183. t.Errorf("%v: unexpected result: %d", test, result)
  184. }
  185. if exact != test.exact {
  186. t.Errorf("%v: unexpected exact: %t", test, exact)
  187. }
  188. }
  189. }