MCPcopy
hub / github.com/shopspring/decimal / TestDecimal_QuoRem

Function TestDecimal_QuoRem

decimal_test.go:1911–1963  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1909}
1910
1911func TestDecimal_QuoRem(t *testing.T) {
1912 type Inp4 struct {
1913 d string
1914 d2 string
1915 exp int32
1916 q string
1917 r string
1918 }
1919 cases := []Inp4{
1920 {"10", "1", 0, "10", "0"},
1921 {"1", "10", 0, "0", "1"},
1922 {"1", "4", 2, "0.25", "0"},
1923 {"1", "8", 2, "0.12", "0.04"},
1924 {"10", "3", 1, "3.3", "0.1"},
1925 {"100", "3", 1, "33.3", "0.1"},
1926 {"1000", "10", -3, "0", "1000"},
1927 {"1e-3", "2e-5", 0, "50", "0"},
1928 {"1e-3", "2e-3", 1, "0.5", "0"},
1929 {"4e-3", "0.8", 4, "5e-3", "0"},
1930 {"4.1e-3", "0.8", 3, "5e-3", "1e-4"},
1931 {"-4", "-3", 0, "1", "-1"},
1932 {"-4", "3", 0, "-1", "-1"},
1933 }
1934
1935 for _, inp4 := range cases {
1936 d, _ := NewFromString(inp4.d)
1937 d2, _ := NewFromString(inp4.d2)
1938 prec := inp4.exp
1939 q, r := d.QuoRem(d2, prec)
1940 expectedQ, _ := NewFromString(inp4.q)
1941 expectedR, _ := NewFromString(inp4.r)
1942 if !q.Equal(expectedQ) || !r.Equal(expectedR) {
1943 t.Errorf("bad QuoRem division %s , %s , %d got %v, %v expected %s , %s",
1944 inp4.d, inp4.d2, prec, q, r, inp4.q, inp4.r)
1945 }
1946 if !d.Equal(d2.Mul(q).Add(r)) {
1947 t.Errorf("not fitting: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1948 d, d2, prec, q, r)
1949 }
1950 if !q.Equal(q.Truncate(prec)) {
1951 t.Errorf("quotient wrong precision: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1952 d, d2, prec, q, r)
1953 }
1954 if r.Abs().Cmp(d2.Abs().Mul(New(1, -prec))) >= 0 {
1955 t.Errorf("remainder too large: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1956 d, d2, prec, q, r)
1957 }
1958 if r.value.Sign()*d.value.Sign() < 0 {
1959 t.Errorf("signum of divisor and rest do not match: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1960 d, d2, prec, q, r)
1961 }
1962 }
1963}
1964
1965type DivTestCase struct {
1966 d Decimal

Callers

nothing calls this directly

Calls 10

NewFromStringFunction · 0.85
NewFunction · 0.85
QuoRemMethod · 0.80
EqualMethod · 0.80
AddMethod · 0.80
MulMethod · 0.80
TruncateMethod · 0.80
CmpMethod · 0.80
AbsMethod · 0.80
SignMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…