Logo AND Algorithmique Numérique Distribuée

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