From a889023b929d2a5a46c4ee46e1e579cfa58856a4 Mon Sep 17 00:00:00 2001 From: Daniel Vedder <daniel.vedder@idiv.de> Date: Thu, 22 Dec 2022 11:36:07 +0100 Subject: [PATCH] Added landscape tests --- test.sh => run.sh | 0 src/Persephone.jl | 1 + src/core/simulation.jl | 2 +- src/crop/crops.jl | 2 +- test/io_tests.jl | 6 ++++++ test/landscape_tests.jl | 48 ++++++++++++++++++++++++++++++++++++++++- test/runtests.jl | 5 +++++ 7 files changed, 61 insertions(+), 3 deletions(-) rename test.sh => run.sh (100%) create mode 100644 test/io_tests.jl mode change 100755 => 100644 test/runtests.jl diff --git a/test.sh b/run.sh similarity index 100% rename from test.sh rename to run.sh diff --git a/src/Persephone.jl b/src/Persephone.jl index 2470a0c..5ab364d 100644 --- a/src/Persephone.jl +++ b/src/Persephone.jl @@ -23,6 +23,7 @@ using TOML ## define exported functions and variables +#XXX export all types? export param, simulate, diff --git a/src/core/simulation.jl b/src/core/simulation.jl index 6440f57..b94b8d6 100644 --- a/src/core/simulation.jl +++ b/src/core/simulation.jl @@ -22,7 +22,7 @@ function initialise(config::String=PARAMFILE) properties = Dict{Symbol,Any}(:date=>param("core.startdate"), :landscape=>landscape, :events=>events) - @debug "Setting up model now" + @debug "Setting up model." model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties, rng=Random.Xoshiro(param("core.seed")), warn=false) # initialise submodels diff --git a/src/crop/crops.jl b/src/crop/crops.jl index fd1bef7..ec4520f 100644 --- a/src/crop/crops.jl +++ b/src/crop/crops.jl @@ -59,7 +59,7 @@ function initfields!(model::AgentBasedModel) @debug "Initialised $n farm plots." end -#XXX only needed during development, can be deleted again +#XXX only needed during development, can be deleted again? function countfields(model::AgentBasedModel) ids::Vector{Int64} = [] width, height = size(model.landscape) diff --git a/test/io_tests.jl b/test/io_tests.jl new file mode 100644 index 0000000..c7d10ce --- /dev/null +++ b/test/io_tests.jl @@ -0,0 +1,6 @@ +### Persephone - a socio-economic-ecological model of European agricultural landscapes. +### +### This tests the core input and output functions. +### + +#TODO diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index 308603c..be848ea 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -3,4 +3,50 @@ ### These are the tests for the core landscape functions. ### -#TODO +@testset "Landscape initialisation" begin + # initialise the landscape part of the model + landscape = Persephone.initlandscape() + space = GridSpace(size(landscape), periodic=false) + properties = Dict{Symbol,Any}(:landscape=>landscape) + model = AgentBasedModel(Persephone.FarmPlot, space, properties=properties, + rng=Random.Xoshiro(param("core.seed")), warn=false) + Persephone.initfields!(model) + # these tests are specific to the Jena maps + @test size(model.landscape) == (1754, 1602) + @test Persephone.landcover(model, (100,100)) == Persephone.forest + @test Persephone.landcover(model, (300,1)) == Persephone.soil + @test Persephone.landcover(model, (500,1)) == Persephone.nodata + @test Persephone.landcover(model, (400,400)) == Persephone.grass + @test Persephone.landcover(model, (800,800)) == Persephone.agriculture + @test Persephone.landcover(model, (1100,1100)) == Persephone.builtup + @test Persephone.countfields(model) == 2092 + @test Persephone.averagefieldsize(model) == 5.37 + @test count(f -> ismissing(f.fieldid), model.landscape) == 1685573 + @test length(Persephone.farmplot(model, (800,800)).pixels) == 4049 +end + +@testset "Event system" begin + # initialise a basic model landscape + landscape = Persephone.initlandscape() + space = GridSpace(size(landscape), periodic=false) + properties = Dict{Symbol,Any}(:date=>param("core.startdate"), + :landscape=>landscape, + :events=>Vector{Persephone.FarmEvent}()) + model = AgentBasedModel(Union{Persephone.Farmer,Persephone.Animal,Persephone.FarmPlot}, + space, properties=properties, + rng=Random.Xoshiro(param("core.seed")), warn=false) + # create some events and see whether they show up on the map and disappear as they should + Persephone.createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Persephone.tillage) + Persephone.createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Persephone.sowing, 2) + @test model.landscape[1,1].events == [Persephone.tillage, Persephone.sowing] + @test model.landscape[2,1].events == [Persephone.tillage] + @test model.landscape[2,2].events == [Persephone.sowing] + stepsimulation!(model) + @test model.landscape[1,1].events == [Persephone.sowing] + @test model.landscape[2,1].events == [] + @test model.landscape[2,2].events == [Persephone.sowing] + stepsimulation!(model) + @test model.landscape[1,1].events == [] + @test model.landscape[2,1].events == [] + @test model.landscape[2,2].events == [] +end diff --git a/test/runtests.jl b/test/runtests.jl old mode 100755 new mode 100644 index bd92cde..5af8e83 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,9 +8,13 @@ Pkg.activate("..") using Persephone using Test +using Random +using Agents @testset "Persephone Tests" begin + Persephone.initsettings("test_parameters.toml") @testset "Core model" begin + include("io_tests.jl") include("landscape_tests.jl") include("simulation_tests.jl") end @@ -23,4 +27,5 @@ using Test @testset "Farm model" begin include("farm_tests.jl") end + rm(param("core.outdir"), force=true, recursive=true) end -- GitLab