-
Notifications
You must be signed in to change notification settings - Fork 62
/
context_test.go
91 lines (83 loc) · 2.27 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package decimal
import "testing"
func TestCondition_String(t *testing.T) {
for i, test := range [...]struct {
c Condition
s string
}{
{Clamped, "clamped"},
{Clamped | Underflow, "clamped, underflow"},
{Inexact | Rounded | Subnormal, "inexact, rounded, subnormal"},
{1 << 31, "unknown(2147483648)"},
} {
s := test.c.String()
if s != test.s {
t.Fatalf("#%d: wanted %q, got %q", i, test.s, s)
}
}
}
func TestToNearestAwayQuoRounding(t *testing.T) {
for _, test := range [...]struct {
x string
y string
expected string // x div y round 20
}{
{"1", "9", "0.11111111111111111111"},
{"2", "9", "0.22222222222222222222"},
{"3", "9", "0.33333333333333333333"},
{"4", "9", "0.44444444444444444444"},
{"5", "9", "0.55555555555555555556"},
{"6", "9", "0.66666666666666666667"},
{"7", "9", "0.77777777777777777778"},
{"8", "9", "0.88888888888888888889"},
} {
x, _ := WithContext(Context128).SetString(test.x)
y, _ := WithContext(Context128).SetString(test.y)
z, _ := WithContext(Context128).SetString("0")
z.Context.Precision = 20
z.Context.RoundingMode = ToNearestAway
actual := z.Quo(x, y).String()
expected := test.expected
if actual != expected {
t.Errorf("Quo(%s,%s) result %s, expected %s", test.x, test.y, actual, expected)
}
}
}
func TestNonStandardRoundingModes(t *testing.T) {
for i, test := range [...]struct {
value int64
mode RoundingMode
expected int64
}{
{55, ToNearestTowardZero, 5},
{25, ToNearestTowardZero, 2},
{16, ToNearestTowardZero, 2},
{11, ToNearestTowardZero, 1},
{10, ToNearestTowardZero, 1},
{-10, ToNearestTowardZero, -1},
{-11, ToNearestTowardZero, -1},
{-16, ToNearestTowardZero, -2},
{-25, ToNearestTowardZero, -2},
{-55, ToNearestTowardZero, -5},
{55, AwayFromZero, 6},
{25, AwayFromZero, 3},
{16, AwayFromZero, 2},
{11, AwayFromZero, 2},
{10, AwayFromZero, 1},
{-10, AwayFromZero, -1},
{-11, AwayFromZero, -2},
{-16, AwayFromZero, -2},
{-25, AwayFromZero, -3},
{-55, AwayFromZero, -6},
} {
v := New(test.value, 1)
v.Context.RoundingMode = test.mode
r, ok := v.RoundToInt().Int64()
if !ok {
t.Fatalf("#%d: failed to convert result to int64", i)
}
if test.expected != r {
t.Fatalf("#%d: wanted %d, got %d", i, test.expected, r)
}
}
}