diff --git a/numeric_test.go b/numeric_test.go index 785e8b0..80300ef 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -865,6 +865,56 @@ func Test_Gte(t *testing.T) { } } +// 8.81% coverage + +func Test_ToU128(t *testing.T) { + testExamples := []struct { + label string + input Numeric + expectation U128 + }{ + {"U8(255))=>U128(255)", U8(255), NewU128(255)}, + {"I8(127)=>U128(127)", I8(127), NewU128(127)}, + {"U16(65535)=>U128(65535)", U16(65535), NewU128(65535)}, + {"I16(32767)=>U128(32767)", I16(32767), NewU128(32767)}, + {"U32(4294967295)=>U128(4294967295)", U32(4294967295), NewU128(4294967295)}, + {"I32(2147483647)=>U128(2147483647)", I32(2147483647), NewU128(2147483647)}, + {"U64(18446744073709551615)=>U128(18446744073709551615)", U64(18446744073709551615), NewU128(uint64(18446744073709551615))}, + {"I64(9223372036854775807)=>U128(9223372036854775807)", I64(9223372036854775807), NewU128(uint64(9223372036854775807))}, + } + + for _, testExample := range testExamples { + t.Run(testExample.label, func(t *testing.T) { + result := To[U128](testExample.input) + assert.Equal(t, testExample.expectation, result) + }) + } +} + +func Test_ToI128(t *testing.T) { + testExamples := []struct { + label string + input Numeric + expectation I128 + }{ + {"U8(255))=>I128(255)", U8(255), NewI128(255)}, + {"I8(127)=>I128(127)", I8(127), NewI128(127)}, + {"U16(65535)=>I128(65535)", U16(65535), NewI128(65535)}, + {"I16(32767)=>I128(32767)", I16(32767), NewI128(32767)}, + {"U32(4294967295)=>I128(4294967295)", U32(4294967295), NewI128(4294967295)}, + {"I32(2147483647)=>I128(2147483647)", I32(2147483647), NewI128(2147483647)}, + // {"U64(18446744073709551615)=>I128(18446744073709551615)", U64(18446744073709551615), NewI128(uint64(18446744073709551615))}, + {"I64(9223372036854775807)=>I128(9223372036854775807)", I64(9223372036854775807), NewI128(uint64(9223372036854775807))}, + } + + for _, testExample := range testExamples { + t.Run(testExample.label, func(t *testing.T) { + result := To[I128](testExample.input) + assert.Equal(t, testExample.expectation, result) + }) + } +} + func Test_U128ToBigInt(t *testing.T) { bnMaxU128Value, _ := new(big.Int).SetString("340282366920938463463374607431768211455", 10) @@ -906,7 +956,7 @@ func Test_I128ToBigInt(t *testing.T) { } func Test_anyIntegerToU128(t *testing.T) { - // maxU128, _ := NewU128FromString("18446744073709551615") + u1, _ := NewU128FromString("340282366920938463463374607431768211455") testExamples := []struct { label string @@ -919,7 +969,7 @@ func Test_anyIntegerToU128(t *testing.T) { {"uint32(math.MaxUint32)=>NewU128(math.MaxUint32)", uint32(math.MaxUint32), NewU128(math.MaxUint32)}, {"uint64(math.MaxUint64)=>NewU128(uint64(math.MaxUint64))", uint64(math.MaxUint64), NewU128(uint64(math.MaxUint64))}, {"big.NewInt(123456789)=>U128(123456789)", big.NewInt(123456789), NewU128(123456789)}, - // {"NewU128(18446744073709551615)=>MaxU128()", maxU128, MaxU128()}, + {"NewU128(340282366920938463463374607431768211455)=>MaxU128()", u1, MaxU128()}, } for _, testExample := range testExamples {