1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/3rd-party/catch.hpp"
7 #include "src/mc/api/ClockVector.hpp"
9 using namespace simgrid::mc;
11 TEST_CASE("simgrid::mc::ClockVector: Constructing Vectors")
14 REQUIRE(cv.size() == 0);
16 // Verify `cv` doesn't map any values
17 REQUIRE_FALSE(cv.get(0).has_value());
18 REQUIRE_FALSE(cv.get(1).has_value());
19 REQUIRE_FALSE(cv.get(2).has_value());
20 REQUIRE_FALSE(cv.get(3).has_value());
23 TEST_CASE("simgrid::mc::ClockVector: Testing operator[]")
27 REQUIRE(cv.size() == 1);
29 REQUIRE(cv.get(0).has_value());
30 REQUIRE(cv.get(0).value() == 1);
32 // Verify `cv` doesn't map other values
33 REQUIRE_FALSE(cv.get(2).has_value());
34 REQUIRE_FALSE(cv.get(3).has_value());
37 REQUIRE(cv.size() == 2);
39 // Old values are still mapped
40 REQUIRE(cv.get(0).has_value());
41 REQUIRE(cv.get(0).value() == 1);
43 REQUIRE(cv.get(10).has_value());
44 REQUIRE(cv.get(10).value() == 31);
45 REQUIRE(cv[10] == 31);
47 // Verify `cv` doesn't map other values
48 REQUIRE_FALSE(cv.get(2).has_value());
49 REQUIRE_FALSE(cv.get(3).has_value());
52 TEST_CASE("simgrid::mc::ClockVector: Testing Maximal Clock Vectors")
54 SECTION("Max with zero clock vector yields self")
62 ClockVector maxCV = ClockVector::max(cv1, cv2);
64 REQUIRE(maxCV.size() == 3);
65 REQUIRE(maxCV.get(1).has_value());
66 REQUIRE(maxCV.get(1).value() == 2);
67 REQUIRE(maxCV[1] == 2);
69 REQUIRE(maxCV.get(2).has_value());
70 REQUIRE(maxCV.get(2).value() == 10);
71 REQUIRE(maxCV[2] == 10);
73 REQUIRE(maxCV.get(3).has_value());
74 REQUIRE(maxCV.get(3).value() == 5);
75 REQUIRE(maxCV[3] == 5);
78 SECTION("Max with self clock vector yields self")
85 ClockVector maxCV = ClockVector::max(cv1, cv1);
87 REQUIRE(maxCV.size() == 3);
88 REQUIRE(maxCV.get(1).has_value());
89 REQUIRE(maxCV.get(1).value() == 2);
90 REQUIRE(maxCV[1] == 2);
92 REQUIRE(maxCV.get(2).has_value());
93 REQUIRE(maxCV.get(2).value() == 10);
94 REQUIRE(maxCV[2] == 10);
96 REQUIRE(maxCV.get(3).has_value());
97 REQUIRE(maxCV.get(3).value() == 5);
98 REQUIRE(maxCV[3] == 5);
101 SECTION("Testing with partial overlaps")
115 ClockVector maxCV = ClockVector::max(cv1, cv2);
117 REQUIRE(maxCV.size() == 4);
118 REQUIRE(maxCV.get(1).has_value());
119 REQUIRE(maxCV.get(1).value() == 5);
120 REQUIRE(maxCV[1] == 5);
122 REQUIRE(maxCV.get(2).has_value());
123 REQUIRE(maxCV.get(2).value() == 10);
124 REQUIRE(maxCV[2] == 10);
126 REQUIRE(maxCV.get(3).has_value());
127 REQUIRE(maxCV.get(3).value() == 5);
128 REQUIRE(maxCV[3] == 5);
130 REQUIRE(maxCV.get(7).has_value());
131 REQUIRE(maxCV.get(7).value() == 10);
132 REQUIRE(maxCV[7] == 10);
138 {1, 2}, {2, 10}, {3, 5}, {4, 40}, {11, 3}, {12, 8},
141 {1, 18}, {2, 4}, {4, 41}, {10, 3}, {12, 8},
143 ClockVector maxCV = ClockVector::max(cv1, cv2);
145 REQUIRE(maxCV.size() == 7);
146 REQUIRE(maxCV.get(1).has_value());
147 REQUIRE(maxCV.get(1).value() == 18);
148 REQUIRE(maxCV[1] == 18);
150 REQUIRE(maxCV.get(2).has_value());
151 REQUIRE(maxCV.get(2).value() == 10);
152 REQUIRE(maxCV[2] == 10);
154 REQUIRE(maxCV.get(3).has_value());
155 REQUIRE(maxCV.get(3).value() == 5);
156 REQUIRE(maxCV[3] == 5);
158 REQUIRE(maxCV.get(4).has_value());
159 REQUIRE(maxCV.get(4).value() == 41);
160 REQUIRE(maxCV[4] == 41);
162 REQUIRE(maxCV.get(10).has_value());
163 REQUIRE(maxCV.get(10).value() == 3);
164 REQUIRE(maxCV[10] == 3);
166 REQUIRE(maxCV.get(11).has_value());
167 REQUIRE(maxCV.get(11).value() == 3);
168 REQUIRE(maxCV[11] == 3);
170 REQUIRE(maxCV.get(12).has_value());
171 REQUIRE(maxCV.get(12).value() == 8);
172 REQUIRE(maxCV[12] == 8);
177 ClockVector cv1{{1, 2}, {4, 41}, {12, 0}, {100, 5}};
178 ClockVector cv2{{2, 4}, {4, 10}, {10, 3}, {12, 8}, {19, 0}, {21, 6}, {22, 0}};
179 ClockVector cv3{{21, 60}, {22, 6}, {100, 3}};
180 ClockVector maxCV = ClockVector::max(cv1, cv2);
181 maxCV = ClockVector::max(maxCV, cv3);
183 REQUIRE(maxCV.size() == 9);
184 REQUIRE(maxCV.get(1).has_value());
185 REQUIRE(maxCV.get(1).value() == 2);
186 REQUIRE(maxCV[1] == 2);
188 REQUIRE(maxCV.get(2).has_value());
189 REQUIRE(maxCV.get(2).value() == 4);
190 REQUIRE(maxCV[2] == 4);
192 REQUIRE(maxCV.get(4).has_value());
193 REQUIRE(maxCV.get(4).value() == 41);
194 REQUIRE(maxCV[4] == 41);
196 REQUIRE(maxCV.get(10).has_value());
197 REQUIRE(maxCV.get(10).value() == 3);
198 REQUIRE(maxCV[10] == 3);
200 REQUIRE(maxCV.get(12).has_value());
201 REQUIRE(maxCV.get(12).value() == 8);
202 REQUIRE(maxCV[12] == 8);
204 REQUIRE(maxCV.get(19).has_value());
205 REQUIRE(maxCV.get(19).value() == 0);
206 REQUIRE(maxCV[19] == 0);
208 REQUIRE(maxCV.get(21).has_value());
209 REQUIRE(maxCV.get(21).value() == 60);
210 REQUIRE(maxCV[21] == 60);
212 REQUIRE(maxCV.get(22).has_value());
213 REQUIRE(maxCV.get(22).value() == 6);
214 REQUIRE(maxCV[22] == 6);
216 REQUIRE(maxCV.get(100).has_value());
217 REQUIRE(maxCV.get(100).value() == 5);
218 REQUIRE(maxCV[100] == 5);
222 SECTION("Testing without overlaps")
226 ClockVector cv1{{1, 2}};
233 ClockVector maxCV = ClockVector::max(cv1, cv2);
235 REQUIRE(maxCV.size() == 5);
236 REQUIRE(maxCV.get(1).has_value());
237 REQUIRE(maxCV.get(1).value() == 2);
238 REQUIRE(maxCV[1] == 2);
240 REQUIRE(maxCV.get(2).has_value());
241 REQUIRE(maxCV.get(2).value() == 4);
242 REQUIRE(maxCV[2] == 4);
244 REQUIRE(maxCV.get(4).has_value());
245 REQUIRE(maxCV.get(4).value() == 41);
246 REQUIRE(maxCV[4] == 41);
248 REQUIRE(maxCV.get(10).has_value());
249 REQUIRE(maxCV.get(10).value() == 3);
250 REQUIRE(maxCV[10] == 3);
252 REQUIRE(maxCV.get(12).has_value());
253 REQUIRE(maxCV.get(12).value() == 8);
254 REQUIRE(maxCV[12] == 8);
259 ClockVector cv1{{1, 2}, {4, 41}};
265 ClockVector maxCV = ClockVector::max(cv1, cv2);
267 REQUIRE(maxCV.size() == 5);
268 REQUIRE(maxCV.get(1).has_value());
269 REQUIRE(maxCV.get(1).value() == 2);
270 REQUIRE(maxCV[1] == 2);
272 REQUIRE(maxCV.get(2).has_value());
273 REQUIRE(maxCV.get(2).value() == 4);
274 REQUIRE(maxCV[2] == 4);
276 REQUIRE(maxCV.get(4).has_value());
277 REQUIRE(maxCV.get(4).value() == 41);
278 REQUIRE(maxCV[4] == 41);
280 REQUIRE(maxCV.get(10).has_value());
281 REQUIRE(maxCV.get(10).value() == 3);
282 REQUIRE(maxCV[10] == 3);
284 REQUIRE(maxCV.get(12).has_value());
285 REQUIRE(maxCV.get(12).value() == 8);
286 REQUIRE(maxCV[12] == 8);
291 ClockVector cv1{{1, 2}, {4, 41}};
292 ClockVector cv2{{2, 4}, {10, 3}, {12, 8}, {19, 0}, {21, 6}};
293 ClockVector cv3{{22, 6}, {100, 3}};
294 ClockVector maxCV = ClockVector::max(cv1, cv2);
295 maxCV = ClockVector::max(maxCV, cv3);
297 REQUIRE(maxCV.size() == 9);
298 REQUIRE(maxCV.get(1).has_value());
299 REQUIRE(maxCV.get(1).value() == 2);
300 REQUIRE(maxCV[1] == 2);
302 REQUIRE(maxCV.get(2).has_value());
303 REQUIRE(maxCV.get(2).value() == 4);
304 REQUIRE(maxCV[2] == 4);
306 REQUIRE(maxCV.get(4).has_value());
307 REQUIRE(maxCV.get(4).value() == 41);
308 REQUIRE(maxCV[4] == 41);
310 REQUIRE(maxCV.get(10).has_value());
311 REQUIRE(maxCV.get(10).value() == 3);
312 REQUIRE(maxCV[10] == 3);
314 REQUIRE(maxCV.get(12).has_value());
315 REQUIRE(maxCV.get(12).value() == 8);
316 REQUIRE(maxCV[12] == 8);
318 REQUIRE(maxCV.get(19).has_value());
319 REQUIRE(maxCV.get(19).value() == 0);
320 REQUIRE(maxCV[19] == 0);
322 REQUIRE(maxCV.get(21).has_value());
323 REQUIRE(maxCV.get(21).value() == 6);
324 REQUIRE(maxCV[21] == 6);
326 REQUIRE(maxCV.get(22).has_value());
327 REQUIRE(maxCV.get(22).value() == 6);
328 REQUIRE(maxCV[22] == 6);
330 REQUIRE(maxCV.get(100).has_value());
331 REQUIRE(maxCV.get(100).value() == 3);
332 REQUIRE(maxCV[100] == 3);