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"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
22 class MockedResource : public simgrid::surf::Resource {
24 explicit MockedResource() : simgrid::surf::Resource(nullptr, "fake", nullptr) {}
25 void apply_event(tmgr_trace_iterator_t event, double value)
27 XBT_VERB("t=%.1f: Change value to %lg (idx: %d)", thedate, value, event->idx);
29 bool isUsed() { return true; }
32 static inline bool doubleEq(double d1, double d2)
34 return std::abs(d1 - d2) < 0.0001;
40 explicit Evt(double d, double v) : date(d), value(v) {}
41 bool operator==(Evt e2) { return (doubleEq(date, e2.date)) && (doubleEq(value, e2.value)); }
42 bool operator!=(Evt e2) { return !(*this == e2); }
44 static std::ostream& operator<<(std::ostream& out, const Evt& e)
46 out << e.date << " " << e.value;
50 static void trace2vector(const char* str, std::vector<Evt>* whereto)
52 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
53 XBT_VERB("data>>\n%s<<data\n", str);
54 for (auto evt : trace->event_list)
55 XBT_VERB("event: d:%lg v:%lg", evt.delta, evt.value);
57 MockedResource daResource;
58 simgrid::trace_mgr::future_evt_set fes;
59 tmgr_trace_iterator_t insertedIt = fes.add_trace(trace, 0.0, &daResource);
61 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
62 thedate = fes.next_date();
64 simgrid::surf::Resource* res;
65 tmgr_trace_iterator_t it = fes.pop_leq(thedate, &value, &res);
69 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
71 res->apply_event(it, value);
72 whereto->push_back(Evt(thedate, value));
74 XBT_DEBUG("%.1f: ignore an event (idx: %d)\n", thedate, it->idx);
79 /* Fails in a way that is difficult to test: xbt_assert should become throw
80 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
82 trace2vector("", &got);
83 std::vector<Evt> want;
84 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
86 BOOST_AUTO_TEST_CASE(one_evt_noloop)
89 trace2vector("9.0 3.0\n", &got);
91 std::vector<Evt> want;
92 want.push_back(Evt(9, 3));
93 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
95 BOOST_AUTO_TEST_CASE(two_evt_noloop)
98 trace2vector("3.0 1.0\n"
102 std::vector<Evt> want;
103 want.push_back(Evt(3, 1));
104 want.push_back(Evt(9, 3));
106 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
108 BOOST_AUTO_TEST_CASE(three_evt_noloop)
110 std::vector<Evt> got;
111 trace2vector("3.0 1.0\n"
116 std::vector<Evt> want;
117 want.push_back(Evt(3, 1));
118 want.push_back(Evt(5, 2));
119 want.push_back(Evt(9, 3));
121 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
124 BOOST_AUTO_TEST_CASE(two_evt_loop)
126 std::vector<Evt> got;
127 trace2vector("1.0 1.0\n"
132 std::vector<Evt> want;
133 want.push_back(Evt(1, 1));
134 want.push_back(Evt(3, 3));
135 want.push_back(Evt(6, 1));
136 want.push_back(Evt(8, 3));
137 want.push_back(Evt(11, 1));
138 want.push_back(Evt(13, 3));
139 want.push_back(Evt(16, 1));
140 want.push_back(Evt(18, 3));
142 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
144 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
146 std::vector<Evt> got;
147 trace2vector("0.0 1\n"
152 std::vector<Evt> want;
153 want.push_back(Evt(0, 1));
154 want.push_back(Evt(5, 2));
155 want.push_back(Evt(10, 1));
156 want.push_back(Evt(15, 2));
157 want.push_back(Evt(20, 1));
159 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
162 static bool init_function()
164 // do your own initialization here (and return true on success)
165 // But, you CAN'T use testing tools here
169 int main(int argc, char** argv)
171 xbt_log_init(&argc, argv);
172 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);