From e19dc7b449afdb9078ac7c38aa6aa68482a68b4f Mon Sep 17 00:00:00 2001 From: "James P. Howard, II" Date: Sun, 7 Aug 2022 14:56:42 -0400 Subject: [PATCH] Add unit testing for Model --- include/kami/model.h | 11 ++++ test/unit-kami-model.cc | 135 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 test/unit-kami-model.cc diff --git a/include/kami/model.h b/include/kami/model.h index 6fc4b7b..3b06293 100644 --- a/include/kami/model.h +++ b/include/kami/model.h @@ -118,8 +118,19 @@ namespace kami { std::shared_ptr set_scheduler(std::shared_ptr scheduler); protected: + /** + * @brief Reference copy of the `Domain` + */ std::shared_ptr _domain = nullptr; + + /** + * @brief Reference copy of the `Population` + */ std::shared_ptr _pop = nullptr; + + /** + * @brief Reference copy of the `Scheduler` + */ std::shared_ptr _sched = nullptr; }; diff --git a/test/unit-kami-model.cc b/test/unit-kami-model.cc new file mode 100644 index 0000000..37e631d --- /dev/null +++ b/test/unit-kami-model.cc @@ -0,0 +1,135 @@ +/*- + * Copyright (c) 2022 The Johns Hopkins University Applied Physics + * Laboratory LLC + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +class TestAgent : public kami::Agent { +public: + kami::AgentID step(std::shared_ptr model) override { + return get_agent_id(); + } +}; + +class TestModel : public kami::Model { +public: + std::shared_ptr step() override { + return shared_from_this(); + } + + std::shared_ptr run(unsigned int) override { + return shared_from_this(); + } +}; + +TEST(Model, DefaultConstructor) { + // There is really no way this can go wrong, but + // we add this check anyway in case of future + // changes. + EXPECT_NO_THROW( + const TestModel model_foo + ); +} + +TEST(Model, set_population) { + auto model_foo = std::make_shared(); + auto popul_foo = std::make_shared(); + + auto popul_bar = model_foo->set_population(popul_foo); + EXPECT_EQ(popul_foo, popul_bar); +} + +TEST(Model, get_population) { + auto model_foo = std::make_shared(); + auto popul_foo = std::make_shared(); + + auto popul_nul = model_foo->get_population(); + + auto popul_bar = model_foo->set_population(popul_foo); + auto popul_baz = model_foo->get_population(); + + EXPECT_TRUE(popul_baz); + EXPECT_EQ(popul_foo, popul_baz); + EXPECT_EQ(popul_bar, popul_baz); +} + +TEST(Model, set_scheduler) { + auto model_foo = std::make_shared(); + auto sched_foo = std::make_shared(); + + auto sched_bar = model_foo->set_scheduler(sched_foo); + EXPECT_EQ(sched_foo, sched_bar); +} + +TEST(Model, get_scheduler) { + auto model_foo = std::make_shared(); + auto sched_foo = std::make_shared(); + + auto sched_nul = model_foo->get_scheduler(); + EXPECT_FALSE(sched_nul); + + auto sched_bar = model_foo->set_scheduler(sched_foo); + auto sched_baz = model_foo->get_scheduler(); + + EXPECT_TRUE(sched_baz); + EXPECT_EQ(sched_foo, sched_baz); + EXPECT_EQ(sched_bar, sched_baz); +} + +TEST(Model, set_domain) { + auto model_foo = std::make_shared(); + auto grid2_foo = std::make_shared(10, 10, true, true); + + auto grid2_bar = model_foo->set_domain(grid2_foo); + EXPECT_EQ(grid2_foo, grid2_bar); +} + +TEST(Model, get_domain) { + auto model_foo = std::make_shared(); + auto grid2_foo = std::make_shared(10, 10, true, true); + + auto grid2_nul = model_foo->get_domain(); + EXPECT_FALSE(grid2_nul); + + auto grid2_bar = model_foo->set_domain(grid2_foo); + auto grid2_baz = model_foo->get_domain(); + + EXPECT_TRUE(grid2_baz); + EXPECT_EQ(grid2_foo, grid2_baz); + EXPECT_EQ(grid2_bar, grid2_baz); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} +