TestSize tests the Size method on BigInt and Decimal. Unlike Sizeof, which returns the shallow size of the structs, the Size method reports the total memory footprint of each struct and all referenced objects.
(t *testing.T)
| 870 | // returns the shallow size of the structs, the Size method reports the total |
| 871 | // memory footprint of each struct and all referenced objects. |
| 872 | func TestSize(t *testing.T) { |
| 873 | // map[uint_size][is_inline][type]size |
| 874 | exp := map[int]map[bool]map[string]uintptr{ |
| 875 | 32: { |
| 876 | true: { |
| 877 | "BigInt": 20, |
| 878 | "Decimal": 28, |
| 879 | }, |
| 880 | false: { |
| 881 | "BigInt": 72, |
| 882 | "Decimal": 80, |
| 883 | }, |
| 884 | }, |
| 885 | 64: { |
| 886 | true: { |
| 887 | "BigInt": 24, |
| 888 | "Decimal": 32, |
| 889 | }, |
| 890 | false: { |
| 891 | "BigInt": 112, |
| 892 | "Decimal": 120, |
| 893 | }, |
| 894 | }, |
| 895 | }[bits.UintSize] |
| 896 | |
| 897 | var d Decimal |
| 898 | if e, s := exp[true]["Decimal"], d.Size(); e != s { |
| 899 | t.Errorf("(*Decimal).Size() != %d: %d", e, s) |
| 900 | } |
| 901 | if e, s := exp[true]["BigInt"], d.Coeff.Size(); e != s { |
| 902 | t.Errorf("(*BigInt).Size() != %d: %d", e, s) |
| 903 | } |
| 904 | // Set to an inlinable value. |
| 905 | d.SetInt64(1234) |
| 906 | if e, s := exp[true]["Decimal"], d.Size(); e != s { |
| 907 | t.Errorf("(*Decimal).Size() != %d: %d", e, s) |
| 908 | } |
| 909 | if e, s := exp[true]["BigInt"], d.Coeff.Size(); e != s { |
| 910 | t.Errorf("(*BigInt).Size() != %d: %d", e, s) |
| 911 | } |
| 912 | // Set to a non-inlinable value. |
| 913 | if _, _, err := d.SetString("123456789123456789123456789.123456789123456789"); err != nil { |
| 914 | t.Fatal(err) |
| 915 | } |
| 916 | if d.Coeff.isInline() { |
| 917 | // Sanity-check, in case inlineWords changes. |
| 918 | t.Fatal("BigInt inlined large value. Did inlineWords change?") |
| 919 | } |
| 920 | if e, s := exp[false]["Decimal"], d.Size(); e != s { |
| 921 | t.Errorf("(*Decimal).Size() != %d: %d", e, s) |
| 922 | } |
| 923 | if e, s := exp[false]["BigInt"], d.Coeff.Size(); e != s { |
| 924 | t.Errorf("(*BigInt).Size() != %d: %d", e, s) |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | func TestJSONEncoding(t *testing.T) { |
| 929 | var encodingTests = []string{ |