|
| 1 | +package decimal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestDecomposerRoundTrip(t *testing.T) { |
| 9 | + list := []struct { |
| 10 | + N string // Name. |
| 11 | + S string // String value. |
| 12 | + E bool // Expect an error. |
| 13 | + }{ |
| 14 | + {N: "Normal-1", S: "123.456"}, |
| 15 | + {N: "Normal-2", S: "-123.456"}, |
| 16 | + {N: "NaN-1", S: "NaN"}, |
| 17 | + {N: "NaN-2", S: "-NaN"}, |
| 18 | + {N: "Infinity-1", S: "Infinity"}, |
| 19 | + {N: "Infinity-2", S: "-Infinity"}, |
| 20 | + } |
| 21 | + for _, item := range list { |
| 22 | + t.Run(item.N, func(t *testing.T) { |
| 23 | + d := &Big{} |
| 24 | + d, ok := d.SetString(item.S) |
| 25 | + if !ok { |
| 26 | + t.Fatal("unable to set value") |
| 27 | + } |
| 28 | + set := &Big{} |
| 29 | + err := set.Compose(d.Decompose(nil)) |
| 30 | + if err == nil && item.E { |
| 31 | + t.Fatal("expected error, got <nil>") |
| 32 | + } |
| 33 | + if err != nil && !item.E { |
| 34 | + t.Fatalf("unexpected error: %v", err) |
| 35 | + } |
| 36 | + if set.Cmp(d) != 0 { |
| 37 | + t.Fatalf("values incorrect, got %v want %v (%s)", set, d, item.S) |
| 38 | + } |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestDecomposerCompose(t *testing.T) { |
| 44 | + list := []struct { |
| 45 | + N string // Name. |
| 46 | + S string // String value. |
| 47 | + |
| 48 | + Form byte // Form |
| 49 | + Neg bool |
| 50 | + Coef []byte // Coefficent |
| 51 | + Exp int32 |
| 52 | + |
| 53 | + Err bool // Expect an error. |
| 54 | + }{ |
| 55 | + {N: "Zero", S: "0", Coef: nil, Exp: 0}, |
| 56 | + {N: "Normal-1", S: "123.456", Coef: []byte{0x40, 0xE2, 0x01}, Exp: -3}, |
| 57 | + {N: "Neg-1", S: "-123.456", Neg: true, Coef: []byte{0x40, 0xE2, 0x01}, Exp: -3}, |
| 58 | + {N: "PosExp-1", S: "123456000", Coef: []byte{0x40, 0xE2, 0x01}, Exp: 3}, |
| 59 | + {N: "PosExp-2", S: "-123456000", Neg: true, Coef: []byte{0x40, 0xE2, 0x01}, Exp: 3}, |
| 60 | + {N: "AllDec-1", S: "0.123456", Coef: []byte{0x40, 0xE2, 0x01}, Exp: -6}, |
| 61 | + {N: "AllDec-2", S: "-0.123456", Neg: true, Coef: []byte{0x40, 0xE2, 0x01}, Exp: -6}, |
| 62 | + {N: "NaN-1", S: "NaN", Form: 2}, |
| 63 | + {N: "Infinity-1", S: "Infinity", Form: 1}, |
| 64 | + {N: "Infinity-2", S: "-Infinity", Form: 1, Neg: true}, |
| 65 | + } |
| 66 | + |
| 67 | + for _, item := range list { |
| 68 | + t.Run(item.N, func(t *testing.T) { |
| 69 | + d := &Big{} |
| 70 | + d, ok := d.SetString(item.S) |
| 71 | + if !ok { |
| 72 | + t.Fatal("unable to set value") |
| 73 | + } |
| 74 | + err := d.Compose(item.Form, item.Neg, item.Coef, item.Exp) |
| 75 | + if err != nil && !item.Err { |
| 76 | + t.Fatalf("unexpected error, got %v", err) |
| 77 | + } |
| 78 | + if item.Err { |
| 79 | + if err == nil { |
| 80 | + t.Fatal("expected error, got <nil>") |
| 81 | + } |
| 82 | + return |
| 83 | + } |
| 84 | + if s := fmt.Sprintf("%f", d); s != item.S { |
| 85 | + t.Fatalf("unexpected value, got %q want %q", s, item.S) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments