File: nan-inf.jl

package info (click to toggle)
julia 1.5.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 91,132 kB
  • sloc: lisp: 278,486; ansic: 60,186; cpp: 29,801; sh: 2,403; makefile: 1,998; pascal: 1,313; objc: 647; javascript: 516; asm: 226; python: 161; xml: 34
file content (35 lines) | stat: -rw-r--r-- 1,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@testset begin
    test_str = """
        {
            "x": NaN,
            "y": Infinity,
            "z": -Infinity,
            "q": [true, null, "hello", 1, -1, 1.5, -1.5, [true]]
        }"""

    test_dict = Dict(
        "x" => NaN,
        "y" => Inf,
        "z" => -Inf,
        "q" => [true, nothing, "hello", 1, -1, 1.5, -1.5, [true]]
    )

    @test_throws ErrorException JSON.parse(test_str, allownan=false)
    val = JSON.parse(test_str)
    @test isequal(val, test_dict)

    @test_throws ErrorException JSON.parse(IOBuffer(test_str), allownan=false)
    val2 = JSON.parse(IOBuffer(test_str))
    @test isequal(val2, test_dict)

    # Test that the number following -Infinity parses correctly
    @test isequal(JSON.parse("[-Infinity, 1]"), [-Inf, 1])
    @test isequal(JSON.parse("[-Infinity, -1]"), [-Inf, -1])
    @test isequal(JSON.parse("""{"a": -Infinity, "b": 1.0}"""), Dict("a" => -Inf, "b"=> 1.0))
    @test isequal(JSON.parse("""{"a": -Infinity, "b": -1.0}"""), Dict("a" => -Inf, "b"=> -1.0))

    @test isequal(JSON.parse(IOBuffer("[-Infinity, 1]")), [-Inf, 1])
    @test isequal(JSON.parse(IOBuffer("[-Infinity, -1]")), [-Inf, -1])
    @test isequal(JSON.parse(IOBuffer("""{"a": -Infinity, "b": 1.0}""")), Dict("a" => -Inf, "b"=> 1.0))
    @test isequal(JSON.parse(IOBuffer("""{"a": -Infinity, "b": -1.0}""")), Dict("a" => -Inf, "b"=> -1.0))
end