forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionRegistry.test.cc
164 lines (135 loc) · 4.82 KB
/
ActionRegistry.test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/global/ActionRegistry.test.cc
//---------------------------------------------------------------------------//
#include "celeritas/global/ActionRegistry.hh"
#include "celeritas/global/ActionRegistryOutput.hh"
#include "celeritas_test.hh"
namespace celeritas
{
namespace test
{
//---------------------------------------------------------------------------//
class MyExplicitAction final : public ExplicitCoreActionInterface,
public BeginRunActionInterface
{
public:
//@{
//! \name Type aliases
using ExplicitCoreActionInterface::CoreStateDevice;
using ExplicitCoreActionInterface::CoreStateHost;
//@}
public:
MyExplicitAction(ActionId ai, ActionOrder ao) : action_id_(ai), order_{ao}
{
}
ActionId action_id() const final { return action_id_; }
std::string_view label() const final { return "explicit"; }
std::string_view description() const final
{
return "explicit action test";
}
void begin_run(CoreParams const&, CoreStateHost&) final
{
host_count_ = 0;
}
void begin_run(CoreParams const&, CoreStateDevice&) final
{
device_count_ = 0;
}
void execute(CoreParams const&, CoreStateHost&) const final
{
++host_count_;
}
void execute(CoreParams const&, CoreStateDevice&) const final
{
++device_count_;
}
int host_count() const { return host_count_; }
int device_count() const { return device_count_; }
ActionOrder order() const final { return order_; }
private:
ActionId action_id_;
ActionOrder order_;
mutable int host_count_{-100};
mutable int device_count_{-100};
};
class MyImplicitAction final : public ConcreteAction
{
public:
// Construct with ID and label
using ConcreteAction::ConcreteAction;
};
//---------------------------------------------------------------------------//
// TEST HARNESS
//---------------------------------------------------------------------------//
class ActionRegistryTest : public Test
{
protected:
void SetUp() override
{
EXPECT_EQ(ActionId{0}, mgr.next_id());
EXPECT_EQ(0, mgr.num_actions());
EXPECT_TRUE(mgr.empty());
// Add actions
auto impl1 = std::make_shared<MyImplicitAction>(mgr.next_id(), "impl1");
mgr.insert(impl1);
expl_action = std::make_shared<MyExplicitAction>(mgr.next_id(),
ActionOrder::pre);
mgr.insert(expl_action);
auto impl2 = std::make_shared<MyImplicitAction>(
mgr.next_id(), "impl2", "the second implicit action");
mgr.insert(impl2);
}
ActionRegistry mgr;
std::shared_ptr<MyExplicitAction> expl_action;
};
//---------------------------------------------------------------------------//
// TESTS
//---------------------------------------------------------------------------//
TEST_F(ActionRegistryTest, accessors)
{
EXPECT_FALSE(mgr.empty());
EXPECT_EQ(3, mgr.num_actions());
// Find IDs
auto expl_id = mgr.find_action("explicit");
EXPECT_EQ(1, expl_id.unchecked_get());
EXPECT_EQ(0, mgr.find_action("impl1").unchecked_get());
EXPECT_EQ(ActionId{}, mgr.find_action("nonexistent"));
// Access an action
EXPECT_EQ("explicit action test", mgr.action(expl_id)->description());
EXPECT_EQ("explicit", mgr.id_to_label(expl_id));
EXPECT_STREQ("pre", to_cstring(expl_action->order()));
ASSERT_EQ(1, mgr.mutable_actions().size());
EXPECT_EQ(expl_action, mgr.mutable_actions().front());
}
TEST_F(ActionRegistryTest, output)
{
// Create output handler from a shared pointer (with a null deleter)
ActionRegistryOutput out(std::shared_ptr<ActionRegistry const>(
&mgr, [](ActionRegistry const*) {}));
EXPECT_EQ("actions", out.label());
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(
R"json({"_category":"internal","_label":"actions","description":["","explicit action test","the second implicit action"],"label":["impl1","explicit","impl2"]})json",
to_string(out));
}
}
TEST_F(ActionRegistryTest, errors)
{
// Incorrect ID
EXPECT_THROW(
mgr.insert(std::make_shared<MyImplicitAction>(ActionId{100}, "impl3")),
RuntimeError);
// Duplicate label
EXPECT_THROW(
mgr.insert(std::make_shared<MyImplicitAction>(mgr.next_id(), "impl1")),
RuntimeError);
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas