Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename a type to better match its purpose
[simgrid.git] / src / surf / trace_mgr_test.cpp
1 /* Copyright (c) 2017. 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
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;
13
14 #include "src/surf/surf_interface.hpp"
15 #include "src/surf/trace_mgr.hpp"
16 #include "xbt/log.h"
17 #include "xbt/misc.h"
18
19 #include <math.h>
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
22
23 double thedate;
24 class MockedResource : public simgrid::surf::Resource {
25 public:
26   explicit MockedResource() : simgrid::surf::Resource(nullptr, "fake", nullptr) {}
27   void apply_event(tmgr_trace_event_t event, double value)
28   {
29     XBT_VERB("t=%.1f: Change value to %lg (idx: %d)", thedate, value, event->idx);
30   }
31   bool isUsed() { return true; }
32 };
33
34 static inline bool doubleEq(double d1, double d2)
35 {
36   return fabs(d1 - d2) < 0.0001;
37 }
38 class Evt {
39 public:
40   double date;
41   double value;
42   explicit Evt(double d, double v) : date(d), value(v) {}
43   bool operator==(Evt e2) { return (doubleEq(date, e2.date)) && (doubleEq(value, e2.value)); }
44   bool operator!=(Evt e2) { return !(*this == e2); }
45 };
46 static std::ostream& operator<<(std::ostream& out, const Evt& e)
47 {
48   out << e.date << " " << e.value;
49   return out;
50 }
51
52 static void trace2vector(const char* str, std::vector<Evt>* whereto)
53 {
54   simgrid::trace_mgr::trace* trace = tmgr_trace_new_from_string("TheName", str, 0);
55   XBT_VERB("data>>\n%s<<data\n", str);
56   for (auto evt : trace->event_list)
57     XBT_VERB("event: d:%lg v:%lg", evt.delta, evt.value);
58
59   MockedResource daResource;
60   simgrid::trace_mgr::future_evt_set fes;
61   tmgr_trace_event_t insertedIt = fes.add_trace(trace, &daResource);
62
63   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
64     thedate = fes.next_date();
65     double value;
66     simgrid::surf::Resource* res;
67     tmgr_trace_event_t it = fes.pop_leq(thedate, &value, &res);
68     if (it == nullptr)
69       continue;
70
71     BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put
72     if (value >= 0) {
73       res->apply_event(it, value);
74       whereto->push_back(Evt(thedate, value));
75     } else {
76       XBT_DEBUG("%.1f: ignore an event (idx: %d)\n", thedate, it->idx);
77     }
78   }
79 }
80
81 /* Fails in a way that is difficult to test: xbt_assert should become throw
82 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
83   std::vector<Evt> got;
84   trace2vector("", &got);
85   std::vector<Evt> want;
86   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
87 }*/
88 BOOST_AUTO_TEST_CASE(one_evt_noloop)
89 {
90   std::vector<Evt> got;
91   trace2vector("9.0 3.0\n", &got);
92
93   std::vector<Evt> want;
94   want.push_back(Evt(9, 3));
95   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
96 }
97 BOOST_AUTO_TEST_CASE(two_evt_noloop)
98 {
99   std::vector<Evt> got;
100   trace2vector("3.0 1.0\n"
101                "9.0 3.0\n",
102                &got);
103
104   std::vector<Evt> want;
105   want.push_back(Evt(3, 1));
106   want.push_back(Evt(9, 3));
107
108   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
109 }
110 BOOST_AUTO_TEST_CASE(three_evt_noloop)
111 {
112   std::vector<Evt> got;
113   trace2vector("3.0 1.0\n"
114                "5.0 2.0\n"
115                "9.0 3.0\n",
116                &got);
117
118   std::vector<Evt> want;
119   want.push_back(Evt(3, 1));
120   want.push_back(Evt(5, 2));
121   want.push_back(Evt(9, 3));
122
123   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
124 }
125
126 BOOST_AUTO_TEST_CASE(two_evt_loop)
127 {
128   std::vector<Evt> got;
129   trace2vector("1.0 1.0\n"
130                "3.0 3.0\n"
131                "WAITFOR 2\n",
132                &got);
133
134   std::vector<Evt> want;
135   want.push_back(Evt(1, 1));
136   want.push_back(Evt(3, 3));
137   want.push_back(Evt(6, 1));
138   want.push_back(Evt(8, 3));
139   want.push_back(Evt(11, 1));
140   want.push_back(Evt(13, 3));
141   want.push_back(Evt(16, 1));
142   want.push_back(Evt(18, 3));
143
144   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
145 }
146 BOOST_AUTO_TEST_CASE(two_evt_start0_loop)
147 {
148   std::vector<Evt> got;
149   trace2vector("0.0 1\n"
150                "5.0 2\n"
151                "WAITFOR 5\n",
152                &got);
153
154   std::vector<Evt> want;
155   want.push_back(Evt(0, 1));
156   want.push_back(Evt(5, 2));
157   want.push_back(Evt(10, 1));
158   want.push_back(Evt(15, 2));
159   want.push_back(Evt(20, 1));
160
161   BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end());
162 }
163
164 static bool init_function()
165 {
166   // do your own initialization here (and return true on success)
167   // But, you CAN'T use testing tools here
168   return true;
169 }
170
171 int main(int argc, char** argv)
172 {
173   xbt_log_init(&argc, argv);
174   return ::boost::unit_test::unit_test_main(&init_function, argc, argv);
175 }