Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 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 | |
| 5 | package strconv_test |
| 6 | |
| 7 | import ( |
| 8 | . "strconv" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | type shiftTest struct { |
| 13 | i uint64 |
| 14 | shift int |
| 15 | out string |
| 16 | } |
| 17 | |
| 18 | var shifttests = []shiftTest{ |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 19 | {0, -100, "0"}, |
| 20 | {0, 100, "0"}, |
| 21 | {1, 100, "1267650600228229401496703205376"}, |
| 22 | {1, -100, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 23 | "0.00000000000000000000000000000078886090522101180541" + |
| 24 | "17285652827862296732064351090230047702789306640625", |
| 25 | }, |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 26 | {12345678, 8, "3160493568"}, |
| 27 | {12345678, -8, "48225.3046875"}, |
| 28 | {195312, 9, "99999744"}, |
| 29 | {1953125, 9, "1000000000"}, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | func 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 Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 37 | t.Errorf("Decimal %v << %v = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 38 | test.i, test.shift, s, test.out) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | type roundTest struct { |
| 44 | i uint64 |
| 45 | nd int |
| 46 | down, round, up string |
| 47 | int uint64 |
| 48 | } |
| 49 | |
| 50 | var roundtests = []roundTest{ |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 51 | {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 Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 58 | |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 59 | {99994999, 4, "99990000", "99990000", "100000000", 99990000}, |
| 60 | {99995000, 4, "99990000", "100000000", "100000000", 100000000}, |
| 61 | {99999999, 4, "99990000", "100000000", "100000000", 100000000}, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 62 | |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 63 | {12994999, 4, "12990000", "12990000", "13000000", 12990000}, |
| 64 | {12995000, 4, "12990000", "13000000", "13000000", 13000000}, |
| 65 | {12999999, 4, "12990000", "13000000", "13000000", 13000000}, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func 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 Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 73 | t.Errorf("Decimal %v RoundDown %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 74 | test.i, test.nd, s, test.down) |
| 75 | } |
| 76 | s = NewDecimal(test.i).Round(test.nd).String() |
| 77 | if s != test.round { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 78 | t.Errorf("Decimal %v Round %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 79 | test.i, test.nd, s, test.down) |
| 80 | } |
| 81 | s = NewDecimal(test.i).RoundUp(test.nd).String() |
| 82 | if s != test.up { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 83 | t.Errorf("Decimal %v RoundUp %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 84 | test.i, test.nd, s, test.up) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | type roundIntTest struct { |
| 90 | i uint64 |
| 91 | shift int |
| 92 | int uint64 |
| 93 | } |
| 94 | |
| 95 | var roundinttests = []roundIntTest{ |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 96 | {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 Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func 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 Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame^] | 113 | t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 114 | test.i, test.shift, int, test.int) |
| 115 | } |
| 116 | } |
| 117 | } |