github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/compile/internal/big/decimal_test.go (about) 1 // Copyright 2015 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 big 6 7 import "testing" 8 9 func TestDecimalString(t *testing.T) { 10 for _, test := range []struct { 11 x decimal 12 want string 13 }{ 14 {want: "0"}, 15 {decimal{nil, 1000}, "0"}, // exponent of 0 is ignored 16 {decimal{[]byte("12345"), 0}, "0.12345"}, 17 {decimal{[]byte("12345"), -3}, "0.00012345"}, 18 {decimal{[]byte("12345"), +3}, "123.45"}, 19 {decimal{[]byte("12345"), +10}, "1234500000"}, 20 } { 21 if got := test.x.String(); got != test.want { 22 t.Errorf("%v == %s; want %s", test.x, got, test.want) 23 } 24 } 25 } 26 27 func TestDecimalInit(t *testing.T) { 28 for _, test := range []struct { 29 x Word 30 shift int 31 want string 32 }{ 33 {0, 0, "0"}, 34 {0, -100, "0"}, 35 {0, 100, "0"}, 36 {1, 0, "1"}, 37 {1, 10, "1024"}, 38 {1, 100, "1267650600228229401496703205376"}, 39 {1, -100, "0.0000000000000000000000000000007888609052210118054117285652827862296732064351090230047702789306640625"}, 40 {12345678, 8, "3160493568"}, 41 {12345678, -8, "48225.3046875"}, 42 {195312, 9, "99999744"}, 43 {1953125, 9, "1000000000"}, 44 } { 45 var d decimal 46 d.init(nat{test.x}.norm(), test.shift) 47 if got := d.String(); got != test.want { 48 t.Errorf("%d << %d == %s; want %s", test.x, test.shift, got, test.want) 49 } 50 } 51 } 52 53 func TestDecimalRounding(t *testing.T) { 54 for _, test := range []struct { 55 x uint64 56 n int 57 down, even, up string 58 }{ 59 {0, 0, "0", "0", "0"}, 60 {0, 1, "0", "0", "0"}, 61 62 {1, 0, "0", "0", "10"}, 63 {5, 0, "0", "0", "10"}, 64 {9, 0, "0", "10", "10"}, 65 66 {15, 1, "10", "20", "20"}, 67 {45, 1, "40", "40", "50"}, 68 {95, 1, "90", "100", "100"}, 69 70 {12344999, 4, "12340000", "12340000", "12350000"}, 71 {12345000, 4, "12340000", "12340000", "12350000"}, 72 {12345001, 4, "12340000", "12350000", "12350000"}, 73 {23454999, 4, "23450000", "23450000", "23460000"}, 74 {23455000, 4, "23450000", "23460000", "23460000"}, 75 {23455001, 4, "23450000", "23460000", "23460000"}, 76 77 {99994999, 4, "99990000", "99990000", "100000000"}, 78 {99995000, 4, "99990000", "100000000", "100000000"}, 79 {99999999, 4, "99990000", "100000000", "100000000"}, 80 81 {12994999, 4, "12990000", "12990000", "13000000"}, 82 {12995000, 4, "12990000", "13000000", "13000000"}, 83 {12999999, 4, "12990000", "13000000", "13000000"}, 84 } { 85 x := nat(nil).setUint64(test.x) 86 87 var d decimal 88 d.init(x, 0) 89 d.roundDown(test.n) 90 if got := d.String(); got != test.down { 91 t.Errorf("roundDown(%d, %d) = %s; want %s", test.x, test.n, got, test.down) 92 } 93 94 d.init(x, 0) 95 d.round(test.n) 96 if got := d.String(); got != test.even { 97 t.Errorf("round(%d, %d) = %s; want %s", test.x, test.n, got, test.even) 98 } 99 100 d.init(x, 0) 101 d.roundUp(test.n) 102 if got := d.String(); got != test.up { 103 t.Errorf("roundUp(%d, %d) = %s; want %s", test.x, test.n, got, test.up) 104 } 105 } 106 }