Skip to content

Commit 909bfc4

Browse files
committed
Fix int64 value handling in tests for cross-architecture compatibility
Update test cases with overflow errors on 32-bit (i386) architectures. Signed-off-by: Arthur Diniz <[email protected]>
1 parent a842674 commit 909bfc4

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

yaml_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ func strPtr(str string) *string {
3737
return &str
3838
}
3939

40+
// newIntPtr returns a pointer to a new int or int64 based on architecture.
41+
// On 64-bit platforms, it returns *int to test unsized int behavior.
42+
// On 32-bit platforms, it returns *int64 to test the large value that doesn't fit in 32-bit int.
43+
func newIntPtr() interface{} {
44+
if strconv.IntSize == 64 {
45+
return new(int)
46+
}
47+
return new(int64)
48+
}
49+
50+
// expectedIntValue returns the expected decoded value for architecture-specific int tests.
51+
// On 64-bit platforms, it returns the value as int.
52+
// On 32-bit platforms, it returns the value as int64 since the large value doesn't fit in 32-bit int.
53+
func expectedIntValue(value int64) interface{} {
54+
if strconv.IntSize == 64 {
55+
return int(value)
56+
}
57+
return value
58+
}
59+
4060
type errorType int
4161

4262
const (
@@ -183,7 +203,7 @@ func TestMarshal(t *testing.T) {
183203
f32String := strconv.FormatFloat(math.MaxFloat32, 'g', -1, 32)
184204
f64String := strconv.FormatFloat(math.MaxFloat64, 'g', -1, 64)
185205
s := MarshalTest{"a", math.MaxInt64, math.MaxFloat32, math.MaxFloat64}
186-
e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\nD: %s\n", math.MaxInt64, f32String, f64String))
206+
e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\nD: %s\n", int64(math.MaxInt64), f32String, f64String))
187207

188208
y, err := Marshal(s)
189209
if err != nil {
@@ -409,10 +429,15 @@ func TestUnmarshal(t *testing.T) {
409429
},
410430

411431
// decoding integers
412-
"decode 2^53 + 1 into int": {
432+
"decode 2^53 + 1 into int64": {
433+
encoded: []byte("9007199254740993"),
434+
decodeInto: new(int64),
435+
decoded: int64(9007199254740993),
436+
},
437+
"decode 2^53 + 1 into int (architecture-specific)": {
413438
encoded: []byte("9007199254740993"),
414-
decodeInto: new(int),
415-
decoded: 9007199254740993,
439+
decodeInto: newIntPtr(),
440+
decoded: expectedIntValue(9007199254740993),
416441
},
417442
"decode 2^53 + 1 into interface": {
418443
encoded: []byte("9007199254740993"),

0 commit comments

Comments
 (0)