Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert a first unit test to Catch
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 31 Jan 2019 00:06:47 +0000 (01:06 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 31 Jan 2019 07:42:06 +0000 (08:42 +0100)
src/surf/trace_mgr.cpp
src/surf/trace_mgr.hpp
src/surf/trace_mgr_test.cpp
tools/cmake/Tests.cmake

index d84999c..037b89e 100644 (file)
@@ -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_));
 }
index 1e749de..fe48da2 100644 (file)
@@ -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);
 
index 064b2a1..610657d 100644 (file)
@@ -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 <boost/test/unit_test.hpp>
+#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 <cmath>
 
-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<simgrid::kernel::profile::DatedValue>* whereto)
+static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
 {
+  std::vector<simgrid::kernel::profile::DatedValue> res;
+
   simgrid::kernel::profile::Profile* trace = tmgr_trace_new_from_string("TheName", str, 0);
   XBT_VERB("---------------------------------------------------------");
   XBT_VERB("data>>\n%s<<data\n", str);
@@ -49,114 +46,104 @@ static void trace2vector(const char* str, std::vector<simgrid::kernel::profile::
   while (fes.next_date() <= 20.0 && fes.next_date() >= 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<Evt> got;
-  trace2vector("", &got);
-  std::vector<Evt> 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<simgrid::kernel::profile::DatedValue> got;
-  trace2vector("9.0 3.0\n", &got);
 
-  std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got;
-  trace2vector("3.0 1.0\n"
-               "9.0 3.0\n",
-               &got);
+  SECTION("No event, no loop")
+  {
+    std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
+    std::vector<simgrid::kernel::profile::DatedValue> want;
+    REQUIRE(want == got);
+  }
 
-  std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got;
-  trace2vector("3.0 1.0\n"
-               "5.0 2.0\n"
-               "9.0 3.0\n",
-               &got);
-
-  std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> want;
+    want.push_back(simgrid::kernel::profile::DatedValue(9, 3));
+    REQUIRE(want == got);
+  }
 
-BOOST_AUTO_TEST_CASE(two_evt_loop)
-{
-  std::vector<simgrid::kernel::profile::DatedValue> got;
-  trace2vector("1.0 1.0\n"
-               "3.0 3.0\n"
-               "LOOPAFTER 2\n",
-               &got);
-
-  std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got;
-  trace2vector("0.0 1\n"
-               "5.0 2\n"
-               "LOOPAFTER 5\n",
-               &got);
-
-  std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
+                                                                         "5.0 2.0\n"
+                                                                         "9.0 3.0\n");
+
+    std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
+                                                                         "3.0 3.0\n"
+                                                                         "LOOPAFTER 2\n");
+
+    std::vector<simgrid::kernel::profile::DatedValue> 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<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
+                                                                         "5.0 2\n"
+                                                                         "LOOPAFTER 5\n");
+
+    std::vector<simgrid::kernel::profile::DatedValue> 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);
+  }
 }
index 530884a..c0f192f 100644 (file)
@@ -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)