|
32 | 32 | end |
33 | 33 |
|
34 | 34 | @testset "setproperties" begin |
35 | | - o = AB(1,2) |
36 | | - @test setproperties(o, (a=2, b=3)) === AB(2,3) |
37 | | - @test setproperties(o, (a=2, b=3.0)) === AB(2,3.0) |
38 | | - @test setproperties(o, a=2, b=3.0) === AB(2,3.0) |
39 | 35 |
|
40 | | - res = @test_throws ArgumentError setproperties(o, (a=2, this_field_does_not_exist=3.0)) |
| 36 | + @test setproperties(NamedTuple(), NamedTuple()) === NamedTuple() |
| 37 | + @test setproperties((), NamedTuple()) === () |
| 38 | + @test setproperties(NamedTuple(), ()) === NamedTuple() |
| 39 | + @test setproperties((), ()) === () |
| 40 | + @test setproperties(1, ()) === 1 |
| 41 | + @test setproperties(1, NamedTuple()) === 1 |
| 42 | + |
| 43 | + @test setproperties((1,), ()) === (1,) |
| 44 | + @test setproperties((1,), NamedTuple()) === (1,) |
| 45 | + @test setproperties((a=1,), ()) === (a=1,) |
| 46 | + @test setproperties((a=1,), NamedTuple()) === (a=1,) |
| 47 | + @test setproperties(AB(1,2), ()) === AB(1,2) |
| 48 | + @test setproperties(AB(1,2), NamedTuple()) === AB(1,2) |
| 49 | + |
| 50 | + @test setproperties(AB(1,2), (a=2, b=3)) === AB(2,3) |
| 51 | + @test setproperties(AB(1,2), (a=2, b=3.0)) === AB(2,3.0) |
| 52 | + @test setproperties(AB(1,2), a=2, b=3.0) === AB(2,3.0) |
| 53 | + |
| 54 | + res = @test_throws ArgumentError setproperties(AB(1,2), (a=2, this_field_does_not_exist=3.0)) |
41 | 55 | msg = sprint(showerror, res.value) |
42 | 56 | @test occursin("this_field_does_not_exist", msg) |
43 | 57 | @test occursin("overload", msg) |
44 | 58 | @test occursin("ConstructionBase.setproperties", msg) |
45 | 59 |
|
46 | | - res = @test_throws ArgumentError setproperties(o, a=2, this_field_does_not_exist=3.0) |
| 60 | + res = @test_throws ArgumentError setproperties(AB(1,2), a=2, this_field_does_not_exist=3.0) |
47 | 61 | msg = sprint(showerror, res.value) |
48 | 62 | @test occursin("this_field_does_not_exist", msg) |
49 | 63 | @test occursin("overload", msg) |
|
58 | 72 | @test setproperties((a=1, b=2), (a=1.0,)) === (a=1.0, b=2) |
59 | 73 | @test setproperties((a=1, b=2), a=1.0) === (a=1.0, b=2) |
60 | 74 |
|
61 | | - @inferred setproperties(o, a=2, b=3.0) |
| 75 | + @inferred setproperties(AB(1,2), a=2, b=3.0) |
62 | 76 | @inferred setproperties(Empty(), NamedTuple()) |
63 | 77 | @inferred setproperties((a=1, b=2), a=1.0) |
64 | 78 | @inferred setproperties((a=1, b=2), (a=1.0,)) |
65 | 79 |
|
66 | | - @test setproperties((),()) === () |
67 | 80 | @test setproperties((1,), ()) === (1,) |
68 | 81 | @test setproperties((1,), (10,)) === (10,) |
69 | 82 | @test_throws ArgumentError setproperties((1,), (10,20)) === (10,) |
|
75 | 88 | @test setproperties((1,2,3), (10.0,20,30)) === (10.0,20,30) |
76 | 89 | @test_throws ArgumentError setproperties((1,2,3), (10.0,20,30,40)) |
77 | 90 |
|
78 | | - @test_throws MethodError setproperties((a=1,b=2), (10,20)) |
| 91 | + @test_throws ArgumentError setproperties((a=1,b=2), (10,20)) |
79 | 92 | @test_throws ArgumentError setproperties((), (10,)) |
80 | 93 | @test_throws ArgumentError setproperties((1,2), (a=10,b=20)) |
81 | 94 | end |
@@ -225,29 +238,76 @@ end |
225 | 238 | end |
226 | 239 | end |
227 | 240 |
|
228 | | - |
229 | | -function funny_numbers(n) |
| 241 | +function funny_numbers(n)::Tuple |
230 | 242 | types = [ |
231 | 243 | Int128, Int16, Int32, Int64, Int8, |
232 | 244 | UInt128, UInt16, UInt32, UInt64, UInt8, |
233 | 245 | Float16, Float32, Float64, |
234 | 246 | ] |
235 | | - [T(true) for T in rand(types, n)] |
| 247 | + Tuple([T(true) for T in rand(types, n)]) |
| 248 | +end |
| 249 | + |
| 250 | +function funny_numbers(::Type{NamedTuple}, n)::NamedTuple |
| 251 | + t = funny_numbers(n) |
| 252 | + pairs = map(1:n) do i |
| 253 | + Symbol("a$i") => t[i] |
| 254 | + end |
| 255 | + (;pairs...) |
| 256 | +end |
| 257 | + |
| 258 | +for n in [0,1,20,40] |
| 259 | + Sn = Symbol("S$n") |
| 260 | + types = [Symbol("A$i") for i in 1:n] |
| 261 | + fields = [Symbol("a$i") for i in 1:n] |
| 262 | + typed_fields = [:($ai::$Ai) for (ai,Ai) in zip(fields, types)] |
| 263 | + @eval struct $(Sn){$(types...)} |
| 264 | + $(typed_fields...) |
| 265 | + end |
| 266 | + @eval funny_numbers(::Type{$Sn}) = ($Sn)(funny_numbers($n)...) |
236 | 267 | end |
237 | 268 |
|
238 | 269 | @testset "inference" begin |
239 | 270 | @testset "Tuple n=$n" for n in [0,1,2,3,4,5,10,20,30,40] |
240 | | - t = Tuple(funny_numbers(n)) |
| 271 | + t = funny_numbers(n) |
241 | 272 | @test length(t) == n |
242 | 273 | @test getproperties(t) === t |
243 | 274 | @inferred getproperties(t) |
244 | 275 | for k in 0:n |
245 | | - t2 = Tuple(funny_numbers(k)) |
| 276 | + t2 = funny_numbers(k) |
246 | 277 | @inferred setproperties(t, t2) |
247 | 278 | @test setproperties(t, t2)[1:k] === t2 |
248 | 279 | @test setproperties(t, t2) isa Tuple |
249 | 280 | @test length(setproperties(t, t2)) == n |
250 | 281 | @test setproperties(t, t2)[k+1:n] === t[k+1:n] |
251 | 282 | end |
252 | 283 | end |
| 284 | + @inferred getproperties(funny_numbers(100)) |
| 285 | + @inferred setproperties(funny_numbers(100), funny_numbers(90)) |
| 286 | + @testset "NamedTuple n=$n" for n in [0,1,2,3,4,5,10,20,30,40] |
| 287 | + nt = funny_numbers(NamedTuple, n) |
| 288 | + @test nt isa NamedTuple |
| 289 | + @test length(nt) == n |
| 290 | + @test getproperties(nt) === nt |
| 291 | + @inferred getproperties(nt) |
| 292 | + for k in 0:n |
| 293 | + nt2 = funny_numbers(NamedTuple, k) |
| 294 | + @inferred setproperties(nt, nt2) |
| 295 | + @test Tuple(setproperties(nt, nt2))[1:k] === Tuple(nt2) |
| 296 | + @test setproperties(nt, nt2) isa NamedTuple |
| 297 | + @test length(setproperties(nt, nt2)) == n |
| 298 | + @test Tuple(setproperties(nt, nt2))[k+1:n] === Tuple(nt)[k+1:n] |
| 299 | + end |
| 300 | + end |
| 301 | + @inferred getproperties(funny_numbers(NamedTuple, 100)) |
| 302 | + @inferred setproperties(funny_numbers(NamedTuple, 100), funny_numbers(NamedTuple, 90)) |
| 303 | + |
| 304 | + |
| 305 | + @inferred setproperties(funny_numbers(S0), funny_numbers(NamedTuple, 0)) |
| 306 | + @inferred setproperties(funny_numbers(S1), funny_numbers(NamedTuple, 1)) |
| 307 | + @inferred setproperties(funny_numbers(S20), funny_numbers(NamedTuple, 18)) |
| 308 | + @inferred setproperties(funny_numbers(S40), funny_numbers(NamedTuple, 38)) |
| 309 | + @inferred getproperties(funny_numbers(S0)) |
| 310 | + @inferred getproperties(funny_numbers(S1)) |
| 311 | + @inferred getproperties(funny_numbers(S20)) |
| 312 | + @inferred getproperties(funny_numbers(S40)) |
253 | 313 | end |
0 commit comments