Logo AND Algorithmique Numérique Distribuée

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