From: Martin Quinson Date: Thu, 31 Jan 2019 00:06:47 +0000 (+0100) Subject: Convert a first unit test to Catch X-Git-Tag: v3_22~441 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8b34cbb38dddd7feb3af283c525abcc11df44263 Convert a first unit test to Catch --- diff --git a/src/surf/trace_mgr.cpp b/src/surf/trace_mgr.cpp index d84999c855..037b89ecb1 100644 --- a/src/surf/trace_mgr.cpp +++ b/src/surf/trace_mgr.cpp @@ -29,7 +29,7 @@ namespace simgrid { namespace kernel { namespace profile { -bool DatedValue::operator==(DatedValue e2) +bool DatedValue::operator==(DatedValue const& e2) const { return (doubleEq(date_, e2.date_)) && (doubleEq(value_, e2.value_)); } diff --git a/src/surf/trace_mgr.hpp b/src/surf/trace_mgr.hpp index 1e749dedf4..fe48da21a7 100644 --- a/src/surf/trace_mgr.hpp +++ b/src/surf/trace_mgr.hpp @@ -65,8 +65,8 @@ public: double value_ = 0; explicit DatedValue() = default; explicit DatedValue(double d, double v) : date_(d), value_(v) {} - bool operator==(DatedValue e2); - bool operator!=(DatedValue e2) { return not(*this == e2); } + bool operator==(DatedValue const& e2) const; + bool operator!=(DatedValue const& e2) const { return not(*this == e2); } }; std::ostream& operator<<(std::ostream& out, const DatedValue& e); diff --git a/src/surf/trace_mgr_test.cpp b/src/surf/trace_mgr_test.cpp index 064b2a1d0a..610657d3f2 100644 --- a/src/surf/trace_mgr_test.cpp +++ b/src/surf/trace_mgr_test.cpp @@ -3,11 +3,8 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#define BOOST_TEST_MODULE Trace Manager tests -bool init_unit_test(); // boost sometimes forget to give this prototype (NetBSD and other), which does not fit our paranoid flags -#define BOOST_TEST_DYN_LINK -#define BOOST_TEST_NO_MAIN -#include +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file +#include "src/include/catch.hpp" #include "simgrid/kernel/resource/Resource.hpp" #include "src/surf/surf_interface.hpp" @@ -18,8 +15,6 @@ bool init_unit_test(); // boost sometimes forget to give this prototype (NetBSD #include -namespace utf = boost::unit_test; - XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager"); double thedate; @@ -34,8 +29,10 @@ public: bool is_used() override { return true; } }; -static void trace2vector(const char* str, std::vector* whereto) +static std::vector trace2vector(const char* str) { + std::vector res; + simgrid::kernel::profile::Profile* trace = tmgr_trace_new_from_string("TheName", str, 0); XBT_VERB("---------------------------------------------------------"); XBT_VERB("data>>\n%s<= 0) { thedate = fes.next_date(); double value; - simgrid::kernel::resource::Resource* res; - simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &res); + simgrid::kernel::resource::Resource* resource; + simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource); if (it == nullptr) continue; - BOOST_CHECK_EQUAL(it, insertedIt); // Check that we find what we've put + REQUIRE(it == insertedIt); // Check that we find what we've put if (value >= 0) { - res->apply_event(it, value); - whereto->push_back(simgrid::kernel::profile::DatedValue(thedate, value)); + resource->apply_event(it, value); + res.push_back(simgrid::kernel::profile::DatedValue(thedate, value)); } else { XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx); } } tmgr_finalize(); + return res; } /* Fails in a way that is difficult to test: xbt_assert should become throw BOOST_AUTO_TEST_CASE(no_evt_noloop) { - std::vector got; - trace2vector("", &got); - std::vector want; - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); }*/ -BOOST_AUTO_TEST_CASE(one_evt_noloop) +TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile") { - std::vector got; - trace2vector("9.0 3.0\n", &got); - std::vector want; - want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); -} -BOOST_AUTO_TEST_CASE(two_evt_noloop) -{ - std::vector got; - trace2vector("3.0 1.0\n" - "9.0 3.0\n", - &got); + SECTION("No event, no loop") + { + std::vector got = trace2vector(""); + std::vector want; + REQUIRE(want == got); + } - std::vector want; - want.push_back(simgrid::kernel::profile::DatedValue(3, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); + SECTION("One event no loop") + { + std::vector got = trace2vector("9.0 3.0\n"); - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); -} -BOOST_AUTO_TEST_CASE(three_evt_noloop) -{ - std::vector got; - trace2vector("3.0 1.0\n" - "5.0 2.0\n" - "9.0 3.0\n", - &got); - - std::vector want; - want.push_back(simgrid::kernel::profile::DatedValue(3, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(5, 2)); - want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); - - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); -} + std::vector want; + want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); + REQUIRE(want == got); + } -BOOST_AUTO_TEST_CASE(two_evt_loop) -{ - std::vector got; - trace2vector("1.0 1.0\n" - "3.0 3.0\n" - "LOOPAFTER 2\n", - &got); - - std::vector want; - want.push_back(simgrid::kernel::profile::DatedValue(1, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(3, 3)); - want.push_back(simgrid::kernel::profile::DatedValue(6, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(8, 3)); - want.push_back(simgrid::kernel::profile::DatedValue(11, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(13, 3)); - want.push_back(simgrid::kernel::profile::DatedValue(16, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(18, 3)); - - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); -} -BOOST_AUTO_TEST_CASE(two_evt_start0_loop) -{ - std::vector got; - trace2vector("0.0 1\n" - "5.0 2\n" - "LOOPAFTER 5\n", - &got); - - std::vector want; - want.push_back(simgrid::kernel::profile::DatedValue(0, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(5, 2)); - want.push_back(simgrid::kernel::profile::DatedValue(10, 1)); - want.push_back(simgrid::kernel::profile::DatedValue(15, 2)); - want.push_back(simgrid::kernel::profile::DatedValue(20, 1)); - - BOOST_CHECK_EQUAL_COLLECTIONS(want.begin(), want.end(), got.begin(), got.end()); -} + SECTION("Two events, no loop") + { + std::vector got = trace2vector("3.0 1.0\n" + "9.0 3.0\n"); -static bool init_function() -{ - // do your own initialization here (and return true on success) - // But, you CAN'T use testing tools here - return true; -} + std::vector want; + want.push_back(simgrid::kernel::profile::DatedValue(3, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); -int main(int argc, char** argv) -{ - xbt_log_init(&argc, argv); - return ::boost::unit_test::unit_test_main(&init_function, argc, argv); + REQUIRE(want == got); + } + + SECTION("Three events, no loop") + { + + std::vector got = trace2vector("3.0 1.0\n" + "5.0 2.0\n" + "9.0 3.0\n"); + + std::vector want; + want.push_back(simgrid::kernel::profile::DatedValue(3, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(5, 2)); + want.push_back(simgrid::kernel::profile::DatedValue(9, 3)); + + REQUIRE(want == got); + } + + SECTION("Two events, looping") + { + std::vector got = trace2vector("1.0 1.0\n" + "3.0 3.0\n" + "LOOPAFTER 2\n"); + + std::vector want; + want.push_back(simgrid::kernel::profile::DatedValue(1, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(3, 3)); + want.push_back(simgrid::kernel::profile::DatedValue(6, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(8, 3)); + want.push_back(simgrid::kernel::profile::DatedValue(11, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(13, 3)); + want.push_back(simgrid::kernel::profile::DatedValue(16, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(18, 3)); + + REQUIRE(want == got); + } + + SECTION("Two events, looping, start at 0") + { + std::vector got = trace2vector("0.0 1\n" + "5.0 2\n" + "LOOPAFTER 5\n"); + + std::vector want; + want.push_back(simgrid::kernel::profile::DatedValue(0, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(5, 2)); + want.push_back(simgrid::kernel::profile::DatedValue(10, 1)); + want.push_back(simgrid::kernel::profile::DatedValue(15, 2)); + want.push_back(simgrid::kernel::profile::DatedValue(20, 1)); + + REQUIRE(want == got); + } } diff --git a/tools/cmake/Tests.cmake b/tools/cmake/Tests.cmake index 530884ae21..c0f192fb33 100644 --- a/tools/cmake/Tests.cmake +++ b/tools/cmake/Tests.cmake @@ -128,8 +128,8 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND) set_target_properties(boost_unit_test_framework PROPERTIES IMPORTED_LOCATION ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) add_executable (unit-tmgr src/surf/trace_mgr_test.cpp) - target_link_libraries(unit-tmgr simgrid boost_unit_test_framework) - ADD_TEST(unit-tmgr ${CMAKE_BINARY_DIR}/unit-tmgr --build_info=yes) + target_link_libraries(unit-tmgr simgrid) + ADD_TEST(unit-tmgr ${CMAKE_BINARY_DIR}/unit-tmgr) set_property(TARGET unit-tmgr APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}") add_dependencies(tests unit-tmgr) if (SIMGRID_HAVE_MC)