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] |
Ian Lance Taylor | 4a99259 | 2011-10-26 16:19:57 -0700 | [diff] [blame] | 35 | d := NewDecimal(test.i) |
| 36 | d.Shift(test.shift) |
| 37 | s := d.String() |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 38 | if s != test.out { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 39 | t.Errorf("Decimal %v << %v = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 40 | test.i, test.shift, s, test.out) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | type roundTest struct { |
| 46 | i uint64 |
| 47 | nd int |
| 48 | down, round, up string |
| 49 | int uint64 |
| 50 | } |
| 51 | |
| 52 | var roundtests = []roundTest{ |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 53 | {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 Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 60 | |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 61 | {99994999, 4, "99990000", "99990000", "100000000", 99990000}, |
| 62 | {99995000, 4, "99990000", "100000000", "100000000", 100000000}, |
| 63 | {99999999, 4, "99990000", "100000000", "100000000", 100000000}, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 64 | |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 65 | {12994999, 4, "12990000", "12990000", "13000000", 12990000}, |
| 66 | {12995000, 4, "12990000", "13000000", "13000000", 13000000}, |
| 67 | {12999999, 4, "12990000", "13000000", "13000000", 13000000}, |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | func TestDecimalRound(t *testing.T) { |
| 71 | for i := 0; i < len(roundtests); i++ { |
| 72 | test := &roundtests[i] |
Ian Lance Taylor | 83ef2ed | 2011-12-12 15:37:11 -0800 | [diff] [blame] | 73 | d := NewDecimal(test.i) |
| 74 | d.RoundDown(test.nd) |
| 75 | s := d.String() |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 76 | if s != test.down { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 77 | t.Errorf("Decimal %v RoundDown %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 78 | test.i, test.nd, s, test.down) |
| 79 | } |
Ian Lance Taylor | 83ef2ed | 2011-12-12 15:37:11 -0800 | [diff] [blame] | 80 | d = NewDecimal(test.i) |
| 81 | d.Round(test.nd) |
| 82 | s = d.String() |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 83 | if s != test.round { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 84 | t.Errorf("Decimal %v Round %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 85 | test.i, test.nd, s, test.down) |
| 86 | } |
Ian Lance Taylor | 83ef2ed | 2011-12-12 15:37:11 -0800 | [diff] [blame] | 87 | d = NewDecimal(test.i) |
| 88 | d.RoundUp(test.nd) |
| 89 | s = d.String() |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 90 | if s != test.up { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 91 | t.Errorf("Decimal %v RoundUp %d = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 92 | test.i, test.nd, s, test.up) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | type roundIntTest struct { |
| 98 | i uint64 |
| 99 | shift int |
| 100 | int uint64 |
| 101 | } |
| 102 | |
| 103 | var roundinttests = []roundIntTest{ |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 104 | {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 Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func TestDecimalRoundedInteger(t *testing.T) { |
| 117 | for i := 0; i < len(roundinttests); i++ { |
| 118 | test := roundinttests[i] |
Ian Lance Taylor | 4a99259 | 2011-10-26 16:19:57 -0700 | [diff] [blame] | 119 | d := NewDecimal(test.i) |
| 120 | d.Shift(test.shift) |
| 121 | int := d.RoundedInteger() |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 122 | if int != test.int { |
Ian Lance Taylor | bc35485 | 2010-11-10 21:50:23 -0800 | [diff] [blame] | 123 | t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v", |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 124 | test.i, test.shift, int, test.int) |
| 125 | } |
| 126 | } |
| 127 | } |