mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-08 22:38:03 -05:00
Draft Population unit tests
This commit is contained in:
196
test/unit-kami-population.cc
Normal file
196
test/unit-kami-population.cc
Normal file
@@ -0,0 +1,196 @@
|
||||
/*-
|
||||
* 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 <kami/agent.h>
|
||||
#include <kami/population.h>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace kami;
|
||||
|
||||
class TestAgent : public Agent {
|
||||
private:
|
||||
int _x;
|
||||
|
||||
public:
|
||||
explicit TestAgent(int x) : _x(x) {};
|
||||
|
||||
AgentID step(std::shared_ptr<Model> model) override {
|
||||
return get_agent_id();
|
||||
}
|
||||
|
||||
int getval() {
|
||||
return _x;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Population, 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 Population population_foo;
|
||||
);
|
||||
}
|
||||
|
||||
TEST(Population, add_agent) {
|
||||
Population population_foo;
|
||||
auto agent_foo = std::make_shared<TestAgent>(8675309);
|
||||
auto agent_bar = std::make_shared<TestAgent>(1729);
|
||||
|
||||
{
|
||||
auto agent_id_baz = population_foo.add_agent(agent_foo);
|
||||
EXPECT_EQ(agent_id_baz, agent_foo->get_agent_id());
|
||||
}
|
||||
{
|
||||
auto agent_id_baz = population_foo.add_agent(agent_bar);
|
||||
EXPECT_EQ(agent_id_baz, agent_bar->get_agent_id());
|
||||
}
|
||||
{
|
||||
auto agent_id_baz = population_foo.add_agent(agent_foo);
|
||||
EXPECT_EQ(agent_id_baz, agent_foo->get_agent_id());
|
||||
auto agent_id_qux = population_foo.add_agent(agent_bar);
|
||||
EXPECT_EQ(agent_id_qux, agent_bar->get_agent_id());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Population, get_agent_by_id) {
|
||||
auto agent_foo = std::make_shared<TestAgent>(8675309);
|
||||
auto agent_bar = std::make_shared<TestAgent>(1729);
|
||||
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
auto agent_baz_opt = population_foo.get_agent_by_id(agent_foo->get_agent_id());
|
||||
EXPECT_TRUE(agent_baz_opt);
|
||||
|
||||
auto agent_baz = std::dynamic_pointer_cast<TestAgent>(agent_baz_opt.value());
|
||||
EXPECT_EQ(agent_baz->getval(), 8675309);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
auto agent_baz_opt = population_foo.get_agent_by_id(agent_bar->get_agent_id());
|
||||
EXPECT_FALSE(agent_baz_opt);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
static_cast<void>(population_foo.add_agent(agent_bar));
|
||||
|
||||
auto agent_baz_opt = population_foo.get_agent_by_id(agent_foo->get_agent_id());
|
||||
EXPECT_TRUE(agent_baz_opt);
|
||||
|
||||
auto agent_baz = std::dynamic_pointer_cast<TestAgent>(agent_baz_opt.value());
|
||||
EXPECT_EQ(agent_baz->getval(), 8675309);
|
||||
|
||||
auto agent_qux_opt = population_foo.get_agent_by_id(agent_bar->get_agent_id());
|
||||
EXPECT_TRUE(agent_qux_opt);
|
||||
|
||||
auto agent_qux = std::dynamic_pointer_cast<TestAgent>(agent_qux_opt.value());
|
||||
EXPECT_EQ(agent_qux->getval(), 1729);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
static_cast<void>(population_foo.add_agent(agent_bar));
|
||||
|
||||
auto agent_qux_opt = population_foo.get_agent_by_id(agent_bar->get_agent_id());
|
||||
EXPECT_TRUE(agent_qux_opt);
|
||||
|
||||
auto agent_qux = std::dynamic_pointer_cast<TestAgent>(agent_qux_opt.value());
|
||||
EXPECT_EQ(agent_qux->getval(), 1729);
|
||||
|
||||
auto agent_baz_opt = population_foo.get_agent_by_id(agent_foo->get_agent_id());
|
||||
EXPECT_TRUE(agent_baz_opt);
|
||||
|
||||
auto agent_baz = std::dynamic_pointer_cast<TestAgent>(agent_baz_opt.value());
|
||||
EXPECT_EQ(agent_baz->getval(), 8675309);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Population, get_agent_list) {
|
||||
auto agent_foo = std::make_shared<TestAgent>(8675309);
|
||||
auto agent_bar = std::make_shared<TestAgent>(1729);
|
||||
auto agent_baz = std::make_shared<TestAgent>(4104);
|
||||
auto agent_qux = std::make_shared<TestAgent>(196);
|
||||
|
||||
{
|
||||
Population population_foo;
|
||||
auto tval = std::make_shared<std::vector<AgentID>>();
|
||||
auto rval = population_foo.get_agent_list();
|
||||
|
||||
EXPECT_EQ(*tval, *rval);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
auto tval = std::make_shared<std::vector<AgentID>>();
|
||||
auto rval = population_foo.get_agent_list();
|
||||
|
||||
tval->push_back(agent_foo->get_agent_id());
|
||||
EXPECT_NE(*tval, *rval);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
static_cast<void>(population_foo.add_agent(agent_bar));
|
||||
|
||||
auto tval = std::make_shared<std::vector<AgentID>>();
|
||||
auto uval = std::make_shared<std::vector<AgentID>>();
|
||||
auto rval = population_foo.get_agent_list();
|
||||
|
||||
tval->push_back(agent_foo->get_agent_id());
|
||||
tval->push_back(agent_bar->get_agent_id());
|
||||
EXPECT_EQ(*tval, *rval);
|
||||
|
||||
// Ordering matters
|
||||
uval->push_back(agent_bar->get_agent_id());
|
||||
uval->push_back(agent_foo->get_agent_id());
|
||||
EXPECT_NE(*uval, *rval);
|
||||
}
|
||||
{
|
||||
Population population_foo;
|
||||
static_cast<void>(population_foo.add_agent(agent_foo));
|
||||
static_cast<void>(population_foo.add_agent(agent_bar));
|
||||
static_cast<void>(population_foo.add_agent(agent_baz));
|
||||
static_cast<void>(population_foo.add_agent(agent_qux));
|
||||
|
||||
auto tval = std::make_shared<std::vector<AgentID>>();
|
||||
auto rval = population_foo.get_agent_list();
|
||||
|
||||
tval->push_back(agent_foo->get_agent_id());
|
||||
tval->push_back(agent_bar->get_agent_id());
|
||||
tval->push_back(agent_baz->get_agent_id());
|
||||
tval->push_back(agent_qux->get_agent_id());
|
||||
EXPECT_EQ(*tval, *rval);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user