From bdc6d6d78aaf0489b9ac7160e60b563f75ef75dd Mon Sep 17 00:00:00 2001 From: MartinuzziFrancesco Date: Wed, 6 Mar 2024 09:33:14 +0100 Subject: [PATCH 1/3] added aqua, format --- .JuliaFormatter.toml | 9 +++++ Project.toml | 6 ++-- docs/make.jl | 12 ++++--- docs/pages.jl | 6 ++-- src/CellularAutomata.jl | 32 +++++++++-------- src/cca.jl | 54 +++++++++++++++-------------- src/dca.jl | 71 +++++++++++++++++-------------------- src/life.jl | 42 ++++++++++++---------- src/measures.jl | 13 +++---- src/tca.jl | 75 +++++++++++++++++++--------------------- test/blinker_test.jl | 13 ++++--- test/cca_test.jl | 2 +- test/dca_test.jl | 6 ++-- test/eca_ruleset_test.jl | 1 - test/glider_test.jl | 16 +++++---- test/qa.jl | 4 +++ test/runtests.jl | 28 +++++++++++---- test/tca_test.jl | 6 ++-- 18 files changed, 217 insertions(+), 179 deletions(-) create mode 100644 .JuliaFormatter.toml create mode 100644 test/qa.jl diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..86dd6b4 --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,9 @@ +style = "blue" +whitespace_in_kwargs = false +always_use_return = true +margin = 92 +indent = 4 +format_docstrings = true +separate_kwargs_with_semicolon = true +always_for_in = true +annotate_untyped_fields_with_any = false diff --git a/Project.toml b/Project.toml index 0da9a74..3685b5e 100644 --- a/Project.toml +++ b/Project.toml @@ -5,14 +5,14 @@ version = "0.0.3" [deps] - [compat] julia = "1" [extras] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [targets] -test = ["Test", "SafeTestsets", "Random"] +test = ["Test", "SafeTestsets", "Random", "Aqua"] diff --git a/docs/make.jl b/docs/make.jl index 576b8af..7fd197a 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,14 +1,16 @@ - using Documenter, CellularAutomata - include("pages.jl") +using Documenter, CellularAutomata +include("pages.jl") -makedocs(;sitename="CellularAutomata.jl", +makedocs(; + sitename="CellularAutomata.jl", modules=[CellularAutomata], clean=true, doctest=true, linkcheck=true, warnonly=[:missing_docs], - pages = pages) + pages=pages, +) deploydocs(; repo="github.com/MartinuzziFrancesco/CellularAutomata.jl.git", push_preview=true -) \ No newline at end of file +) diff --git a/docs/pages.jl b/docs/pages.jl index c74ada5..6dbd394 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -3,10 +3,10 @@ pages = [ "Examples" => [ "One dimensional CA" => "onedim/onedimensionca.md" "Two dimensional CA" => "twodim/twodimensionca.md" - ], + ], "API Documentation" => Any[ "General APIs" => "api/general.md" "One Dimensional CA" => "api/onedim.md" "Two Dimensial CA" => "api/twodim.md" - ] - ] \ No newline at end of file + ], +] diff --git a/src/CellularAutomata.jl b/src/CellularAutomata.jl index e4e0a53..5bf6d70 100644 --- a/src/CellularAutomata.jl +++ b/src/CellularAutomata.jl @@ -11,7 +11,6 @@ struct CellularAutomaton{F,E} <: AbstractCA evolution::E end - """ CellularAutomaton(rule::AbstractODRule, initial_conditions, generations) CellularAutomaton(rule::AbstractTDRule, initial_conditions, generations) @@ -21,35 +20,38 @@ with given initial conditions and number of generations. OD indicates one-diomen indicates two-dimensiona cellular automata rules. """ function CellularAutomaton(rule::AbstractODRule, initial_conditions, generations) + evolution = zeros( + typeof(initial_conditions[2]), generations, length(initial_conditions) + ) + evolution[1, :] = initial_conditions - evolution = zeros(typeof(initial_conditions[2]), generations, length(initial_conditions)) - evolution[1,:] = initial_conditions - - for i=2:generations - evolution[i,:] = rule(evolution[i-1,:]) + for i in 2:generations + evolution[i, :] = rule(evolution[i - 1, :]) end - CellularAutomaton(generations, rule, evolution) - + return CellularAutomaton(generations, rule, evolution) end function CellularAutomaton(rule::AbstractTDRule, initial_conditions, generations) - - evolution = zeros(typeof(initial_conditions[2]), size(initial_conditions, 1), size(initial_conditions, 2), generations) + evolution = zeros( + typeof(initial_conditions[2]), + size(initial_conditions, 1), + size(initial_conditions, 2), + generations, + ) evolution[:, :, 1] = initial_conditions - for i=2:generations - evolution[:, :, i] = rule(evolution[:, :, i-1]) + for i in 2:generations + evolution[:, :, i] = rule(evolution[:, :, i - 1]) end - CellularAutomaton(generations, rule, evolution) - + return CellularAutomaton(generations, rule, evolution) end export CellularAutomaton include("dca.jl") -export DCA, ECA +export DCA include("cca.jl") export CCA include("tca.jl") diff --git a/src/cca.jl b/src/cca.jl index 4fd9a25..61bbbf3 100644 --- a/src/cca.jl +++ b/src/cca.jl @@ -1,51 +1,55 @@ -abstract type AbstractCCARule <: AbstractODRule end +abstract type AbstractCCARule <: AbstractODRule end struct CCA{T} <: AbstractCCARule rule::T radius::Int end -""" +""" TCA(code; radius=1) -Returns a ```CCA``` object given a specific code and radius. +Returns a `CCA` object given a specific code and radius. """ function CCA(rule; radius=1) - CCA(rule, radius) + return CCA(rule, radius) end function (cca::CCA)(starting_array) - - nextgen = evolution(starting_array, cca.rule, cca.radius) + return nextgen = evolution(starting_array, cca.rule, cca.radius) end function c_state_reader(neighborhood, radius) - - sum(neighborhood)/length(neighborhood) + return sum(neighborhood) / length(neighborhood) end function evolution(cell, rule, radius::Number) - - neighborhood_size = radius*2+1 + neighborhood_size = radius * 2 + 1 output = zeros(length(cell)) - cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = modf(c_state_reader(cell[i:i+neighborhood_size-1], radius)+rule)[1] + cell = vcat( + cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)] + ) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = modf( + c_state_reader(cell[i:(i + neighborhood_size - 1)], radius) + rule + )[1] end - - output + + return output end function evolution(cell, rule, radius::Tuple) - - neighborhood_size = sum(radius)+1 + neighborhood_size = sum(radius) + 1 output = zeros(length(cell)) - cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = modf(c_state_reader(cell[i:i+neighborhood_size-1], radius)+rule)[1] + cell = vcat( + cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)] + ) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = modf( + c_state_reader(cell[i:(i + neighborhood_size - 1)], radius) + rule + )[1] end - - output -end \ No newline at end of file + + return output +end diff --git a/src/dca.jl b/src/dca.jl index b630607..85e1877 100644 --- a/src/dca.jl +++ b/src/dca.jl @@ -1,4 +1,4 @@ -abstract type AbstractDCARule <: AbstractODRule end +abstract type AbstractDCARule <: AbstractODRule end struct DCA{B,R,T} <: AbstractDCARule rule::B @@ -7,68 +7,61 @@ struct DCA{B,R,T} <: AbstractDCARule radius::T end -""" +""" DCA(rule; states=2, radius=1) -Returns a ```DCA``` object given a specific rule, number of states and radius. The ruleset for the rule is computed and +Returns a `DCA` object given a specific rule, number of states and radius. The ruleset for the rule is computed and stored in the struct as well. """ -function DCA(rule; - states=2, - radius=1) - +function DCA(rule; states=2, radius=1) ruleset = conversion(rule, states, radius) - DCA(rule, ruleset, states, radius) + return DCA(rule, ruleset, states, radius) end function (dca::DCA)(starting_array) - - nextgen = evolution(starting_array, dca.ruleset, dca.states, dca.radius) + return nextgen = evolution(starting_array, dca.ruleset, dca.states, dca.radius) end function conversion(rule, states, radius::Int) - - rule_len = states^(2*radius+1) - rule_bin = parse.(Int, split(string(rule, base=states), "")) - rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len-length(rule_bin)), rule_bin) - reverse!(rule_bin) + rule_len = states^(2 * radius + 1) + rule_bin = parse.(Int, split(string(rule; base=states), "")) + rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin) + return reverse!(rule_bin) end function conversion(rule, states, radius::Tuple) - - rule_len = states^(sum(radius)+1) - rule_bin = parse.(Int, split(string(rule, base=states), "")) - rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len-length(rule_bin)), rule_bin) - reverse!(rule_bin) + rule_len = states^(sum(radius) + 1) + rule_bin = parse.(Int, split(string(rule; base=states), "")) + rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin) + return reverse!(rule_bin) end function state_reader(neighborhood, states) - - parse(Int,join(convert(Array{Int},neighborhood)), base=states)+1 #ugly + return parse(Int, join(convert(Array{Int}, neighborhood)); base=states) + 1 #ugly end function evolution(cell, ruleset, states, radius::Int) - - neighborhood_size = radius*2+1 + neighborhood_size = radius * 2 + 1 output = zeros(length(cell)) - cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = ruleset[state_reader(cell[i:i+neighborhood_size-1], states)] + cell = vcat( + cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)] + ) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = ruleset[state_reader(cell[i:(i + neighborhood_size - 1)], states)] end - - output + + return output end function evolution(cell, ruleset, states, radius::Tuple) - - neighborhood_size = sum(radius)+1 + neighborhood_size = sum(radius) + 1 output = zeros(length(cell))#da qui in poi da modificare - cell = vcat(cell[end-radius[1]+1:end], cell, cell[1:radius[2]]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = ruleset[state_reader(cell[i:i+neighborhood_size-1], states)] + cell = vcat(cell[(end - radius[1] + 1):end], cell, cell[1:radius[2]]) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = ruleset[state_reader(cell[i:(i + neighborhood_size - 1)], states)] end - - output -end \ No newline at end of file + + return output +end diff --git a/src/life.jl b/src/life.jl index 8fc089e..ed464b5 100644 --- a/src/life.jl +++ b/src/life.jl @@ -7,16 +7,16 @@ struct Life{T,A,C} <: AbstractLifeRule radius::C end -""" +""" Life(life_description; radius=1) -Returns a ```Life``` object given a tuple of tuples that follows the Golly notation ((b), (s)), where b stands for birth and s for survival. -These values indicates the number of neighbouring cells needed to birth a new one in the following generation, or to make the current alive +Returns a `Life` object given a tuple of tuples that follows the Golly notation ((b), (s)), where b stands for birth and s for survival. +These values indicates the number of neighbouring cells needed to birth a new one in the following generation, or to make the current alive one survive. """ function Life(life_description; radius=1) born, survive = life_description[1], life_description[2] - Life(born, survive, radius) + return Life(born, survive, radius) end function (life::Life)(starting_array) @@ -24,21 +24,26 @@ function (life::Life)(starting_array) end function virtual_expansion(starting_array, radius) - height, width = size(starting_array) - nh, nw = height-radius+1, width-radius+1 - left = vcat(starting_array[nh:end,nw:end], starting_array[:,nw:end], starting_array[1:radius, nw:end]) - right = vcat(starting_array[nh:end, 1:radius], starting_array[:,1:radius], starting_array[1:radius,1:radius]) - middle = vcat(starting_array[nh:end,:], starting_array, starting_array[1:radius,:]) + nh, nw = height - radius + 1, width - radius + 1 + left = vcat( + starting_array[nh:end, nw:end], + starting_array[:, nw:end], + starting_array[1:radius, nw:end], + ) + right = vcat( + starting_array[nh:end, 1:radius], + starting_array[:, 1:radius], + starting_array[1:radius, 1:radius], + ) + middle = vcat(starting_array[nh:end, :], starting_array, starting_array[1:radius, :]) - hcat(left, middle, right) + return hcat(left, middle, right) end function life_application(state, born, survive) - - - past_value = state[size(state, 1)÷2+1, size(state, 2)÷2+1] #save past cell value - state[size(state, 1)÷2+1, size(state, 2)÷2+1] = 0 #past cell value set to zero + past_value = state[size(state, 1) ÷ 2 + 1, size(state, 2) ÷ 2 + 1] #save past cell value + state[size(state, 1) ÷ 2 + 1, size(state, 2) ÷ 2 + 1] = 0 #past cell value set to zero alive = sum(state) #the sum is the number of cell alive in the neighborhood since the central value is equal to zero if past_value == 1 && alive in survive @@ -51,13 +56,14 @@ function life_application(state, born, survive) end function life_evolution(starting_array, born, survive, radius) - height, width = size(starting_array) output = zeros(typeof(starting_array[2]), height, width) virtual_output = virtual_expansion(starting_array, radius) - for i = 1:height-radius+1, j = 1:width-radius+1 - output[i, j] = life_application(virtual_output[i:i+radius+1, j:j+radius+1], born, survive) + for i in 1:(height - radius + 1), j in 1:(width - radius + 1) + output[i, j] = life_application( + virtual_output[i:(i + radius + 1), j:(j + radius + 1)], born, survive + ) end - output + return output end diff --git a/src/measures.jl b/src/measures.jl index e5c4c66..e551f78 100644 --- a/src/measures.jl +++ b/src/measures.jl @@ -8,7 +8,7 @@ function lempel_ziv_complexity(sequence) if ind + inc > n break end - sub_str = sequence[ind : ind + inc] + sub_str = sequence[ind:(ind + inc)] if sub_str in sub_strings inc += 1 else @@ -22,14 +22,15 @@ end """ function lempel_ziv(ca::AbstractCA) + Computes the lempel ziv complexity of a given Cellular Automaton. """ function lempel_ziv(ca::AbstractCA) ca_size = size(ca.evolution, 1) lz_tot = 0 - - for i=1:ca_size - lz_tot += lempel_ziv_complexity(ca.evolution[i,:]) + + for i in 1:ca_size + lz_tot += lempel_ziv_complexity(ca.evolution[i, :]) end - lz_tot/ca_size -end \ No newline at end of file + return lz_tot / ca_size +end diff --git a/src/tca.jl b/src/tca.jl index 7111678..abfc094 100644 --- a/src/tca.jl +++ b/src/tca.jl @@ -1,4 +1,4 @@ -abstract type AbstractTCARule <: AbstractDCARule end +abstract type AbstractTCARule <: AbstractDCARule end struct TCA{B,R,T} <: AbstractTCARule code::B @@ -7,68 +7,65 @@ struct TCA{B,R,T} <: AbstractTCARule radius::T end -""" +""" TCA(code; states=2, radius=1) -Returns a ```TCA``` object given a specific code, number of states and radius. +Returns a `TCA` object given a specific code, number of states and radius. The ruleset for the rule is computed and stored in the struct as well. """ -function TCA(code; - states=2, - radius=1) - +function TCA(code; states=2, radius=1) codeset = tca_conversion(code, states, radius) - TCA(code, codeset, states, radius) + return TCA(code, codeset, states, radius) end function (tca::TCA)(starting_array) - - nextgen = tca_evolution(starting_array, tca.codeset, tca.states, tca.radius) + return nextgen = tca_evolution(starting_array, tca.codeset, tca.states, tca.radius) end function tca_conversion(code, states, radius::Number) - - code_len = (2*radius+1)*states-2 - code_bin = parse.(Int, split(string(code, base=states), "")) - code_bin = vcat(zeros(typeof(code_bin[1]), code_len-length(code_bin)), code_bin) - reverse!(code_bin) + code_len = (2 * radius + 1) * states - 2 + code_bin = parse.(Int, split(string(code; base=states), "")) + code_bin = vcat(zeros(typeof(code_bin[1]), code_len - length(code_bin)), code_bin) + return reverse!(code_bin) end function tca_conversion(code, states, radius::Tuple) - - code_len = (sum(radius)+1)*states-2 - code_bin = parse.(Int, split(string(code, base=states), "")) - code_bin = vcat(zeros(typeof(code_bin[1]), code_len-length(code_bin)), code_bin) - reverse!(code_bin) + code_len = (sum(radius) + 1) * states - 2 + code_bin = parse.(Int, split(string(code; base=states), "")) + code_bin = vcat(zeros(typeof(code_bin[1]), code_len - length(code_bin)), code_bin) + return reverse!(code_bin) end function tca_state_reader(neighborhood, codeset_len) - - mod1(sum(neighborhood)+1,codeset_len) + return mod1(sum(neighborhood) + 1, codeset_len) end function tca_evolution(cell, codeset, states, radius::Number) - - neighborhood_size = radius*2+1 + neighborhood_size = radius * 2 + 1 output = zeros(length(cell)) - cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = codeset[tca_state_reader(cell[i:i+neighborhood_size-1], length(codeset))] + cell = vcat( + cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)] + ) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = codeset[tca_state_reader( + cell[i:(i + neighborhood_size - 1)], length(codeset) + )] end - - output + + return output end function tca_evolution(cell, codeset, states, radius::Tuple) - - neighborhood_size = sum(radius)+1 + neighborhood_size = sum(radius) + 1 output = zeros(length(cell)) - cell = vcat(cell[end-radius[1]+1:end], cell, cell[1:radius[2]]) - - for i=1:length(cell)-neighborhood_size+1 - output[i] = codeset[tca_state_reader(cell[i:i+neighborhood_size-1], length(codeset))] + cell = vcat(cell[(end - radius[1] + 1):end], cell, cell[1:radius[2]]) + + for i in 1:(length(cell) - neighborhood_size + 1) + output[i] = codeset[tca_state_reader( + cell[i:(i + neighborhood_size - 1)], length(codeset) + )] end - - output -end \ No newline at end of file + + return output +end diff --git a/test/blinker_test.jl b/test/blinker_test.jl index 5440080..a7caeed 100644 --- a/test/blinker_test.jl +++ b/test/blinker_test.jl @@ -4,8 +4,11 @@ const gens = 3 blinker = [[0, 0, 0, 0, 0] [0, 0, 1, 0, 0] [0, 0, 1, 0, 0] [0, 0, 1, 0, 0] [0, 0, 0, 0, 0]] -ca = CellularAutomaton(Life((3, (2,3))), blinker, gens) - -@test ca.evolution == cat([0 0 0 0 0; 0 0 0 0 0; 0 1 1 1 0; 0 0 0 0 0; 0 0 0 0 0], -[0 0 0 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 0 0 0 0], -[0 0 0 0 0; 0 0 0 0 0; 0 1 1 1 0; 0 0 0 0 0; 0 0 0 0 0], dims=3) \ No newline at end of file +ca = CellularAutomaton(Life((3, (2, 3))), blinker, gens) + +@test ca.evolution == cat( + [0 0 0 0 0; 0 0 0 0 0; 0 1 1 1 0; 0 0 0 0 0; 0 0 0 0 0], + [0 0 0 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 0 0 0 0], + [0 0 0 0 0; 0 0 0 0 0; 0 1 1 1 0; 0 0 0 0 0; 0 0 0 0 0]; + dims=3, +) diff --git a/test/cca_test.jl b/test/cca_test.jl index 4db3267..742eb9e 100644 --- a/test/cca_test.jl +++ b/test/cca_test.jl @@ -10,4 +10,4 @@ const rule = 0.05 ca = ca = CellularAutomaton(CCA(rule), starting_val, generations) -@test size(ca.evolution) == (generations, ncells) +@test size(ca.evolution) == (generations, ncells) diff --git a/test/dca_test.jl b/test/dca_test.jl index bfbab99..b83d63a 100644 --- a/test/dca_test.jl +++ b/test/dca_test.jl @@ -5,11 +5,11 @@ const states = 4 const radius = 1 const generations = 10 const ncells = 11 -const starting_array = rand(0:states-1, ncells) +const starting_array = rand(0:(states - 1), ncells) const rule = 107396 #testing states > 2 -ca = CellularAutomaton(DCA(rule, states=states), starting_array, generations) +ca = CellularAutomaton(DCA(rule; states=states), starting_array, generations) @test size(ca.evolution) == (generations, ncells) @@ -20,4 +20,4 @@ const bstarting_array = rand(Bool, ncells) bca = ca = CellularAutomaton(DCA(brule), bstarting_array, generations) -@test size(bca.evolution) == (generations, ncells) \ No newline at end of file +@test size(bca.evolution) == (generations, ncells) diff --git a/test/eca_ruleset_test.jl b/test/eca_ruleset_test.jl index a536029..2908b8a 100644 --- a/test/eca_ruleset_test.jl +++ b/test/eca_ruleset_test.jl @@ -14,4 +14,3 @@ const rule3 = DCA(3) const rule30 = DCA(30) @test rule30.ruleset == [0, 1, 1, 1, 1, 0, 0, 0] #http://atlas.wolfram.com/01/01/30/ - diff --git a/test/glider_test.jl b/test/glider_test.jl index ad63969..030f794 100644 --- a/test/glider_test.jl +++ b/test/glider_test.jl @@ -7,11 +7,13 @@ glider = [[0, 0, 1, 0, 0] [0, 0, 0, 1, 0] [0, 1, 1, 1, 0]] space = zeros(Bool, size_space, size_space) space[1:size(glider, 1), 1:size(glider, 2)] = glider -ca = CellularAutomaton(Life((3, (2,3))), space, gens) - -@test ca.evolution == cat([0 0 0 0 0 0; 0 0 1 0 0 0; 1 0 1 0 0 0; 0 1 1 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], -[0 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 1 0 0; 0 1 1 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], -[0 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 1 0 0; 0 1 1 1 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], -[0 0 0 0 0 0; 0 0 0 0 0 0; 0 1 0 1 0 0; 0 0 1 1 0 0; 0 0 1 0 0 0; 0 0 0 0 0 0], -[0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 1 0 0; 0 1 0 1 0 0; 0 0 1 1 0 0; 0 0 0 0 0 0], dims=3) +ca = CellularAutomaton(Life((3, (2, 3))), space, gens) +@test ca.evolution == cat( + [0 0 0 0 0 0; 0 0 1 0 0 0; 1 0 1 0 0 0; 0 1 1 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], + [0 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 1 0 0; 0 1 1 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], + [0 0 0 0 0 0; 0 0 1 0 0 0; 0 0 0 1 0 0; 0 1 1 1 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0], + [0 0 0 0 0 0; 0 0 0 0 0 0; 0 1 0 1 0 0; 0 0 1 1 0 0; 0 0 1 0 0 0; 0 0 0 0 0 0], + [0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 1 0 0; 0 1 0 1 0 0; 0 0 1 1 0 0; 0 0 0 0 0 0]; + dims=3, +) diff --git a/test/qa.jl b/test/qa.jl new file mode 100644 index 0000000..d54f6f1 --- /dev/null +++ b/test/qa.jl @@ -0,0 +1,4 @@ +using CellularAutomata +using Aqua: Aqua + +Aqua.test_all(CellularAutomata; ambiguities=false, deps_compat=(check_extras = false)) diff --git a/test/runtests.jl b/test/runtests.jl index bf7e278..642abc2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,20 +1,36 @@ using Test using SafeTestsets +@safetestset "Quality Assurance" begin + include("qa.jl") +end + @testset "DCA" begin - @safetestset "Size tests" begin include("dca_test.jl") end - @safetestset "ECA ruleset tests" begin include("eca_ruleset_test.jl") end + @safetestset "Size tests" begin + include("dca_test.jl") + end + @safetestset "ECA ruleset tests" begin + include("eca_ruleset_test.jl") + end end @testset "TCA" begin - @safetestset "Size tests" begin include("tca_test.jl") end + @safetestset "Size tests" begin + include("tca_test.jl") + end end @testset "CCA" begin - @safetestset "Size tests" begin include("cca_test.jl") end + @safetestset "Size tests" begin + include("cca_test.jl") + end end @testset "Life-like" begin - @safetestset "Life glider" begin include("glider_test.jl") end - @safetestset "Life blinker" begin include("blinker_test.jl") end + @safetestset "Life glider" begin + include("glider_test.jl") + end + @safetestset "Life blinker" begin + include("blinker_test.jl") + end end diff --git a/test/tca_test.jl b/test/tca_test.jl index fcc7dca..618dbbc 100644 --- a/test/tca_test.jl +++ b/test/tca_test.jl @@ -5,11 +5,11 @@ const states = 4 const radius = 1 const generations = 10 const ncells = 11 -const starting_array = rand(0:states-1, ncells) +const starting_array = rand(0:(states - 1), ncells) const rule = 107396 #testing states > 2 -ca = CellularAutomaton(DCA(rule, states=states), starting_array, generations) +ca = CellularAutomaton(DCA(rule; states=states), starting_array, generations) @test size(ca.evolution) == (generations, ncells) @@ -22,4 +22,4 @@ const bstarting_array = rand(Bool, ncells) bca = ca = CellularAutomaton(DCA(brule), bstarting_array, generations) @test size(bca.evolution) == (generations, ncells) -=# \ No newline at end of file +=# From 30325ccfabbd0c7815d9e6fa38a75d0863d2753c Mon Sep 17 00:00:00 2001 From: MartinuzziFrancesco Date: Wed, 6 Mar 2024 09:34:24 +0100 Subject: [PATCH 2/3] added formatcheck --- .github/workflows/FormatCheck.yml | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .github/workflows/FormatCheck.yml diff --git a/.github/workflows/FormatCheck.yml b/.github/workflows/FormatCheck.yml new file mode 100644 index 0000000..dd55150 --- /dev/null +++ b/.github/workflows/FormatCheck.yml @@ -0,0 +1,42 @@ +name: format-check + +on: + push: + branches: + - 'master' + - 'release-' + tags: '*' + pull_request: + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + julia-version: [1] + julia-arch: [x86] + os: [ubuntu-latest] + steps: + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.julia-version }} + + - uses: actions/checkout@v4 + - name: Install JuliaFormatter and format + # This will use the latest version by default but you can set the version like so: + # + # julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))' + run: | + julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))' + julia -e 'using JuliaFormatter; format(".", verbose=true)' + - name: Format check + run: | + julia -e ' + out = Cmd(`git diff --name-only`) |> read |> String + if out == "" + exit(0) + else + @error "Some files have not been formatted !!!" + write(stdout, out) + exit(1) + end' From b15f9e1cee927618b9fc24f562cd9223f6450d2c Mon Sep 17 00:00:00 2001 From: MartinuzziFrancesco Date: Wed, 6 Mar 2024 09:41:13 +0100 Subject: [PATCH 3/3] up --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3685b5e..2586f04 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CellularAutomata" uuid = "878138dc-5b27-11ea-1a71-cb95d38d6b29" authors = ["Francesco Martinuzzi"] -version = "0.0.3" +version = "0.0.4" [deps]