1 /* Copyright (c) 2017-2019. The SimGrid Team. All rights reserved. */
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. */
6 #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
7 #include "src/include/catch.hpp"
9 #include "simgrid/kernel/resource/Resource.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "src/surf/trace_mgr.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
21 class MockedResource : public simgrid::kernel::resource::Resource {
23 explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
24 void apply_event(simgrid::kernel::profile::Event* event, double value) override
26 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
27 tmgr_trace_event_unref(&event);
29 bool is_used() override { return true; }
32 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
34 std::vector<simgrid::kernel::profile::DatedValue> res;
36 simgrid::kernel::profile::Profile* trace = tmgr_trace_new_from_string("TheName", str, 0);
37 XBT_VERB("---------------------------------------------------------");
38 XBT_VERB("data>>\n%s<<data\n", str);
39 for (auto const& evt : trace->event_list)
40 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
42 MockedResource daResource;
43 simgrid::kernel::profile::FutureEvtSet fes;
44 simgrid::kernel::profile::Event* insertedIt = fes.add_trace(trace, &daResource);
46 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
47 thedate = fes.next_date();
49 simgrid::kernel::resource::Resource* resource;
50 simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
54 REQUIRE(it == insertedIt); // Check that we find what we've put
56 resource->apply_event(it, value);
57 res.push_back(simgrid::kernel::profile::DatedValue(thedate, value));
59 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
66 /* Fails in a way that is difficult to test: xbt_assert should become throw
67 BOOST_AUTO_TEST_CASE(no_evt_noloop) {
69 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
72 SECTION("No event, no loop")
74 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
75 std::vector<simgrid::kernel::profile::DatedValue> want;
79 SECTION("One event no loop")
81 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
83 std::vector<simgrid::kernel::profile::DatedValue> want;
84 want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
88 SECTION("Two events, no loop")
90 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
93 std::vector<simgrid::kernel::profile::DatedValue> want;
94 want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
95 want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
100 SECTION("Three events, no loop")
103 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
107 std::vector<simgrid::kernel::profile::DatedValue> want;
108 want.push_back(simgrid::kernel::profile::DatedValue(3, 1));
109 want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
110 want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
112 REQUIRE(want == got);
115 SECTION("Two events, looping")
117 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
121 std::vector<simgrid::kernel::profile::DatedValue> want;
122 want.push_back(simgrid::kernel::profile::DatedValue(1, 1));
123 want.push_back(simgrid::kernel::profile::DatedValue(3, 3));
124 want.push_back(simgrid::kernel::profile::DatedValue(6, 1));
125 want.push_back(simgrid::kernel::profile::DatedValue(8, 3));
126 want.push_back(simgrid::kernel::profile::DatedValue(11, 1));
127 want.push_back(simgrid::kernel::profile::DatedValue(13, 3));
128 want.push_back(simgrid::kernel::profile::DatedValue(16, 1));
129 want.push_back(simgrid::kernel::profile::DatedValue(18, 3));
131 REQUIRE(want == got);
134 SECTION("Two events, looping, start at 0")
136 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
140 std::vector<simgrid::kernel::profile::DatedValue> want;
141 want.push_back(simgrid::kernel::profile::DatedValue(0, 1));
142 want.push_back(simgrid::kernel::profile::DatedValue(5, 2));
143 want.push_back(simgrid::kernel::profile::DatedValue(10, 1));
144 want.push_back(simgrid::kernel::profile::DatedValue(15, 2));
145 want.push_back(simgrid::kernel::profile::DatedValue(20, 1));
147 REQUIRE(want == got);