Debian Codegraph @

tests/src/utility/test_PartitionElements.cpp

raw
1 #include <pbcopper/utility/PartitionElements.h>
2
3 #include <tuple>
4 #include <vector>
5
6 #include <gtest/gtest.h>
7
8 using namespace PacBio;
9
10 // ------------------------
11 // PartitionElementCounts
12 // ------------------------
13
14 TEST(Utility_PartitionElementCounts, throws_on_negative_max_bucket_count)
15 {
16 const int numElements = 10;
17 const int maxBuckets = -1;
18 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
19 Utility::PartitionBehavior::DISCARD_EMPTY),
20 std::runtime_error);
21 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
22 Utility::PartitionBehavior::KEEP_EMPTY),
23 std::runtime_error);
24 }
25
26 TEST(Utility_PartitionElementCounts, throws_on_zero_max_bucket_count)
27 {
28 const int numElements = 10;
29 const int maxBuckets = 0;
30 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
31 Utility::PartitionBehavior::DISCARD_EMPTY),
32 std::runtime_error);
33 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
34 Utility::PartitionBehavior::KEEP_EMPTY),
35 std::runtime_error);
36 }
37
38 TEST(Utility_PartitionElementCounts, throws_on_negative_num_elements)
39 {
40 const int numElements = -1;
41 const int maxBuckets = 5;
42 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
43 Utility::PartitionBehavior::DISCARD_EMPTY),
44 std::runtime_error);
45 EXPECT_THROW(Utility::PartitionElementCounts(numElements, maxBuckets,
46 Utility::PartitionBehavior::KEEP_EMPTY),
47 std::runtime_error);
48 }
49
50 TEST(Utility_PartitionElementCounts, shrink_to_fit_zero_num_elements)
51 {
52 const int numElements = 0;
53 const int maxBuckets = 5;
54 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
55 Utility::PartitionBehavior::DISCARD_EMPTY);
56 const std::vector<int> expected;
57 EXPECT_EQ(expected, result);
58 }
59
60 TEST(Utility_PartitionElementCounts, shrink_to_fit_smaller_num_elements)
61 {
62 const int numElements = 3;
63 const int maxBuckets = 5;
64 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
65 Utility::PartitionBehavior::DISCARD_EMPTY);
66 const std::vector<int> expected{1, 1, 1};
67 EXPECT_EQ(expected, result);
68 }
69
70 TEST(Utility_PartitionElementCounts, shrink_to_fit_larger_num_elements)
71 {
72 const int numElements = 20;
73 const int maxBuckets = 3;
74 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
75 Utility::PartitionBehavior::DISCARD_EMPTY);
76 const std::vector<int> expected{7, 7, 6};
77 EXPECT_EQ(expected, result);
78 }
79
80 TEST(Utility_PartitionElementCounts, shrink_to_fit_num_elements_multiple_of_max_buckets)
81 {
82 const int numElements = 20;
83 const int maxBuckets = 5;
84 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
85 Utility::PartitionBehavior::DISCARD_EMPTY);
86 const std::vector<int> expected{4, 4, 4, 4, 4};
87 }
88
89 TEST(Utility_PartitionElementCounts, keep_empty_zero_num_elements)
90 {
91 const int numElements = 0;
92 const int maxBuckets = 5;
93 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
94 Utility::PartitionBehavior::KEEP_EMPTY);
95 const std::vector<int> expected{0, 0, 0, 0, 0};
96 EXPECT_EQ(expected, result);
97 }
98
99 TEST(Utility_PartitionElementCounts, keep_empty_smaller_num_elements)
100 {
101 const int numElements = 3;
102 const int maxBuckets = 5;
103 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
104 Utility::PartitionBehavior::KEEP_EMPTY);
105 const std::vector<int> expected{1, 1, 1, 0, 0};
106 EXPECT_EQ(expected, result);
107 }
108
109 TEST(Utility_PartitionElementCounts, keep_empty_larger_num_elements)
110 {
111 const int numElements = 20;
112 const int maxBuckets = 3;
113 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
114 Utility::PartitionBehavior::KEEP_EMPTY);
115 const std::vector<int> expected{7, 7, 6};
116 EXPECT_EQ(expected, result);
117 }
118
119 TEST(Utility_PartitionElementCounts, keep_empty_num_elements_multiple_of_max_buckets)
120 {
121 const int numElements = 20;
122 const int maxBuckets = 5;
123 const auto result = Utility::PartitionElementCounts(numElements, maxBuckets,
124 Utility::PartitionBehavior::KEEP_EMPTY);
125 const std::vector<int> expected{4, 4, 4, 4, 4};
126 EXPECT_EQ(expected, result);
127 }
128
129 // ------------------------
130 // PartitionElements
131 // ------------------------
132
133 TEST(Utility_PartitionElements, throws_on_negative_max_bucket_count)
134 {
135 const std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
136 const int maxBuckets = -1;
137 EXPECT_THROW(
138 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY),
139 std::runtime_error);
140 EXPECT_THROW(
141 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY),
142 std::runtime_error);
143 }
144
145 TEST(Utility_PartitionElements, throws_on_zero_max_bucket_count)
146 {
147 const std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
148 const int maxBuckets = 0;
149 EXPECT_THROW(
150 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY),
151 std::runtime_error);
152 EXPECT_THROW(
153 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY),
154 std::runtime_error);
155 }
156
157 TEST(Utility_PartitionElements, shrink_to_fit_zero_num_elements)
158 {
159 const std::vector<int> input;
160 const int maxBuckets = 5;
161 const auto result =
162 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY);
163 const std::vector<std::vector<int>> expected;
164 EXPECT_EQ(expected, result);
165 }
166
167 TEST(Utility_PartitionElements, shrink_to_fit_empty_smaller_input)
168 {
169 const std::vector<int> input{1, 2, 3};
170 const int maxBuckets = 5;
171 const auto result =
172 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY);
173 const std::vector<std::vector<int>> expected{{1}, {2}, {3}};
174 EXPECT_EQ(expected, result);
175 }
176
177 TEST(Utility_PartitionElements, shrink_to_fit_larger_input)
178 {
179 const std::vector<int> input{1, 2, 3, 4, 5, 6};
180 const int maxBuckets = 4;
181 const auto result =
182 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY);
183 const std::vector<std::vector<int>> expected{{1, 2}, {3, 4}, {5}, {6}};
184 EXPECT_EQ(expected, result);
185 }
186
187 TEST(Utility_PartitionElements, shrink_to_fit_num_elements_multiple_of_max_buckets)
188 {
189 const std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
190 const int maxBuckets = 4;
191 const auto result =
192 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::DISCARD_EMPTY);
193 const std::vector<std::vector<int>> expected{{1, 2}, {3, 4}, {5, 6}, {7, 8}};
194 EXPECT_EQ(expected, result);
195 }
196
197 TEST(Utility_PartitionElements, keep_empty_zero_num_elements)
198 {
199 const std::vector<int> input;
200 const int maxBuckets = 5;
201 const auto result =
202 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY);
203 const std::vector<std::vector<int>> expected(5, std::vector<int>{});
204 EXPECT_EQ(expected, result);
205 }
206
207 TEST(Utility_PartitionElements, keep_empty_smaller_input)
208 {
209 const std::vector<int> input{1, 2, 3};
210 const int maxBuckets = 5;
211 const auto result =
212 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY);
213 const std::vector<std::vector<int>> expected{{1}, {2}, {3}, {}, {}};
214 EXPECT_EQ(expected, result);
215 }
216
217 TEST(Utility_PartitionElements, keep_empty_larger_input)
218 {
219 const std::vector<int> input{1, 2, 3, 4, 5, 6};
220 const int maxBuckets = 4;
221 const auto result =
222 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY);
223 const std::vector<std::vector<int>> expected{{1, 2}, {3, 4}, {5}, {6}};
224 EXPECT_EQ(expected, result);
225 }
226
227 TEST(Utility_PartitionElements, keep_empty_num_elements_multiple_of_max_buckets)
228 {
229 const std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
230 const int maxBuckets = 4;
231 const auto result =
232 Utility::PartitionElements(input, maxBuckets, Utility::PartitionBehavior::KEEP_EMPTY);
233 const std::vector<std::vector<int>> expected{{1, 2}, {3, 4}, {5, 6}, {7, 8}};
234 EXPECT_EQ(expected, result);
235 }
236
237 TEST(Utility_PartitionElements, input_string_vector)
238 {
239 const std::vector<std::string> input{"one", "two", "three", "four", "five", "six"};
240 const int maxBuckets = 4;
241 const auto result = Utility::PartitionElements(input, maxBuckets);
242 const std::vector<std::vector<std::string>> expected{
243 {"one", "two"}, {"three", "four"}, {"five"}, {"six"}};
244 EXPECT_EQ(expected, result);
245 }
246
247 TEST(Utility_PartitionElements, input_user_type)
248 {
249 struct UserType
250 {
251 int i = 0;
252 float f = 0.0f;
253 bool operator==(const UserType& other) const noexcept
254 {
255 return std::tie(i, f) == std::tie(other.i, other.f);
256 }
257 };
258
259 const std::vector<UserType> input{{1, 1.0f}, {2, 2.0f}, {3, 3.0f},
260 {4, 4.0f}, {5, 5.0f}, {6, 6.0f}};
261 const int maxBuckets = 4;
262 const auto result = Utility::PartitionElements(input, maxBuckets);
263 const std::vector<std::vector<UserType>> expected{
264 {{{1, 1.0f}, {2, 2.0f}}}, {{{3, 3.0f}, {4, 4.0f}}}, {{5, 5.0f}}, {{6, 6.0f}}};
265
266 ASSERT_EQ(result.size(), expected.size());
267 for (size_t i = 0; i < result.size(); ++i) {
268 const auto& o = result[i];
269 const auto& e = expected[i];
270 EXPECT_EQ(o, e);
271 }
272 }
273
274 TEST(Utility_PartitionElements, rvalue_partition)
275 {
276 std::vector<std::string> input{"one", "two", "three", "four", "five", "six"};
277 const int maxBuckets = 4;
278 const auto result = Utility::PartitionElements(std::move(input), maxBuckets);
279 const std::vector<std::vector<std::string>> expected{
280 {"one", "two"}, {"three", "four"}, {"five"}, {"six"}};
281 EXPECT_EQ(expected, result);
282 }