MCPcopy
hub / github.com/tidwall/gjson / TestNaNInf

Function TestNaNInf

gjson_test.go:2342–2377  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2340}
2341
2342func TestNaNInf(t *testing.T) {
2343 json := `[+Inf,-Inf,Inf,iNF,-iNF,+iNF,NaN,nan,nAn,-0,+0]`
2344 raws := []string{"+Inf", "-Inf", "Inf", "iNF", "-iNF", "+iNF", "NaN", "nan",
2345 "nAn", "-0", "+0"}
2346 nums := []float64{math.Inf(+1), math.Inf(-1), math.Inf(0), math.Inf(0),
2347 math.Inf(-1), math.Inf(+1), math.NaN(), math.NaN(), math.NaN(),
2348 math.Copysign(0, -1), 0}
2349
2350 assert(t, int(Get(json, `#`).Int()) == len(raws))
2351 for i := 0; i < len(raws); i++ {
2352 r := Get(json, fmt.Sprintf("%d", i))
2353 assert(t, r.Raw == raws[i])
2354 assert(t, r.Num == nums[i] || (math.IsNaN(r.Num) && math.IsNaN(nums[i])))
2355 assert(t, r.Type == Number)
2356 }
2357
2358 var i int
2359 Parse(json).ForEach(func(_, r Result) bool {
2360 assert(t, r.Raw == raws[i])
2361 assert(t, r.Num == nums[i] || (math.IsNaN(r.Num) && math.IsNaN(nums[i])))
2362 assert(t, r.Type == Number)
2363 i++
2364 return true
2365 })
2366
2367 // Parse should also return valid numbers
2368 assert(t, math.IsNaN(Parse("nan").Float()))
2369 assert(t, math.IsNaN(Parse("NaN").Float()))
2370 assert(t, math.IsNaN(Parse(" NaN").Float()))
2371 assert(t, math.IsInf(Parse("+inf").Float(), +1))
2372 assert(t, math.IsInf(Parse("-inf").Float(), -1))
2373 assert(t, math.IsInf(Parse("+INF").Float(), +1))
2374 assert(t, math.IsInf(Parse("-INF").Float(), -1))
2375 assert(t, math.IsInf(Parse(" +INF").Float(), +1))
2376 assert(t, math.IsInf(Parse(" -INF").Float(), -1))
2377}
2378
2379func TestEmptyValueQuery(t *testing.T) {
2380 // issue: https://github.com/tidwall/gjson/issues/246

Callers

nothing calls this directly

Calls 6

assertFunction · 0.85
GetFunction · 0.85
ParseFunction · 0.85
IntMethod · 0.80
ForEachMethod · 0.80
FloatMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…