Skip to content

Commit 8e42699

Browse files
committed
add test cases for float64 and float32
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 9671ecd commit 8e42699

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

yaml_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ type UnmarshalTaggedStruct struct {
211211
IntBig2 string `json:"1000000000000000000000000000000000000"`
212212
IntBig2Scientific string `json:"1e+36"`
213213
Float3dot3 string `json:"3.3"`
214+
FloatMax32 string `json:"3.4028234663852886e+38"`
215+
FloatMax64 string `json:"1.7976931348623157e+308"`
214216
}
215217

216218
type UnmarshalStruct struct {
@@ -321,6 +323,16 @@ func TestUnmarshal(t *testing.T) {
321323
decodeInto: new(UnmarshalTaggedStruct),
322324
decoded: UnmarshalTaggedStruct{Float3dot3: "test"},
323325
},
326+
"tagged max float32 key": {
327+
encoded: []byte("3.4028234663852886e+38: test"),
328+
decodeInto: new(UnmarshalTaggedStruct),
329+
decoded: UnmarshalTaggedStruct{FloatMax32: "test"},
330+
},
331+
"tagged max float64 key": {
332+
encoded: []byte("1.7976931348623157e+308: test"),
333+
decodeInto: new(UnmarshalTaggedStruct),
334+
decoded: UnmarshalTaggedStruct{FloatMax64: "test"},
335+
},
324336

325337
// decode into string field
326338
"string value into string field": {
@@ -438,6 +450,38 @@ func TestUnmarshal(t *testing.T) {
438450
},
439451
},
440452

453+
// decoding floats
454+
"decode max float32 into float32": {
455+
encoded: []byte("3.4028234663852886e+38"),
456+
decodeInto: new(float32),
457+
decoded: math.MaxFloat32,
458+
},
459+
"decode max float32 into interface": {
460+
encoded: []byte("3.4028234663852886e+38"),
461+
decodeInto: new(interface{}),
462+
decoded: math.MaxFloat32,
463+
},
464+
"decode max float32 into string": {
465+
encoded: []byte("3.4028234663852886e+38"),
466+
decodeInto: new(string),
467+
decoded: "3.4028234663852886e+38",
468+
},
469+
"decode max float64 into float64": {
470+
encoded: []byte("1.7976931348623157e+308"),
471+
decodeInto: new(float64),
472+
decoded: math.MaxFloat64,
473+
},
474+
"decode max float64 into interface": {
475+
encoded: []byte("1.7976931348623157e+308"),
476+
decodeInto: new(interface{}),
477+
decoded: math.MaxFloat64,
478+
},
479+
"decode max float64 into string": {
480+
encoded: []byte("1.7976931348623157e+308"),
481+
decodeInto: new(string),
482+
decoded: "1.7976931348623157e+308",
483+
},
484+
441485
// duplicate (non-casematched) keys (NOTE: this is very non-ideal behaviour!)
442486
"decode duplicate (non-casematched) into nested struct 1": {
443487
encoded: []byte("a:\n a: 1\n b: 1\n c: test\n\nA:\n a: 2"),

0 commit comments

Comments
 (0)