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")
13 SECTION("Without values")
16 REQUIRE(cv.size() == 0);
18 // Verify `cv` doesn't map any values
19 REQUIRE_FALSE(cv.get(0).has_value());
20 REQUIRE_FALSE(cv.get(1).has_value());
21 REQUIRE_FALSE(cv.get(2).has_value());
22 REQUIRE_FALSE(cv.get(3).has_value());
25 SECTION("With initial values")
28 {1, 5}, {3, 1}, {7, 10}, {6, 5}, {8, 1}, {10, 10},
30 REQUIRE(cv.size() == 6);
32 // Verify `cv` maps each value
33 REQUIRE(cv.get(1).has_value());
34 REQUIRE(cv.get(1).value() == 5);
36 REQUIRE(cv.get(3).has_value());
37 REQUIRE(cv.get(3).value() == 1);
39 REQUIRE(cv.get(7).has_value());
40 REQUIRE(cv.get(7).value() == 10);
42 REQUIRE(cv.get(6).has_value());
43 REQUIRE(cv.get(6).value() == 5);
45 REQUIRE(cv.get(8).has_value());
46 REQUIRE(cv.get(8).value() == 1);
48 REQUIRE(cv.get(10).has_value());
49 REQUIRE(cv.get(10).value() == 10);
50 REQUIRE(cv[10] == 10);
54 TEST_CASE("simgrid::mc::ClockVector: Testing operator[]")
58 REQUIRE(cv.size() == 1);
60 REQUIRE(cv.get(0).has_value());
61 REQUIRE(cv.get(0).value() == 1);
63 // Verify `cv` doesn't map other values
64 REQUIRE_FALSE(cv.get(2).has_value());
65 REQUIRE_FALSE(cv.get(3).has_value());
68 REQUIRE(cv.size() == 2);
70 // Old values are still mapped
71 REQUIRE(cv.get(0).has_value());
72 REQUIRE(cv.get(0).value() == 1);
74 REQUIRE(cv.get(10).has_value());
75 REQUIRE(cv.get(10).value() == 31);
76 REQUIRE(cv[10] == 31);
78 // Verify `cv` doesn't map other values
79 REQUIRE_FALSE(cv.get(2).has_value());
80 REQUIRE_FALSE(cv.get(3).has_value());
83 TEST_CASE("simgrid::mc::ClockVector: Testing Maximal Clock Vectors")
85 SECTION("Max with zero clock vector yields self")
93 ClockVector maxCV = ClockVector::max(cv1, cv2);
95 REQUIRE(maxCV.size() == 3);
96 REQUIRE(maxCV.get(1).has_value());
97 REQUIRE(maxCV.get(1).value() == 2);
98 REQUIRE(maxCV[1] == 2);
100 REQUIRE(maxCV.get(2).has_value());
101 REQUIRE(maxCV.get(2).value() == 10);
102 REQUIRE(maxCV[2] == 10);
104 REQUIRE(maxCV.get(3).has_value());
105 REQUIRE(maxCV.get(3).value() == 5);
106 REQUIRE(maxCV[3] == 5);
109 SECTION("Max with self clock vector yields self")
116 ClockVector maxCV = ClockVector::max(cv1, cv1);
118 REQUIRE(maxCV.size() == 3);
119 REQUIRE(maxCV.get(1).has_value());
120 REQUIRE(maxCV.get(1).value() == 2);
121 REQUIRE(maxCV[1] == 2);
123 REQUIRE(maxCV.get(2).has_value());
124 REQUIRE(maxCV.get(2).value() == 10);
125 REQUIRE(maxCV[2] == 10);
127 REQUIRE(maxCV.get(3).has_value());
128 REQUIRE(maxCV.get(3).value() == 5);
129 REQUIRE(maxCV[3] == 5);
132 SECTION("Testing with partial overlaps")
146 ClockVector maxCV = ClockVector::max(cv1, cv2);
148 REQUIRE(maxCV.size() == 4);
149 REQUIRE(maxCV.get(1).has_value());
150 REQUIRE(maxCV.get(1).value() == 5);
151 REQUIRE(maxCV[1] == 5);
153 REQUIRE(maxCV.get(2).has_value());
154 REQUIRE(maxCV.get(2).value() == 10);
155 REQUIRE(maxCV[2] == 10);
157 REQUIRE(maxCV.get(3).has_value());
158 REQUIRE(maxCV.get(3).value() == 5);
159 REQUIRE(maxCV[3] == 5);
161 REQUIRE(maxCV.get(7).has_value());
162 REQUIRE(maxCV.get(7).value() == 10);
163 REQUIRE(maxCV[7] == 10);
169 {1, 2}, {2, 10}, {3, 5}, {4, 40}, {11, 3}, {12, 8},
172 {1, 18}, {2, 4}, {4, 41}, {10, 3}, {12, 8},
174 ClockVector maxCV = ClockVector::max(cv1, cv2);
176 REQUIRE(maxCV.size() == 7);
177 REQUIRE(maxCV.get(1).has_value());
178 REQUIRE(maxCV.get(1).value() == 18);
179 REQUIRE(maxCV[1] == 18);
181 REQUIRE(maxCV.get(2).has_value());
182 REQUIRE(maxCV.get(2).value() == 10);
183 REQUIRE(maxCV[2] == 10);
185 REQUIRE(maxCV.get(3).has_value());
186 REQUIRE(maxCV.get(3).value() == 5);
187 REQUIRE(maxCV[3] == 5);
189 REQUIRE(maxCV.get(4).has_value());
190 REQUIRE(maxCV.get(4).value() == 41);
191 REQUIRE(maxCV[4] == 41);
193 REQUIRE(maxCV.get(10).has_value());
194 REQUIRE(maxCV.get(10).value() == 3);
195 REQUIRE(maxCV[10] == 3);
197 REQUIRE(maxCV.get(11).has_value());
198 REQUIRE(maxCV.get(11).value() == 3);
199 REQUIRE(maxCV[11] == 3);
201 REQUIRE(maxCV.get(12).has_value());
202 REQUIRE(maxCV.get(12).value() == 8);
203 REQUIRE(maxCV[12] == 8);
208 ClockVector cv1{{1, 2}, {4, 41}, {12, 0}, {100, 5}};
209 ClockVector cv2{{2, 4}, {4, 10}, {10, 3}, {12, 8}, {19, 0}, {21, 6}, {22, 0}};
210 ClockVector cv3{{21, 60}, {22, 6}, {100, 3}};
211 ClockVector maxCV = ClockVector::max(cv1, cv2);
212 maxCV = ClockVector::max(maxCV, cv3);
214 REQUIRE(maxCV.size() == 9);
215 REQUIRE(maxCV.get(1).has_value());
216 REQUIRE(maxCV.get(1).value() == 2);
217 REQUIRE(maxCV[1] == 2);
219 REQUIRE(maxCV.get(2).has_value());
220 REQUIRE(maxCV.get(2).value() == 4);
221 REQUIRE(maxCV[2] == 4);
223 REQUIRE(maxCV.get(4).has_value());
224 REQUIRE(maxCV.get(4).value() == 41);
225 REQUIRE(maxCV[4] == 41);
227 REQUIRE(maxCV.get(10).has_value());
228 REQUIRE(maxCV.get(10).value() == 3);
229 REQUIRE(maxCV[10] == 3);
231 REQUIRE(maxCV.get(12).has_value());
232 REQUIRE(maxCV.get(12).value() == 8);
233 REQUIRE(maxCV[12] == 8);
235 REQUIRE(maxCV.get(19).has_value());
236 REQUIRE(maxCV.get(19).value() == 0);
237 REQUIRE(maxCV[19] == 0);
239 REQUIRE(maxCV.get(21).has_value());
240 REQUIRE(maxCV.get(21).value() == 60);
241 REQUIRE(maxCV[21] == 60);
243 REQUIRE(maxCV.get(22).has_value());
244 REQUIRE(maxCV.get(22).value() == 6);
245 REQUIRE(maxCV[22] == 6);
247 REQUIRE(maxCV.get(100).has_value());
248 REQUIRE(maxCV.get(100).value() == 5);
249 REQUIRE(maxCV[100] == 5);
253 SECTION("Testing without overlaps")
257 ClockVector cv1{{1, 2}};
264 ClockVector maxCV = ClockVector::max(cv1, cv2);
266 REQUIRE(maxCV.size() == 5);
267 REQUIRE(maxCV.get(1).has_value());
268 REQUIRE(maxCV.get(1).value() == 2);
269 REQUIRE(maxCV[1] == 2);
271 REQUIRE(maxCV.get(2).has_value());
272 REQUIRE(maxCV.get(2).value() == 4);
273 REQUIRE(maxCV[2] == 4);
275 REQUIRE(maxCV.get(4).has_value());
276 REQUIRE(maxCV.get(4).value() == 41);
277 REQUIRE(maxCV[4] == 41);
279 REQUIRE(maxCV.get(10).has_value());
280 REQUIRE(maxCV.get(10).value() == 3);
281 REQUIRE(maxCV[10] == 3);
283 REQUIRE(maxCV.get(12).has_value());
284 REQUIRE(maxCV.get(12).value() == 8);
285 REQUIRE(maxCV[12] == 8);
290 ClockVector cv1{{1, 2}, {4, 41}};
296 ClockVector maxCV = ClockVector::max(cv1, cv2);
298 REQUIRE(maxCV.size() == 5);
299 REQUIRE(maxCV.get(1).has_value());
300 REQUIRE(maxCV.get(1).value() == 2);
301 REQUIRE(maxCV[1] == 2);
303 REQUIRE(maxCV.get(2).has_value());
304 REQUIRE(maxCV.get(2).value() == 4);
305 REQUIRE(maxCV[2] == 4);
307 REQUIRE(maxCV.get(4).has_value());
308 REQUIRE(maxCV.get(4).value() == 41);
309 REQUIRE(maxCV[4] == 41);
311 REQUIRE(maxCV.get(10).has_value());
312 REQUIRE(maxCV.get(10).value() == 3);
313 REQUIRE(maxCV[10] == 3);
315 REQUIRE(maxCV.get(12).has_value());
316 REQUIRE(maxCV.get(12).value() == 8);
317 REQUIRE(maxCV[12] == 8);
322 ClockVector cv1{{1, 2}, {4, 41}};
323 ClockVector cv2{{2, 4}, {10, 3}, {12, 8}, {19, 0}, {21, 6}};
324 ClockVector cv3{{22, 6}, {100, 3}};
325 ClockVector maxCV = ClockVector::max(cv1, cv2);
326 maxCV = ClockVector::max(maxCV, cv3);
328 REQUIRE(maxCV.size() == 9);
329 REQUIRE(maxCV.get(1).has_value());
330 REQUIRE(maxCV.get(1).value() == 2);
331 REQUIRE(maxCV[1] == 2);
333 REQUIRE(maxCV.get(2).has_value());
334 REQUIRE(maxCV.get(2).value() == 4);
335 REQUIRE(maxCV[2] == 4);
337 REQUIRE(maxCV.get(4).has_value());
338 REQUIRE(maxCV.get(4).value() == 41);
339 REQUIRE(maxCV[4] == 41);
341 REQUIRE(maxCV.get(10).has_value());
342 REQUIRE(maxCV.get(10).value() == 3);
343 REQUIRE(maxCV[10] == 3);
345 REQUIRE(maxCV.get(12).has_value());
346 REQUIRE(maxCV.get(12).value() == 8);
347 REQUIRE(maxCV[12] == 8);
349 REQUIRE(maxCV.get(19).has_value());
350 REQUIRE(maxCV.get(19).value() == 0);
351 REQUIRE(maxCV[19] == 0);
353 REQUIRE(maxCV.get(21).has_value());
354 REQUIRE(maxCV.get(21).value() == 6);
355 REQUIRE(maxCV[21] == 6);
357 REQUIRE(maxCV.get(22).has_value());
358 REQUIRE(maxCV.get(22).value() == 6);
359 REQUIRE(maxCV[22] == 6);
361 REQUIRE(maxCV.get(100).has_value());
362 REQUIRE(maxCV.get(100).value() == 3);
363 REQUIRE(maxCV[100] == 3);