Logo AND Algorithmique Numérique Distribuée

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