Skip to content

Commit ae924a0

Browse files
committed
More tests on meshes
1 parent f457070 commit ae924a0

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

test/runtests.jl

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ function mesh_node(stp::Union{Number,NTuple{N,Number}}, org::Union{Real,NTuple{N
2828
return (idx .- org) .* stp
2929
end
3030

31+
function string_from_show(x, m=nothing)
32+
io = IOBuffer()
33+
if m === nothing
34+
show(io, x)
35+
else
36+
show(io, m, x)
37+
end
38+
return String(take!(io))
39+
end
40+
3141
@testset "StructuredArrays package" begin
3242

3343
@testset "Utilities" begin
@@ -545,6 +555,8 @@ end
545555

546556
@testset "Cartesian meshes (step=$stp, origin=$(repr(org)))" for (stp, org) in ((0.1f0, nothing),
547557
((0.1f0, 0.2f0), nothing),
558+
((1.4, 0.9), (11, 12)),
559+
((1//2, 3//4), (10.5, 4.7)),
548560
((0.1f0, 0.2f0, 0.3f0), (-1, 0, 1)))
549561
A = @inferred CartesianMesh(stp, org)
550562
stp′ = @inferred step(A)
@@ -598,17 +610,27 @@ end
598610

599611
inds = ntuple(Returns(-15:20), N)
600612
X = @inferred StructuredArray(A, inds)
601-
@test X === @inferred CartesianMeshArray(inds...; step=stp, origin=org)
602-
@test N === @inferred ndims(X)
603-
@test typeof(stp′) === @inferred step_type(X)
604-
@test typeof(org′) === @inferred origin_type(X)
605-
@test T === @inferred eltype(X)
606-
@test A_I === @inferred X[I...]
607-
@test A_J === @inferred X[J...]
608-
@test A_K === @inferred X[K...]
609-
@test A_I === @inferred X[CartesianIndex(I)]
610-
@test A_J === @inferred X[CartesianIndex(J)]
611-
@test A_K === @inferred X[CartesianIndex(K)]
613+
@test X === @inferred CartesianMeshArray(inds...; step=stp, origin=org)
614+
@test N === @inferred ndims(X)
615+
@test typeof(stp′) === @inferred step_type(X)
616+
@test typeof(org′) === @inferred origin_type(X)
617+
@test T === @inferred eltype(X)
618+
@test stp′ === @inferred step(X)
619+
@test org′ === @inferred origin(X)
620+
@test step(Tuple, A) === @inferred step(Tuple, X)
621+
@test origin(Tuple, A) === @inferred origin(Tuple, X)
622+
@test A_I === @inferred X[I...]
623+
@test A_J === @inferred X[J...]
624+
@test A_K === @inferred X[K...]
625+
@test A_I === @inferred X[CartesianIndex(I)]
626+
@test A_J === @inferred X[CartesianIndex(J)]
627+
@test A_K === @inferred X[CartesianIndex(K)]
628+
@test startswith(string_from_show(X), "CartesianMeshArray{")
629+
@test startswith(string_from_show(X, MIME"text/plain"()), "CartesianMeshArray{")
630+
631+
# Unary plus and minus.
632+
@test @inferred(+A) === A
633+
@test @inferred(-A) === @inferred(-one(R)*A)
612634

613635
# Multiplication of mesh by a scalar.
614636
B = @inferred 3A
@@ -628,6 +650,35 @@ end
628650
@test all(B(J) .≈ (A(J) ./ 2))
629651
@test all(B(K) .≈ (A(K) ./ 2))
630652

653+
# Shift of a mesh.
654+
s = map(convert_real_type(R), ((1:N)...,)) .* step(A)
655+
B = @inferred(A + s)
656+
@test all(B(I) .≈ (A(I) .+ s))
657+
@test all(B(J) .≈ (A(J) .+ s))
658+
@test all(B(K) .≈ (A(K) .+ s))
659+
@test B === @inferred(s + A)
660+
B = @inferred(A - s)
661+
@test all(B(I) .≈ (A(I) .- s))
662+
@test all(B(J) .≈ (A(J) .- s))
663+
@test all(B(K) .≈ (A(K) .- s))
664+
B = @inferred(s - A)
665+
@test all(B(I) .≈ (s .- A(I)))
666+
@test all(B(J) .≈ (s .- A(J)))
667+
@test all(B(K) .≈ (s .- A(K)))
668+
669+
# Comparison.
670+
z = ntuple(Returns(zero(R)), Val(N)) .* step(A)
671+
@test !isequal(A, CartesianMesh{N+1}(0.1, nothing))
672+
@test isequal(@inferred(A + z), A)
673+
@test isequal(@inferred(z + A), A)
674+
@test isequal(@inferred(A - z), A)
675+
@test isequal(@inferred(z - A), -A)
676+
@test A != CartesianMesh{N+1}(0.1, nothing)
677+
@test @inferred(A + z) == A
678+
@test @inferred(z + A) == A
679+
@test @inferred(A - z) == A
680+
@test @inferred(z - A) == -A
681+
631682
if stp isa Number && org isa Union{Nothing,Real}
632683
A = @inferred CartesianMesh{2}(stp, org)
633684
@test A isa CartesianMesh{2}

0 commit comments

Comments
 (0)