blob: 13a127f5b2cfc9a1123a86d2711132ca69555891 [file] [log] [blame]
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -08001// 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
5package strconv_test
6
7import (
8 . "strconv"
9 "testing"
10)
11
12type shiftTest struct {
13 i uint64
14 shift int
15 out string
16}
17
18var shifttests = []shiftTest{
Ian Lance Taylorbc354852010-11-10 21:50:23 -080019 {0, -100, "0"},
20 {0, 100, "0"},
21 {1, 100, "1267650600228229401496703205376"},
22 {1, -100,
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080023 "0.00000000000000000000000000000078886090522101180541" +
24 "17285652827862296732064351090230047702789306640625",
25 },
Ian Lance Taylorbc354852010-11-10 21:50:23 -080026 {12345678, 8, "3160493568"},
27 {12345678, -8, "48225.3046875"},
28 {195312, 9, "99999744"},
29 {1953125, 9, "1000000000"},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080030}
31
32func TestDecimalShift(t *testing.T) {
33 for i := 0; i < len(shifttests); i++ {
34 test := &shifttests[i]
Ian Lance Taylor4a992592011-10-26 16:19:57 -070035 d := NewDecimal(test.i)
36 d.Shift(test.shift)
37 s := d.String()
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080038 if s != test.out {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080039 t.Errorf("Decimal %v << %v = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080040 test.i, test.shift, s, test.out)
41 }
42 }
43}
44
45type roundTest struct {
46 i uint64
47 nd int
48 down, round, up string
49 int uint64
50}
51
52var roundtests = []roundTest{
Ian Lance Taylorbc354852010-11-10 21:50:23 -080053 {0, 4, "0", "0", "0", 0},
54 {12344999, 4, "12340000", "12340000", "12350000", 12340000},
55 {12345000, 4, "12340000", "12340000", "12350000", 12340000},
56 {12345001, 4, "12340000", "12350000", "12350000", 12350000},
57 {23454999, 4, "23450000", "23450000", "23460000", 23450000},
58 {23455000, 4, "23450000", "23460000", "23460000", 23460000},
59 {23455001, 4, "23450000", "23460000", "23460000", 23460000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080060
Ian Lance Taylorbc354852010-11-10 21:50:23 -080061 {99994999, 4, "99990000", "99990000", "100000000", 99990000},
62 {99995000, 4, "99990000", "100000000", "100000000", 100000000},
63 {99999999, 4, "99990000", "100000000", "100000000", 100000000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080064
Ian Lance Taylorbc354852010-11-10 21:50:23 -080065 {12994999, 4, "12990000", "12990000", "13000000", 12990000},
66 {12995000, 4, "12990000", "13000000", "13000000", 13000000},
67 {12999999, 4, "12990000", "13000000", "13000000", 13000000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080068}
69
70func TestDecimalRound(t *testing.T) {
71 for i := 0; i < len(roundtests); i++ {
72 test := &roundtests[i]
Ian Lance Taylor83ef2ed2011-12-12 15:37:11 -080073 d := NewDecimal(test.i)
74 d.RoundDown(test.nd)
75 s := d.String()
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080076 if s != test.down {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080077 t.Errorf("Decimal %v RoundDown %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080078 test.i, test.nd, s, test.down)
79 }
Ian Lance Taylor83ef2ed2011-12-12 15:37:11 -080080 d = NewDecimal(test.i)
81 d.Round(test.nd)
82 s = d.String()
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080083 if s != test.round {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080084 t.Errorf("Decimal %v Round %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080085 test.i, test.nd, s, test.down)
86 }
Ian Lance Taylor83ef2ed2011-12-12 15:37:11 -080087 d = NewDecimal(test.i)
88 d.RoundUp(test.nd)
89 s = d.String()
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080090 if s != test.up {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080091 t.Errorf("Decimal %v RoundUp %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080092 test.i, test.nd, s, test.up)
93 }
94 }
95}
96
97type roundIntTest struct {
98 i uint64
99 shift int
100 int uint64
101}
102
103var roundinttests = []roundIntTest{
Ian Lance Taylorbc354852010-11-10 21:50:23 -0800104 {0, 100, 0},
105 {512, -8, 2},
106 {513, -8, 2},
107 {640, -8, 2},
108 {641, -8, 3},
109 {384, -8, 2},
110 {385, -8, 2},
111 {383, -8, 1},
112 {1, 100, 1<<64 - 1},
113 {1000, 0, 1000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800114}
115
116func TestDecimalRoundedInteger(t *testing.T) {
117 for i := 0; i < len(roundinttests); i++ {
118 test := roundinttests[i]
Ian Lance Taylor4a992592011-10-26 16:19:57 -0700119 d := NewDecimal(test.i)
120 d.Shift(test.shift)
121 int := d.RoundedInteger()
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800122 if int != test.int {
Ian Lance Taylorbc354852010-11-10 21:50:23 -0800123 t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800124 test.i, test.shift, int, test.int)
125 }
126 }
127}