1 /* Copyright (c) 2017-2018. 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 "simgrid/kernel/resource/Resource.hpp"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/trace_mgr.hpp"
21 namespace utf = boost::unit_test;
22 namespace tmgr = simgrid::trace_mgr;
24 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
27 class MockedResource : public simgrid::kernel::resource::Resource {
29 explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
30 void apply_event(tmgr_trace_event_t event, double value) override
32 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
33 tmgr_trace_event_unref(&event);
35 bool is_used() override { return true; }
38 static void trace2vector(const char* str, std::vector<tmgr::DatedValue>* whereto)
40 simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
41 XBT_VERB("---------------------------------------------------------");
42 XBT_VERB("data>>\n%s<<data\n", str);
43 for (auto const& evt : trace->event_list)
44 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
46 MockedResource daResource;
47 simgrid::trace_mgr::future_evt_set fes;
48 tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
50 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
51 thedate = fes.next_date();
53 simgrid::kernel::resource::Resource* res;
54 tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
58 BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
60 res->apply_event(it, value);
61 whereto->push_back(tmgr::DatedValue(thedate, value));
63 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
69 /* Fails in a way that is difficult to test: xbt_assert should become throw
70 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
72 trace2vector("", &got);
73 std::vector<Evt> want;
74 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
76 BOOST_AUTO_TEST_CASE(one_evt_noloop)
78 std::vector<tmgr::DatedValue> got;
79 trace2vector("9.0 3.0\n", &got);
81 std::vector<tmgr::DatedValue> want;
82 want.push_back(tmgr::DatedValue(9, 3));
83 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
85 BOOST_AUTO_TEST_CASE(two_evt_noloop)
87 std::vector<tmgr::DatedValue> got;
88 trace2vector("3.0 1.0\n"
92 std::vector<tmgr::DatedValue> want;
93 want.push_back(tmgr::DatedValue(3, 1));
94 want.push_back(tmgr::DatedValue(9, 3));
96 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
98 BOOST_AUTO_TEST_CASE(three_evt_noloop)
100 std::vector<tmgr::DatedValue> got;
101 trace2vector("3.0 1.0\n"
106 std::vector<tmgr::DatedValue> want;
107 want.push_back(tmgr::DatedValue(3, 1));
108 want.push_back(tmgr::DatedValue(5, 2));
109 want.push_back(tmgr::DatedValue(9, 3));
111 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
114 BOOST_AUTO_TEST_CASE(two_evt_loop)
116 std::vector<tmgr::DatedValue> got;
117 trace2vector("1.0 1.0\n"
122 std::vector<tmgr::DatedValue> want;
123 want.push_back(tmgr::DatedValue(1, 1));
124 want.push_back(tmgr::DatedValue(3, 3));
125 want.push_back(tmgr::DatedValue(6, 1));
126 want.push_back(tmgr::DatedValue(8, 3));
127 want.push_back(tmgr::DatedValue(11, 1));
128 want.push_back(tmgr::DatedValue(13, 3));
129 want.push_back(tmgr::DatedValue(16, 1));
130 want.push_back(tmgr::DatedValue(18, 3));
132 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
134 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
136 std::vector<tmgr::DatedValue> got;
137 trace2vector("0.0 1\n"
142 std::vector<tmgr::DatedValue> want;
143 want.push_back(tmgr::DatedValue(0, 1));
144 want.push_back(tmgr::DatedValue(5, 2));
145 want.push_back(tmgr::DatedValue(10, 1));
146 want.push_back(tmgr::DatedValue(15, 2));
147 want.push_back(tmgr::DatedValue(20, 1));
149 BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
152 static bool init_function()
154 // do your own initialization here (and return true on success)
155 // But, you CAN'T use testing tools here
159 int main(int argc, char** argv)
161 xbt_log_init(&argc, argv);
162 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);