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"
16 namespace tmgr = simgrid::trace_mgr;
23 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
26 class MockedResource : public simgrid::surf::Resource {
28 explicit MockedResource() : simgrid::surf::Resource(nullptr, "fake", nullptr) {}
29 void apply_event(tmgr_trace_event_t event, double value)
31 XBT_VERB("t=%.1f: Change value to %lg (idx: %d)", thedate, value, event->idx);
33 bool isUsed() { return true; }
36 static void trace2vector(const char* str, std::vector<tmgr::DatedValue>* whereto)
38 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
39 XBT_VERB("---------------------------------------------------------");
40 XBT_VERB("data>>\n%s<<data\n", str);
41 for (auto evt : trace->event_list)
42 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
44 MockedResource daResource;
45 simgrid::trace_mgr::future_evt_set fes;
46 tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
48 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
49 thedate = fes.next_date();
51 simgrid::surf::Resource* res;
52 tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
56 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
58 res->apply_event(it, value);
59 whereto->push_back(tmgr::DatedValue(thedate, value));
61 XBT_DEBUG("%.1f: ignore an event (idx: %d)\n", thedate, it->idx);
66 /* Fails in a way that is difficult to test: xbt_assert should become throw
67 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
69 trace2vector("", &got);
70 std::vector<Evt> want;
71 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
73 BOOST_AUTO_TEST_CASE(one_evt_noloop)
75 std::vector<tmgr::DatedValue> got;
76 trace2vector("9.0 3.0\n", &got);
78 std::vector<tmgr::DatedValue> want;
79 want.push_back(tmgr::DatedValue(9, 3));
80 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
82 BOOST_AUTO_TEST_CASE(two_evt_noloop)
84 std::vector<tmgr::DatedValue> got;
85 trace2vector("3.0 1.0\n"
89 std::vector<tmgr::DatedValue> want;
90 want.push_back(tmgr::DatedValue(3, 1));
91 want.push_back(tmgr::DatedValue(9, 3));
93 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
95 BOOST_AUTO_TEST_CASE(three_evt_noloop)
97 std::vector<tmgr::DatedValue> got;
98 trace2vector("3.0 1.0\n"
103 std::vector<tmgr::DatedValue> want;
104 want.push_back(tmgr::DatedValue(3, 1));
105 want.push_back(tmgr::DatedValue(5, 2));
106 want.push_back(tmgr::DatedValue(9, 3));
108 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
111 BOOST_AUTO_TEST_CASE(two_evt_loop)
113 std::vector<tmgr::DatedValue> got;
114 trace2vector("1.0 1.0\n"
119 std::vector<tmgr::DatedValue> want;
120 want.push_back(tmgr::DatedValue(1, 1));
121 want.push_back(tmgr::DatedValue(3, 3));
122 want.push_back(tmgr::DatedValue(6, 1));
123 want.push_back(tmgr::DatedValue(8, 3));
124 want.push_back(tmgr::DatedValue(11, 1));
125 want.push_back(tmgr::DatedValue(13, 3));
126 want.push_back(tmgr::DatedValue(16, 1));
127 want.push_back(tmgr::DatedValue(18, 3));
129 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
131 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
133 std::vector<tmgr::DatedValue> got;
134 trace2vector("0.0 1\n"
139 std::vector<tmgr::DatedValue> want;
140 want.push_back(tmgr::DatedValue(0, 1));
141 want.push_back(tmgr::DatedValue(5, 2));
142 want.push_back(tmgr::DatedValue(10, 1));
143 want.push_back(tmgr::DatedValue(15, 2));
144 want.push_back(tmgr::DatedValue(20, 1));
146 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
149 static bool init_function()
151 // do your own initialization here (and return true on success)
152 // But, you CAN'T use testing tools here
156 int main(int argc, char** argv)
158 xbt_log_init(&argc, argv);
159 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);