Skip to content

Commit c997c1a

Browse files
committed
add tests
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent 9e54be9 commit c997c1a

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/core_utils.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ constexpr void run_functor_with_tuple_index_return_void(Functor functor, std::in
3737
(functor.template operator()<std::tuple_element_t<Indices, Tuple>>(), ...);
3838
}
3939

40-
template <typename Tuple, class Functor, std::size_t... Indices>
41-
constexpr auto run_functor_with_tuple_index_return_array(Functor functor, std::index_sequence<Indices...>) {
42-
return std::array { functor.template operator()<std::tuple_element_t<Indices, Tuple>>()... };
43-
}
44-
4540
} // namespace detail
4641

4742
constexpr Idx invalid_index{-1};
@@ -96,11 +91,6 @@ template <typename Tuple, class Functor> constexpr void run_functor_with_tuple_r
9691
std::make_index_sequence<std::tuple_size_v<Tuple>>{});
9792
}
9893

99-
template <typename Tuple, class Functor> constexpr auto run_functor_with_tuple_return_array(Functor functor) {
100-
return detail::run_functor_with_tuple_index_return_array<Tuple>(
101-
functor, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
102-
}
103-
10494
template <class T, class U> struct MainModelType;
10595

10696
// TODO: discussion on checking dependent types can also be done here.

tests/cpp_unit_tests/test_core_utils.cpp

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,40 @@ namespace power_grid_model::main_core::utils {
1111

1212
namespace {
1313
// TODO test with a non used type
14-
struct AComponent {};
14+
struct AComponent {
15+
using UpdateType = void;
16+
static constexpr char const* name = "a_component";
17+
};
1518
} // namespace
1619

1720
TEST_CASE("MainModelType") {
1821

1922
SUBCASE("Node Source") {
2023
using ModelType = MainModelType<ExtraRetrievableTypes<Base, Node, Appliance>, ComponentList<Node, Source>>;
21-
CHECK(true);
24+
2225
static_assert(std::is_same_v<typename ModelType::ComponentContainer,
2326
Container<ExtraRetrievableTypes<Base, Node, Appliance>, Node, Source>>);
2427
static_assert(std::is_same_v<typename ModelType::ComponentTypesTuple, std::tuple<Node, Source>>);
2528
static_assert(std::is_same_v<typename ModelType::TopologyTypesTuple, std::tuple<Node, Source>>);
2629
static_assert(std::is_same_v<typename ModelType::TopologyConnectionTypesTuple, std::tuple<Source>>);
30+
static_assert(ModelType::index_of_component<Node> == 0);
31+
static_assert(ModelType::index_of_component<Source> == 1);
2732
static_assert(ModelType::n_types == 2);
33+
34+
CHECK(ModelType::run_functor_with_all_component_types_return_array(
35+
[]<typename CompType>() { return CompType::name; }) == std::array<const char*, 2>{"node", "source"});
36+
37+
std::vector<const char*> calls;
38+
ModelType::run_functor_with_all_component_types_return_void(
39+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
40+
CHECK(calls == std::vector<const char*>{"node", "source"});
41+
42+
calls.clear();
43+
run_functor_with_tuple_return_void<typename ModelType::TopologyTypesTuple>(
44+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
45+
CHECK(calls == std::vector<const char*>{"node", "source"});
46+
47+
// static_assert(is_constructible_v<MainModelImpl<ModelType>>);
2848
}
2949
SUBCASE("Node Line Source") {
3050
using ModelType =
@@ -36,7 +56,24 @@ TEST_CASE("MainModelType") {
3656
static_assert(std::is_same_v<typename ModelType::ComponentTypesTuple, std::tuple<Node, Line, Source>>);
3757
static_assert(std::is_same_v<typename ModelType::TopologyTypesTuple, std::tuple<Node, Branch, Source>>);
3858
static_assert(std::is_same_v<typename ModelType::TopologyConnectionTypesTuple, std::tuple<Branch, Source>>);
59+
static_assert(ModelType::index_of_component<Node> == 0);
60+
static_assert(ModelType::index_of_component<Line> == 1);
61+
static_assert(ModelType::index_of_component<Source> == 2);
3962
static_assert(ModelType::n_types == 3);
63+
64+
CHECK(ModelType::run_functor_with_all_component_types_return_array([]<typename CompType>() {
65+
return CompType::name;
66+
}) == std::array<const char*, 3>{"node", "line", "source"});
67+
68+
std::vector<const char*> calls;
69+
ModelType::run_functor_with_all_component_types_return_void(
70+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
71+
CHECK(calls == std::vector<const char*>{"node", "line", "source"});
72+
calls.clear();
73+
74+
run_functor_with_tuple_return_void<typename ModelType::TopologyTypesTuple>(
75+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
76+
CHECK(calls == std::vector<const char*>{"node", "branch", "source"});
4077
}
4178
SUBCASE("Different component order: Line Source Node") {
4279
using ModelType =
@@ -48,8 +85,56 @@ TEST_CASE("MainModelType") {
4885
static_assert(std::is_same_v<typename ModelType::ComponentTypesTuple, std::tuple<Line, Source, Node>>);
4986
static_assert(std::is_same_v<typename ModelType::TopologyTypesTuple, std::tuple<Node, Branch, Source>>);
5087
static_assert(std::is_same_v<typename ModelType::TopologyConnectionTypesTuple, std::tuple<Branch, Source>>);
88+
static_assert(ModelType::index_of_component<Line> == 0);
89+
static_assert(ModelType::index_of_component<Source> == 1);
90+
static_assert(ModelType::index_of_component<Node> == 2);
91+
static_assert(ModelType::n_types == 3);
92+
93+
CHECK(ModelType::run_functor_with_all_component_types_return_array([]<typename CompType>() {
94+
return CompType::name;
95+
}) == std::array<const char*, 3>{"line", "source", "node"});
96+
97+
std::vector<const char*> calls;
98+
ModelType::run_functor_with_all_component_types_return_void(
99+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
100+
CHECK(calls == std::vector<const char*>{"line", "source", "node"});
101+
calls.clear();
102+
103+
run_functor_with_tuple_return_void<typename ModelType::TopologyTypesTuple>(
104+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
105+
CHECK(calls == std::vector<const char*>{"node", "branch", "source"});
106+
}
107+
108+
SUBCASE("Node AComponent Source") {
109+
using ModelType =
110+
MainModelType<ExtraRetrievableTypes<Base, Node, Appliance>, ComponentList<Node, AComponent, Source>>;
111+
112+
static_assert(
113+
std::is_same_v<typename ModelType::ComponentContainer,
114+
Container<ExtraRetrievableTypes<Base, Node, Appliance>, Node, AComponent, Source>>);
115+
static_assert(std::is_same_v<typename ModelType::ComponentTypesTuple, std::tuple<Node, AComponent, Source>>);
116+
static_assert(std::is_same_v<typename ModelType::TopologyTypesTuple, std::tuple<Node, Source>>);
117+
static_assert(std::is_same_v<typename ModelType::TopologyConnectionTypesTuple, std::tuple<Source>>);
118+
static_assert(ModelType::index_of_component<Node> == 0);
119+
static_assert(ModelType::index_of_component<AComponent> == 1);
120+
static_assert(ModelType::index_of_component<Source> == 2);
51121
static_assert(ModelType::n_types == 3);
122+
123+
CHECK(ModelType::run_functor_with_all_component_types_return_array([]<typename CompType>() {
124+
return CompType::name;
125+
}) == std::array<const char*, 3>{"node", "a_component", "source"});
126+
127+
std::vector<const char*> calls;
128+
ModelType::run_functor_with_all_component_types_return_void(
129+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
130+
CHECK(calls == std::vector<const char*>{"node", "a_component", "source"});
131+
132+
calls.clear();
133+
run_functor_with_tuple_return_void<typename ModelType::TopologyTypesTuple>(
134+
[&calls]<typename CompType>() { calls.push_back(CompType::name); });
135+
CHECK(calls == std::vector<const char*>{"node", "source"});
52136
}
137+
53138
SUBCASE("Bad case: Line Source") {
54139
// TODO rewrite for checking fail instead of pass
55140
using ModelType = MainModelType<ExtraRetrievableTypes<Base, Branch, Appliance>, ComponentList<Line, Source>>;

0 commit comments

Comments
 (0)