From: Arthur Diniz Date: Fri, 15 Nov 2024 16:59:35 +0000 Subject: Fix failed tests on 32 bit arch Forwarded: https://github.com/kubernetes-sigs/yaml/pull/120 * Updated test cases in `goyaml.v2` and `goyaml.v3` `decode_test.go` to explicitly use `int64` for representing large integer values. * Adjusted `yaml_test.go` to: - Use `int64` for `math.MaxInt64` in marshaling tests. - Decode integers greater than `2^53` into `int64` instead of `int` to ensure proper handling of large values. These changes address potential issues with integer overflow and ensure compatibility across platforms with differing integer size representations. --- goyaml.v2/decode_test.go | 25 ++++++++++++++++++++++--- goyaml.v3/decode_test.go | 25 ++++++++++++++++++++++--- yaml_test.go | 6 +++--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/goyaml.v2/decode_test.go b/goyaml.v2/decode_test.go index ce3ae0b..c76f020 100644 --- a/goyaml.v2/decode_test.go +++ b/goyaml.v2/decode_test.go @@ -131,7 +131,7 @@ var unmarshalTests = []struct { map[string]interface{}{"bin": -42}, }, { "bin: -0b1000000000000000000000000000000000000000000000000000000000000000", - map[string]interface{}{"bin": -9223372036854775808}, + map[string]interface{}{"bin": int64(-9223372036854775808)}, }, { "decimal: +685_230", map[string]int{"decimal": 685230}, @@ -747,6 +747,25 @@ type inlineC struct { C int } +func assertDeepEqualWithAlignedTypes(c *C, actual interface{}, expected interface{}, err error) { + // Ensure type consistency for comparison + expectedMap, isExpectedMap := expected.(map[string]interface{}) + actualMap, isActualMap := actual.(map[string]interface{}) + if isExpectedMap && isActualMap { + for key, actualVal := range actualMap { + if expectedVal, exists := expectedMap[key]; exists { + // Align types if the expected value is int64 + if _, ok := expectedVal.(int64); ok { + if actualInt, ok := actualVal.(int); ok { + actualMap[key] = int64(actualInt) + } + } + } + } + } + c.Assert(actual, DeepEquals, expected, Commentf("error: %v", err)) +} + func (s *S) TestUnmarshal(c *C) { for i, item := range unmarshalTests { c.Logf("test %d: %q", i, item.data) @@ -756,7 +775,7 @@ func (s *S) TestUnmarshal(c *C) { if _, ok := err.(*yaml.TypeError); !ok { c.Assert(err, IsNil) } - c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err)) + assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) } } @@ -787,7 +806,7 @@ func (s *S) TestDecoderSingleDocument(c *C) { if _, ok := err.(*yaml.TypeError); !ok { c.Assert(err, IsNil) } - c.Assert(value.Elem().Interface(), DeepEquals, item.value) + assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) } } diff --git a/goyaml.v3/decode_test.go b/goyaml.v3/decode_test.go index c2f7888..4cef6ba 100644 --- a/goyaml.v3/decode_test.go +++ b/goyaml.v3/decode_test.go @@ -177,7 +177,7 @@ var unmarshalTests = []struct { map[string]interface{}{"bin": -42}, }, { "bin: -0b1000000000000000000000000000000000000000000000000000000000000000", - map[string]interface{}{"bin": -9223372036854775808}, + map[string]interface{}{"bin": int64(-9223372036854775808)}, }, { "decimal: +685_230", map[string]int{"decimal": 685230}, @@ -820,6 +820,25 @@ type inlineD struct { D int } +func assertDeepEqualWithAlignedTypes(c *C, actual interface{}, expected interface{}, err error) { + // Ensure type consistency for comparison + expectedMap, isExpectedMap := expected.(map[string]interface{}) + actualMap, isActualMap := actual.(map[string]interface{}) + if isExpectedMap && isActualMap { + for key, actualVal := range actualMap { + if expectedVal, exists := expectedMap[key]; exists { + // Align types if the expected value is int64 + if _, ok := expectedVal.(int64); ok { + if actualInt, ok := actualVal.(int); ok { + actualMap[key] = int64(actualInt) + } + } + } + } + } + c.Assert(actual, DeepEquals, expected, Commentf("error: %v", err)) +} + func (s *S) TestUnmarshal(c *C) { for i, item := range unmarshalTests { c.Logf("test %d: %q", i, item.data) @@ -829,7 +848,7 @@ func (s *S) TestUnmarshal(c *C) { if _, ok := err.(*yaml.TypeError); !ok { c.Assert(err, IsNil) } - c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err)) + assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) } } @@ -859,7 +878,7 @@ func (s *S) TestDecoderSingleDocument(c *C) { if _, ok := err.(*yaml.TypeError); !ok { c.Assert(err, IsNil) } - c.Assert(value.Elem().Interface(), DeepEquals, item.value) + assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) } } diff --git a/yaml_test.go b/yaml_test.go index 00cb371..489d5f0 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -183,7 +183,7 @@ type MarshalTest struct { func TestMarshal(t *testing.T) { f32String := strconv.FormatFloat(math.MaxFloat32, 'g', -1, 32) s := MarshalTest{"a", math.MaxInt64, math.MaxFloat32} - e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", math.MaxInt64, f32String)) + e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", int64(math.MaxInt64), f32String)) y, err := Marshal(s) if err != nil { @@ -411,8 +411,8 @@ func TestUnmarshal(t *testing.T) { // decoding integers "decode 2^53 + 1 into int": { encoded: []byte("9007199254740993"), - decodeInto: new(int), - decoded: 9007199254740993, + decodeInto: new(int64), + decoded: int64(9007199254740993), }, "decode 2^53 + 1 into interface": { encoded: []byte("9007199254740993"),