comparison test/SbpOperators/readoperator_test.jl @ 831:760c11e81fd4 operator_storage_array_of_table

Introduce parse_tuple and parse_scalar and replace all external calls to parse_rational
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 12 Jan 2022 15:32:58 +0100
parents 21ab60cc0a5c
children 06c510d40ebb
comparison
equal deleted inserted replaced
830:21ab60cc0a5c 831:760c11e81fd4
2 2
3 using TOML 3 using TOML
4 using Sbplib.SbpOperators 4 using Sbplib.SbpOperators
5 5
6 import Sbplib.SbpOperators.Stencil 6 import Sbplib.SbpOperators.Stencil
7
8
9 @testset "parse_rational" begin
10 @test SbpOperators.parse_rational("1") isa Rational
11 @test SbpOperators.parse_rational("1") == 1//1
12 @test SbpOperators.parse_rational("1/2") isa Rational
13 @test SbpOperators.parse_rational("1/2") == 1//2
14 @test SbpOperators.parse_rational("37/13") isa Rational
15 @test SbpOperators.parse_rational("37/13") == 37//13
16
17 @test SbpOperators.parse_rational(0.5) isa Rational
18 @test SbpOperators.parse_rational(0.5) == 1//2
19
20 @test SbpOperators.parse_rational("0.5") isa Rational
21 @test SbpOperators.parse_rational("0.5") == 1//2
22
23 @test SbpOperators.parse_rational(2) isa Rational
24 @test SbpOperators.parse_rational(2) == 2//1
25 end
26 7
27 @testset "readoperator" begin 8 @testset "readoperator" begin
28 toml_str = """ 9 toml_str = """
29 [meta] 10 [meta]
30 authors = "Ken Mattson" 11 authors = "Ken Mattson"
124 105
125 @test parse_stencil(Float64, TOML.parse(toml)["s1"]) == CenteredStencil(-1/12, 4/3, -5/2, 4/3, -1/12) 106 @test parse_stencil(Float64, TOML.parse(toml)["s1"]) == CenteredStencil(-1/12, 4/3, -5/2, 4/3, -1/12)
126 @test parse_stencil(Float64, TOML.parse(toml)["s2"]) == Stencil(2/1, -5/1, 4/1, -1/1, 0/1, 0/1; center=1) 107 @test parse_stencil(Float64, TOML.parse(toml)["s2"]) == Stencil(2/1, -5/1, 4/1, -1/1, 0/1, 0/1; center=1)
127 @test parse_stencil(Float64, TOML.parse(toml)["s3"]) == Stencil(1/1, -2/1, 1/1, 0/1, 0/1, 0/1; center=2) 108 @test parse_stencil(Float64, TOML.parse(toml)["s3"]) == Stencil(1/1, -2/1, 1/1, 0/1, 0/1, 0/1; center=2)
128 end 109 end
110
111 @testset "parse_scalar" begin
112 toml = TOML.parse("""
113 a1 = 1
114 a2 = 1.5
115 a3 = 1.0
116 a4 = 10
117 a5 = "1/2"
118 a6 = "1.5"
119
120 e1 = [1,2,3]
121 e2 = "a string value"
122 """)
123
124 @test parse_scalar(toml["a1"]) == 1//1
125 @test parse_scalar(toml["a2"]) == 3//2
126 @test parse_scalar(toml["a3"]) == 1//1
127 @test parse_scalar(toml["a4"]) == 10//1
128 @test parse_scalar(toml["a5"]) == 1//2
129 @test parse_scalar(toml["a6"]) == 3//2
130
131 @test_throws ArgumentError parse_scalar(toml["e1"])
132 @test_throws ArgumentError parse_scalar(toml["e2"])
133 end
134
135 @testset "parse_tuple" begin
136 toml = TOML.parse("""
137 t1 = [1,3,4]
138 t2 = ["1/2","3/4","2/1"]
139
140 e1 = "not a tuple"
141 e2.a="1"
142 e3 = 1
143 e4 = ["1/2","3/4","not a number"]
144 """)
145
146 @test parse_tuple(toml["t1"]) == (1//1,3//1,4//1)
147 @test parse_tuple(toml["t2"]) == (1//2,3//4,2//1)
148
149 @test_throws ArgumentError parse_tuple(toml["e1"])
150 @test_throws ArgumentError parse_tuple(toml["e2"])
151 @test_throws ArgumentError parse_tuple(toml["e3"])
152 @test_throws ArgumentError parse_tuple(toml["e4"])
153 end
129 end 154 end
155
156 @testset "parse_rational" begin
157 @test SbpOperators.parse_rational("1") isa Rational
158 @test SbpOperators.parse_rational("1") == 1//1
159 @test SbpOperators.parse_rational("1/2") isa Rational
160 @test SbpOperators.parse_rational("1/2") == 1//2
161 @test SbpOperators.parse_rational("37/13") isa Rational
162 @test SbpOperators.parse_rational("37/13") == 37//13
163
164 @test SbpOperators.parse_rational(0.5) isa Rational
165 @test SbpOperators.parse_rational(0.5) == 1//2
166
167 @test SbpOperators.parse_rational("0.5") isa Rational
168 @test SbpOperators.parse_rational("0.5") == 1//2
169
170 @test SbpOperators.parse_rational(2) isa Rational
171 @test SbpOperators.parse_rational(2) == 2//1
172 end