Schedulers tests

This commit is contained in:
James P. Howard, II
2022-08-19 20:27:04 -04:00
parent f18e2c99da
commit 9333e1c2fa
3 changed files with 314 additions and 8 deletions

152
test/unit-kami-random.cc Normal file
View File

@@ -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 <memory>
#include <random>
#include <set>
#include <utility>
#include <vector>
#include <kami/agent.h>
#include <kami/population.h>
#include <kami/random.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace kami;
using namespace std;
class TestAgent : public Agent {
public:
AgentID step(shared_ptr<Model> model) override {
return get_agent_id();
}
};
class TestModel : public Model {
public:
optional<shared_ptr<vector<AgentID>>> step() {
return _sched->step(shared_from_this());
}
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
return _sched->step(shared_from_this(), move(agent_list));
}
};
class RandomSchedulerTest : public ::testing::Test {
protected:
shared_ptr<TestModel> mod = nullptr;
shared_ptr<ranlux24> rng = nullptr;
void SetUp() override {
mod = make_shared<TestModel>();
rng = make_shared<ranlux24>();
auto popul_foo = make_shared<Population>();
auto sched_foo = make_shared<RandomScheduler>(rng);
// Domain is not required for this test
static_cast<void>(mod->set_population(popul_foo));
static_cast<void>(mod->set_scheduler(sched_foo));
for (auto i = 0; i < 10; i++) {
auto agent_foo = make_shared<TestAgent>();
static_cast<void>(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<AgentID> tval_set = set(tval->begin(), tval->end());
set<AgentID> 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<AgentID> tval_set = set(tval->begin(), tval->end());
set<AgentID> 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<AgentID> 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<AgentID> 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<RandomScheduler>(mod->get_scheduler().value())->get_rng();
EXPECT_EQ(rng, rval);
}
TEST_F(RandomSchedulerTest, set_rng) {
auto new_rng = make_shared<ranlux24>();
auto rval1 = static_pointer_cast<RandomScheduler>(mod->get_scheduler().value())->get_rng();
static_cast<void>(static_pointer_cast<RandomScheduler>(mod->get_scheduler().value())->set_rng(new_rng));
auto rval2 = static_pointer_cast<RandomScheduler>(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();
}

View File

@@ -23,6 +23,10 @@
* SOFTWARE.
*/
#include <memory>
#include <utility>
#include <vector>
#include <kami/agent.h>
#include <kami/population.h>
#include <kami/sequential.h>
@@ -35,33 +39,37 @@ using namespace std;
class TestAgent : public Agent {
public:
AgentID step(std::shared_ptr<Model> model) override {
AgentID step(shared_ptr<Model> model) override {
return get_agent_id();
}
};
class TestModel : public Model {
public:
std::optional<std::shared_ptr<std::vector<AgentID>>> step() {
optional<shared_ptr<vector<AgentID>>> step() {
return _sched->step(shared_from_this());
}
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
return _sched->step(shared_from_this(), move(agent_list));
}
};
class SequentialSchedulerTest : public ::testing::Test {
protected:
std::shared_ptr<TestModel> mod = nullptr;
shared_ptr<TestModel> mod = nullptr;
void SetUp() override {
mod = std::make_shared<TestModel>();
auto popul_foo = std::make_shared<Population>();
auto sched_foo = std::make_shared<SequentialScheduler>();
mod = make_shared<TestModel>();
auto popul_foo = make_shared<Population>();
auto sched_foo = make_shared<SequentialScheduler>();
// Domain is not required for this test
static_cast<void>(mod->set_population(popul_foo));
static_cast<void>(mod->set_scheduler(sched_foo));
for (auto i = 0; i < 10; i++) {
auto agent_foo = std::make_shared<TestAgent>();
auto agent_foo = make_shared<TestAgent>();
static_cast<void>(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();

125
test/unit-kami-staged.cc Normal file
View File

@@ -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 <memory>
#include <utility>
#include <vector>
#include <kami/agent.h>
#include <kami/population.h>
#include <kami/staged.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace kami;
using namespace std;
class TestAgent : public StagedAgent {
public:
AgentID step(shared_ptr<Model> model) override {
return get_agent_id();
}
AgentID advance(shared_ptr<Model> model) override {
return get_agent_id();
}
};
class TestModel : public Model {
public:
optional<shared_ptr<vector<AgentID>>> step() {
return _sched->step(shared_from_this());
}
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
return _sched->step(shared_from_this(), move(agent_list));
}
};
class StagedSchedulerTest : public ::testing::Test {
protected:
shared_ptr<TestModel> mod = nullptr;
void SetUp() override {
mod = make_shared<TestModel>();
auto popul_foo = make_shared<Population>();
auto sched_foo = make_shared<StagedScheduler>();
// Domain is not required for this test
static_cast<void>(mod->set_population(popul_foo));
static_cast<void>(mod->set_scheduler(sched_foo));
for (auto i = 0; i < 10; i++) {
auto agent_foo = make_shared<TestAgent>();
static_cast<void>(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();
}