Skip to content

Commit e11e6d6

Browse files
committed
Add some tests
1 parent 10a426d commit e11e6d6

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

test/build_function.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@ using ModelingToolkit, Test
22
@variables a b c1 c2 c3 d e g
33

44
# Multiple argument matrix
5-
h = [a + b + c1 + c2; c3 + d + e + g] # uses the same number of arguments as our application
6-
h_julia(a, b, c, d, e, g) = [a[1] + b[1] + c[1] + c[2]; c[3] + d[1] + e[1] + g[1]]
5+
h = [a + b + c1 + c2,
6+
c3 + d + e + g,
7+
0] # uses the same number of arguments as our application
8+
h_julia(a, b, c, d, e, g) = [a[1] + b[1] + c[1] + c[2],
9+
c[3] + d[1] + e[1] + g[1],
10+
0]
711
function h_julia!(out, a, b, c, d, e, g)
8-
out .= [a[1] + b[1] + c[1] + c[2]; c[3] + d[1] + e[1] + g[1]]
12+
out .= [a[1] + b[1] + c[1] + c[2], c[3] + d[1] + e[1] + g[1], 0]
913
end
1014

1115
h_str = ModelingToolkit.build_function(h, [a], [b], [c1, c2, c3], [d], [e], [g])
1216
h_oop = eval(h_str[1])
1317
h_ip! = eval(h_str[2])
18+
h_ip_skip! = eval(ModelingToolkit.build_function(h, [a], [b], [c1, c2, c3], [d], [e], [g], skipzeros=true)[2])
1419
inputs = ([1], [2], [3, 4, 5], [6], [7], [8])
1520

1621
@test h_oop(inputs...) == h_julia(inputs...)
17-
out_1 = Array{Int64}(undef, 2)
22+
out_1 = similar(h, Int)
1823
out_2 = similar(out_1)
1924
h_ip!(out_1, inputs...)
2025
h_julia!(out_2, inputs...)
2126
@test out_1 == out_2
27+
fill!(out_1, 10)
28+
h_ip_skip!(out_1, inputs...)
29+
@test out_1[3] == 10
30+
out_1[3] = 0
31+
@test out_1 == out_2
2232

2333
# Multiple input matrix, some unused arguments
2434
h_skip = [a + b + c1; c2 + c3 + g] # skip d, e

test/build_function_arrayofarray.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ function h_dense_arraymat_julia!(out, x)
1818
out[3] .= [a[1] c[1]; 1 0]
1919
end
2020

21-
h_dense_arraymat_str = ModelingToolkit.build_function(h_dense_arraymat, [a, b, c])
22-
h_dense_arraymat_ip! = eval(h_dense_arraymat_str[2])
23-
out_1_arraymat = [Array{Int64}(undef, 2, 2) for i in 1:3]
24-
out_2_arraymat = [similar(x) for x in out_1_arraymat]
21+
h_dense_arraymat_ip! = eval(ModelingToolkit.build_function(h_dense_arraymat, [a, b, c])[2])
22+
h_dense_arraymat_ip_skip! = eval(ModelingToolkit.build_function(h_dense_arraymat, [a, b, c], skipzeros=true)[2])
23+
out_1_arraymat = [fill(42, 2, 2) for i in 1:3]
24+
out_2_arraymat = deepcopy(out_1_arraymat)
2525
h_dense_arraymat_julia!(out_1_arraymat, input)
26+
h_dense_arraymat_ip_skip!(out_2_arraymat, input)
27+
@test all(isequal(42), out_2_arraymat[2])
28+
foreach(mat->fill!(mat, 0), out_2_arraymat)
29+
h_dense_arraymat_ip_skip!(out_2_arraymat, input)
30+
@test out_1_arraymat == out_2_arraymat
2631
h_dense_arraymat_ip!(out_2_arraymat, input)
2732
@test out_1_arraymat == out_2_arraymat
2833

0 commit comments

Comments
 (0)