Skip to content

Commit 3191a39

Browse files
committed
Increase test coverage
1 parent fadf1b0 commit 3191a39

2 files changed

Lines changed: 95 additions & 16 deletions

File tree

ext/VortexStepMethodControlPlotsExt.jl

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,37 @@ function generate_polar_data(
584584
return polar_data, reynolds_number[1]
585585
end
586586

587+
function _extract_literature_polar_data(raw_data, path)
588+
table, header = if raw_data isa Tuple
589+
# readdlm(...; header=true) returns (data, header)
590+
raw_table, raw_header = raw_data
591+
raw_table, lowercase.(strip.(string.(vec(raw_header))))
592+
else
593+
# Header is in first row when a single matrix is returned
594+
raw_data[2:end, :], lowercase.(strip.(string.(raw_data[1, :])))
595+
end
596+
597+
# Find column indices for alpha, CL, CD, CS (case-insensitive, allow common variants)
598+
alpha_idx = findfirst(x -> occursin("alpha", x) || x == "aoa", header)
599+
cl_idx = findfirst(x -> occursin("cl", x), header)
600+
cd_idx = findfirst(x -> occursin("cd", x), header)
601+
cs_idx = findfirst(x -> occursin("cs", x), header)
602+
603+
(isnothing(alpha_idx) || isnothing(cl_idx) || isnothing(cd_idx)) &&
604+
throw(ArgumentError("Literature CSV must contain alpha/aoa, cl and cd columns: $path"))
605+
606+
# Fallback: if CS not found, fill with zeros
607+
cs_col = cs_idx === nothing ? zeros(size(table, 1)) : table[:, cs_idx]
608+
609+
# Return as [alpha, CL, CD, CS]
610+
return [
611+
table[:, alpha_idx],
612+
table[:, cl_idx],
613+
table[:, cd_idx],
614+
cs_col
615+
]
616+
end
617+
587618
"""
588619
plot_polars(solver_list, body_aero_list, label_list;
589620
literature_path_list=String[],
@@ -658,22 +689,8 @@ function VortexStepMethod.plot_polars(
658689
# Load literature data if provided
659690
if !isempty(literature_path_list)
660691
for path in literature_path_list
661-
data = readdlm(path, ',')
662-
header = lowercase.(string.(data[1, :]))
663-
# Find column indices for alpha, CL, CD, CS (case-insensitive, allow common variants)
664-
alpha_idx = findfirst(x -> occursin("alpha", x), header)
665-
cl_idx = findfirst(x -> occursin("cl", x), header)
666-
cd_idx = findfirst(x -> occursin("cd", x), header)
667-
cs_idx = findfirst(x -> occursin("cs", x), header)
668-
# Fallback: if CS not found, fill with zeros
669-
cs_col = cs_idx === nothing ? zeros(size(data, 1)-1) : data[2:end, cs_idx]
670-
# Push as [alpha, CL, CD, CS]
671-
push!(polar_data_list, [
672-
data[2:end, alpha_idx],
673-
data[2:end, cl_idx],
674-
data[2:end, cd_idx],
675-
cs_col
676-
])
692+
raw_data = readdlm(path, ',')
693+
push!(polar_data_list, _extract_literature_polar_data(raw_data, path))
677694
end
678695
end
679696

test/plotting/test_plotting.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,68 @@ end
227227
show_plot(fig_dpi)
228228
@test fig_dpi.get_dpi() == 130
229229
plt.close(fig_dpi)
230+
231+
ext = Base.get_extension(VortexStepMethod, :VortexStepMethodControlPlotsExt)
232+
@test ext !== nothing
233+
234+
# Unit-test tuple parsing branch (e.g. readdlm(...; header=true) shape).
235+
tuple_table = [0.0 0.10 0.010; 5.0 0.20 0.020]
236+
tuple_header = [" AoA " "CL" "CD"]
237+
tuple_pd = ext._extract_literature_polar_data((tuple_table, tuple_header), "tuple.csv")
238+
@test tuple_pd[1] == tuple_table[:, 1]
239+
@test tuple_pd[2] == tuple_table[:, 2]
240+
@test tuple_pd[3] == tuple_table[:, 3]
241+
@test tuple_pd[4] == zeros(size(tuple_table, 1))
242+
243+
# Unit-test matrix parsing branch (header in first row + explicit CS column).
244+
matrix_data = Any[
245+
"alpha" "cl" "cd" "cs";
246+
0.0 0.11 0.011 0.001;
247+
4.0 0.21 0.021 0.002
248+
]
249+
matrix_pd = ext._extract_literature_polar_data(matrix_data, "matrix.csv")
250+
@test Float64.(matrix_pd[1]) == [0.0, 4.0]
251+
@test Float64.(matrix_pd[2]) == [0.11, 0.21]
252+
@test Float64.(matrix_pd[3]) == [0.011, 0.021]
253+
@test Float64.(matrix_pd[4]) == [0.001, 0.002]
254+
255+
# Missing required columns should throw a clear ArgumentError.
256+
bad_data = Any[
257+
"aoa" "cl" "cs";
258+
0.0 0.1 0.0
259+
]
260+
@test_throws ArgumentError ext._extract_literature_polar_data(bad_data, "bad.csv")
261+
262+
# Integration: literature CSV with AoA alias and no CS should still plot.
263+
lit_no_cs_path = tempname() * "_lit_no_cs.csv"
264+
open(lit_no_cs_path, "w") do io_no_cs
265+
write(io_no_cs, "aoa,cl,cd\n0.0,0.10,0.010\n5.0,0.20,0.020\n")
266+
end
267+
fig_lit_no_cs = plot_polars(
268+
Any[],
269+
Any[],
270+
["Literature no CS"];
271+
literature_path_list=[lit_no_cs_path],
272+
is_save=false,
273+
is_show=false
274+
)
275+
@test fig_lit_no_cs !== nothing
276+
safe_rm(lit_no_cs_path)
277+
278+
# Integration: missing CD column should fail.
279+
lit_bad_path = tempname() * "_lit_bad.csv"
280+
open(lit_bad_path, "w") do io_bad
281+
write(io_bad, "alpha,cl\n0.0,0.10\n5.0,0.20\n")
282+
end
283+
@test_throws ArgumentError plot_polars(
284+
Any[],
285+
Any[],
286+
["Literature bad"];
287+
literature_path_list=[lit_bad_path],
288+
is_save=false,
289+
is_show=false
290+
)
291+
safe_rm(lit_bad_path)
230292
end
231293
end
232294
nothing

0 commit comments

Comments
 (0)