Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74564 - in trunk/libs/chrono/test: . io
From: vicente.botet_at_[hidden]
Date: 2011-09-25 09:27:10


Author: viboes
Date: 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
New Revision: 74564
URL: http://svn.boost.org/trac/boost/changeset/74564

Log:
Chrono: Added time_point i/o tests for generic clocks and check duration_style_io_saver
Added:
   trunk/libs/chrono/test/io/time_point_input.cpp (contents, props changed)
   trunk/libs/chrono/test/io/time_point_output.cpp (contents, props changed)
Text files modified:
   trunk/libs/chrono/test/Jamfile.v2 | 2 ++
   trunk/libs/chrono/test/io/duration_input.cpp | 14 +++-----------
   trunk/libs/chrono/test/io/duration_output.cpp | 40 +++++++++++++++++++++++++++++-----------
   3 files changed, 34 insertions(+), 22 deletions(-)

Modified: trunk/libs/chrono/test/Jamfile.v2
==============================================================================
--- trunk/libs/chrono/test/Jamfile.v2 (original)
+++ trunk/libs/chrono/test/Jamfile.v2 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
@@ -275,6 +275,8 @@
         :
         [ chrono-run io/duration_input.cpp ]
         [ chrono-run io/duration_output.cpp ]
+ [ chrono-run io/time_point_input.cpp ]
+ [ chrono-run io/time_point_output.cpp ]
         ;
 
     test-suite "win32"

Modified: trunk/libs/chrono/test/io/duration_input.cpp
==============================================================================
--- trunk/libs/chrono/test/io/duration_input.cpp (original)
+++ trunk/libs/chrono/test/io/duration_input.cpp 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
@@ -1,17 +1,7 @@
-// io_ex2.cpp ----------------------------------------------------------//
-
-// Copyright 2010 Howard Hinnant
-// Copyright 2010 Vicente J. Botet Escriba
-
+// Copyright 2011 Vicente J. Botet Escriba
 // Distributed under the Boost Software License, Version 1.0.
 // See http://www.boost.org/LICENSE_1_0.txt
 
-/*
- This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation.
- Many thanks to Howard for making his code available under the Boost license.
-
- */
-
 #include <boost/chrono/chrono_io.hpp>
 #include <sstream>
 #include <boost/detail/lightweight_test.hpp>
@@ -53,6 +43,8 @@
   test_good("5000 hours", hours(5000));
   test_good("5000 minutes", minutes(5000));
   test_good("5000 seconds", seconds(5000));
+ test_good("1 seconds", seconds(1));
+ test_good("-1 seconds", seconds(-1));
   test_good("5000 milliseconds", milliseconds(5000));
   test_good("5000 microseconds", microseconds(5000));
   test_good("5000 nanoseconds", nanoseconds(5000));

Modified: trunk/libs/chrono/test/io/duration_output.cpp
==============================================================================
--- trunk/libs/chrono/test/io/duration_output.cpp (original)
+++ trunk/libs/chrono/test/io/duration_output.cpp 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
@@ -1,17 +1,7 @@
-// io_ex2.cpp ----------------------------------------------------------//
-
-// Copyright 2010 Howard Hinnant
-// Copyright 2010 Vicente J. Botet Escriba
-
+// Copyright 2011 Vicente J. Botet Escriba
 // Distributed under the Boost Software License, Version 1.0.
 // See http://www.boost.org/LICENSE_1_0.txt
 
-/*
- This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation.
- Many thanks to Howard for making his code available under the Boost license.
-
- */
-
 #include <boost/chrono/chrono_io.hpp>
 #include <sstream>
 #include <boost/detail/lightweight_test.hpp>
@@ -43,6 +33,30 @@
   BOOST_TEST(out.str() == str);
 }
 
+template<typename D>
+void test_state_saver(const char* str, const char* str2, D d, boost::chrono::duration_style::type style)
+{
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ std::ostringstream out;
+ {
+ boost::chrono::duration_style_io_saver<> ios(out);
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ out << boost::chrono::duration_fmt(style) << d;
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ BOOST_TEST(out.good());
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ BOOST_TEST(out.str() == str);
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ }
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ out << " " << d;
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ BOOST_TEST(out.good());
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+ BOOST_TEST(out.str() == str2);
+ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
+}
+
 int main()
 {
   using namespace boost::chrono;
@@ -54,6 +68,8 @@
   test_good_prefix("5000 hours", hours(5000));
   test_good_prefix("5000 minutes", minutes(5000));
   test_good_prefix("5000 seconds", seconds(5000));
+ test_good_prefix("1 seconds", seconds(1));
+ test_good_prefix("-1 seconds", seconds(-1));
   test_good_prefix("5000 milliseconds", milliseconds(5000));
   test_good_prefix("5000 microseconds", microseconds(5000));
   test_good_prefix("5000 nanoseconds", nanoseconds(5000));
@@ -68,5 +84,7 @@
   test_good_symbol("5000 ds", duration<int_least64_t, deci> (5000));
   test_good_symbol("5000 [1/30]s", duration<int_least64_t, ratio<1, 30> > (5000));
 
+ test_state_saver("5000 h", "5000 h 5000 hours", hours(5000), duration_style::symbol);
+
 }
 

Added: trunk/libs/chrono/test/io/time_point_input.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/chrono/test/io/time_point_input.cpp 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
@@ -0,0 +1,110 @@
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+
+#include <boost/chrono/chrono_io.hpp>
+#include <sstream>
+#include <boost/detail/lightweight_test.hpp>
+
+template<typename Clock, typename D>
+void test_good(std::string str, D res)
+{
+ std::istringstream in(str+boost::chrono::clock_string<Clock,char>::since());
+ boost::chrono::time_point<Clock,D> tp;
+ in >> tp;
+ BOOST_TEST(in.good());
+ BOOST_TEST((tp == boost::chrono::time_point<Clock,D>(res)));
+}
+
+template<typename Clock, typename D>
+void test_fail(const char* str, D r)
+{
+ std::istringstream in(str+boost::chrono::clock_string<Clock,char>::since());
+ boost::chrono::time_point<Clock,D> tp;
+ in >> tp;
+ BOOST_TEST(in.fail());
+ BOOST_TEST((tp == boost::chrono::time_point<Clock,D>()));
+}
+
+template<typename Clock, typename D>
+void test_fail_no_epoch(const char* str, D r)
+{
+ std::istringstream in(str);
+ boost::chrono::time_point<Clock,D> tp;
+ in >> tp;
+ BOOST_TEST(in.fail());
+ BOOST_TEST((tp == boost::chrono::time_point<Clock,D>()));
+}
+
+template<typename Clock, typename D>
+void test_fail_epoch(const char* str, D r)
+{
+ std::istringstream in(str);
+ boost::chrono::time_point<Clock,D> tp;
+ in >> tp;
+ BOOST_TEST(in.fail());
+ BOOST_TEST((tp == boost::chrono::time_point<Clock,D>()));
+}
+
+template<typename Clock>
+void check_all()
+{
+ using namespace boost::chrono;
+ using namespace boost;
+
+ test_good<Clock>("5000 hours", hours(5000));
+ test_good<Clock>("5000 minutes", minutes(5000));
+ test_good<Clock>("5000 seconds", seconds(5000));
+ test_good<Clock>("1 seconds", seconds(1));
+ test_good<Clock>("-1 seconds", seconds(-1));
+ test_good<Clock>("5000 milliseconds", milliseconds(5000));
+ test_good<Clock>("5000 microseconds", microseconds(5000));
+ test_good<Clock>("5000 nanoseconds", nanoseconds(5000));
+ test_good<Clock>("5000 deciseconds", duration<int_least64_t, deci> (5000));
+ test_good<Clock>("5000 [1/30]seconds", duration<int_least64_t, ratio<1, 30> > (5000));
+
+ test_good<Clock>("5000 h", hours(5000));
+ test_good<Clock>("5000 m", minutes(5000));
+ test_good<Clock>("5000 s", seconds(5000));
+ test_good<Clock>("5000 ms", milliseconds(5000));
+ test_good<Clock>("5000 ns", nanoseconds(5000));
+ test_good<Clock>("5000 ds", duration<int_least64_t, deci> (5000));
+ test_good<Clock>("5000 [1/30]s", duration<int_least64_t, ratio<1, 30> > (5000));
+
+ test_good<Clock>("5000 milliseconds", seconds(5));
+ test_good<Clock>("5 milliseconds", nanoseconds(5000000));
+ test_good<Clock>("4000 ms", seconds(4));
+ test_fail<Clock>("3001 ms", seconds(3));
+ test_fail_epoch<Clock>("3001 ms", seconds(3));
+ test_fail_epoch<Clock>("3001 ms since", seconds(3));
+
+}
+
+int main()
+{
+ std::cout << "high_resolution_clock=";
+ check_all<boost::chrono::high_resolution_clock>();
+#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
+ std::cout << "steady_clock=";
+ check_all<boost::chrono::steady_clock>();
+#endif
+ //std::cout << "system_clock=";
+ //check_all<boost::chrono::system_clock>();
+
+#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
+ std::cout << "thread_clock=";
+ check_all<boost::chrono::thread_clock>();
+#endif
+
+#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
+ std::cout << "process_real_cpu_clock=";
+ check_all<boost::chrono::process_real_cpu_clock>();
+ std::cout << "process_user_cpu_clock=";
+ check_all<boost::chrono::process_user_cpu_clock>();
+ std::cout << "process_system_cpu_clock=";
+ check_all<boost::chrono::process_system_cpu_clock>();
+ std::cout << "process_cpu_clock=";
+ check_all<boost::chrono::process_cpu_clock>();
+#endif
+}
+

Added: trunk/libs/chrono/test/io/time_point_output.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/chrono/test/io/time_point_output.cpp 2011-09-25 09:27:09 EDT (Sun, 25 Sep 2011)
@@ -0,0 +1,96 @@
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+
+#include <boost/chrono/chrono_io.hpp>
+#include <sstream>
+#include <boost/detail/lightweight_test.hpp>
+
+template<typename Clock, typename D>
+void test_good_prefix(const char* str, D d)
+{
+ std::ostringstream out;
+ boost::chrono::time_point<Clock,D> tp(d);
+ out << tp;
+ BOOST_TEST(out.good());
+ BOOST_TEST((out.str() == std::string(str)+boost::chrono::clock_string<Clock,char>::since()));
+}
+
+template<typename Clock, typename D>
+void test_good_symbol(const char* str, D d)
+{
+ std::ostringstream out;
+ boost::chrono::time_point<Clock,D> tp(d);
+ out << boost::chrono::duration_short << tp;
+ BOOST_TEST(out.good());
+ BOOST_TEST((out.str() == std::string(str)+boost::chrono::clock_string<Clock,char>::since()));
+}
+
+template<typename Clock, typename D>
+void test_good(const char* str, D d, boost::chrono::duration_style::type style)
+{
+ std::ostringstream out;
+ boost::chrono::time_point<Clock,D> tp(d);
+ out << boost::chrono::duration_fmt(style) << tp;
+ BOOST_TEST(out.good());
+ BOOST_TEST((out.str() == std::string(str)+boost::chrono::clock_string<Clock,char>::since()));
+}
+
+template<typename Clock>
+void check_all()
+{
+ using namespace boost::chrono;
+ using namespace boost;
+
+ test_good<Clock>("2 hours", hours(2), duration_style::prefix_text);
+ test_good<Clock>("2 h", hours(2), duration_style::symbol);
+
+ test_good_prefix<Clock>("2 hours", hours(2));
+ test_good_prefix<Clock>("2 minutes", minutes(2));
+ test_good_prefix<Clock>("2 seconds", seconds(2));
+ test_good_prefix<Clock>("1 seconds", seconds(1));
+ test_good_prefix<Clock>("-1 seconds", seconds(-1));
+ test_good_prefix<Clock>("2 milliseconds", milliseconds(2));
+ test_good_prefix<Clock>("2 microseconds", microseconds(2));
+ test_good_prefix<Clock>("2 nanoseconds", nanoseconds(2));
+ test_good_prefix<Clock>("2 deciseconds", duration<int_least64_t, deci> (2));
+ test_good_prefix<Clock>("2 [1/30]seconds", duration<int_least64_t, ratio<1, 30> > (2));
+
+ test_good_symbol<Clock>("2 h", hours(2));
+ test_good_symbol<Clock>("2 m", minutes(2));
+ test_good_symbol<Clock>("2 s", seconds(2));
+ test_good_symbol<Clock>("2 ms", milliseconds(2));
+ test_good_symbol<Clock>("2 ns", nanoseconds(2));
+ test_good_symbol<Clock>("2 ds", duration<int_least64_t, deci> (2));
+ test_good_symbol<Clock>("2 [1/30]s", duration<int_least64_t, ratio<1, 30> > (2));
+}
+
+int main()
+{
+
+ std::cout << "high_resolution_clock=";
+ check_all<boost::chrono::high_resolution_clock>();
+#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
+ std::cout << "steady_clock=";
+ check_all<boost::chrono::steady_clock>();
+#endif
+ //std::cout << "system_clock=";
+ //check_all<boost::chrono::system_clock>();
+
+#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
+ std::cout << "thread_clock=";
+ check_all<boost::chrono::thread_clock>();
+#endif
+
+#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
+ std::cout << "process_real_cpu_clock=";
+ check_all<boost::chrono::process_real_cpu_clock>();
+ std::cout << "process_user_cpu_clock=";
+ check_all<boost::chrono::process_user_cpu_clock>();
+ std::cout << "process_system_cpu_clock=";
+ check_all<boost::chrono::process_system_cpu_clock>();
+ std::cout << "process_cpu_clock=";
+ check_all<boost::chrono::process_cpu_clock>();
+#endif
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk