| 1830 | } |
| 1831 | |
| 1832 | func TestDecimal_Div(t *testing.T) { |
| 1833 | type Inp struct { |
| 1834 | a string |
| 1835 | b string |
| 1836 | } |
| 1837 | |
| 1838 | inputs := map[Inp]string{ |
| 1839 | Inp{"6", "3"}: "2", |
| 1840 | Inp{"10", "2"}: "5", |
| 1841 | Inp{"2.2", "1.1"}: "2", |
| 1842 | Inp{"-2.2", "-1.1"}: "2", |
| 1843 | Inp{"12.88", "5.6"}: "2.3", |
| 1844 | Inp{"1023427554493", "43432632"}: "23563.5628642767953828", // rounded |
| 1845 | Inp{"1", "434324545566634"}: "0.0000000000000023", |
| 1846 | Inp{"1", "3"}: "0.3333333333333333", |
| 1847 | Inp{"2", "3"}: "0.6666666666666667", // rounded |
| 1848 | Inp{"10000", "3"}: "3333.3333333333333333", |
| 1849 | Inp{"10234274355545544493", "-3"}: "-3411424785181848164.3333333333333333", |
| 1850 | Inp{"-4612301402398.4753343454", "23.5"}: "-196268144782.9138440146978723", |
| 1851 | } |
| 1852 | |
| 1853 | for inp, expectedStr := range inputs { |
| 1854 | num, err := NewFromString(inp.a) |
| 1855 | if err != nil { |
| 1856 | t.FailNow() |
| 1857 | } |
| 1858 | denom, err := NewFromString(inp.b) |
| 1859 | if err != nil { |
| 1860 | t.FailNow() |
| 1861 | } |
| 1862 | got := num.Div(denom) |
| 1863 | expected, _ := NewFromString(expectedStr) |
| 1864 | if !got.Equal(expected) { |
| 1865 | t.Errorf("expected %v when dividing %v by %v, got %v", |
| 1866 | expected, num, denom, got) |
| 1867 | } |
| 1868 | got2 := num.DivRound(denom, int32(DivisionPrecision)) |
| 1869 | if !got2.Equal(expected) { |
| 1870 | t.Errorf("expected %v on DivRound (%v,%v), got %v", expected, num, denom, got2) |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | type Inp2 struct { |
| 1875 | n int64 |
| 1876 | exp int32 |
| 1877 | n2 int64 |
| 1878 | exp2 int32 |
| 1879 | } |
| 1880 | |
| 1881 | // test code path where exp > 0 |
| 1882 | inputs2 := map[Inp2]string{ |
| 1883 | Inp2{124, 10, 3, 1}: "41333333333.3333333333333333", |
| 1884 | Inp2{124, 10, 3, 0}: "413333333333.3333333333333333", |
| 1885 | Inp2{124, 10, 6, 1}: "20666666666.6666666666666667", |
| 1886 | Inp2{124, 10, 6, 0}: "206666666666.6666666666666667", |
| 1887 | Inp2{10, 10, 10, 1}: "1000000000", |
| 1888 | } |
| 1889 | |