decimal_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright 2017 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 number
  5. import (
  6. "fmt"
  7. "math"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. )
  12. func mkfloat(num string) float64 {
  13. u, _ := strconv.ParseUint(num, 10, 32)
  14. return float64(u)
  15. }
  16. // mkdec creates a decimal from a string. All ASCII digits are converted to
  17. // digits in the decimal. The dot is used to indicate the scale by which the
  18. // digits are shifted. Numbers may have an additional exponent or be the special
  19. // value NaN, Inf, or -Inf.
  20. func mkdec(num string) (d Decimal) {
  21. var r RoundingContext
  22. d.Convert(r, dec(num))
  23. return
  24. }
  25. type dec string
  26. func (s dec) Convert(d *Decimal, _ RoundingContext) {
  27. num := string(s)
  28. if num[0] == '-' {
  29. d.Neg = true
  30. num = num[1:]
  31. }
  32. switch num {
  33. case "NaN":
  34. d.NaN = true
  35. return
  36. case "Inf":
  37. d.Inf = true
  38. return
  39. }
  40. if p := strings.IndexAny(num, "eE"); p != -1 {
  41. i64, err := strconv.ParseInt(num[p+1:], 10, 32)
  42. if err != nil {
  43. panic(err)
  44. }
  45. d.Exp = int32(i64)
  46. num = num[:p]
  47. }
  48. if p := strings.IndexByte(num, '.'); p != -1 {
  49. d.Exp += int32(p)
  50. num = num[:p] + num[p+1:]
  51. } else {
  52. d.Exp += int32(len(num))
  53. }
  54. d.Digits = []byte(num)
  55. for i := range d.Digits {
  56. d.Digits[i] -= '0'
  57. }
  58. *d = d.normalize()
  59. }
  60. func byteNum(s string) []byte {
  61. b := make([]byte, len(s))
  62. for i := 0; i < len(s); i++ {
  63. if c := s[i]; '0' <= c && c <= '9' {
  64. b[i] = s[i] - '0'
  65. } else {
  66. b[i] = s[i] - 'a' + 10
  67. }
  68. }
  69. return b
  70. }
  71. func strNum(s string) string {
  72. return string(byteNum(s))
  73. }
  74. func TestDecimalString(t *testing.T) {
  75. for _, test := range []struct {
  76. x Decimal
  77. want string
  78. }{
  79. {want: "0"},
  80. {Decimal{digits: digits{Digits: nil, Exp: 1000}}, "0"}, // exponent of 1000 is ignored
  81. {Decimal{digits: digits{Digits: byteNum("12345"), Exp: 0}}, "0.12345"},
  82. {Decimal{digits: digits{Digits: byteNum("12345"), Exp: -3}}, "0.00012345"},
  83. {Decimal{digits: digits{Digits: byteNum("12345"), Exp: +3}}, "123.45"},
  84. {Decimal{digits: digits{Digits: byteNum("12345"), Exp: +10}}, "1234500000"},
  85. } {
  86. if got := test.x.String(); got != test.want {
  87. t.Errorf("%v == %q; want %q", test.x, got, test.want)
  88. }
  89. }
  90. }
  91. func TestRounding(t *testing.T) {
  92. testCases := []struct {
  93. x string
  94. n int
  95. // modes is the result for modes. Signs are left out of the result.
  96. // The results are stored in the following order:
  97. // zero, negInf
  98. // nearZero, nearEven, nearAway
  99. // away, posInf
  100. modes [numModes]string
  101. }{
  102. {"0", 1, [numModes]string{
  103. "0", "0",
  104. "0", "0", "0",
  105. "0", "0"}},
  106. {"1", 1, [numModes]string{
  107. "1", "1",
  108. "1", "1", "1",
  109. "1", "1"}},
  110. {"5", 1, [numModes]string{
  111. "5", "5",
  112. "5", "5", "5",
  113. "5", "5"}},
  114. {"15", 1, [numModes]string{
  115. "10", "10",
  116. "10", "20", "20",
  117. "20", "20"}},
  118. {"45", 1, [numModes]string{
  119. "40", "40",
  120. "40", "40", "50",
  121. "50", "50"}},
  122. {"95", 1, [numModes]string{
  123. "90", "90",
  124. "90", "100", "100",
  125. "100", "100"}},
  126. {"12344999", 4, [numModes]string{
  127. "12340000", "12340000",
  128. "12340000", "12340000", "12340000",
  129. "12350000", "12350000"}},
  130. {"12345000", 4, [numModes]string{
  131. "12340000", "12340000",
  132. "12340000", "12340000", "12350000",
  133. "12350000", "12350000"}},
  134. {"12345001", 4, [numModes]string{
  135. "12340000", "12340000",
  136. "12350000", "12350000", "12350000",
  137. "12350000", "12350000"}},
  138. {"12345100", 4, [numModes]string{
  139. "12340000", "12340000",
  140. "12350000", "12350000", "12350000",
  141. "12350000", "12350000"}},
  142. {"23454999", 4, [numModes]string{
  143. "23450000", "23450000",
  144. "23450000", "23450000", "23450000",
  145. "23460000", "23460000"}},
  146. {"23455000", 4, [numModes]string{
  147. "23450000", "23450000",
  148. "23450000", "23460000", "23460000",
  149. "23460000", "23460000"}},
  150. {"23455001", 4, [numModes]string{
  151. "23450000", "23450000",
  152. "23460000", "23460000", "23460000",
  153. "23460000", "23460000"}},
  154. {"23455100", 4, [numModes]string{
  155. "23450000", "23450000",
  156. "23460000", "23460000", "23460000",
  157. "23460000", "23460000"}},
  158. {"99994999", 4, [numModes]string{
  159. "99990000", "99990000",
  160. "99990000", "99990000", "99990000",
  161. "100000000", "100000000"}},
  162. {"99995000", 4, [numModes]string{
  163. "99990000", "99990000",
  164. "99990000", "100000000", "100000000",
  165. "100000000", "100000000"}},
  166. {"99999999", 4, [numModes]string{
  167. "99990000", "99990000",
  168. "100000000", "100000000", "100000000",
  169. "100000000", "100000000"}},
  170. {"12994999", 4, [numModes]string{
  171. "12990000", "12990000",
  172. "12990000", "12990000", "12990000",
  173. "13000000", "13000000"}},
  174. {"12995000", 4, [numModes]string{
  175. "12990000", "12990000",
  176. "12990000", "13000000", "13000000",
  177. "13000000", "13000000"}},
  178. {"12999999", 4, [numModes]string{
  179. "12990000", "12990000",
  180. "13000000", "13000000", "13000000",
  181. "13000000", "13000000"}},
  182. }
  183. modes := []RoundingMode{
  184. ToZero, ToNegativeInf,
  185. ToNearestZero, ToNearestEven, ToNearestAway,
  186. AwayFromZero, ToPositiveInf,
  187. }
  188. for _, tc := range testCases {
  189. // Create negative counterpart tests: the sign is reversed and
  190. // ToPositiveInf and ToNegativeInf swapped.
  191. negModes := tc.modes
  192. negModes[1], negModes[6] = negModes[6], negModes[1]
  193. for i, res := range negModes {
  194. negModes[i] = "-" + res
  195. }
  196. for i, m := range modes {
  197. t.Run(fmt.Sprintf("x:%s/n:%d/%s", tc.x, tc.n, m), func(t *testing.T) {
  198. d := mkdec(tc.x)
  199. d.round(m, tc.n)
  200. if got := d.String(); got != tc.modes[i] {
  201. t.Errorf("pos decimal: got %q; want %q", d.String(), tc.modes[i])
  202. }
  203. mult := math.Pow(10, float64(len(tc.x)-tc.n))
  204. f := mkfloat(tc.x)
  205. f = m.roundFloat(f/mult) * mult
  206. if got := fmt.Sprintf("%.0f", f); got != tc.modes[i] {
  207. t.Errorf("pos float: got %q; want %q", got, tc.modes[i])
  208. }
  209. // Test the negative case. This is the same as the positive
  210. // case, but with ToPositiveInf and ToNegativeInf swapped.
  211. d = mkdec(tc.x)
  212. d.Neg = true
  213. d.round(m, tc.n)
  214. if got, want := d.String(), negModes[i]; got != want {
  215. t.Errorf("neg decimal: got %q; want %q", d.String(), want)
  216. }
  217. f = -mkfloat(tc.x)
  218. f = m.roundFloat(f/mult) * mult
  219. if got := fmt.Sprintf("%.0f", f); got != negModes[i] {
  220. t.Errorf("neg float: got %q; want %q", got, negModes[i])
  221. }
  222. })
  223. }
  224. }
  225. }
  226. func TestConvert(t *testing.T) {
  227. scale2 := RoundingContext{}
  228. scale2.SetScale(2)
  229. scale2away := RoundingContext{Mode: AwayFromZero}
  230. scale2away.SetScale(2)
  231. inc0_05 := RoundingContext{Increment: 5, IncrementScale: 2}
  232. inc0_05.SetScale(2)
  233. inc50 := RoundingContext{Increment: 50}
  234. incScaleEqualToScalesLen := RoundingContext{Increment: 1, IncrementScale: 0}
  235. if len(scales) <= math.MaxUint8 {
  236. incScaleEqualToScalesLen.IncrementScale = uint8(len(scales))
  237. }
  238. prec3 := RoundingContext{}
  239. prec3.SetPrecision(3)
  240. roundShift := RoundingContext{DigitShift: 2, MaxFractionDigits: 2}
  241. testCases := []struct {
  242. x interface{}
  243. rc RoundingContext
  244. out string
  245. }{
  246. {-0.001, scale2, "-0.00"},
  247. {0.1234, prec3, "0.123"},
  248. {1234.0, prec3, "1230"},
  249. {1.2345e10, prec3, "12300000000"},
  250. {int8(-34), scale2, "-34"},
  251. {int16(-234), scale2, "-234"},
  252. {int32(-234), scale2, "-234"},
  253. {int64(-234), scale2, "-234"},
  254. {int(-234), scale2, "-234"},
  255. {uint8(234), scale2, "234"},
  256. {uint16(234), scale2, "234"},
  257. {uint32(234), scale2, "234"},
  258. {uint64(234), scale2, "234"},
  259. {uint(234), scale2, "234"},
  260. {-1e9, scale2, "-1000000000.00"},
  261. // The following two causes this result to have a lot of digits:
  262. // 1) 0.234 cannot be accurately represented as a float64, and
  263. // 2) as strconv does not support the rounding AwayFromZero, Convert
  264. // leaves the rounding to caller.
  265. {0.234, scale2away,
  266. "0.2340000000000000135447209004269097931683063507080078125"},
  267. {0.0249, inc0_05, "0.00"},
  268. {0.025, inc0_05, "0.00"},
  269. {0.0251, inc0_05, "0.05"},
  270. {0.03, inc0_05, "0.05"},
  271. {0.049, inc0_05, "0.05"},
  272. {0.05, inc0_05, "0.05"},
  273. {0.051, inc0_05, "0.05"},
  274. {0.0749, inc0_05, "0.05"},
  275. {0.075, inc0_05, "0.10"},
  276. {0.0751, inc0_05, "0.10"},
  277. {324, inc50, "300"},
  278. {325, inc50, "300"},
  279. {326, inc50, "350"},
  280. {349, inc50, "350"},
  281. {350, inc50, "350"},
  282. {351, inc50, "350"},
  283. {374, inc50, "350"},
  284. {375, inc50, "400"},
  285. {376, inc50, "400"},
  286. // Here the scale is 2, but the digits get shifted left. As we use
  287. // AppendFloat to do the rounding an exta 0 gets added.
  288. {0.123, roundShift, "0.1230"},
  289. {converter(3), scale2, "100"},
  290. {math.Inf(1), inc50, "Inf"},
  291. {math.Inf(-1), inc50, "-Inf"},
  292. {math.NaN(), inc50, "NaN"},
  293. {"clearly not a number", scale2, "NaN"},
  294. {0.0, incScaleEqualToScalesLen, "0"},
  295. }
  296. for _, tc := range testCases {
  297. var d Decimal
  298. t.Run(fmt.Sprintf("%T:%v-%v", tc.x, tc.x, tc.rc), func(t *testing.T) {
  299. d.Convert(tc.rc, tc.x)
  300. if got := d.String(); got != tc.out {
  301. t.Errorf("got %q; want %q", got, tc.out)
  302. }
  303. })
  304. }
  305. }
  306. type converter int
  307. func (c converter) Convert(d *Decimal, r RoundingContext) {
  308. d.Digits = append(d.Digits, 1, 0, 0)
  309. d.Exp = 3
  310. }