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