decimal_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package strconv_test
  5. import (
  6. . "strconv"
  7. "testing"
  8. )
  9. type shiftTest struct {
  10. i uint64
  11. shift int
  12. out string
  13. }
  14. var shifttests = []shiftTest{
  15. {0, -100, "0"},
  16. {0, 100, "0"},
  17. {1, 100, "1267650600228229401496703205376"},
  18. {1, -100,
  19. "0.00000000000000000000000000000078886090522101180541" +
  20. "17285652827862296732064351090230047702789306640625",
  21. },
  22. {12345678, 8, "3160493568"},
  23. {12345678, -8, "48225.3046875"},
  24. {195312, 9, "99999744"},
  25. {1953125, 9, "1000000000"},
  26. }
  27. func TestDecimalShift(t *testing.T) {
  28. for i := 0; i < len(shifttests); i++ {
  29. test := &shifttests[i]
  30. d := NewDecimal(test.i)
  31. d.Shift(test.shift)
  32. s := d.String()
  33. if s != test.out {
  34. t.Errorf("Decimal %v << %v = %v, want %v",
  35. test.i, test.shift, s, test.out)
  36. }
  37. }
  38. }
  39. type roundTest struct {
  40. i uint64
  41. nd int
  42. down, round, up string
  43. int uint64
  44. }
  45. var roundtests = []roundTest{
  46. {0, 4, "0", "0", "0", 0},
  47. {12344999, 4, "12340000", "12340000", "12350000", 12340000},
  48. {12345000, 4, "12340000", "12340000", "12350000", 12340000},
  49. {12345001, 4, "12340000", "12350000", "12350000", 12350000},
  50. {23454999, 4, "23450000", "23450000", "23460000", 23450000},
  51. {23455000, 4, "23450000", "23460000", "23460000", 23460000},
  52. {23455001, 4, "23450000", "23460000", "23460000", 23460000},
  53. {99994999, 4, "99990000", "99990000", "100000000", 99990000},
  54. {99995000, 4, "99990000", "100000000", "100000000", 100000000},
  55. {99999999, 4, "99990000", "100000000", "100000000", 100000000},
  56. {12994999, 4, "12990000", "12990000", "13000000", 12990000},
  57. {12995000, 4, "12990000", "13000000", "13000000", 13000000},
  58. {12999999, 4, "12990000", "13000000", "13000000", 13000000},
  59. }
  60. func TestDecimalRound(t *testing.T) {
  61. for i := 0; i < len(roundtests); i++ {
  62. test := &roundtests[i]
  63. d := NewDecimal(test.i)
  64. d.RoundDown(test.nd)
  65. s := d.String()
  66. if s != test.down {
  67. t.Errorf("Decimal %v RoundDown %d = %v, want %v",
  68. test.i, test.nd, s, test.down)
  69. }
  70. d = NewDecimal(test.i)
  71. d.Round(test.nd)
  72. s = d.String()
  73. if s != test.round {
  74. t.Errorf("Decimal %v Round %d = %v, want %v",
  75. test.i, test.nd, s, test.down)
  76. }
  77. d = NewDecimal(test.i)
  78. d.RoundUp(test.nd)
  79. s = d.String()
  80. if s != test.up {
  81. t.Errorf("Decimal %v RoundUp %d = %v, want %v",
  82. test.i, test.nd, s, test.up)
  83. }
  84. }
  85. }
  86. type roundIntTest struct {
  87. i uint64
  88. shift int
  89. int uint64
  90. }
  91. var roundinttests = []roundIntTest{
  92. {0, 100, 0},
  93. {512, -8, 2},
  94. {513, -8, 2},
  95. {640, -8, 2},
  96. {641, -8, 3},
  97. {384, -8, 2},
  98. {385, -8, 2},
  99. {383, -8, 1},
  100. {1, 100, 1<<64 - 1},
  101. {1000, 0, 1000},
  102. }
  103. func TestDecimalRoundedInteger(t *testing.T) {
  104. for i := 0; i < len(roundinttests); i++ {
  105. test := roundinttests[i]
  106. d := NewDecimal(test.i)
  107. d.Shift(test.shift)
  108. int := d.RoundedInteger()
  109. if int != test.int {
  110. t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v",
  111. test.i, test.shift, int, test.int)
  112. }
  113. }
  114. }