@@ -18,6 +18,7 @@ import (
1818 "encoding/json"
1919 "fmt"
2020 "math"
21+ "math/bits"
2122 "testing"
2223 "unsafe"
2324)
@@ -787,16 +788,30 @@ func TestReduce(t *testing.T) {
787788// TestSizeof is meant to catch changes that unexpectedly increase
788789// the size of the BigInt, Decimal, and Context structs.
789790func TestSizeof (t * testing.T ) {
791+ // map[uint_size][type]sizeof
792+ exp := map [int ]map [string ]uintptr {
793+ 32 : {
794+ "BigInt" : 20 ,
795+ "Decimal" : 28 ,
796+ "Context" : 24 ,
797+ },
798+ 64 : {
799+ "BigInt" : 24 ,
800+ "Decimal" : 32 ,
801+ "Context" : 32 ,
802+ },
803+ }[bits .UintSize ]
804+
790805 var b BigInt
791- if s := unsafe .Sizeof (b ); s != 24 {
806+ if s := unsafe .Sizeof (b ); s != exp [ "BigInt" ] {
792807 t .Errorf ("sizeof(BigInt) changed: %d" , s )
793808 }
794809 var d Decimal
795- if s := unsafe .Sizeof (d ); s != 32 {
810+ if s := unsafe .Sizeof (d ); s != exp [ "Decimal" ] {
796811 t .Errorf ("sizeof(Decimal) changed: %d" , s )
797812 }
798813 var c Context
799- if s := unsafe .Sizeof (c ); s != 32 {
814+ if s := unsafe .Sizeof (c ); s != exp [ "Context" ] {
800815 t .Errorf ("sizeof(Context) changed: %d" , s )
801816 }
802817}
@@ -805,19 +820,43 @@ func TestSizeof(t *testing.T) {
805820// returns the shallow size of the structs, the Size method reports the total
806821// memory footprint of each struct and all referenced objects.
807822func TestSize (t * testing.T ) {
823+ // map[uint_size][is_inline][type]size
824+ exp := map [int ]map [bool ]map [string ]uintptr {
825+ 32 : {
826+ true : {
827+ "BigInt" : 20 ,
828+ "Decimal" : 28 ,
829+ },
830+ false : {
831+ "BigInt" : 72 ,
832+ "Decimal" : 80 ,
833+ },
834+ },
835+ 64 : {
836+ true : {
837+ "BigInt" : 24 ,
838+ "Decimal" : 32 ,
839+ },
840+ false : {
841+ "BigInt" : 112 ,
842+ "Decimal" : 120 ,
843+ },
844+ },
845+ }[bits .UintSize ]
846+
808847 var d Decimal
809- if e , s := uintptr ( 32 ) , d .Size (); e != s {
848+ if e , s := exp [ true ][ "Decimal" ] , d .Size (); e != s {
810849 t .Errorf ("(*Decimal).Size() != %d: %d" , e , s )
811850 }
812- if e , s := uintptr ( 24 ) , d .Coeff .Size (); e != s {
851+ if e , s := exp [ true ][ "BigInt" ] , d .Coeff .Size (); e != s {
813852 t .Errorf ("(*BigInt).Size() != %d: %d" , e , s )
814853 }
815854 // Set to an inlinable value.
816855 d .SetInt64 (1234 )
817- if e , s := uintptr ( 32 ) , d .Size (); e != s {
856+ if e , s := exp [ true ][ "Decimal" ] , d .Size (); e != s {
818857 t .Errorf ("(*Decimal).Size() != %d: %d" , e , s )
819858 }
820- if e , s := uintptr ( 24 ) , d .Coeff .Size (); e != s {
859+ if e , s := exp [ true ][ "BigInt" ] , d .Coeff .Size (); e != s {
821860 t .Errorf ("(*BigInt).Size() != %d: %d" , e , s )
822861 }
823862 // Set to a non-inlinable value.
@@ -828,10 +867,10 @@ func TestSize(t *testing.T) {
828867 // Sanity-check, in case inlineWords changes.
829868 t .Fatal ("BigInt inlined large value. Did inlineWords change?" )
830869 }
831- if e , s := uintptr ( 120 ) , d .Size (); e != s {
870+ if e , s := exp [ false ][ "Decimal" ] , d .Size (); e != s {
832871 t .Errorf ("(*Decimal).Size() != %d: %d" , e , s )
833872 }
834- if e , s := uintptr ( 112 ) , d .Coeff .Size (); e != s {
873+ if e , s := exp [ false ][ "BigInt" ] , d .Coeff .Size (); e != s {
835874 t .Errorf ("(*BigInt).Size() != %d: %d" , e , s )
836875 }
837876}
0 commit comments