Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74561 - trunk/libs/chrono/test/io
From: vicente.botet_at_[hidden]
Date: 2011-09-25 07:57:22


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

Log:
Chrono: Added some duration io tests
Added:
   trunk/libs/chrono/test/io/
   trunk/libs/chrono/test/io/duration_input.cpp (contents, props changed)
   trunk/libs/chrono/test/io/duration_output.cpp (contents, props changed)

Added: trunk/libs/chrono/test/io/duration_input.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/chrono/test/io/duration_input.cpp 2011-09-25 07:57:21 EDT (Sun, 25 Sep 2011)
@@ -0,0 +1,76 @@
+// io_ex2.cpp ----------------------------------------------------------//
+
+// Copyright 2010 Howard Hinnant
+// Copyright 2010 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>
+
+template<typename D>
+void test_good(const char* str, D res)
+{
+ std::istringstream in(str);
+ D d(0);
+ in >> d;
+ BOOST_TEST(in.good());
+ BOOST_TEST(d == res);
+}
+
+template<typename DFail, typename DGood>
+void test_fail(const char* str, DFail r, DGood res)
+{
+ {
+ std::istringstream in(str);
+ DFail d = DFail::zero();
+ in >> d;
+ BOOST_TEST(in.fail());
+ BOOST_TEST(d == DFail::zero());
+ }
+ {
+ std::istringstream in(str);
+ DGood d = DGood::zero();
+ in >> d;
+ BOOST_TEST(in.good());
+ BOOST_TEST(d == res);
+ }
+}
+
+int main()
+{
+ using namespace boost::chrono;
+ using namespace boost;
+
+ test_good("5000 hours", hours(5000));
+ test_good("5000 minutes", minutes(5000));
+ test_good("5000 seconds", seconds(5000));
+ test_good("5000 milliseconds", milliseconds(5000));
+ test_good("5000 microseconds", microseconds(5000));
+ test_good("5000 nanoseconds", nanoseconds(5000));
+ test_good("5000 deciseconds", duration<int_least64_t, deci> (5000));
+ test_good("5000 [1/30]seconds", duration<int_least64_t, ratio<1, 30> > (5000));
+
+ test_good("5000 h", hours(5000));
+ test_good("5000 m", minutes(5000));
+ test_good("5000 s", seconds(5000));
+ test_good("5000 ms", milliseconds(5000));
+ test_good("5000 ns", nanoseconds(5000));
+ test_good("5000 ds", duration<int_least64_t, deci> (5000));
+ test_good("5000 [1/30]s", duration<int_least64_t, ratio<1, 30> > (5000));
+
+ test_good("5000 milliseconds", seconds(5));
+ test_good("5 milliseconds", nanoseconds(5000000));
+ test_good("4000 ms", seconds(4));
+ test_fail("3001 ms", seconds(3), milliseconds(3001));
+
+}
+

Added: trunk/libs/chrono/test/io/duration_output.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/chrono/test/io/duration_output.cpp 2011-09-25 07:57:21 EDT (Sun, 25 Sep 2011)
@@ -0,0 +1,72 @@
+// io_ex2.cpp ----------------------------------------------------------//
+
+// Copyright 2010 Howard Hinnant
+// Copyright 2010 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>
+
+template<typename D>
+void test_good_prefix(const char* str, D d)
+{
+ std::ostringstream out;
+ out << d;
+ BOOST_TEST(out.good());
+ BOOST_TEST(out.str() == str);
+}
+
+template<typename D>
+void test_good_symbol(const char* str, D d)
+{
+ std::ostringstream out;
+ out << boost::chrono::duration_short << d;
+ BOOST_TEST(out.good());
+ BOOST_TEST(out.str() == str);
+}
+
+template<typename D>
+void test_good(const char* str, D d, boost::chrono::duration_style::type style)
+{
+ std::ostringstream out;
+ out << boost::chrono::duration_fmt(style) << d;
+ BOOST_TEST(out.good());
+ BOOST_TEST(out.str() == str);
+}
+
+int main()
+{
+ using namespace boost::chrono;
+ using namespace boost;
+
+ test_good("5000 hours", hours(5000), duration_style::prefix_text);
+ test_good("5000 h", hours(5000), duration_style::symbol);
+
+ test_good_prefix("5000 hours", hours(5000));
+ test_good_prefix("5000 minutes", minutes(5000));
+ test_good_prefix("5000 seconds", seconds(5000));
+ test_good_prefix("5000 milliseconds", milliseconds(5000));
+ test_good_prefix("5000 microseconds", microseconds(5000));
+ test_good_prefix("5000 nanoseconds", nanoseconds(5000));
+ test_good_prefix("5000 deciseconds", duration<int_least64_t, deci> (5000));
+ test_good_prefix("5000 [1/30]seconds", duration<int_least64_t, ratio<1, 30> > (5000));
+
+ test_good_symbol("5000 h", hours(5000));
+ test_good_symbol("5000 m", minutes(5000));
+ test_good_symbol("5000 s", seconds(5000));
+ test_good_symbol("5000 ms", milliseconds(5000));
+ test_good_symbol("5000 ns", nanoseconds(5000));
+ 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));
+
+}
+


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