blob: 9b790351673069e5185451b546cad120c0aa1c01 [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]
35 s := NewDecimal(test.i).Shift(test.shift).String()
36 if s != test.out {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080037 t.Errorf("Decimal %v << %v = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080038 test.i, test.shift, s, test.out)
39 }
40 }
41}
42
43type roundTest struct {
44 i uint64
45 nd int
46 down, round, up string
47 int uint64
48}
49
50var roundtests = []roundTest{
Ian Lance Taylorbc354852010-11-10 21:50:23 -080051 {0, 4, "0", "0", "0", 0},
52 {12344999, 4, "12340000", "12340000", "12350000", 12340000},
53 {12345000, 4, "12340000", "12340000", "12350000", 12340000},
54 {12345001, 4, "12340000", "12350000", "12350000", 12350000},
55 {23454999, 4, "23450000", "23450000", "23460000", 23450000},
56 {23455000, 4, "23450000", "23460000", "23460000", 23460000},
57 {23455001, 4, "23450000", "23460000", "23460000", 23460000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080058
Ian Lance Taylorbc354852010-11-10 21:50:23 -080059 {99994999, 4, "99990000", "99990000", "100000000", 99990000},
60 {99995000, 4, "99990000", "100000000", "100000000", 100000000},
61 {99999999, 4, "99990000", "100000000", "100000000", 100000000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080062
Ian Lance Taylorbc354852010-11-10 21:50:23 -080063 {12994999, 4, "12990000", "12990000", "13000000", 12990000},
64 {12995000, 4, "12990000", "13000000", "13000000", 13000000},
65 {12999999, 4, "12990000", "13000000", "13000000", 13000000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080066}
67
68func TestDecimalRound(t *testing.T) {
69 for i := 0; i < len(roundtests); i++ {
70 test := &roundtests[i]
71 s := NewDecimal(test.i).RoundDown(test.nd).String()
72 if s != test.down {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080073 t.Errorf("Decimal %v RoundDown %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080074 test.i, test.nd, s, test.down)
75 }
76 s = NewDecimal(test.i).Round(test.nd).String()
77 if s != test.round {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080078 t.Errorf("Decimal %v Round %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080079 test.i, test.nd, s, test.down)
80 }
81 s = NewDecimal(test.i).RoundUp(test.nd).String()
82 if s != test.up {
Ian Lance Taylorbc354852010-11-10 21:50:23 -080083 t.Errorf("Decimal %v RoundUp %d = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080084 test.i, test.nd, s, test.up)
85 }
86 }
87}
88
89type roundIntTest struct {
90 i uint64
91 shift int
92 int uint64
93}
94
95var roundinttests = []roundIntTest{
Ian Lance Taylorbc354852010-11-10 21:50:23 -080096 {0, 100, 0},
97 {512, -8, 2},
98 {513, -8, 2},
99 {640, -8, 2},
100 {641, -8, 3},
101 {384, -8, 2},
102 {385, -8, 2},
103 {383, -8, 1},
104 {1, 100, 1<<64 - 1},
105 {1000, 0, 1000},
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800106}
107
108func TestDecimalRoundedInteger(t *testing.T) {
109 for i := 0; i < len(roundinttests); i++ {
110 test := roundinttests[i]
111 int := NewDecimal(test.i).Shift(test.shift).RoundedInteger()
112 if int != test.int {
Ian Lance Taylorbc354852010-11-10 21:50:23 -0800113 t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v",
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800114 test.i, test.shift, int, test.int)
115 }
116 }
117}