clerk/internal/decimal/decimal_test.go (view raw)
| 1 | package decimal |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestNewFromString(t *testing.T) { |
| 6 | tests := []struct{ in, want string }{ |
| 7 | {"10.00", "10"}, |
| 8 | {"150.60", "150.6"}, |
| 9 | {".33", "0.33"}, |
| 10 | {"1.", "1"}, |
| 11 | {"0001.2300", "1.23"}, |
| 12 | {"0", "0"}, |
| 13 | {"0.00", "0"}, |
| 14 | {"-0.00", "0"}, |
| 15 | {"-20", "-20"}, |
| 16 | {"-.75", "-0.75"}, |
| 17 | } |
| 18 | for _, tt := range tests { |
| 19 | got, err := FromString(tt.in) |
| 20 | if err != nil { |
| 21 | t.Fatalf("NewFromString(%q) unexpected error: %v", tt.in, err) |
| 22 | } |
| 23 | if got.String() != tt.want { |
| 24 | t.Fatalf("NewFromString(%q).String() = %q, want %q", tt.in, got.String(), tt.want) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestNewFromStringInvalid(t *testing.T) { |
| 30 | tests := []string{ |
| 31 | "", ".", "+", "-", |
| 32 | "1_000.00", "1,000.00", |
| 33 | "1..0", |
| 34 | "a1", "1a", |
| 35 | } |
| 36 | for _, in := range tests { |
| 37 | if _, err := FromString(in); err == nil { |
| 38 | t.Fatalf("NewFromString(%q) expected error", in) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestStringFixed(t *testing.T) { |
| 44 | tests := []struct { |
| 45 | in string |
| 46 | places int |
| 47 | decSep byte |
| 48 | thousandsSep byte |
| 49 | want string |
| 50 | }{ |
| 51 | {"0", 0, 0, 0, "0"}, |
| 52 | {"0", 1, 0, 0, "0.0"}, |
| 53 | {"0", 2, 0, 0, "0.00"}, |
| 54 | {"0", 3, 0, 0, "0.000"}, |
| 55 | {"-0", 2, 0, 0, "0.00"}, |
| 56 | {"10", 2, 0, 0, "10.00"}, |
| 57 | {"10", 0, 0, 0, "10"}, |
| 58 | {"1.2", 2, 0, 0, "1.20"}, |
| 59 | {"1.2", 1, 0, 0, "1.2"}, |
| 60 | {"1.2", 3, 0, 0, "1.200"}, |
| 61 | {"1.234", 2, 0, 0, "1.23"}, |
| 62 | {"1.235", 2, 0, 0, "1.23"}, |
| 63 | {"-1.2", 2, 0, 0, "-1.20"}, |
| 64 | {"-1.234", 2, 0, 0, "-1.23"}, |
| 65 | {"0.001", 2, 0, 0, "0.00"}, |
| 66 | {"0.001", 4, 0, 0, "0.0010"}, |
| 67 | {"123.456789", 3, 0, 0, "123.456"}, |
| 68 | {"-123.456789", 3, 0, 0, "-123.456"}, |
| 69 | {"1.23", 2, ',', 0, "1,23"}, |
| 70 | {"0", 2, ',', 0, "0,00"}, |
| 71 | {"-1.5", 2, ',', 0, "-1,50"}, |
| 72 | {"1234.56", 2, 0, ',', "1,234.56"}, |
| 73 | {"1234567.89", 2, 0, ',', "1,234,567.89"}, |
| 74 | {"123.45", 2, 0, ',', "123.45"}, |
| 75 | {"-1234.56", 2, 0, ',', "-1,234.56"}, |
| 76 | {"1234567.89", 2, ',', '.', "1.234.567,89"}, |
| 77 | {"-1234567.89", 2, ',', '.', "-1.234.567,89"}, |
| 78 | {"0", 2, ',', '.', "0,00"}, |
| 79 | } |
| 80 | for _, tt := range tests { |
| 81 | d, err := FromString(tt.in) |
| 82 | if err != nil { |
| 83 | t.Fatalf("FromString(%q) unexpected error: %v", tt.in, err) |
| 84 | } |
| 85 | if got := d.StringFixed(tt.places, tt.decSep, tt.thousandsSep); got != tt.want { |
| 86 | t.Fatalf("FromString(%q).StringFixed(%d, %q, %q) = %q, want %q", |
| 87 | tt.in, tt.places, tt.decSep, tt.thousandsSep, got, tt.want) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestArithmetic(t *testing.T) { |
| 93 | a, _ := FromString("1.20") |
| 94 | b, _ := FromString(".30") |
| 95 | |
| 96 | if got := a.Add(b).String(); got != "1.5" { |
| 97 | t.Fatalf("a+b = %q, want %q", got, "1.5") |
| 98 | } |
| 99 | if got := a.Sub(b).String(); got != "0.9" { |
| 100 | t.Fatalf("a-b = %q, want %q", got, "0.9") |
| 101 | } |
| 102 | if got := a.Mul(b).String(); got != "0.36" { |
| 103 | t.Fatalf("a*b = %q, want %q", got, "0.36") |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestCmpAndNeg(t *testing.T) { |
| 108 | a, _ := FromString("1.0") |
| 109 | b, _ := FromString("1") |
| 110 | c, _ := FromString("1.01") |
| 111 | |
| 112 | if a.Cmp(b) != 0 { |
| 113 | t.Fatalf("expected %q and %q to be equal", a.String(), b.String()) |
| 114 | } |
| 115 | if b.Cmp(c) >= 0 { |
| 116 | t.Fatalf("expected %q to be less than %q", b.String(), c.String()) |
| 117 | } |
| 118 | if got := b.Neg().String(); got != "-1" { |
| 119 | t.Fatalf("Neg(1) = %q, want %q", got, "-1") |
| 120 | } |
| 121 | if got := (Decimal{}).Neg().String(); got != "0" { |
| 122 | t.Fatalf("Neg(0) = %q, want %q", got, "0") |
| 123 | } |
| 124 | } |