Skip to content

Commit ef31947

Browse files
Add function to validate profile values (#25)
Co-authored-by: Abel Soares Siqueira <nepper271@gmail.com>
1 parent 29c395f commit ef31947

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/model.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export find_solution, find_search_interval
33
"""
44
(a, b) = find_search_interval(f)
55
6-
Returns an interval such that ``f(a) × f(b) 0``.
6+
Returns an interval such that ``f(a) × f(b) 0``.
77
It could be 0 for either endpoint, but it is not positive,
88
ensuring that there is a root in `[a, b]`.
99
"""
@@ -32,6 +32,7 @@ S(x) = \\frac{1}{|P|} \\sum_{p ∈ P:\\ p > 0} p^x.
3232
If not possible, return either 0 or 1000, depending on what is most appropriate.
3333
"""
3434
function find_solution(P, μ)
35+
validate_profile(P)
3536
σ(P, x) = P > 0 ? P^x : 0.0
3637
S(P, x) = mean(σ.(P, x))
3738
m = length(P)
@@ -46,3 +47,14 @@ function find_solution(P, μ)
4647
return 1000.0
4748
end
4849
end
50+
51+
"""
52+
validate_profile(P)
53+
54+
Validates if the values of profile P are within 0 and 1. If not, throws an error message and stops the calculation.
55+
"""
56+
function validate_profile(P)
57+
if !all(0 .≤ P .≤ 1)
58+
error("Profile values must be within 0 and 1. Calculation stopped.")
59+
end
60+
end

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ function test_fit_profile()
2525
@test find_solution(profile_values, target_mean) expected_coefficient atol = 1e-3
2626
end
2727

28+
function test_throws_error()
29+
profile_values = [-0.5, 0.2, 0.8, 1.5]
30+
target_mean = 0.5
31+
@test_throws ErrorException find_solution(profile_values, target_mean)
32+
end
33+
2834
@testset "TulipaProfileFitting" begin
2935
@testset "fit_profile" begin
3036
test_fit_profile()
37+
test_throws_error()
3138
end
3239
end

0 commit comments

Comments
 (0)