Last active
April 25, 2024 16:23
-
-
Save alpe/5a27babec8005fa347fdd0f0094b1a75 to your computer and use it in GitHub Desktop.
test Quo edge cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestEdgeCases(t *testing.T) { | |
one := mustV(NewDecFromString("1")) | |
zero := mustV(NewDecFromString("0")) | |
NaN := mustV(NewDecFromString("NaN")) | |
big := NewDecFinite(math.MaxInt64, 100_000-1) | |
small := NewDecFinite(1, -100_000) | |
specs := map[string]struct { | |
a, b Dec | |
expVal Dec | |
expErr bool | |
}{ | |
"div by 0": { | |
a: one, b: zero, expErr: true, | |
}, | |
"div by Nan": { | |
a: one, b: NaN, expErr: true, | |
}, | |
"out of precision range - after comma": { | |
a: one, b: NewDecFinite(1, 35), expErr: true, | |
}, | |
"out of precision range - before comma": { | |
a: NewDecFinite(1, 34), b: NewDecFinite(1, -1), expErr: true, | |
}, | |
"-1/-1=1": { | |
a: NewDecFromInt64(-1), b: NewDecFromInt64(-1), expVal: NewDecFromInt64(1), | |
}, | |
"-1/1=-1": { | |
a: NewDecFromInt64(-1), b: NewDecFromInt64(1), expVal: NewDecFromInt64(-1), | |
}, | |
"1/-1=-1": { | |
a: NewDecFromInt64(1), b: NewDecFromInt64(-1), expVal: NewDecFromInt64(-1), | |
}, | |
"equal with trailing zeros": { | |
a: mustV(NewDecFromString("0.10000")), b: NewDecFromInt64(1), expVal: mustV(NewDecFromString("0.1")), | |
}, | |
"big/big":{ | |
a: big, b: big, expVal: one, | |
}, | |
"small/small":{ | |
a: small, b: small, expVal: one, | |
}, | |
"exceed max exponent":{ | |
a: big, b: mustV(NewDecFromString("0.1")), expErr: true, | |
}, | |
"exceed min exponent":{ | |
a: small, b: mustV(NewDecFromString("10")), expErr: true, | |
}, | |
} | |
for name, spec := range specs { | |
t.Run(name, func(t *testing.T) { | |
gotV, gotErr := spec.a.Quo(spec.b) | |
if spec.expErr { | |
require.Error(t, gotErr, gotV) | |
return | |
} | |
require.NoError(t, gotErr, gotV) | |
require.True(t, spec.expVal.Equal(gotV)) // not same but equal | |
}) | |
} | |
} | |
func mustV[T any](v T, err error) T { | |
if err != nil { | |
panic(err) | |
} | |
return v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment