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 bool init_unit_test(); // boost forget to give this prototype on NetBSD, which does not fit our paranoid flags
9 #define BOOST_TEST_DYN_LINK
10 #define BOOST_TEST_NO_MAIN
11 #include <boost/test/unit_test.hpp>
12 namespace utf = boost::unit_test;
14 #include "src/surf/surf_interface.hpp"
15 #include "src/surf/trace_mgr.hpp"
21 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
24 class MockedResource : public simgrid::surf::Resource {
26 explicit MockedResource() : simgrid::surf::Resource(nullptr, "fake", nullptr) {}
27 void apply_event(tmgr_trace_event_t event, double value)
29 XBT_VERB("t=%.1f: Change value to %lg (idx: %d)", thedate, value, event->idx);
31 bool isUsed() { return true; }
34 static inline bool doubleEq(double d1, double d2)
36 return fabs(d1 - d2) < 0.0001;
42 explicit Evt(double d, double v) : date(d), value(v) {}
43 bool operator==(Evt e2) { return (doubleEq(date, e2.date)) && (doubleEq(value, e2.value)); }
44 bool operator!=(Evt e2) { return !(*this == e2); }
46 static std::ostream& operator<<(std::ostream& out, const Evt& e)
48 out << e.date << " " << e.value;
52 static void trace2vector(const char* str, std::vector<Evt>* whereto)
54 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
55 XBT_VERB("data>>\n%s<<data\n", str);
56 for (auto evt : trace->event_list)
57 XBT_VERB("event: d:%lg v:%lg", evt.delta, evt.value);
59 MockedResource daResource;
60 simgrid::trace_mgr::future_evt_set fes;
61 tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
63 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
64 thedate = fes.next_date();
66 simgrid::surf::Resource* res;
67 tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
71 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
73 res->apply_event(it, value);
74 whereto->push_back(Evt(thedate, value));
76 XBT_DEBUG("%.1f: ignore an event (idx: %d)\n", thedate, it->idx);
81 /* Fails in a way that is difficult to test: xbt_assert should become throw
82 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
84 trace2vector("", &got);
85 std::vector<Evt> want;
86 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
88 BOOST_AUTO_TEST_CASE(one_evt_noloop)
91 trace2vector("9.0 3.0\n", &got);
93 std::vector<Evt> want;
94 want.push_back(Evt(9, 3));
95 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
97 BOOST_AUTO_TEST_CASE(two_evt_noloop)
100 trace2vector("3.0 1.0\n"
104 std::vector<Evt> want;
105 want.push_back(Evt(3, 1));
106 want.push_back(Evt(9, 3));
108 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
110 BOOST_AUTO_TEST_CASE(three_evt_noloop)
112 std::vector<Evt> got;
113 trace2vector("3.0 1.0\n"
118 std::vector<Evt> want;
119 want.push_back(Evt(3, 1));
120 want.push_back(Evt(5, 2));
121 want.push_back(Evt(9, 3));
123 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
126 BOOST_AUTO_TEST_CASE(two_evt_loop)
128 std::vector<Evt> got;
129 trace2vector("1.0 1.0\n"
134 std::vector<Evt> want;
135 want.push_back(Evt(1, 1));
136 want.push_back(Evt(3, 3));
137 want.push_back(Evt(6, 1));
138 want.push_back(Evt(8, 3));
139 want.push_back(Evt(11, 1));
140 want.push_back(Evt(13, 3));
141 want.push_back(Evt(16, 1));
142 want.push_back(Evt(18, 3));
144 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
146 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
148 std::vector<Evt> got;
149 trace2vector("0.0 1\n"
154 std::vector<Evt> want;
155 want.push_back(Evt(0, 1));
156 want.push_back(Evt(5, 2));
157 want.push_back(Evt(10, 1));
158 want.push_back(Evt(15, 2));
159 want.push_back(Evt(20, 1));
161 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
164 static bool init_function()
166 // do your own initialization here (and return true on success)
167 // But, you CAN'T use testing tools here
171 int main(int argc, char** argv)
173 xbt_log_init(&argc, argv);
174 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);