From 9333e1c2fab0d26cb2a15d39c3eb9d8543b107c7 Mon Sep 17 00:00:00 2001 From: "James P. Howard, II" Date: Fri, 19 Aug 2022 20:27:04 -0400 Subject: [PATCH] Schedulers tests --- test/unit-kami-random.cc | 152 +++++++++++++++++++++++++++++++++++ test/unit-kami-sequential.cc | 45 +++++++++-- test/unit-kami-staged.cc | 125 ++++++++++++++++++++++++++++ 3 files changed, 314 insertions(+), 8 deletions(-) create mode 100644 test/unit-kami-random.cc create mode 100644 test/unit-kami-staged.cc diff --git a/test/unit-kami-random.cc b/test/unit-kami-random.cc new file mode 100644 index 0000000..819427e --- /dev/null +++ b/test/unit-kami-random.cc @@ -0,0 +1,152 @@ +/*- + * 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 +#include + +#include +#include + +using namespace kami; +using namespace std; + +class TestAgent : public Agent { +public: + AgentID step(shared_ptr model) override { + return get_agent_id(); + } +}; + +class TestModel : public Model { +public: + optional>> step() { + return _sched->step(shared_from_this()); + } + + optional>> step(shared_ptr> agent_list) { + return _sched->step(shared_from_this(), move(agent_list)); + } +}; + +class RandomSchedulerTest : public ::testing::Test { +protected: + shared_ptr mod = nullptr; + shared_ptr rng = nullptr; + + void SetUp() override { + mod = make_shared(); + rng = make_shared(); + auto popul_foo = make_shared(); + auto sched_foo = make_shared(rng); + + // Domain is not required for this test + static_cast(mod->set_population(popul_foo)); + static_cast(mod->set_scheduler(sched_foo)); + + for (auto i = 0; i < 10; i++) { + auto agent_foo = make_shared(); + static_cast(popul_foo->add_agent(agent_foo)); + } + } +}; + +TEST(RandomScheduler, 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 RandomScheduler sched_foo; + ); +} + +TEST_F(RandomSchedulerTest, step_interface1) { + auto tval = mod->get_population().value()->get_agent_list(); + auto rval = mod->step(); + + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + + // Sort both return values and just make sure all of them all the same... + // We cannot test permutation since, well, you know... + set tval_set = set(tval->begin(), tval->end()); + set rval_set = set(rval.value()->begin(), rval.value()->end()); + EXPECT_EQ(tval_set, rval_set); +} + +TEST_F(RandomSchedulerTest, step_interface2) { + auto tval = mod->get_population().value()->get_agent_list(); + auto rval = mod->step(tval); + + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + + set tval_set = set(tval->begin(), tval->end()); + set rval_set = set(rval.value()->begin(), rval.value()->end()); + EXPECT_EQ(tval_set, rval_set); +} + +TEST_F(RandomSchedulerTest, step_10000) { + auto tval = mod->get_population().value()->get_agent_list(); + set tval_set = set(tval->begin(), tval->end()); + + // Do it a lot... + for (auto i = 0; i < 10000; i++) { + auto rval = mod->step(); + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + + set rval_set = set(rval.value()->begin(), rval.value()->end()); + EXPECT_EQ(tval_set, rval_set); + } +} + +TEST_F(RandomSchedulerTest, get_rng) { + auto rval = static_pointer_cast(mod->get_scheduler().value())->get_rng(); + + EXPECT_EQ(rng, rval); +} + +TEST_F(RandomSchedulerTest, set_rng) { + auto new_rng = make_shared(); + auto rval1 = static_pointer_cast(mod->get_scheduler().value())->get_rng(); + + static_cast(static_pointer_cast(mod->get_scheduler().value())->set_rng(new_rng)); + auto rval2 = static_pointer_cast(mod->get_scheduler().value())->get_rng(); + + EXPECT_EQ(new_rng, rval2); + EXPECT_NE(new_rng, rval1); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/unit-kami-sequential.cc b/test/unit-kami-sequential.cc index 08bc7c9..9fef136 100644 --- a/test/unit-kami-sequential.cc +++ b/test/unit-kami-sequential.cc @@ -23,6 +23,10 @@ * SOFTWARE. */ +#include +#include +#include + #include #include #include @@ -35,33 +39,37 @@ using namespace std; class TestAgent : public Agent { public: - AgentID step(std::shared_ptr model) override { + AgentID step(shared_ptr model) override { return get_agent_id(); } }; class TestModel : public Model { public: - std::optional>> step() { + optional>> step() { return _sched->step(shared_from_this()); } + + optional>> step(shared_ptr> agent_list) { + return _sched->step(shared_from_this(), move(agent_list)); + } }; class SequentialSchedulerTest : public ::testing::Test { protected: - std::shared_ptr mod = nullptr; + shared_ptr mod = nullptr; void SetUp() override { - mod = std::make_shared(); - auto popul_foo = std::make_shared(); - auto sched_foo = std::make_shared(); + mod = make_shared(); + auto popul_foo = make_shared(); + auto sched_foo = make_shared(); // Domain is not required for this test static_cast(mod->set_population(popul_foo)); static_cast(mod->set_scheduler(sched_foo)); for (auto i = 0; i < 10; i++) { - auto agent_foo = std::make_shared(); + auto agent_foo = make_shared(); static_cast(popul_foo->add_agent(agent_foo)); } } @@ -76,7 +84,7 @@ TEST(SequentialScheduler, DefaultConstructor) { ); } -TEST_F(SequentialSchedulerTest, step) { +TEST_F(SequentialSchedulerTest, step_interface1) { auto tval = mod->get_population().value()->get_agent_list(); auto rval = mod->step(); @@ -85,6 +93,27 @@ TEST_F(SequentialSchedulerTest, step) { EXPECT_EQ(*rval.value(), *tval); } +TEST_F(SequentialSchedulerTest, step_interface2) { + auto tval = mod->get_population().value()->get_agent_list(); + auto rval = mod->step(tval); + + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + EXPECT_EQ(*rval.value(), *tval); +} + +TEST_F(SequentialSchedulerTest, step_10000) { + auto tval = mod->get_population().value()->get_agent_list(); + + // Do it a lot... + for (auto i = 0; i < 10000; i++) { + auto rval = mod->step(); + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + EXPECT_EQ(*rval.value(), *tval); + } +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/test/unit-kami-staged.cc b/test/unit-kami-staged.cc new file mode 100644 index 0000000..77e69ef --- /dev/null +++ b/test/unit-kami-staged.cc @@ -0,0 +1,125 @@ +/*- + * 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 +#include + +using namespace kami; +using namespace std; + +class TestAgent : public StagedAgent { +public: + AgentID step(shared_ptr model) override { + return get_agent_id(); + } + + AgentID advance(shared_ptr model) override { + return get_agent_id(); + } +}; + +class TestModel : public Model { +public: + optional>> step() { + return _sched->step(shared_from_this()); + } + + optional>> step(shared_ptr> agent_list) { + return _sched->step(shared_from_this(), move(agent_list)); + } +}; + +class StagedSchedulerTest : public ::testing::Test { +protected: + shared_ptr mod = nullptr; + + void SetUp() override { + mod = make_shared(); + auto popul_foo = make_shared(); + auto sched_foo = make_shared(); + + // Domain is not required for this test + static_cast(mod->set_population(popul_foo)); + static_cast(mod->set_scheduler(sched_foo)); + + for (auto i = 0; i < 10; i++) { + auto agent_foo = make_shared(); + static_cast(popul_foo->add_agent(agent_foo)); + } + } +}; + +TEST(StagedScheduler, 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 StagedScheduler sched_foo; + ); +} + +TEST_F(StagedSchedulerTest, step_interface1) { + auto tval = mod->get_population().value()->get_agent_list(); + auto rval = mod->step(); + + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + EXPECT_EQ(*rval.value(), *tval); +} + +TEST_F(StagedSchedulerTest, step_interface2) { + auto tval = mod->get_population().value()->get_agent_list(); + auto rval = mod->step(tval); + + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + EXPECT_EQ(*rval.value(), *tval); +} + +TEST_F(StagedSchedulerTest, step_10000) { + auto tval = mod->get_population().value()->get_agent_list(); + + // Do it a lot... + for (auto i = 0; i < 10000; i++) { + auto rval = mod->step(); + EXPECT_TRUE(rval); + EXPECT_EQ(rval.value()->size(), 10); + EXPECT_EQ(*rval.value(), *tval); + } +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} +