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
7 bool init_unit_test(); // boost forget to give this prototype on NetBSD, which does not fit our paranoid flags
8 #define BOOST_TEST_DYN_LINK
9 #define BOOST_TEST_NO_MAIN
10 #include <boost/test/unit_test.hpp>
12 #include "src/surf/surf_interface.hpp"
13 #include "src/surf/trace_mgr.hpp"
20 namespace utf = boost::unit_test;
21 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: %u)", thedate, value, event->idx);
32 tmgr_trace_event_unref(&event);
34 bool isUsed() { return true; }
37 static void trace2vector(const char* str, std::vector<tmgr::DatedValue>* whereto)
39 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
40 XBT_VERB("---------------------------------------------------------");
41 XBT_VERB("data>>\n%s<<data\n", str);
42 for (auto const& evt : trace->event_list)
43 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
45 MockedResource daResource;
46 simgrid::trace_mgr::future_evt_set fes;
47 tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
49 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
50 thedate = fes.next_date();
52 simgrid::surf::Resource* res;
53 tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
57 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
59 res->apply_event(it, value);
60 whereto->push_back(tmgr::DatedValue(thedate, value));
62 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
68 /* Fails in a way that is difficult to test: xbt_assert should become throw
69 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
71 trace2vector("", &got);
72 std::vector<Evt> want;
73 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
75 BOOST_AUTO_TEST_CASE(one_evt_noloop)
77 std::vector<tmgr::DatedValue> got;
78 trace2vector("9.0 3.0\n", &got);
80 std::vector<tmgr::DatedValue> want;
81 want.push_back(tmgr::DatedValue(9, 3));
82 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
84 BOOST_AUTO_TEST_CASE(two_evt_noloop)
86 std::vector<tmgr::DatedValue> got;
87 trace2vector("3.0 1.0\n"
91 std::vector<tmgr::DatedValue> want;
92 want.push_back(tmgr::DatedValue(3, 1));
93 want.push_back(tmgr::DatedValue(9, 3));
95 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
97 BOOST_AUTO_TEST_CASE(three_evt_noloop)
99 std::vector<tmgr::DatedValue> got;
100 trace2vector("3.0 1.0\n"
105 std::vector<tmgr::DatedValue> want;
106 want.push_back(tmgr::DatedValue(3, 1));
107 want.push_back(tmgr::DatedValue(5, 2));
108 want.push_back(tmgr::DatedValue(9, 3));
110 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
113 BOOST_AUTO_TEST_CASE(two_evt_loop)
115 std::vector<tmgr::DatedValue> got;
116 trace2vector("1.0 1.0\n"
121 std::vector<tmgr::DatedValue> want;
122 want.push_back(tmgr::DatedValue(1, 1));
123 want.push_back(tmgr::DatedValue(3, 3));
124 want.push_back(tmgr::DatedValue(6, 1));
125 want.push_back(tmgr::DatedValue(8, 3));
126 want.push_back(tmgr::DatedValue(11, 1));
127 want.push_back(tmgr::DatedValue(13, 3));
128 want.push_back(tmgr::DatedValue(16, 1));
129 want.push_back(tmgr::DatedValue(18, 3));
131 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
133 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
135 std::vector<tmgr::DatedValue> got;
136 trace2vector("0.0 1\n"
141 std::vector<tmgr::DatedValue> want;
142 want.push_back(tmgr::DatedValue(0, 1));
143 want.push_back(tmgr::DatedValue(5, 2));
144 want.push_back(tmgr::DatedValue(10, 1));
145 want.push_back(tmgr::DatedValue(15, 2));
146 want.push_back(tmgr::DatedValue(20, 1));
148 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
151 static bool init_function()
153 // do your own initialization here (and return true on success)
154 // But, you CAN'T use testing tools here
158 int main(int argc, char** argv)
160 XBT_LOG_CONNECT(unit);
161 xbt_log_init(&argc, argv);
162 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);