Skip to content

Commit 4fd94ca

Browse files
committed
added tests for type pack loop
1 parent 625a9dd commit 4fd94ca

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/pack_loops.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <catch.hpp>
22
#include <ut/pack_loops/pack_loops.hpp>
33

4+
#include <type_traits>
5+
46
template<typename... T>
57
void loopOverArgs(T... args) {
68
int count = 0;
@@ -65,6 +67,52 @@ void loopOverParameterPackContinue() {
6567
REQUIRE(count == 4);
6668
}
6769

70+
template<typename... Ts>
71+
void loopOverTypes() {
72+
int count = 0;
73+
int float_count = 0;
74+
UT_PACK_FOR_T(I, Ts, {
75+
count++;
76+
if constexpr (std::is_same_v<I, float>) {
77+
float_count++;
78+
}
79+
});
80+
REQUIRE(count == 4);
81+
REQUIRE(float_count == 2);
82+
}
83+
84+
template<typename... Ts>
85+
void loopOverTypesBreak() {
86+
int count = 0;
87+
int float_count = 0;
88+
UT_PACK_FOR_T(I, Ts, {
89+
count++;
90+
if constexpr (std::is_same_v<I, float>) {
91+
float_count++;
92+
} else if constexpr (std::is_same_v<I, double>) {
93+
UT_PACK_BREAK;
94+
}
95+
});
96+
REQUIRE(count == 4);
97+
REQUIRE(float_count == 2);
98+
}
99+
100+
template<typename... Ts>
101+
void loopOverTypesContinue() {
102+
int count = 0;
103+
int float_count = 0;
104+
UT_PACK_FOR_T(I, Ts, {
105+
if constexpr (std::is_same_v<I, float>) {
106+
float_count++;
107+
} else if constexpr (std::is_same_v<I, double>) {
108+
UT_PACK_CONTINUE;
109+
}
110+
count++;
111+
});
112+
REQUIRE(count == 4);
113+
REQUIRE(float_count == 2);
114+
}
115+
68116
template<typename... T>
69117
int indexArgs(T... args) {
70118
return UT_PACK_IDX(args, 2);
@@ -90,6 +138,12 @@ TEST_CASE("pack for loop", "[pack_loops]") {
90138
loopOverParameterPackContinue<0, -100, 1, -100, 2, -100, 3>();
91139
}
92140

141+
TEST_CASE("type pack for loop", "[pack_loops]") {
142+
loopOverTypes<int, float, double, float>();
143+
loopOverTypesBreak<int, float, float, double, float, int>();
144+
loopOverTypesContinue<int, float, double, float, int>();
145+
}
146+
93147
TEST_CASE("pack index", "[pack_loops]") {
94148
REQUIRE(indexArgs(0, 0, 3, 0) == 3);
95149
REQUIRE(indexAndModifyArgs(0, 0, 3, 0) == 4);

0 commit comments

Comments
 (0)