diff --git a/src/Persephone.jl b/src/Persephone.jl index 904f54d2c656c0580ed650d8ec96d198856be823..eb7fd2b6e237ead0c4255d97da27e4d798dd20f7 100644 --- a/src/Persephone.jl +++ b/src/Persephone.jl @@ -30,6 +30,10 @@ export FarmPlot, Animal, Farmer, + #macros + @species, + @phase, + @habitat, #functions param, simulate, diff --git a/test/io_tests.jl b/test/io_tests.jl index 19a66376f75a37c2133196b038c74566991e1f57..cb06bedcfd7a033d350695c322fd22a6ba22a361 100644 --- a/test/io_tests.jl +++ b/test/io_tests.jl @@ -14,15 +14,15 @@ end @testset "Output functions" begin # test that the output directory is created with all files logstring = "Setting up output directory results_testsuite_$(Dates.today())_s1" - @test_logs (:info, logstring) Persephone.setupdatadir() + @test_logs (:debug, logstring) min_level=Logging.Debug Ps.setupdatadir() @test isdir(param("core.outdir")) @test isfile(joinpath(param("core.outdir"), param("core.landcovermap"))) @test isfile(joinpath(param("core.outdir"), param("core.farmfieldsmap"))) @test isfile(joinpath(param("core.outdir"), param("core.configfile"))) - @test isfile(joinpath(param("core.outdir"), Persephone.LOGFILE)) + @test isfile(joinpath(param("core.outdir"), Ps.LOGFILE)) # check whether the overwrite warning/protection works logstring = "Overwriting existing output directory $(param("core.outdir"))." - @test_logs (:warn, logstring) match_mode=:any Persephone.setupdatadir() + @test_logs (:warn, logstring) match_mode=:any Ps.setupdatadir() #TODO test overwrite protection (requires parameter mutability) rm(param("core.outdir"), force=true, recursive=true) #TODO test that creating a DataOutput works, and outputs data with the required frequency diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index c54ca64785c93d92db9ecdd2098852c60b1877f6..b899d53d1c48d569b08042bf5eed28157a20941b 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -3,31 +3,57 @@ ### These are the tests for the core landscape functions. ### +""" + smalltestlandscape() + +Create a 6x6 landscape with three land cover types for testing: + + F F F F F F + F F F F F F + F F F F F F + F F F F F W + F F G G G G + F F G G G G +""" +function smalltestlandscape() + landscape = Matrix{Pixel}(undef, 6, 6) + for x in 1:6 + for y in 1:6 + (x in (1:2) || y in (1:4)) ? lc = Ps.forest : lc = Ps.grass + landscape[x,y] = Pixel(lc, 0, []) + end + end + landscape[6,4] = Pixel(Ps.water, 0, []) + space = GridSpace(size(landscape), periodic=false) + properties = Dict{Symbol,Any}(:landscape=>landscape) + return AgentBasedModel(Animal, space, properties=properties, warn=false) +end + @testset "Landscape initialisation" begin # initialise the landscape part of the model - landscape = Persephone.initlandscape() + landscape = Ps.initlandscape() space = GridSpace(size(landscape), periodic=false) properties = Dict{Symbol,Any}(:landscape=>landscape) model = AgentBasedModel(FarmPlot, space, properties=properties, rng=Random.Xoshiro(param("core.seed")), warn=false) - Persephone.initfields!(model) + Ps.initfields!(model) # these tests are specific to the Jena maps @test size(model.landscape) == (1754, 1602) - @test Persephone.landcover((100,100), model) == Persephone.forest - @test Persephone.landcover((300,1), model) == Persephone.soil - @test Persephone.landcover((500,1), model) == Persephone.nodata - @test Persephone.landcover((400,400), model) == Persephone.grass - @test Persephone.landcover((800,800), model) == Persephone.agriculture - @test Persephone.landcover((1100,1100), model) == Persephone.builtup - @test Persephone.countfields(model) == 2092 - @test Persephone.averagefieldsize(model) == 5.37 + @test Ps.landcover((100,100), model) == Ps.forest + @test Ps.landcover((300,1), model) == Ps.soil + @test Ps.landcover((500,1), model) == Ps.nodata + @test Ps.landcover((400,400), model) == Ps.grass + @test Ps.landcover((800,800), model) == Ps.agriculture + @test Ps.landcover((1100,1100), model) == Ps.builtup + @test Ps.countfields(model) == 2092 + @test Ps.averagefieldsize(model) == 5.37 @test count(f -> ismissing(f.fieldid), model.landscape) == 1685573 - @test length(Persephone.farmplot((800,800), model).pixels) == 4049 + @test length(Ps.farmplot((800,800), model).pixels) == 4049 end @testset "Event system" begin # initialise a basic model landscape - landscape = Persephone.initlandscape() + landscape = Ps.initlandscape() space = GridSpace(size(landscape), periodic=false) properties = Dict{Symbol,Any}(:date=>param("core.startdate"), :landscape=>landscape, @@ -35,15 +61,15 @@ end model = AgentBasedModel(Union{Farmer,Animal,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 - createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Persephone.tillage) - 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] + createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Ps.tillage) + createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Ps.sowing, 2) + @test model.landscape[1,1].events == [Ps.tillage, Ps.sowing] + @test model.landscape[2,1].events == [Ps.tillage] + @test model.landscape[2,2].events == [Ps.sowing] stepsimulation!(model) - @test model.landscape[1,1].events == [Persephone.sowing] + @test model.landscape[1,1].events == [Ps.sowing] @test model.landscape[2,1].events == [] - @test model.landscape[2,2].events == [Persephone.sowing] + @test model.landscape[2,2].events == [Ps.sowing] stepsimulation!(model) @test model.landscape[1,1].events == [] @test model.landscape[2,1].events == [] @@ -51,24 +77,12 @@ end end @testset "Landscape functions" begin - # initialise a simple 6x6 test landscape - landscape = Matrix{Pixel}(undef, 6, 6) - for x in 1:6 - for y in 1:6 - (x in (1:2) || y in (1:4)) ? lc = Persephone.forest : lc = Persephone.grass - landscape[x,y] = Pixel(lc, 0, []) - end - end - landscape[6,4] = Pixel(Persephone.water, 0, []) - space = GridSpace(size(landscape), periodic=false) - properties = Dict{Symbol,Any}(:landscape=>landscape) - model = AgentBasedModel(Animal, space, properties=properties, warn=false) - # test the distance functions - @test Persephone.distanceto((2,3), model, Persephone.forest) == 0 - @test Persephone.distanceto((2,3), model, Persephone.grass) == 2 - @test Persephone.distanceto((2,3), model, Persephone.water) == 4 - @test Persephone.distanceto((2,3), model, Persephone.soil) == Inf - @test Persephone.distancetoedge((1,1), model) == 4 - @test Persephone.distancetoedge((4,4), model) == 1 - @test Persephone.distancetoedge((6,6), model) == 2 + model = smalltestlandscape() + @test Ps.distanceto((2,3), model, Ps.forest) == 0 + @test Ps.distanceto((2,3), model, Ps.grass) == 2 + @test Ps.distanceto((2,3), model, Ps.water) == 4 + @test Ps.distanceto((2,3), model, Ps.soil) == Inf + @test Ps.distancetoedge((1,1), model) == 4 + @test Ps.distancetoedge((4,4), model) == 1 + @test Ps.distancetoedge((6,6), model) == 2 end diff --git a/test/runtests.jl b/test/runtests.jl index e185c554be424dbe2be3c4eb9cee76a0ea78a341..8ea59761a66df9357bbd5a8e285aa5a8c5a3e943 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,13 +9,16 @@ Pkg.activate("..") using Persephone using Test using Random +using Logging using Dates using Agents +const Ps = Persephone + const TESTPARAMETERS = "test_parameters.toml" @testset "Persephone Tests" begin - Persephone.initsettings(TESTPARAMETERS) + Ps.initsettings(TESTPARAMETERS) @testset "Core model" begin include("io_tests.jl") include("landscape_tests.jl")