dec_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package inf
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "fmt"
  6. "math/big"
  7. "strings"
  8. "testing"
  9. )
  10. type decFunZZ func(z, x, y *Dec) *Dec
  11. type decArgZZ struct {
  12. z, x, y *Dec
  13. }
  14. var decSumZZ = []decArgZZ{
  15. {NewDecInt64(0), NewDecInt64(0), NewDecInt64(0)},
  16. {NewDecInt64(1), NewDecInt64(1), NewDecInt64(0)},
  17. {NewDecInt64(1111111110), NewDecInt64(123456789), NewDecInt64(987654321)},
  18. {NewDecInt64(-1), NewDecInt64(-1), NewDecInt64(0)},
  19. {NewDecInt64(864197532), NewDecInt64(-123456789), NewDecInt64(987654321)},
  20. {NewDecInt64(-1111111110), NewDecInt64(-123456789), NewDecInt64(-987654321)},
  21. {NewDec(big.NewInt(12), 2), NewDec(big.NewInt(1), 1), NewDec(big.NewInt(2), 2)},
  22. }
  23. var decProdZZ = []decArgZZ{
  24. {NewDecInt64(0), NewDecInt64(0), NewDecInt64(0)},
  25. {NewDecInt64(0), NewDecInt64(1), NewDecInt64(0)},
  26. {NewDecInt64(1), NewDecInt64(1), NewDecInt64(1)},
  27. {NewDecInt64(-991 * 991), NewDecInt64(991), NewDecInt64(-991)},
  28. {NewDec(big.NewInt(2), 3), NewDec(big.NewInt(1), 1), NewDec(big.NewInt(2), 2)},
  29. {NewDec(big.NewInt(2), -3), NewDec(big.NewInt(1), -1), NewDec(big.NewInt(2), -2)},
  30. {NewDec(big.NewInt(2), 3), NewDec(big.NewInt(1), 1), NewDec(big.NewInt(2), 2)},
  31. }
  32. func TestDecSignZ(t *testing.T) {
  33. var zero Dec
  34. for _, a := range decSumZZ {
  35. s := a.z.Sign()
  36. e := a.z.Cmp(&zero)
  37. if s != e {
  38. t.Errorf("got %d; want %d for z = %v", s, e, a.z)
  39. }
  40. }
  41. }
  42. func TestDecAbsZ(t *testing.T) {
  43. var zero Dec
  44. for _, a := range decSumZZ {
  45. var z Dec
  46. z.Abs(a.z)
  47. var e Dec
  48. e.Set(a.z)
  49. if e.Cmp(&zero) < 0 {
  50. e.Sub(&zero, &e)
  51. }
  52. if z.Cmp(&e) != 0 {
  53. t.Errorf("got z = %v; want %v", z, e)
  54. }
  55. }
  56. }
  57. func testDecFunZZ(t *testing.T, msg string, f decFunZZ, a decArgZZ) {
  58. var z Dec
  59. f(&z, a.x, a.y)
  60. if (&z).Cmp(a.z) != 0 {
  61. t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
  62. }
  63. }
  64. func TestDecSumZZ(t *testing.T) {
  65. AddZZ := func(z, x, y *Dec) *Dec { return z.Add(x, y) }
  66. SubZZ := func(z, x, y *Dec) *Dec { return z.Sub(x, y) }
  67. for _, a := range decSumZZ {
  68. arg := a
  69. testDecFunZZ(t, "AddZZ", AddZZ, arg)
  70. arg = decArgZZ{a.z, a.y, a.x}
  71. testDecFunZZ(t, "AddZZ symmetric", AddZZ, arg)
  72. arg = decArgZZ{a.x, a.z, a.y}
  73. testDecFunZZ(t, "SubZZ", SubZZ, arg)
  74. arg = decArgZZ{a.y, a.z, a.x}
  75. testDecFunZZ(t, "SubZZ symmetric", SubZZ, arg)
  76. }
  77. }
  78. func TestDecProdZZ(t *testing.T) {
  79. MulZZ := func(z, x, y *Dec) *Dec { return z.Mul(x, y) }
  80. for _, a := range decProdZZ {
  81. arg := a
  82. testDecFunZZ(t, "MulZZ", MulZZ, arg)
  83. arg = decArgZZ{a.z, a.y, a.x}
  84. testDecFunZZ(t, "MulZZ symmetric", MulZZ, arg)
  85. }
  86. }
  87. var decQuoRemZZZ = []struct {
  88. z, x, y *Dec
  89. r *big.Rat
  90. srA, srB int
  91. }{
  92. // basic examples
  93. {NewDec(big.NewInt(1), 0), NewDecInt64(2), NewDecInt64(2), big.NewRat(0, 1), 0, 1},
  94. {NewDec(big.NewInt(15), 1), NewDecInt64(3), NewDecInt64(2), big.NewRat(0, 1), 0, 1},
  95. {NewDec(big.NewInt(1), 1), NewDecInt64(1), NewDecInt64(10), big.NewRat(0, 1), 0, 1},
  96. {NewDec(big.NewInt(0), 0), NewDecInt64(2), NewDecInt64(3), big.NewRat(2, 3), 1, 1},
  97. {NewDec(big.NewInt(0), 0), NewDecInt64(2), NewDecInt64(6), big.NewRat(1, 3), 1, 1},
  98. {NewDec(big.NewInt(1), 1), NewDecInt64(2), NewDecInt64(12), big.NewRat(2, 3), 1, 1},
  99. // examples from the Go Language Specification
  100. {NewDec(big.NewInt(1), 0), NewDecInt64(5), NewDecInt64(3), big.NewRat(2, 3), 1, 1},
  101. {NewDec(big.NewInt(-1), 0), NewDecInt64(-5), NewDecInt64(3), big.NewRat(-2, 3), -1, 1},
  102. {NewDec(big.NewInt(-1), 0), NewDecInt64(5), NewDecInt64(-3), big.NewRat(-2, 3), 1, -1},
  103. {NewDec(big.NewInt(1), 0), NewDecInt64(-5), NewDecInt64(-3), big.NewRat(2, 3), -1, -1},
  104. }
  105. func TestDecQuoRem(t *testing.T) {
  106. for i, a := range decQuoRemZZZ {
  107. z, rA, rB := new(Dec), new(big.Int), new(big.Int)
  108. s := ScaleQuoExact.Scale(a.x, a.y)
  109. z.quoRem(a.x, a.y, s, true, rA, rB)
  110. if a.z.Cmp(z) != 0 || a.r.Cmp(new(big.Rat).SetFrac(rA, rB)) != 0 {
  111. t.Errorf("#%d QuoRemZZZ got %v, %v, %v; expected %v, %v", i, z, rA, rB, a.z, a.r)
  112. }
  113. if a.srA != rA.Sign() || a.srB != rB.Sign() {
  114. t.Errorf("#%d QuoRemZZZ wrong signs, got %v, %v; expected %v, %v", i, rA.Sign(), rB.Sign(), a.srA, a.srB)
  115. }
  116. }
  117. }
  118. var decRoundTests = [...]struct {
  119. in *Dec
  120. s Scale
  121. r Rounder
  122. exp *Dec
  123. }{
  124. {NewDec(big.NewInt(123424999999999993), 15), 2, RoundHalfUp, NewDec(big.NewInt(12342), 2)},
  125. {NewDec(big.NewInt(123425000000000001), 15), 2, RoundHalfUp, NewDec(big.NewInt(12343), 2)},
  126. {NewDec(big.NewInt(123424999999999993), 15), 15, RoundHalfUp, NewDec(big.NewInt(123424999999999993), 15)},
  127. {NewDec(big.NewInt(123424999999999993), 15), 16, RoundHalfUp, NewDec(big.NewInt(1234249999999999930), 16)},
  128. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -1, RoundHalfUp, NewDec(big.NewInt(1844674407370955162), -1)},
  129. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -2, RoundHalfUp, NewDec(big.NewInt(184467440737095516), -2)},
  130. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -3, RoundHalfUp, NewDec(big.NewInt(18446744073709552), -3)},
  131. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -4, RoundHalfUp, NewDec(big.NewInt(1844674407370955), -4)},
  132. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -5, RoundHalfUp, NewDec(big.NewInt(184467440737096), -5)},
  133. {NewDec(new(big.Int).Lsh(big.NewInt(1), 64), 0), -6, RoundHalfUp, NewDec(big.NewInt(18446744073710), -6)},
  134. }
  135. func TestDecRound(t *testing.T) {
  136. for i, tt := range decRoundTests {
  137. z := new(Dec).Round(tt.in, tt.s, tt.r)
  138. if tt.exp.Cmp(z) != 0 {
  139. t.Errorf("#%d Round got %v; expected %v", i, z, tt.exp)
  140. }
  141. }
  142. }
  143. var decStringTests = []struct {
  144. in string
  145. out string
  146. val int64
  147. scale Scale // skip SetString if negative
  148. ok bool
  149. scanOk bool
  150. }{
  151. {in: "", ok: false, scanOk: false},
  152. {in: "a", ok: false, scanOk: false},
  153. {in: "z", ok: false, scanOk: false},
  154. {in: "+", ok: false, scanOk: false},
  155. {in: "-", ok: false, scanOk: false},
  156. {in: "g", ok: false, scanOk: false},
  157. {in: ".", ok: false, scanOk: false},
  158. {in: ".-0", ok: false, scanOk: false},
  159. {in: ".+0", ok: false, scanOk: false},
  160. // Scannable but not SetStringable
  161. {"0b", "ignored", 0, 0, false, true},
  162. {"0x", "ignored", 0, 0, false, true},
  163. {"0xg", "ignored", 0, 0, false, true},
  164. {"0.0g", "ignored", 0, 1, false, true},
  165. // examples from godoc for Dec
  166. {"0", "0", 0, 0, true, true},
  167. {"0.00", "0.00", 0, 2, true, true},
  168. {"ignored", "0", 0, -2, true, false},
  169. {"1", "1", 1, 0, true, true},
  170. {"1.00", "1.00", 100, 2, true, true},
  171. {"10", "10", 10, 0, true, true},
  172. {"ignored", "10", 1, -1, true, false},
  173. // other tests
  174. {"+0", "0", 0, 0, true, true},
  175. {"-0", "0", 0, 0, true, true},
  176. {"0.0", "0.0", 0, 1, true, true},
  177. {"0.1", "0.1", 1, 1, true, true},
  178. {"0.", "0", 0, 0, true, true},
  179. {"-10", "-10", -1, -1, true, true},
  180. {"-1", "-1", -1, 0, true, true},
  181. {"-0.1", "-0.1", -1, 1, true, true},
  182. {"-0.01", "-0.01", -1, 2, true, true},
  183. {"+0.", "0", 0, 0, true, true},
  184. {"-0.", "0", 0, 0, true, true},
  185. {".0", "0.0", 0, 1, true, true},
  186. {"+.0", "0.0", 0, 1, true, true},
  187. {"-.0", "0.0", 0, 1, true, true},
  188. {"0.0000000000", "0.0000000000", 0, 10, true, true},
  189. {"0.0000000001", "0.0000000001", 1, 10, true, true},
  190. {"-0.0000000000", "0.0000000000", 0, 10, true, true},
  191. {"-0.0000000001", "-0.0000000001", -1, 10, true, true},
  192. {"-10", "-10", -10, 0, true, true},
  193. {"+10", "10", 10, 0, true, true},
  194. {"00", "0", 0, 0, true, true},
  195. {"023", "23", 23, 0, true, true}, // decimal, not octal
  196. {"-02.3", "-2.3", -23, 1, true, true}, // decimal, not octal
  197. }
  198. func TestDecGetString(t *testing.T) {
  199. z := new(Dec)
  200. for i, test := range decStringTests {
  201. if !test.ok {
  202. continue
  203. }
  204. z.SetUnscaled(big.NewInt(test.val))
  205. z.SetScale(test.scale)
  206. s := z.String()
  207. if s != test.out {
  208. t.Errorf("#%da got %s; want %s", i, s, test.out)
  209. }
  210. s = fmt.Sprintf("%d", z)
  211. if s != test.out {
  212. t.Errorf("#%db got %s; want %s", i, s, test.out)
  213. }
  214. }
  215. }
  216. func TestDecSetString(t *testing.T) {
  217. tmp := new(Dec)
  218. for i, test := range decStringTests {
  219. if test.scale < 0 {
  220. // SetString only supports scale >= 0
  221. continue
  222. }
  223. // initialize to a non-zero value so that issues with parsing
  224. // 0 are detected
  225. tmp.Set(NewDecInt64(1234567890).SetScale(123))
  226. n1, ok1 := new(Dec).SetString(test.in)
  227. n2, ok2 := tmp.SetString(test.in)
  228. expected := NewDecInt64(test.val).SetScale(test.scale)
  229. if ok1 != test.ok || ok2 != test.ok {
  230. t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok)
  231. continue
  232. }
  233. if !ok1 {
  234. if n1 != nil {
  235. t.Errorf("#%d (input '%s') n1 != nil", i, test.in)
  236. }
  237. continue
  238. }
  239. if !ok2 {
  240. if n2 != nil {
  241. t.Errorf("#%d (input '%s') n2 != nil", i, test.in)
  242. }
  243. continue
  244. }
  245. if n1.Cmp(expected) != 0 {
  246. t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
  247. }
  248. if n2.Cmp(expected) != 0 {
  249. t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
  250. }
  251. }
  252. }
  253. func TestDecScan(t *testing.T) {
  254. tmp := new(Dec)
  255. for i, test := range decStringTests {
  256. if test.scale < 0 {
  257. // SetString only supports scale >= 0
  258. continue
  259. }
  260. // initialize to a non-zero value so that issues with parsing
  261. // 0 are detected
  262. tmp.Set(NewDecInt64(1234567890).SetScale(123))
  263. n1, n2 := new(Dec), tmp
  264. nn1, err1 := fmt.Sscan(test.in, n1)
  265. nn2, err2 := fmt.Sscan(test.in, n2)
  266. if !test.scanOk {
  267. if err1 == nil || err2 == nil {
  268. t.Errorf("#%d (input '%s') ok incorrect, should be %t", i, test.in, test.scanOk)
  269. }
  270. continue
  271. }
  272. expected := NewDecInt64(test.val).SetScale(test.scale)
  273. if nn1 != 1 || err1 != nil || nn2 != 1 || err2 != nil {
  274. t.Errorf("#%d (input '%s') error %d %v, %d %v", i, test.in, nn1, err1, nn2, err2)
  275. continue
  276. }
  277. if n1.Cmp(expected) != 0 {
  278. t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
  279. }
  280. if n2.Cmp(expected) != 0 {
  281. t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
  282. }
  283. }
  284. }
  285. var decScanNextTests = []struct {
  286. in string
  287. ok bool
  288. next rune
  289. }{
  290. {"", false, 0},
  291. {"a", false, 'a'},
  292. {"z", false, 'z'},
  293. {"+", false, 0},
  294. {"-", false, 0},
  295. {"g", false, 'g'},
  296. {".", false, 0},
  297. {".-0", false, '-'},
  298. {".+0", false, '+'},
  299. {"0b", true, 'b'},
  300. {"0x", true, 'x'},
  301. {"0xg", true, 'x'},
  302. {"0.0g", true, 'g'},
  303. }
  304. func TestDecScanNext(t *testing.T) {
  305. for i, test := range decScanNextTests {
  306. rdr := strings.NewReader(test.in)
  307. n1 := new(Dec)
  308. nn1, _ := fmt.Fscan(rdr, n1)
  309. if (test.ok && nn1 == 0) || (!test.ok && nn1 > 0) {
  310. t.Errorf("#%d (input '%s') ok incorrect should be %t", i, test.in, test.ok)
  311. continue
  312. }
  313. r := rune(0)
  314. nn2, err := fmt.Fscanf(rdr, "%c", &r)
  315. if test.next != r {
  316. t.Errorf("#%d (input '%s') next incorrect, got %c should be %c, %d, %v", i, test.in, r, test.next, nn2, err)
  317. }
  318. }
  319. }
  320. var decGobEncodingTests = []string{
  321. "0",
  322. "1",
  323. "2",
  324. "10",
  325. "42",
  326. "1234567890",
  327. "298472983472983471903246121093472394872319615612417471234712061",
  328. }
  329. func TestDecGobEncoding(t *testing.T) {
  330. var medium bytes.Buffer
  331. enc := gob.NewEncoder(&medium)
  332. dec := gob.NewDecoder(&medium)
  333. for i, test := range decGobEncodingTests {
  334. for j := 0; j < 2; j++ {
  335. for k := Scale(-5); k <= 5; k++ {
  336. medium.Reset() // empty buffer for each test case (in case of failures)
  337. stest := test
  338. if j != 0 {
  339. // negative numbers
  340. stest = "-" + test
  341. }
  342. var tx Dec
  343. tx.SetString(stest)
  344. tx.SetScale(k) // test with positive, negative, and zero scale
  345. if err := enc.Encode(&tx); err != nil {
  346. t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
  347. }
  348. var rx Dec
  349. if err := dec.Decode(&rx); err != nil {
  350. t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
  351. }
  352. if rx.Cmp(&tx) != 0 {
  353. t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
  354. }
  355. }
  356. }
  357. }
  358. }