1 /* Copyright (c) 2017. 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 #define BOOST_TEST_MODULE Trace Manager tests
8 #define BOOST_TEST_DYN_LINK
9 #define BOOST_TEST_NO_MAIN
10 #include <boost/test/unit_test.hpp>
11 namespace utf = boost::unit_test;
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/trace_mgr.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
21 class MockedResource : public simgrid::surf::Resource {
23 explicit MockedResource() : simgrid::surf::Resource(nullptr, "fake", nullptr) {}
24 void apply_event(tmgr_trace_iterator_t event, double value)
26 XBT_VERB("t=%.1f: Change value to %lg (idx: %d)", thedate, value, event->idx);
28 bool isUsed() { return true; }
35 explicit Evt(double d, double v) : date(d), value(v) {}
36 bool operator==(Evt e2) { return (date == e2.date) && (value == e2.value); }
37 bool operator!=(Evt e2) { return (date != e2.date) || (value != e2.value); }
39 static std::ostream& operator<<(std::ostream& out, const Evt& e)
41 out << e.date << " " << e.value;
45 static void trace2vector(const char* str, std::vector<Evt>* whereto)
47 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
48 XBT_VERB("data>>\n%s<<data\n", str);
49 for (auto evt : trace->event_list)
50 XBT_VERB("event: d:%lg v:%lg", evt.delta, evt.value);
52 MockedResource daResource;
53 simgrid::trace_mgr::future_evt_set fes;
54 tmgr_trace_iterator_t insertedIt = fes.add_trace(trace, 0.0, &daResource);
56 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
57 thedate = fes.next_date();
59 simgrid::surf::Resource* res;
60 tmgr_trace_iterator_t it = fes.pop_leq(thedate, &value, &res);
64 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
66 res->apply_event(it, value);
67 whereto->push_back(Evt(thedate, value));
69 XBT_DEBUG("%.1f: ignore an event (idx: %d)\n", thedate, it->idx);
74 /* Fails in a way that is difficult to test: xbt_assert should become throw
75 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
77 trace2vector("", &got);
78 std::vector<Evt> want;
79 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
81 BOOST_AUTO_TEST_CASE(one_evt_noloop)
84 trace2vector("9.0 3.0\n", &got);
86 std::vector<Evt> want;
87 want.push_back(Evt(9, 3));
88 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
90 BOOST_AUTO_TEST_CASE(two_evt_noloop)
93 trace2vector("3.0 1.0\n"
97 std::vector<Evt> want;
98 want.push_back(Evt(3, 1));
99 want.push_back(Evt(9, 3));
101 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
103 BOOST_AUTO_TEST_CASE(three_evt_noloop)
105 std::vector<Evt> got;
106 trace2vector("3.0 1.0\n"
111 std::vector<Evt> want;
112 want.push_back(Evt(3, 1));
113 want.push_back(Evt(5, 2));
114 want.push_back(Evt(9, 3));
116 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
119 BOOST_AUTO_TEST_CASE(two_evt_loop)
121 std::vector<Evt> got;
122 trace2vector("1.0 1.0\n"
127 std::vector<Evt> want;
128 want.push_back(Evt(1, 1));
129 want.push_back(Evt(3, 3));
130 want.push_back(Evt(6, 1));
131 want.push_back(Evt(8, 3));
132 want.push_back(Evt(11, 1));
133 want.push_back(Evt(13, 3));
134 want.push_back(Evt(16, 1));
135 want.push_back(Evt(18, 3));
137 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
139 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
141 std::vector<Evt> got;
142 trace2vector("0.0 1\n"
147 std::vector<Evt> want;
148 want.push_back(Evt(0, 1));
149 want.push_back(Evt(5, 2));
150 want.push_back(Evt(10, 1));
151 want.push_back(Evt(15, 2));
152 want.push_back(Evt(20, 1));
154 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
157 static bool init_function()
159 // do your own initialization here (and return true on success)
160 // But, you CAN'T use testing tools here
164 int main(int argc, char** argv)
166 xbt_log_init(&argc, argv);
167 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);