amount_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 TestInt64AmountAsInt64(t *testing.T) {
  18. for _, test := range []struct {
  19. value int64
  20. scale Scale
  21. result int64
  22. ok bool
  23. }{
  24. {100, 0, 100, true},
  25. {100, 1, 1000, true},
  26. {100, -5, 0, false},
  27. {100, 100, 0, false},
  28. } {
  29. r, ok := int64Amount{value: test.value, scale: test.scale}.AsInt64()
  30. if r != test.result {
  31. t.Errorf("%v: unexpected result: %d", test, r)
  32. }
  33. if ok != test.ok {
  34. t.Errorf("%v: unexpected ok: %t", test, ok)
  35. }
  36. }
  37. }
  38. func TestInt64AmountAdd(t *testing.T) {
  39. for _, test := range []struct {
  40. a, b, c int64Amount
  41. ok bool
  42. }{
  43. {int64Amount{value: 100, scale: 1}, int64Amount{value: 10, scale: 2}, int64Amount{value: 200, scale: 1}, true},
  44. {int64Amount{value: 100, scale: 1}, int64Amount{value: 1, scale: 2}, int64Amount{value: 110, scale: 1}, true},
  45. {int64Amount{value: 100, scale: 1}, int64Amount{value: 1, scale: 100}, int64Amount{value: 1, scale: 100}, false},
  46. {int64Amount{value: -5, scale: 2}, int64Amount{value: 50, scale: 1}, int64Amount{value: 0, scale: 1}, true},
  47. {int64Amount{value: -5, scale: 2}, int64Amount{value: 5, scale: 2}, int64Amount{value: 0, scale: 2}, true},
  48. {int64Amount{value: mostPositive, scale: -1}, int64Amount{value: 1, scale: -1}, int64Amount{value: 0, scale: -1}, false},
  49. {int64Amount{value: mostPositive, scale: -1}, int64Amount{value: 0, scale: -1}, int64Amount{value: mostPositive, scale: -1}, true},
  50. {int64Amount{value: mostPositive / 10, scale: 1}, int64Amount{value: 10, scale: 0}, int64Amount{value: mostPositive, scale: -1}, false},
  51. } {
  52. c := test.a
  53. ok := c.Add(test.b)
  54. if ok != test.ok {
  55. t.Errorf("%v: unexpected ok: %t", test, ok)
  56. }
  57. if ok {
  58. if c != test.c {
  59. t.Errorf("%v: unexpected result: %d", test, c)
  60. }
  61. } else {
  62. if c != test.a {
  63. t.Errorf("%v: overflow addition mutated source: %d", test, c)
  64. }
  65. }
  66. // addition is commutative
  67. c = test.b
  68. if ok := c.Add(test.a); ok != test.ok {
  69. t.Errorf("%v: unexpected ok: %t", test, ok)
  70. }
  71. if ok {
  72. if c != test.c {
  73. t.Errorf("%v: unexpected result: %d", test, c)
  74. }
  75. } else {
  76. if c != test.b {
  77. t.Errorf("%v: overflow addition mutated source: %d", test, c)
  78. }
  79. }
  80. }
  81. }
  82. func TestInt64AsCanonicalString(t *testing.T) {
  83. for _, test := range []struct {
  84. value int64
  85. scale Scale
  86. result string
  87. exponent int32
  88. }{
  89. {100, 0, "100", 0},
  90. {100, 1, "1", 3},
  91. {100, -1, "10", 0},
  92. {10800, -10, "1080", -9},
  93. } {
  94. r, exp := int64Amount{value: test.value, scale: test.scale}.AsCanonicalBytes(nil)
  95. if string(r) != test.result {
  96. t.Errorf("%v: unexpected result: %s", test, r)
  97. }
  98. if exp != test.exponent {
  99. t.Errorf("%v: unexpected exponent: %d", test, exp)
  100. }
  101. }
  102. }