| 1 |
From: Arthur Diniz <[email protected]> |
| 2 |
Date: Fri, 15 Nov 2024 16:59:35 +0000 |
| 3 |
Subject: Fix failed tests on 32 bit arch |
| 4 |
Forwarded: https://github.com/kubernetes-sigs/yaml/pull/120 |
| 5 |
|
| 6 |
* Updated test cases in `goyaml.v2` and `goyaml.v3` `decode_test.go` to |
| 7 |
explicitly use `int64` for representing large integer values. |
| 8 |
* Adjusted `yaml_test.go` to: |
| 9 |
- Use `int64` for `math.MaxInt64` in marshaling tests. |
| 10 |
- Decode integers greater than `2^53` into `int64` instead of `int` |
| 11 |
to ensure proper handling of large values. |
| 12 |
|
| 13 |
These changes address potential issues with integer overflow and ensure |
| 14 |
compatibility across platforms with differing integer size representations. |
| 15 |
|
| 16 |
|
| 17 |
--- |
| 18 |
goyaml.v2/decode_test.go | 25 ++++++++++++++++++++++--- |
| 19 |
goyaml.v3/decode_test.go | 25 ++++++++++++++++++++++--- |
| 20 |
yaml_test.go | 6 +++--- |
| 21 |
3 files changed, 47 insertions(+), 9 deletions(-) |
| 22 |
|
| 23 |
diff --git a/goyaml.v2/decode_test.go b/goyaml.v2/decode_test.go |
| 24 |
index ce3ae0b..c76f020 100644 |
| 25 |
--- a/goyaml.v2/decode_test.go |
| 26 |
+++ b/goyaml.v2/decode_test.go |
| 27 |
@@ -131,7 +131,7 @@ var unmarshalTests = []struct { |
| 28 |
map[string]interface{}{"bin": -42}, |
| 29 |
}, { |
| 30 |
"bin: -0b1000000000000000000000000000000000000000000000000000000000000000", |
| 31 |
- map[string]interface{}{"bin": -9223372036854775808}, |
| 32 |
+ map[string]interface{}{"bin": int64(-9223372036854775808)}, |
| 33 |
}, { |
| 34 |
"decimal: +685_230", |
| 35 |
map[string]int{"decimal": 685230}, |
| 36 |
@@ -747,6 +747,25 @@ type inlineC struct { |
| 37 |
C int |
| 38 |
} |
| 39 |
|
| 40 |
+func assertDeepEqualWithAlignedTypes(c *C, actual interface{}, expected interface{}, err error) { |
| 41 |
+ // Ensure type consistency for comparison |
| 42 |
+ expectedMap, isExpectedMap := expected.(map[string]interface{}) |
| 43 |
+ actualMap, isActualMap := actual.(map[string]interface{}) |
| 44 |
+ if isExpectedMap && isActualMap { |
| 45 |
+ for key, actualVal := range actualMap { |
| 46 |
+ if expectedVal, exists := expectedMap[key]; exists { |
| 47 |
+ // Align types if the expected value is int64 |
| 48 |
+ if _, ok := expectedVal.(int64); ok { |
| 49 |
+ if actualInt, ok := actualVal.(int); ok { |
| 50 |
+ actualMap[key] = int64(actualInt) |
| 51 |
+ } |
| 52 |
+ } |
| 53 |
+ } |
| 54 |
+ } |
| 55 |
+ } |
| 56 |
+ c.Assert(actual, DeepEquals, expected, Commentf("error: %v", err)) |
| 57 |
+} |
| 58 |
+ |
| 59 |
func (s *S) TestUnmarshal(c *C) { |
| 60 |
for i, item := range unmarshalTests { |
| 61 |
c.Logf("test %d: %q", i, item.data) |
| 62 |
@@ -756,7 +775,7 @@ func (s *S) TestUnmarshal(c *C) { |
| 63 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 64 |
c.Assert(err, IsNil) |
| 65 |
} |
| 66 |
- c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err)) |
| 67 |
+ assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@@ -787,7 +806,7 @@ func (s *S) TestDecoderSingleDocument(c *C) { |
| 72 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 73 |
c.Assert(err, IsNil) |
| 74 |
} |
| 75 |
- c.Assert(value.Elem().Interface(), DeepEquals, item.value) |
| 76 |
+ assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
diff --git a/goyaml.v3/decode_test.go b/goyaml.v3/decode_test.go |
| 81 |
index c2f7888..4cef6ba 100644 |
| 82 |
--- a/goyaml.v3/decode_test.go |
| 83 |
+++ b/goyaml.v3/decode_test.go |
| 84 |
@@ -177,7 +177,7 @@ var unmarshalTests = []struct { |
| 85 |
map[string]interface{}{"bin": -42}, |
| 86 |
}, { |
| 87 |
"bin: -0b1000000000000000000000000000000000000000000000000000000000000000", |
| 88 |
- map[string]interface{}{"bin": -9223372036854775808}, |
| 89 |
+ map[string]interface{}{"bin": int64(-9223372036854775808)}, |
| 90 |
}, { |
| 91 |
"decimal: +685_230", |
| 92 |
map[string]int{"decimal": 685230}, |
| 93 |
@@ -820,6 +820,25 @@ type inlineD struct { |
| 94 |
D int |
| 95 |
} |
| 96 |
|
| 97 |
+func assertDeepEqualWithAlignedTypes(c *C, actual interface{}, expected interface{}, err error) { |
| 98 |
+ // Ensure type consistency for comparison |
| 99 |
+ expectedMap, isExpectedMap := expected.(map[string]interface{}) |
| 100 |
+ actualMap, isActualMap := actual.(map[string]interface{}) |
| 101 |
+ if isExpectedMap && isActualMap { |
| 102 |
+ for key, actualVal := range actualMap { |
| 103 |
+ if expectedVal, exists := expectedMap[key]; exists { |
| 104 |
+ // Align types if the expected value is int64 |
| 105 |
+ if _, ok := expectedVal.(int64); ok { |
| 106 |
+ if actualInt, ok := actualVal.(int); ok { |
| 107 |
+ actualMap[key] = int64(actualInt) |
| 108 |
+ } |
| 109 |
+ } |
| 110 |
+ } |
| 111 |
+ } |
| 112 |
+ } |
| 113 |
+ c.Assert(actual, DeepEquals, expected, Commentf("error: %v", err)) |
| 114 |
+} |
| 115 |
+ |
| 116 |
func (s *S) TestUnmarshal(c *C) { |
| 117 |
for i, item := range unmarshalTests { |
| 118 |
c.Logf("test %d: %q", i, item.data) |
| 119 |
@@ -829,7 +848,7 @@ func (s *S) TestUnmarshal(c *C) { |
| 120 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 121 |
c.Assert(err, IsNil) |
| 122 |
} |
| 123 |
- c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err)) |
| 124 |
+ assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@@ -859,7 +878,7 @@ func (s *S) TestDecoderSingleDocument(c *C) { |
| 129 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 130 |
c.Assert(err, IsNil) |
| 131 |
} |
| 132 |
- c.Assert(value.Elem().Interface(), DeepEquals, item.value) |
| 133 |
+ assertDeepEqualWithAlignedTypes(c, value.Elem().Interface(), item.value, err) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
diff --git a/yaml_test.go b/yaml_test.go |
| 138 |
index 00cb371..489d5f0 100644 |
| 139 |
--- a/yaml_test.go |
| 140 |
+++ b/yaml_test.go |
| 141 |
@@ -183,7 +183,7 @@ type MarshalTest struct { |
| 142 |
func TestMarshal(t *testing.T) { |
| 143 |
f32String := strconv.FormatFloat(math.MaxFloat32, 'g', -1, 32) |
| 144 |
s := MarshalTest{"a", math.MaxInt64, math.MaxFloat32} |
| 145 |
- e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", math.MaxInt64, f32String)) |
| 146 |
+ e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", int64(math.MaxInt64), f32String)) |
| 147 |
|
| 148 |
y, err := Marshal(s) |
| 149 |
if err != nil { |
| 150 |
@@ -411,8 +411,8 @@ func TestUnmarshal(t *testing.T) { |
| 151 |
// decoding integers |
| 152 |
"decode 2^53 + 1 into int": { |
| 153 |
encoded: []byte("9007199254740993"), |
| 154 |
- decodeInto: new(int), |
| 155 |
- decoded: 9007199254740993, |
| 156 |
+ decodeInto: new(int64), |
| 157 |
+ decoded: int64(9007199254740993), |
| 158 |
}, |
| 159 |
"decode 2^53 + 1 into interface": { |
| 160 |
encoded: []byte("9007199254740993"), |