Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Install some kernel header files for the users' plugins and more
[simgrid.git] / src / surf / trace_mgr_test.cpp
1 /* Copyright (c) 2017-2018. The SimGrid Team. All rights reserved.               */
2
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. */
5
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>
11
12 #include "simgrid/kernel/resource/Resource.hpp"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/trace_mgr.hpp"
15
16 #include "xbt/log.h"
17 #include "xbt/misc.h"
18
19 #include <cmath>
20
21 namespace utf  = boost::unit_test;
22 namespace tmgr = simgrid::trace_mgr;
23
24 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
25
26 double thedate;
27 class MockedResource : public simgrid::kernel::resource::Resource {
28 public:
29   explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
30   void apply_event(tmgr_trace_event_t event, double value)
31   {
32     XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
33     tmgr_trace_event_unref(&event);
34   }
35   bool isUsed() { return true; }
36 };
37
38 static void trace2vector(const char* str, std::vector<tmgr::DatedValue>* whereto)
39 {
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_);
45
46   MockedResource daResource;
47   simgrid::trace_mgr::future_evt_set fes;
48   tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
49
50   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
51     thedate = fes.next_date();
52     double value;
53     simgrid::kernel::resource::Resource* res;
54     tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
55     if (it == nullptr)
56       continue;
57
58     BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
59     if (value >= 0) {
60       res->apply_event(it, value);
61       whereto->push_back(tmgr::DatedValue(thedate, value));
62     } else {
63       XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
64     }
65   }
66   tmgr_finalize();
67 }
68
69 /* Fails in a way that is difficult to test: xbt_assert should become throw
70 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
71   std::vector<Evt> got;
72   trace2vector("", &got);
73   std::vector<Evt> want;
74   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
75 }*/
76 BOOST_AUTO_TEST_CASE(one_evt_noloop)
77 {
78   std::vector<tmgr::DatedValue> got;
79   trace2vector("9.0 3.0\n", &got);
80
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());
84 }
85 BOOST_AUTO_TEST_CASE(two_evt_noloop)
86 {
87   std::vector<tmgr::DatedValue> got;
88   trace2vector("3.0 1.0\n"
89                "9.0 3.0\n",
90                &got);
91
92   std::vector<tmgr::DatedValue> want;
93   want.push_back(tmgr::DatedValue(3, 1));
94   want.push_back(tmgr::DatedValue(9, 3));
95
96   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
97 }
98 BOOST_AUTO_TEST_CASE(three_evt_noloop)
99 {
100   std::vector<tmgr::DatedValue> got;
101   trace2vector("3.0 1.0\n"
102                "5.0 2.0\n"
103                "9.0 3.0\n",
104                &got);
105
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));
110
111   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
112 }
113
114 BOOST_AUTO_TEST_CASE(two_evt_loop)
115 {
116   std::vector<tmgr::DatedValue> got;
117   trace2vector("1.0 1.0\n"
118                "3.0 3.0\n"
119                "LOOPAFTER 2\n",
120                &got);
121
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));
131
132   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
133 }
134 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
135 {
136   std::vector<tmgr::DatedValue> got;
137   trace2vector("0.0 1\n"
138                "5.0 2\n"
139                "LOOPAFTER 5\n",
140                &got);
141
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));
148
149   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
150 }
151
152 static bool init_function()
153 {
154   // do your own initialization here (and return true on success)
155   // But, you CAN'T use testing tools here
156   return true;
157 }
158
159 int main(int argc, char** argv)
160 {
161   xbt_log_init(&argc, argv);
162   return ::boost::unit_test::unit_test_main(&init_function, argc, argv);
163 }