Logo AND Algorithmique Numérique Distribuée

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