Skip to content

Commit 1cf2c1a

Browse files
committed
test: add tests for PiecewiseConstantFunction
1 parent 6701497 commit 1cf2c1a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

test/runtests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ using TestSetExtensions
77
@safetestset "Aqua tests" include("Aqua.jl")
88

99
@testset "ConservationLawsParticles.jl" begin
10-
@safetestset "Densities" include("test_densities.jl")
11-
@safetestset "Velocities" include("test_velocities.jl")
12-
@safetestset "Utils" include("test_utils.jl")
10+
@safetestset "Densities" include("test_densities.jl")
11+
@safetestset "Velocities" include("test_velocities.jl")
12+
@safetestset "PC functions" include("test_pcf.jl")
13+
@safetestset "Utils" include("test_utils.jl")
1314
end
1415

1516
end

test/test_pcf.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using ConservationLawsParticles
2+
const PCF = PiecewiseConstantFunction
3+
4+
@testset "map" begin
5+
pcf = PCF([0., 1.], [0., 1., 2.])
6+
@test map(x -> x+1, pcf) == PCF([0., 1.], [1., 2., 3.])
7+
end
8+
9+
@testset "abs" begin
10+
pcf = PCF([0., 1.], [-1., 0., 1.])
11+
@test abs(pcf) == PCF([0., 1.], [1., 0., 1.])
12+
end
13+
14+
@testset "ops" begin
15+
f = PCF([ 0., 1., 2., 3.], [0., 1., 2., -1., 4.])
16+
g = PCF([-1., 0.5, 1., 2.5], [0., 1., -1., 0., 1.])
17+
@test f + g == PCF([-1., 0., 0.5, 1., 2., 2.5, 3.], [0., 1., 2., 0., 2., -1., 0., 5.])
18+
@test f - g == PCF([-1., 0., 0.5, 1., 2., 2.5, 3.], [0., -1., 0., 2., 2., -1., -2., 3.])
19+
@test f * g == PCF([-1., 0., 0.5, 1., 2., 2.5, 3.], [0., 0., 1., -1., 0., 0., -1., 4.])
20+
f_g = f / g # cannot use == directly because f_g.values contains NaN
21+
@test f_g.cuts == [-1., 0., 0.5, 1., 2., 2.5, 3.]
22+
@test isnan(f_g.values[1])
23+
@test f_g.values[2:end] == [0., 1., -1., Inf, -Inf, -1., 4.]
24+
end
25+
26+
@testset "lpnorm" begin
27+
pcf = PCF([0., 1., 11.], [0., 2., 3., 0.])
28+
@test lpnorm(pcf, 1) == 2 + 30
29+
@test lpnorm(pcf, 2) == sqrt(2^2 + 10*3^2)
30+
@test lpnorm(pcf, Inf) == 3
31+
32+
pcf = PCF([0., 1.], [1., 1., 1.])
33+
# @test lpnorm(pcf, 1) == Inf
34+
@test lpnorm(pcf, Inf) == 1
35+
end

0 commit comments

Comments
 (0)