Skip to content

Commit

Permalink
frequency_tables tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosdanieldasilva committed Nov 22, 2024
1 parent 534e73d commit b4997d6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/frequency_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ julia> diametric_table(diameters, 2, plot_area=0.05)
function diametric_table(d::Vector{<:Real}, hi::Real; plot_area::Real=1.0)
if hi <= 0
throw(DomainError("The class width must be positive."))
elseif any(x -> x < 0, d)
elseif plot_area <= 0
throw(DomainError("The plot area must be positive."))
elseif any(x -> x <= 0, d)
throw(DomainError("Diameters must be positive"))
else
ftable = frequency_table(d, hi)
Expand Down
101 changes: 101 additions & 0 deletions test/statistics_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,107 @@
end

end

@testset "Frequency Tables Function Tests" begin
# Test Data
diameters = [10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0]
species = ["Oak", "Oak", "Oak", "Oak", "Oak", "Pine", "Pine", "Pine", "Pine", "Pine"]
data = DataFrame(species=species, diameters=diameters)

# Test 1: Standard Case
result_df = frequency_table(diameters, 2)
@test isa(result_df, DataFrame)

# Expected results for the test data
expected_df = DataFrame(
LI=[10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0],
Xi=[11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0, 25.0],
LS=[12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0],
fi=[1, 1, 1, 2, 1, 2, 1, 1],
Fi=[1, 2, 3, 5, 6, 8, 9, 10],
fri=[10.0, 10.0, 10.0, 20.0, 10.0, 20.0, 10.0, 10.0],
Fri=[10.0, 20.0, 30.0, 50.0, 60.0, 80.0, 90.0, 100.0]
)

# Compare the result with expected values
@test size(result_df) == size(expected_df)

for col in names(expected_df)
@test result_df[!, col] == expected_df[!, col]
end

# Test 2: Invalid Class Width (Negative)
@test_throws DomainError frequency_table(diameters, -2)

# Test 3: Invalid Class Width (Zero)
@test_throws DomainError frequency_table(diameters, 0)

# Test 4: Verify Cumulative Frequencies
@test result_df.Fi[end] == sum(result_df.fi)
@test result_df.Fri[end] == 100.0

# Test 5: Check if total frequency equals number of data points
total_frequency = sum(result_df.fi)
@test total_frequency == length(diameters)
end

@testset "Diametric Table Function Tests" begin
# Test Data
diameters = [10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0]
species = ["Oak", "Oak", "Oak", "Oak", "Oak", "Pine", "Pine", "Pine", "Pine", "Pine"]
data = DataFrame(species=species, diameters=diameters)

# Test 1: Standard Case
result_df = diametric_table(diameters, 2, plot_area=0.05)
@test isa(result_df, DataFrame)

# Expected results for the test data
expected_df = DataFrame(
LI=[10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0],
Xi=[11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0, 25.0],
LS=[12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0],
fi=[1, 1, 1, 2, 1, 2, 1, 1],
Fi=[1, 2, 3, 5, 6, 8, 9, 10],
fri=[10.0, 10.0, 10.0, 20.0, 10.0, 20.0, 10.0, 10.0],
Fri=[10.0, 20.0, 30.0, 50.0, 60.0, 80.0, 90.0, 100.0],
g=[0.00950332, 0.0132732, 0.0176715, 0.022698, 0.0283529, 0.0346361, 0.0415476, 0.0490874],
ng=[0.00950332, 0.0132732, 0.0176715, 0.045396, 0.0283529, 0.0692721, 0.0415476, 0.0490874],
∑ng=[0.00950332, 0.0227765, 0.040448, 0.085844, 0.114197, 0.183469, 0.225017, 0.274104],
fi_ha=[20.0, 20.0, 20.0, 40.0, 20.0, 40.0, 20.0, 20.0],
Fi_ha=[20.0, 40.0, 60.0, 100.0, 120.0, 160.0, 180.0, 200.0],
ng_ha=[0.190066, 0.265465, 0.353429, 0.90792, 0.567057, 1.38544, 0.830951, 0.981748],
∑ng_ha=[0.190066, 0.455531, 0.80896, 1.71688, 2.28394, 3.66938, 4.50033, 5.48208]
)

# Compare the result with expected values
@test size(result_df) == size(expected_df)

for col in names(expected_df)
@test all(isapprox.(result_df[!, col], expected_df[!, col], atol=1e-5))
end

# Test 2: Invalid Class Width (Negative)
@test_throws DomainError diametric_table(diameters, -2)

# Test 3: Invalid Class Width (Zero)
@test_throws DomainError diametric_table(diameters, 0)

# Test 4: Invalid Plot Area (Negative)
@test_throws DomainError diametric_table(diameters, 2, plot_area=-0.05)

# Test 5: Diameters with Negative Values
@test_throws DomainError diametric_table(-diameters, 2)

# Test 6: Diameters with Zero Values
@test_throws DomainError diametric_table([0, 10.5, 12.0], 2)

# Test 7: Check if cumulative ng equals sum of ng
@test result_df.∑ng[end] sum(result_df.ng) atol = 1e-5

# Test 8: Check if cumulative ng_ha equals sum of ng_ha
@test result_df.∑ng_ha[end] sum(result_df.ng_ha) atol = 1e-5
end

end


0 comments on commit b4997d6

Please sign in to comment.