Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65502 - in sandbox/chrono/libs/chrono: example test
From: vicente.botet_at_[hidden]
Date: 2010-09-21 00:56:12


Author: viboes
Date: 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
New Revision: 65502
URL: http://svn.boost.org/trac/boost/changeset/65502

Log:
Added chrono_io examples
Added:
   sandbox/chrono/libs/chrono/example/io_ex1.cpp (contents, props changed)
   sandbox/chrono/libs/chrono/example/io_ex2.cpp (contents, props changed)
   sandbox/chrono/libs/chrono/example/io_ex3.cpp (contents, props changed)
   sandbox/chrono/libs/chrono/example/io_ex4.cpp (contents, props changed)
   sandbox/chrono/libs/chrono/example/io_ex5.cpp (contents, props changed)
Text files modified:
   sandbox/chrono/libs/chrono/test/Jamfile.v2 | 12 ++++++++++++
   1 files changed, 12 insertions(+), 0 deletions(-)

Added: sandbox/chrono/libs/chrono/example/io_ex1.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/chrono/example/io_ex1.cpp 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -0,0 +1,84 @@
+// io_ex1.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 <iostream>
+#include <boost/chrono/chrono_io.hpp>
+
+int main()
+{
+ using namespace std;
+ using namespace boost;
+ using namespace boost::chrono;
+
+ cout << "milliseconds(3) + microseconds(10) = "
+ << milliseconds(3) + microseconds(10) << '\n';
+
+ cout << "hours(3) + minutes(10) = "
+ << hours(3) + minutes(10) << '\n';
+
+ typedef duration<long long, ratio<1, 2500000000> > ClockTick;
+ cout << "ClockTick(3) + nanoseconds(10) = "
+ << ClockTick(3) + nanoseconds(10) << '\n';
+
+ cout << "\nSet cout to use short names:\n";
+ cout << duration_short;
+
+ cout << "milliseconds(3) + microseconds(10) = "
+ << milliseconds(3) + microseconds(10) << '\n';
+
+ cout << "hours(3) + minutes(10) = "
+ << hours(3) + minutes(10) << '\n';
+
+ cout << "ClockTick(3) + nanoseconds(10) = "
+ << ClockTick(3) + nanoseconds(10) << '\n';
+
+ cout << "\nsystem_clock::now() = " << system_clock::now() << '\n';
+#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
+ cout << "monotonic_clock::now() = " << monotonic_clock::now() << '\n';
+#endif
+ cout << "\nSet cout to use long names:\n" << duration_long
+ << "high_resolution_clock::now() = " << high_resolution_clock::now() << '\n';
+ return 0;
+}
+
+//~ milliseconds(3) + microseconds(10) = 3010 microseconds
+//~ hours(3) + minutes(10) = 190 minutes
+//~ ClockTick(3) + nanoseconds(10) = 56 [1/5000000000]seconds
+
+//~ Set cout to use short names:
+//~ milliseconds(3) + microseconds(10) = 3010 µs
+//~ hours(3) + minutes(10) = 190 m
+//~ ClockTick(3) + nanoseconds(10) = 56 [1/5000000000]s
+
+//~ system_clock::now() = 1284923218301231 µs since Jan 1, 1970
+//~ monotonic_clock::now() = 18588963676886 ns since boot
+
+//~ Set cout to use long names:
+//~ high_resolution_clock::now() = 18588963785548 nanoseconds since boot
+
+//~ milliseconds(3) + microseconds(10) = 3010 microseconds
+//~ hours(3) + minutes(10) = 190 minutes
+//~ ClockTick(3) + nanoseconds(10) = 56 [1/5000000000]seconds
+
+//~ Set cout to use short names:
+//~ milliseconds(3) + microseconds(10) = 3010 µs
+//~ hours(3) + minutes(10) = 190 m
+//~ ClockTick(3) + nanoseconds(10) = 56 [1/5000000000]s
+
+//~ system_clock::now() = 1285041215052875000 ns since Jan 1, 1970
+//~ monotonic_clock::now() = 2793 ns since boot
+
+//~ Set cout to use long names:
+//~ high_resolution_clock::now() = 8660 nanoseconds since boot
+

Added: sandbox/chrono/libs/chrono/example/io_ex2.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/chrono/example/io_ex2.cpp 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -0,0 +1,38 @@
+// 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 <cassert>
+
+int main()
+{
+ using namespace std;
+ using namespace boost::chrono;
+
+ istringstream in("5000 milliseconds 4000 ms 3001 ms");
+ seconds d(0);
+ in >> d;
+ assert(in.good());
+ assert(d == seconds(5));
+ in >> d;
+ assert(in.good());
+ assert(d == seconds(4));
+ in >> d;
+ assert(in.fail());
+ assert(d == seconds(4));
+
+ return 0;
+}
+

Added: sandbox/chrono/libs/chrono/example/io_ex3.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/chrono/example/io_ex3.cpp 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -0,0 +1,51 @@
+// io_ex1.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 <iostream>
+#include <cassert>
+
+int main()
+{
+ using namespace std;
+ using namespace boost::chrono;
+
+ high_resolution_clock::time_point t0 = high_resolution_clock::now();
+ stringstream io;
+ io << t0;
+ high_resolution_clock::time_point t1;
+ io >> t1;
+ assert(!io.fail());
+ cout << io.str() << '\n';
+ cout << t0 << '\n';
+ cout << t1 << '\n';
+ high_resolution_clock::time_point t = high_resolution_clock::now();
+ cout << t << '\n';
+
+ cout << "That took " << t - t0 << '\n';
+ cout << "That took " << t - t1 << '\n';
+
+ return 0;
+}
+
+//~ 50908679121461 nanoseconds since boot
+//~ That took 649630 nanoseconds
+
+//~ 2793 nanoseconds since boot
+//~ That took 5410464 nanoseconds
+//~ That took 5454045 nanoseconds
+
+//~ 1285042579381375000 nanoseconds since Jan 1, 1970
+//~ That took 15000000 nanoseconds

Added: sandbox/chrono/libs/chrono/example/io_ex4.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/chrono/example/io_ex4.cpp 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -0,0 +1,32 @@
+// io_ex1.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 <iostream>
+
+int main()
+{
+ using namespace std;
+ using namespace boost;
+ using namespace boost::chrono;
+
+#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
+ typedef time_point<monotonic_clock, duration<double, ratio<3600> > > T;
+ T tp = monotonic_clock::now();
+ std::cout << tp << '\n';
+#endif
+ return 0;
+}
+
+//~ 17.8666 hours since boot

Added: sandbox/chrono/libs/chrono/example/io_ex5.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/chrono/example/io_ex5.cpp 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -0,0 +1,90 @@
+// io_ex1.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 <ostream>
+#include <iostream>
+
+// format duration as [-]d/hh::mm::ss.cc
+template <class CharT, class Traits, class Rep, class Period>
+std::basic_ostream<CharT, Traits>&
+display(std::basic_ostream<CharT, Traits>& os,
+ boost::chrono::duration<Rep, Period> d)
+{
+ using namespace std;
+ using namespace boost;
+ using namespace boost::chrono;
+
+ typedef duration<long long, ratio<86400> > days;
+ typedef duration<long long, centi> centiseconds;
+
+ // if negative, print negative sign and negate
+ if (d < duration<Rep, Period>(0))
+ {
+ d = -d;
+ os << '-';
+ }
+ // round d to nearest centiseconds, to even on tie
+ centiseconds cs = duration_cast<centiseconds>(d);
+ if (d - cs > milliseconds(5)
+ || (d - cs == milliseconds(5) && cs.count() & 1))
+ ++cs;
+ // separate seconds from centiseconds
+ seconds s = duration_cast<seconds>(cs);
+ cs -= s;
+ // separate minutes from seconds
+ minutes m = duration_cast<minutes>(s);
+ s -= m;
+ // separate hours from minutes
+ hours h = duration_cast<hours>(m);
+ m -= h;
+ // separate days from hours
+ days dy = duration_cast<days>(h);
+ h -= dy;
+ // print d/hh:mm:ss.cc
+ os << dy.count() << '/';
+ if (h < hours(10))
+ os << '0';
+ os << h.count() << ':';
+ if (m < minutes(10))
+ os << '0';
+ os << m.count() << ':';
+ if (s < seconds(10))
+ os << '0';
+ os << s.count() << '.';
+ if (cs < centiseconds(10))
+ os << '0';
+ os << cs.count();
+ return os;
+}
+
+int main()
+{
+ using namespace std;
+ using namespace boost;
+ using namespace boost::chrono;
+
+#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
+ display(cout, monotonic_clock::now().time_since_epoch()
+ + duration<long, mega>(1)) << '\n';
+#endif
+ display(cout, -milliseconds(6)) << '\n';
+ display(cout, duration<long, mega>(1)) << '\n';
+ display(cout, -duration<long, mega>(1)) << '\n';
+}
+
+//~ 12/06:03:22.95
+//~ -0/00:00:00.01
+//~ 11/13:46:40.00
+//~ -11/13:46:40.00

Modified: sandbox/chrono/libs/chrono/test/Jamfile.v2
==============================================================================
--- sandbox/chrono/libs/chrono/test/Jamfile.v2 (original)
+++ sandbox/chrono/libs/chrono/test/Jamfile.v2 2010-09-21 00:56:09 EDT (Tue, 21 Sep 2010)
@@ -62,8 +62,11 @@
         [ run manipulate_clock_object.cpp : : : : manipulate_clock_object_dll ]
         [ run chrono_accuracy_test.cpp : : : <link>static ]
         [ run chrono_accuracy_test.cpp : : : : chrono_accuracy_test_dll ]
+ ;
 
 
+ test-suite "examples"
+ :
         [ run ../example/cycle_count.cpp : : : <link>static ]
         [ run ../example/cycle_count.cpp : : : : cycle_count_dll ]
         [ run ../example/runtime_resolution.cpp : : : <link>static ]
@@ -220,3 +223,12 @@
         [ run clock/system/to_time_t.pass.cpp : : : <link>static : clock.system.to_time_t.pass ]
         ;
 
+
+ test-suite "io"
+ :
+ [ run ../example/io_ex1.cpp : : : <link>static ]
+ [ run ../example/io_ex2.cpp : : : <link>static ]
+ [ run ../example/io_ex3.cpp : : : <link>static ]
+ [ run ../example/io_ex4.cpp : : : <link>static ]
+ [ run ../example/io_ex5.cpp : : : <link>static ]
+ ;


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