[mlpack] 18/30: Split tests for PrefixedOutStream and Timer

Barak A. Pearlmutter barak+git at pearlmutter.net
Mon Dec 26 10:15:27 UTC 2016


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit 4eb841ef037e9bd8bd8fbde0dfccbf1257060eb3
Author: shikhar <shikharbhardwaj68 at gmail.com>
Date:   Fri Dec 16 20:31:23 2016 +0530

    Split tests for PrefixedOutStream and Timer
---
 COPYRIGHT.txt                               |   2 +
 src/mlpack/core.hpp                         |   2 +
 src/mlpack/tests/CMakeLists.txt             |   2 +
 src/mlpack/tests/cli_test.cpp               | 211 ----------------------------
 src/mlpack/tests/prefixedoutstream_test.cpp | 165 ++++++++++++++++++++++
 src/mlpack/tests/timer_test.cpp             | 101 +++++++++++++
 6 files changed, 272 insertions(+), 211 deletions(-)

diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt
index fd38ca1..d02ba90 100644
--- a/COPYRIGHT.txt
+++ b/COPYRIGHT.txt
@@ -61,6 +61,8 @@ Copyright:
   Copyright 2016, Nilay Jain <nilayjain13 at gmail.com>
   Copyright 2016, Peter Lehner <peter.lehner at dlr.de>
   Copyright 2016, Anuraj Kanodia <akanuraj200 at gmail.com>
+  Copyright 2016, Ivan Georgiev <ivan at jonan.info>
+  Copyright 2016, Shikhar Bhardwaj <shikharbhardwaj68 at gmail.com>
 License: BSD-3-clause
   All rights reserved.
   .
diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp
index 78a1865..aa9242d 100644
--- a/src/mlpack/core.hpp
+++ b/src/mlpack/core.hpp
@@ -201,6 +201,8 @@
  *   - Nilay Jain <nilayjain13 at gmail.com>
  *   - Peter Lehner <peter.lehner at dlr.de>
  *   - Anuraj Kanodia <akanuraj200 at gmail.com>
+ *   - Ivan Georgiev <ivan at jonan.info>
+ *   - Shikhar Bhardwaj <shikharbhardwaj68 at gmail.com>
  */
 
 // First, include all of the prerequisites.
diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index a43c1b2..cd83ade 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -85,6 +85,8 @@ add_executable(mlpack_test
   armadillo_svd_test.cpp
   ub_tree_test.cpp
   vantage_point_tree_test.cpp
+  prefixedoutstream_test.cpp
+  timer_test.cpp
 )
 # Link dependencies of test executable.
 target_link_libraries(mlpack_test
diff --git a/src/mlpack/tests/cli_test.cpp b/src/mlpack/tests/cli_test.cpp
index 06200ea..ce0e6bd 100644
--- a/src/mlpack/tests/cli_test.cpp
+++ b/src/mlpack/tests/cli_test.cpp
@@ -10,18 +10,6 @@
  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
  */
 
-#include <iostream>
-#include <sstream>
-
-#ifndef _WIN32
-  #include <sys/time.h>
-#endif
-
-// For Sleep().
-#ifdef _WIN32
-  #include <windows.h>
-#endif
-
 #include <mlpack/core.hpp>
 
 #define DEFAULT_INT 42
@@ -29,12 +17,6 @@
 #include <boost/test/unit_test.hpp>
 #include "test_tools.hpp"
 
-#define BASH_RED "\033[0;31m"
-#define BASH_GREEN "\033[0;32m"
-#define BASH_YELLOW "\033[0;33m"
-#define BASH_CYAN "\033[0;36m"
-#define BASH_CLEAR "\033[0m"
-
 using namespace mlpack;
 using namespace mlpack::util;
 
@@ -66,35 +48,6 @@ BOOST_AUTO_TEST_CASE(TestCLIAdd)
       CLI::GetParam<bool>("alias/bool"));
 }
 
-/**
- * Test the output of CLI.  We will pass bogus input to a stringstream so that
- * none of it gets to the screen.
- */
-BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamBasic)
-{
-  std::stringstream ss;
-  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
-
-  pss << "This shouldn't break anything" << std::endl;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "This shouldn't break anything\n");
-
-  ss.str("");
-  pss << "Test the new lines...";
-  pss << "shouldn't get 'Info' here." << std::endl;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR
-      "Test the new lines...shouldn't get 'Info' here.\n");
-
-  pss << "But now I should." << std::endl << std::endl;
-  pss << "";
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR
-      "Test the new lines...shouldn't get 'Info' here.\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "But now I should.\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "");
-}
 
 /**
  * Tests that the various PARAM_* macros work properly.
@@ -200,168 +153,4 @@ BOOST_AUTO_TEST_CASE(TestVectorOption2)
 
 }
 
-/**
- * Test that we can correctly output Armadillo objects to PrefixedOutStream
- * objects.
- */
-BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
-{
-  // We will test this with both a vector and a matrix.
-  arma::vec test("1.0 1.5 2.0 2.5 3.0 3.5 4.0");
-
-  std::stringstream ss;
-  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
-
-  pss << test;
-  // This should result in nothing being on the current line (since it clears
-  // it).
-  BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR "   1.0000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   1.5000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.0000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   3.0000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   3.5000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000\n");
-
-  ss.str("");
-  pss << trans(test);
-  // This should result in there being stuff on the line.
-  BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
-      "   1.0000   1.5000   2.0000   2.5000   3.0000   3.5000   4.0000\n");
-
-  arma::mat test2("1.0 1.5 2.0; 2.5 3.0 3.5; 4.0 4.5 4.99999");
-  ss.str("");
-  pss << test2;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   1.0000   1.5000   2.0000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000   3.0000   3.5000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000   4.5000   5.0000\n");
-
-  // Try and throw a curveball by not clearing the line before outputting
-  // something else.  The PrefixedOutStream should not force Armadillo objects
-  // onto their own lines.
-  ss.str("");
-  pss << "hello" << test2;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "hello   1.0000   1.5000   2.0000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000   3.0000   3.5000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000   4.5000   5.0000\n");
-}
-
-/**
- * Test that we can correctly output things in general.
- */
-BOOST_AUTO_TEST_CASE(TestPrefixedOutStream)
-{
-  std::stringstream ss;
-  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
-
-  pss << "hello world I am ";
-  pss << 7;
-
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7");
-
-  pss << std::endl;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7\n");
-
-  ss.str("");
-  pss << std::endl;
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR "\n");
-}
-
-/**
- * Test format modifiers.
- */
-BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamModifiers)
-{
-  std::stringstream ss;
-  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
-
-  pss << "I have a precise number which is ";
-  pss << std::setw(6) << std::setfill('0') << (int)156;
-
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR
-      "I have a precise number which is 000156");
-}
-
-/**
- * Test formatted output.
- */
-BOOST_AUTO_TEST_CASE(TestFormattedOutput)
-{
-  std::stringstream ss;
-  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ]" BASH_CLEAR);
-
-  const double pi = std::acos(-1.0);
-  pss << std::setprecision(10) << pi;
-
-  BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ]" BASH_CLEAR "3.141592654");
-}
-/**
- * We should be able to start and then stop a timer multiple times and it should
- * save the value.
- */
-BOOST_AUTO_TEST_CASE(MultiRunTimerTest)
-{
-  Timer::Start("test_timer");
-
-  // On Windows (or, at least, in Windows not using VS2010) we cannot use
-  // usleep() because it is not provided.  Instead we will use Sleep() for a
-  // number of milliseconds.
-  #ifdef _WIN32
-  Sleep(10);
-  #else
-  usleep(10000);
-  #endif
-
-  Timer::Stop("test_timer");
-
-  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 10000);
-
-  // Restart it.
-  Timer::Start("test_timer");
-
-  #ifdef _WIN32
-  Sleep(10);
-  #else
-  usleep(10000);
-  #endif
-
-  Timer::Stop("test_timer");
-
-  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 20000);
-
-  // Just one more time, for good measure...
-  Timer::Start("test_timer");
-
-  #ifdef _WIN32
-  Sleep(20);
-  #else
-  usleep(20000);
-  #endif
-
-  Timer::Stop("test_timer");
-
-  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 40000);
-}
-
-BOOST_AUTO_TEST_CASE(TwiceStartTimerTest)
-{
-  Timer::Start("test_timer");
-
-  BOOST_REQUIRE_THROW(Timer::Start("test_timer"), std::runtime_error);
-}
-
-BOOST_AUTO_TEST_CASE(TwiceStopTimerTest)
-{
-  Timer::Stop("test_timer");
-
-  BOOST_REQUIRE_THROW(Timer::Stop("test_timer"), std::runtime_error);
-}
-
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/prefixedoutstream_test.cpp b/src/mlpack/tests/prefixedoutstream_test.cpp
new file mode 100644
index 0000000..8d4652a
--- /dev/null
+++ b/src/mlpack/tests/prefixedoutstream_test.cpp
@@ -0,0 +1,165 @@
+/**
+ * @file prefixedoutstream_test.cpp
+ * @author Matthew Amidon, Ryan Curtin
+ *
+ * Tests for the PrefixedOutStream class
+ *
+ * mlpack is free software; you may redistribute it and/or modify it under the
+ * terms of the 3-clause BSD license.  You should have received a copy of the
+ * 3-clause BSD license along with mlpack.  If not, see
+ * http://www.opensource.org/licenses/BSD-3-Clause for more information.
+ **/
+
+#include <iostream>
+#include <sstream>
+
+#include <mlpack/core.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "test_tools.hpp"
+
+#define BASH_RED "\033[0;31m"
+#define BASH_GREEN "\033[0;32m"
+#define BASH_YELLOW "\033[0;33m"
+#define BASH_CYAN "\033[0;36m"
+#define BASH_CLEAR "\033[0m"
+
+using namespace mlpack;
+using namespace mlpack::util;
+
+BOOST_AUTO_TEST_SUITE(PrefixedOutStreamTest);
+
+/**
+ * Test the output of CLI using PrefixedOutStream.  We will pass bogus 
+ * input to a stringstream so that none of it gets to the screen.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamBasic)
+{
+  std::stringstream ss;
+  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+
+  pss << "This shouldn't break anything" << std::endl;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "This shouldn't break anything\n");
+
+  ss.str("");
+  pss << "Test the new lines...";
+  pss << "shouldn't get 'Info' here." << std::endl;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR
+      "Test the new lines...shouldn't get 'Info' here.\n");
+
+  pss << "But now I should." << std::endl << std::endl;
+  pss << "";
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR
+      "Test the new lines...shouldn't get 'Info' here.\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "But now I should.\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "");
+}
+
+/**
+ * Test that we can correctly output Armadillo objects to PrefixedOutStream
+ * objects.
+ */
+BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
+{
+  // We will test this with both a vector and a matrix.
+  arma::vec test("1.0 1.5 2.0 2.5 3.0 3.5 4.0");
+
+  std::stringstream ss;
+  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+
+  pss << test;
+  // This should result in nothing being on the current line (since it clears
+  // it).
+  BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR "   1.0000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   1.5000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.0000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   3.0000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   3.5000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000\n");
+
+  ss.str("");
+  pss << trans(test);
+  // This should result in there being stuff on the line.
+  BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
+      "   1.0000   1.5000   2.0000   2.5000   3.0000   3.5000   4.0000\n");
+
+  arma::mat test2("1.0 1.5 2.0; 2.5 3.0 3.5; 4.0 4.5 4.99999");
+  ss.str("");
+  pss << test2;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   1.0000   1.5000   2.0000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000   3.0000   3.5000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000   4.5000   5.0000\n");
+
+  // Try and throw a curveball by not clearing the line before outputting
+  // something else.  The PrefixedOutStream should not force Armadillo objects
+  // onto their own lines.
+  ss.str("");
+  pss << "hello" << test2;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "hello   1.0000   1.5000   2.0000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   2.5000   3.0000   3.5000\n"
+      BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000   4.5000   5.0000\n");
+}
+
+/**
+ * Test that we can correctly output things in general.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStream)
+{
+  std::stringstream ss;
+  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+
+  pss << "hello world I am ";
+  pss << 7;
+
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7");
+
+  pss << std::endl;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "hello world I am 7\n");
+
+  ss.str("");
+  pss << std::endl;
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR "\n");
+}
+
+/**
+ * Test format modifiers.
+ */
+BOOST_AUTO_TEST_CASE(TestPrefixedOutStreamModifiers)
+{
+  std::stringstream ss;
+  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ] " BASH_CLEAR);
+
+  pss << "I have a precise number which is ";
+  pss << std::setw(6) << std::setfill('0') << (int)156;
+
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR
+      "I have a precise number which is 000156");
+}
+
+/**
+ * Test formatted floating-point output.
+ */
+BOOST_AUTO_TEST_CASE(TestFormattedOutput)
+{
+  std::stringstream ss;
+  PrefixedOutStream pss(ss, BASH_GREEN "[INFO ]" BASH_CLEAR);
+
+  const double pi = std::acos(-1.0);
+  pss << std::setprecision(10) << pi;
+
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ]" BASH_CLEAR "3.141592654");
+}
+
+BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/timer_test.cpp b/src/mlpack/tests/timer_test.cpp
new file mode 100644
index 0000000..c832750
--- /dev/null
+++ b/src/mlpack/tests/timer_test.cpp
@@ -0,0 +1,101 @@
+/**
+ * @file timer_test.cpp
+ * @author Matthew Amidon, Ryan Curtin
+ *
+ * Test for the timer class
+ *
+ * mlpack is free software; you may redistribute it and/or modify it under the
+ * terms of the 3-clause BSD license.  You should have received a copy of the
+ * 3-clause BSD license along with mlpack.  If not, see
+ * http://www.opensource.org/licenses/BSD-3-Clause for more information.
+ **/
+
+#ifndef _WIN32
+  #include <sys/time.h>
+#endif
+
+// For Sleep().
+#ifdef _WIN32
+  #include <windows.h>
+#endif
+
+#include <mlpack/core.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "test_tools.hpp"
+
+using namespace mlpack;
+
+
+BOOST_AUTO_TEST_SUITE(TimerTest);
+
+/**
+ * We should be able to start and then stop a timer multiple times and it should
+ * save the value.
+ */
+BOOST_AUTO_TEST_CASE(MultiRunTimerTest)
+{
+
+  Timer::Start("test_timer");
+
+  // On Windows (or, at least, in Windows not using VS2010) we cannot use
+  // usleep() because it is not provided.  Instead we will use Sleep() for a
+  // number of milliseconds.
+  #ifdef _WIN32
+  Sleep(10);
+  #else
+  usleep(10000);
+  #endif
+
+  Timer::Stop("test_timer");
+
+  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 10000);
+
+  // Restart it.
+  Timer::Start("test_timer");
+
+  #ifdef _WIN32
+  Sleep(10);
+  #else
+  usleep(10000);
+  #endif
+
+  Timer::Stop("test_timer");
+
+  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 20000);
+
+  // Just one more time, for good measure...
+  Timer::Start("test_timer");
+
+  #ifdef _WIN32
+  Sleep(20);
+  #else
+  usleep(20000);
+  #endif
+
+  Timer::Stop("test_timer");
+
+  BOOST_REQUIRE_GE(Timer::Get("test_timer").count(), 40000);
+
+}
+
+BOOST_AUTO_TEST_CASE(TwiceStopTimerTest)
+{
+
+  Timer::Start("test_timer");
+  Timer::Stop("test_timer");
+
+  BOOST_REQUIRE_THROW(Timer::Stop("test_timer"), std::runtime_error);
+
+}
+
+BOOST_AUTO_TEST_CASE(TwiceStartTimerTest)
+{
+
+  Timer::Start("test_timer");
+
+  BOOST_REQUIRE_THROW(Timer::Start("test_timer"), std::runtime_error);
+
+}
+
+BOOST_AUTO_TEST_SUITE_END();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list