1 package decimal
2
3 import "testing"
4
5 func TestConstApproximation(t *testing.T) {
6 for _, testCase := range []struct {
7 Const string
8 Precision int32
9 ExpectedApproximation string
10 }{
11 {"2.3025850929940456840179914546", 0, "2"},
12 {"2.3025850929940456840179914546", 1, "2.3"},
13 {"2.3025850929940456840179914546", 3, "2.302"},
14 {"2.3025850929940456840179914546", 5, "2.302585"},
15 {"2.3025850929940456840179914546", 10, "2.302585092994045"},
16 {"2.3025850929940456840179914546", 100, "2.3025850929940456840179914546"},
17 {"2.3025850929940456840179914546", -1, "2"},
18 {"2.3025850929940456840179914546", -5, "2"},
19 {"3.14159265359", 0, "3"},
20 {"3.14159265359", 1, "3.1"},
21 {"3.14159265359", 2, "3.141"},
22 {"3.14159265359", 4, "3.1415926"},
23 {"3.14159265359", 13, "3.14159265359"},
24 } {
25 ca := newConstApproximation(testCase.Const)
26 expected, _ := NewFromString(testCase.ExpectedApproximation)
27
28 approximation := ca.withPrecision(testCase.Precision)
29
30 if approximation.Cmp(expected) != 0 {
31 t.Errorf("expected approximation %s, got %s - for const with %s precision %d", testCase.ExpectedApproximation, approximation.String(), testCase.Const, testCase.Precision)
32 }
33 }
34 }
35
View as plain text