Boost.Chrono: Moved to trunk

[SVN r67698]
This commit is contained in:
Vicente J. Botet Escriba 2011-01-06 00:47:42 +00:00
parent f5eb279eee
commit c183fb72fd
89 changed files with 16342 additions and 0 deletions

91
build/Jamfile.v2 Normal file
View File

@ -0,0 +1,91 @@
# Boost Chrono Library Build Jamfile
# Copyright Beman Dawes 2002, 2006, 2008
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or www.boost.org/LICENSE_1_0.txt)
# See library home page at http://www.boost.org/libs/chrono
# uncomment one if the above lines if you build outside the Boost release
#local BOOST_ROOT = /boost_1_41_0 ;
#local BOOST_ROOT = c:/cygwin/boost_1_41_0 ;
if ! $(BOOST_ROOT)
{
BOOST_ROOT = [ modules.peek : BOOST_ROOT ] ;
}
project boost/chrono
: source-location ../src
: requirements
<os>LINUX:<threading>multi
# uncomment the line above if you build outside the Boost release
#<include>$(BOOST_ROOT)
# uncomment the line above if you build outside the Boost release
#<include>../../..
<library>/boost/system//boost_system
#<define>BOOST_SYSTEM_INLINED
<define>BOOST_USE_WINDOWS_H
#<define>BOOST_COMMON_TYPE_USES_STATIC_ASSERT
#<define>BOOST_RATIO_USES_STATIC_ASSERT
#<define>BOOST_CHRONO_USES_STATIC_ASSERT
#<define>BOOST_COMMON_TYPE_USES_MPL_ASSERT
#<define>BOOST_RATIO_USES_MPL_ASSERT
#<define>BOOST_CHRONO_USES_MPL_ASSERT
#<define>BOOST_COMMON_TYPE_USES_ARRAY_ASSERT
#<define>BOOST_RATIO_USES_ARRAY_ASSERT
#<define>BOOST_CHRONO_USES_ARRAY_ASSERT
#<define>BOOST_COMMON_TYPE_DONT_USE_TYPEOF
<define>BOOST_SYSTEM_NO_DEPRECATED
<toolset>gcc-3.4.4:<linkflags>--enable-auto-import
<toolset>gcc-4.3.4:<linkflags>--enable-auto-import
<toolset>gcc-mingw-4.4.0:<linkflags>--enable-auto-import
<toolset>gcc-mingw-4.5.0:<linkflags>--enable-auto-import
<warnings>all
<toolset>gcc:<cxxflags>-Wextra
<toolset>gcc:<cxxflags>-pedantic
<toolset>gcc:<cxxflags>-Wno-long-long
<toolset>darwin:<cxxflags>-Wextra
<toolset>darwin:<cxxflags>-pedantic
<toolset>darwin:<cxxflags>-Wno-long-long
<toolset>gcc-mingw-4.4.0:<cxxflags>-fdiagnostics-show-option
<toolset>gcc-mingw-4.5.0:<cxxflags>-fdiagnostics-show-option
<toolset>gcc-mingw-4.6.0:<cxxflags>-fdiagnostics-show-option
: usage-requirements # pass these requirement to dependents (i.e. users)
#<os>LINUX:<threading>multi
<library>/boost/system//boost_system
#<define>BOOST_SYSTEM_INLINED
#<define>BOOST_COMMON_TYPE_USES_STATIC_ASSERT
#<define>BOOST_RATIO_USES_STATIC_ASSERT
#<define>BOOST_CHRONO_USES_STATIC_ASSERT
#<define>BOOST_COMMON_TYPE_USES_MPL_ASSERT
#<define>BOOST_RATIO_USES_MPL_ASSERT
#<define>BOOST_CHRONO_USES_MPL_ASSERT
#<define>BOOST_COMMON_TYPE_USES_ARRAY_ASSERT
#<define>BOOST_RATIO_USES_ARRAY_ASSERT
#<define>BOOST_CHRONO_USES_ARRAY_ASSERT
#<define>BOOST_COMMON_TYPE_DONT_USE_TYPEOF
<define>BOOST_SYSTEM_NO_DEPRECATED
<link>shared:<define>BOOST_CHRONO_DYN_LINK=1
<link>static:<define>BOOST_CHRONO_STATIC_LINK=1
<toolset>gcc-3.4.4:<linkflags>--enable-auto-import
<toolset>gcc-4.3.4:<linkflags>--enable-auto-import
<toolset>gcc-mingw-4.4.0:<linkflags>--enable-auto-import
<toolset>gcc-mingw-4.5.0:<linkflags>--enable-auto-import
;
SOURCES = chrono thread_clock process_cpu_clocks ;
#process_clock run_timer run_timer_static
lib boost_chrono
: $(SOURCES).cpp
#$(BOOST_ROOT)/libs/system/build//boost_system
:
<link>shared:<define>BOOST_ALL_DYN_LINK=1 # tell source we're building dll's
<link>static:<define>BOOST_All_STATIC_LINK=1 # tell source we're building static lib's
;
#boost-install boost_chrono ;

74
doc/Jamfile.v2 Normal file
View File

@ -0,0 +1,74 @@
# Boost.Chrono library documentation Jamfile ---------------------------------
#
# Copyright Vicente J. Botet Escriba 2009. Use, modification and
# distribution is subject to the Boost Software License, Version
# 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
# See http://www.boost.org for updates, documentation, and revision history.
#import doxygen ;
using quickbook ;
#doxygen autodoc
# :
# [ glob ../../../boost/chrono/*.hpp ]
# [ glob ../../../boost/chrono/allocators/*.hpp ]
# :
# <doxygen:param>EXTRACT_ALL=NO
# <doxygen:param>HIDE_UNDOC_MEMBERS=YES
# <doxygen:param>EXTRACT_PRIVATE=NO
# <doxygen:param>EXPAND_ONLY_PREDEF=YES
# <doxygen:param>PREDEFINED=BOOST_INTERPROCESS_DOXYGEN_INVOKED
# <xsl:param>"boost.doxygen.reftitle=Boost.Chrono Reference"
# ;
xml chrono : chrono.qbk ;
boostbook standalone
:
chrono
:
# HTML options first:
# Use graphics not text for navigation:
<xsl:param>navig.graphics=1
# How far down we chunk nested sections, basically all of them:
<xsl:param>chunk.section.depth=2
# Don't put the first section on the same page as the TOC:
<xsl:param>chunk.first.sections=1
# How far down sections get TOC's
<xsl:param>toc.section.depth=4
# Max depth in each TOC:
<xsl:param>toc.max.depth=2
# How far down we go with TOC's
<xsl:param>generate.section.toc.level=10
# Path for links to Boost:
<xsl:param>boost.root=../../../..
# Path for libraries index:
<xsl:param>boost.libraries=../../../../libs/libraries.htm
# Use the main Boost stylesheet:
<xsl:param>html.stylesheet=../../../../doc/src/boostbook.css
# PDF Options:
# TOC Generation: this is needed for FOP-0.9 and later:
#<xsl:param>fop1.extensions=0
# Or enable this if you're using XEP:
<xsl:param>xep.extensions=1
# TOC generation: this is needed for FOP 0.2, but must not be set to zero for FOP-0.9!
<xsl:param>fop.extensions=0
# No indent on body text:
<xsl:param>body.start.indent=0pt
# Margin size:
<xsl:param>page.margin.inner=0.5in
# Margin size:
<xsl:param>page.margin.outer=0.5in
# Yes, we want graphics for admonishments:
<xsl:param>admon.graphics=1
# Set this one for PDF generation *only*:
# default pnd graphics are awful in PDF form,
# better use SVG's instead:
<format>pdf:<xsl:param>admon.graphics.extension=".svg"
<format>pdf:<xsl:param>admon.graphics.path=$(boost-images)/
;
install pdf-install : standalone : <location>. <install-type>PDF ;

4507
doc/chrono.qbk Normal file

File diff suppressed because it is too large Load Diff

4040
doc/time2_demo.html Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,73 @@
// await_keystroke.cpp -----------------------------------------------------//
// Copyright Beman Dawes 2008
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#define _CRT_SECURE_NO_WARNINGS
#include <boost/chrono/chrono.hpp>
#include <iostream>
#include <iomanip>
using namespace boost::chrono;
template< class Clock >
class timer
{
typename Clock::time_point start;
public:
timer() : start( Clock::now() ) {}
typename Clock::duration elapsed() const
{
return Clock::now() - start;
}
double seconds() const
{
return elapsed().count() * ((double)Clock::period::num/Clock::period::den);
}
};
int main()
{
timer<system_clock> t1;
timer<monotonic_clock> t2;
timer<high_resolution_clock> t3;
std::cout << "Strike any key: ";
std::cin.get();
std::cout << std::fixed << std::setprecision(9);
std::cout << "system_clock-----------: "
<< t1.seconds() << " seconds\n";
std::cout << "monotonic_clock--------: "
<< t2.seconds() << " seconds\n";
std::cout << "high_resolution_clock--: "
<< t3.seconds() << " seconds\n";
system_clock::time_point d4 = system_clock::now();
system_clock::time_point d5 = system_clock::now();
std::cout << "\nsystem_clock latency-----------: " << (d5 - d4).count() << std::endl;
monotonic_clock::time_point d6 = monotonic_clock::now();
monotonic_clock::time_point d7 = monotonic_clock::now();
std::cout << "monotonic_clock latency--------: " << (d7 - d6).count() << std::endl;
high_resolution_clock::time_point d8 = high_resolution_clock::now();
high_resolution_clock::time_point d9 = high_resolution_clock::now();
std::cout << "high_resolution_clock latency--: " << (d9 - d8).count() << std::endl;
std::time_t now = system_clock::to_time_t(system_clock::now());
std::cout << "\nsystem_clock::now() reports UTC is "
<< std::asctime(std::gmtime(&now)) << "\n";
return 0;
}

View File

@ -0,0 +1,102 @@
// boost run_timer_test.cpp -----------------------------------------------------//
// Copyright Beman Dawes 2006, 2008
// Copyright 2009 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono/timer.hpp>
#include <cstdlib> // for atol()
#include <iostream>
#include <sstream>
#include <locale>
#include <ctime>
#include <cmath> // for sqrt(), used to burn time
//using boost::chrono::run_timer;
using boost::system::error_code;
#include <boost/detail/lightweight_test.hpp>
namespace
{
typedef boost::chrono::nanoseconds ns;
// accuracy test
void accuracy_test( int argc, char * argv[] )
{
long timeout_in_secs = 1;
if ( argc > 1 ) timeout_in_secs = std::atol( argv[1] );
std::cout << "accuracy test for " << timeout_in_secs << " second(s)...";
std::clock_t timeout_in_clock_t = std::clock();
timeout_in_clock_t += (timeout_in_secs * CLOCKS_PER_SEC);
boost::chrono::system_timer sys;
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
boost::chrono::monotonic_timer mono;
#endif
boost::chrono::high_resolution_timer hires;
boost::chrono::timer<boost::chrono::process_cpu_clock> process;
std::clock_t now;
do
{
now = std::clock();
} while ( now < timeout_in_clock_t );
boost::chrono::system_timer::duration sys_dur = sys.elapsed();
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
boost::chrono::monotonic_timer::duration mono_dur = mono.elapsed();
#endif
boost::chrono::high_resolution_timer::duration hires_dur = hires.elapsed();
boost::chrono::process_cpu_clock::duration times;
times = process.elapsed();
std::cout << std::endl;
ns timeout_in_nanoseconds( static_cast<long long>(timeout_in_secs) * 1000000000LL );
// Allow 20% leeway. Particularly on Linux, there seems to be a large discrepancy
// between std::clock() and higher resolution clocks.
ns maximum_delta ( static_cast<long long>(timeout_in_nanoseconds.count() * 0.20 ) );
std::cout << timeout_in_nanoseconds.count() << " timeout_in_nanoseconds\n";
std::cout << maximum_delta.count() << " maximum_delta\n";
std::cout << sys_dur.count() << " sys_dur\n";
BOOST_TEST( sys_dur > timeout_in_nanoseconds - maximum_delta
&& sys_dur < timeout_in_nanoseconds + maximum_delta );
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
std::cout << mono_dur.count() << " mono_dur\n";
BOOST_TEST( mono_dur > timeout_in_nanoseconds - maximum_delta
&& mono_dur < timeout_in_nanoseconds + maximum_delta );
#endif
std::cout << hires_dur.count() << " hires_dur\n";
BOOST_TEST( hires_dur > timeout_in_nanoseconds - maximum_delta
&& hires_dur < timeout_in_nanoseconds + maximum_delta );
std::cout << times.count().real << " times.real\n";
// BOOST_TEST( times.count().real > timeout_in_nanoseconds - maximum_delta
// && times.count().real < timeout_in_nanoseconds + maximum_delta );
}
}
int main( int argc, char * argv[] )
{
accuracy_test( argc, argv );
return boost::report_errors();
}

View File

@ -0,0 +1,31 @@
// chrono_unit_test.cpp ----------------------------------------------------//
// Copyright 2008 Beman Dawes
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
#include <boost/chrono/chrono.hpp>
#include <iostream>
int main()
{
boost::chrono::nanoseconds nanosecs;
boost::chrono::microseconds microsecs;
boost::chrono::milliseconds millisecs;
boost::chrono::seconds secs;
boost::chrono::minutes mins;
boost::chrono::hours hrs;
std::time_t sys_time
= boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now());
std::cout
<< "system_clock::to_time_t(system_clock::now()) is "
<< std::asctime(std::gmtime(&sys_time)) << std::endl;
return 0;
}

20
example/clock_name.cpp Normal file
View File

@ -0,0 +1,20 @@
// stopclock_perf.cpp ---------------------------------------------------//
// Copyright 2009 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#include "clock_name.hpp"
#include <iostream>
int main()
{
std::cout << name<boost::chrono::system_clock>::apply() << '\n';
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
std::cout << name<boost::chrono::monotonic_clock>::apply() << '\n';
#endif
std::cout << name<boost::chrono::high_resolution_clock>::apply() << '\n';
}

68
example/clock_name.hpp Normal file
View File

@ -0,0 +1,68 @@
// stopclock_perf.cpp ---------------------------------------------------//
// Copyright 2009 Vicente J. Botet Escriba
// Copyright 2009 Howard Hinnant
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#ifndef BOOST_CHRONO_CLOCK_NAME_HPP
#define BOOST_CHRONO_CLOCK_NAME_HPP
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits/is_same.hpp>
template <typename Clock,
bool = boost::is_same<Clock, boost::chrono::system_clock>::value,
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
bool = boost::is_same<Clock, boost::chrono::monotonic_clock>::value,
#else
bool = false,
#endif
bool = boost::is_same<Clock, boost::chrono::high_resolution_clock>::value
>
struct name;
template <typename Clock>
struct name<Clock, false, false, false> {
static const char* apply() { return "unknown clock";}
};
template <typename Clock>
struct name<Clock, true, false, false> {
static const char* apply() { return "system_clock";}
};
template <typename Clock>
struct name<Clock, false, true, false> {
static const char* apply() { return "monotonic_clock";}
};
template <typename Clock>
struct name<Clock, false, false, true> {
static const char* apply() { return "high_resolution_clock";}
};
template <typename Clock>
struct name<Clock, false, true, true> {
static const char* apply() { return "monotonic_clock and high_resolution_clock";}
};
template <typename Clock>
struct name<Clock, true, false, true> {
static const char* apply() { return "system_clock and high_resolution_clock";}
};
template <typename Clock>
struct name<Clock, true, true, false> {
static const char* apply() { return "system_clock and monotonic_clock";}
};
template <typename Clock>
struct name<Clock, true, true, true> {
static const char* apply() { return "system_clock, monotonic_clock and high_resolution_clock";}
};
#endif

140
example/cycle_count.cpp Normal file
View File

@ -0,0 +1,140 @@
// cycle_count.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
template <long long speed>
struct cycle_count
{
typedef typename boost::ratio_multiply<boost::ratio<speed>, boost::mega>::type frequency; // Mhz
typedef typename boost::ratio_divide<boost::ratio<1>, frequency>::type period;
typedef long long rep;
typedef boost::chrono::duration<rep, period> duration;
typedef boost::chrono::time_point<cycle_count> time_point;
static time_point now()
{
static long long tick = 0;
// return exact cycle count
return time_point(duration(++tick)); // fake access to clock cycle count
}
};
template <long long speed>
struct approx_cycle_count
{
static const long long frequency = speed * 1000000; // MHz
typedef nanoseconds duration;
typedef duration::rep rep;
typedef duration::period period;
static const long long nanosec_per_sec = period::den;
typedef boost::chrono::time_point<approx_cycle_count> time_point;
static time_point now()
{
static long long tick = 0;
// return cycle count as an approximate number of nanoseconds
// compute as if nanoseconds is only duration in the std::lib
return time_point(duration(++tick * nanosec_per_sec / frequency));
}
};
void cycle_count_delay()
{
{
typedef cycle_count<400> clock;
std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of "
<< duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n";
nanoseconds delayns(500);
clock::duration delay = duration_cast<clock::duration>(delayns);
std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) // no multiplies or divides in this loop
;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " cycles ";
std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n";
}
{
typedef approx_cycle_count<400> clock;
std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n";
clock::duration delay = nanoseconds(500);
std::cout << "delay = " << delay.count() << " nanoseconds\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) // 1 multiplication and 1 division in this loop
;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " nanoseconds\n";
}
{
typedef cycle_count<1500> clock;
std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of "
<< duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n";
nanoseconds delayns(500);
clock::duration delay = duration_cast<clock::duration>(delayns);
std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) // no multiplies or divides in this loop
;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " cycles ";
std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n";
}
{
typedef approx_cycle_count<1500> clock;
std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n";
clock::duration delay = nanoseconds(500);
std::cout << "delay = " << delay.count() << " nanoseconds\n";
clock::time_point start = clock::now();
clock::time_point stop = start + delay;
while (clock::now() < stop) // 1 multiplication and 1 division in this loop
;
clock::time_point end = clock::now();
clock::duration elapsed = end - start;
std::cout << "paused " << elapsed.count() << " nanoseconds\n";
}
}
int main()
{
cycle_count_delay();
return 0;
}

View File

@ -0,0 +1,60 @@
// explore_limits.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
void explore_limits()
{
typedef duration<long long, boost::ratio_multiply<boost::ratio<24*3652425,10000>,
hours::period>::type> Years;
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
monotonic_clock::time_point t1( Years(250));
monotonic_clock::time_point t2(-Years(250));
#else
system_clock::time_point t1( Years(250));
system_clock::time_point t2(-Years(250));
#endif
// nanosecond resolution is likely to overflow. "up cast" to microseconds.
// The "up cast" trades precision for range.
microseconds d = time_point_cast<microseconds>(t1) - time_point_cast<microseconds>(t2);
std::cout << d.count() << " microseconds\n";
}
int main()
{
explore_limits();
return 0;
}

View File

@ -0,0 +1,97 @@
// i_dont_like_the_default_duration_behavior.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
namespace I_dont_like_the_default_duration_behavior
{
// Here's how you override the duration's default constructor to do anything you want (in this case zero)
template <class R>
class zero_default
{
public:
typedef R rep;
private:
rep rep_;
public:
zero_default(rep i = 0) : rep_(i) {}
operator rep() const {return rep_;}
zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;}
zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;}
zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;}
zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;}
zero_default operator+ () const {return *this;}
zero_default operator- () const {return zero_default(-rep_);}
zero_default& operator++() {++rep_; return *this;}
zero_default operator++(int) {return zero_default(rep_++);}
zero_default& operator--() {--rep_; return *this;}
zero_default operator--(int) {return zero_default(rep_--);}
friend zero_default operator+(zero_default x, zero_default y) {return x += y;}
friend zero_default operator-(zero_default x, zero_default y) {return x -= y;}
friend zero_default operator*(zero_default x, zero_default y) {return x *= y;}
friend zero_default operator/(zero_default x, zero_default y) {return x /= y;}
friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;}
friend bool operator!=(zero_default x, zero_default y) {return !(x == y);}
friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;}
friend bool operator<=(zero_default x, zero_default y) {return !(y < x);}
friend bool operator> (zero_default x, zero_default y) {return y < x;}
friend bool operator>=(zero_default x, zero_default y) {return !(x < y);}
};
typedef boost::chrono::duration<zero_default<long long>, boost::nano > nanoseconds;
typedef boost::chrono::duration<zero_default<long long>, boost::micro > microseconds;
typedef boost::chrono::duration<zero_default<long long>, boost::milli > milliseconds;
typedef boost::chrono::duration<zero_default<long long> > seconds;
typedef boost::chrono::duration<zero_default<long long>, boost::ratio<60> > minutes;
typedef boost::chrono::duration<zero_default<long long>, boost::ratio<3600> > hours;
void test()
{
milliseconds ms;
std::cout << ms.count() << '\n';
}
} // I_dont_like_the_default_duration_behavior
int main()
{
I_dont_like_the_default_duration_behavior::test();
return 0;
}

61
example/io_ex1.cpp Normal file
View File

@ -0,0 +1,61 @@
// 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>
#include <boost/chrono/thread_clock.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, 2500000000ULL> > 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';
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
cout << "\nthread_clock::now() = " << thread_clock::now() << '\n';
#endif
cout << "\nprocess_real_cpu_clock::now() = " << process_real_cpu_clock::now() << '\n';
cout << "\nprocess_user_cpu_clock::now() = " << process_user_cpu_clock::now() << '\n';
cout << "\nprocess_system_cpu_clock::now() = " << process_system_cpu_clock::now() << '\n';
cout << "\nprocess_cpu_clock::now() = " << process_cpu_clock::now() << '\n';
return 0;
}

39
example/io_ex2.cpp Normal file
View File

@ -0,0 +1,39 @@
// 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/assert.hpp>
int main()
{
using namespace std;
using namespace boost::chrono;
#ifdef BOOST_CHRONO_IO_INPUT
istringstream in("5000 milliseconds 4000 ms 3001 ms");
seconds d(0);
in >> d;
BOOST_ASSERT(in.good());
BOOST_ASSERT(d == seconds(5));
in >> d;
BOOST_ASSERT(in.good());
BOOST_ASSERT(d == seconds(4));
in >> d;
BOOST_ASSERT(in.fail());
BOOST_ASSERT(d == seconds(4));
#endif
return 0;
}

49
example/io_ex3.cpp Normal file
View File

@ -0,0 +1,49 @@
// 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 <boost/assert.hpp>
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;
#ifdef BOOST_CHRONO_IO_INPUT
io >> t1;
BOOST_ASSERT(!io.fail());
cout << io.str() << '\n';
#else
t1=t0;
#endif
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

32
example/io_ex4.cpp Normal file
View File

@ -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

90
example/io_ex5.cpp Normal file
View File

@ -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

View File

@ -0,0 +1,61 @@
// manipulate_clock_object.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
#if defined _MSC_VER
#pragma warning(push)
#pragma warning(disable:4100)
#endif
void manipulate_clock_object(system_clock clock)
#if defined _MSC_VER
#pragma warning(pop)
#endif
{
system_clock::duration delay = milliseconds(5);
system_clock::time_point start = clock.now();
while ((clock.now() - start) <= delay) {}
system_clock::time_point stop = clock.now();
system_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
}
int main()
{
manipulate_clock_object(system_clock());
return 0;
}

View File

@ -0,0 +1,78 @@
// min_time_point.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/typeof/boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
template <class Rep, class Period>
void print_duration(std::ostream& os, duration<Rep, Period> d)
{
os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n";
}
// Example min utility: returns the earliest time_point
// Being able to *easily* write this function is a major feature!
template <class Clock, class Duration1, class Duration2>
inline
typename boost::common_type<time_point<Clock, Duration1>,
time_point<Clock, Duration2> >::type
min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point<Clock, Duration1> t1, time_point<Clock, Duration2> t2)
{
return t2 < t1 ? t2 : t1;
}
void test_min()
{
#if 0
typedef time_point<system_clock,
boost::common_type<system_clock::duration, seconds>::type> T1;
typedef time_point<system_clock,
boost::common_type<system_clock::duration, nanoseconds>::type> T2;
typedef boost::common_type<T1, T2>::type T3;
/*auto*/ T1 t1 = system_clock::now() + seconds(3);
/*auto*/ T2 t2 = system_clock::now() + nanoseconds(3);
/*auto*/ T3 t3 = (min)(t1, t2);
#else
BOOST_AUTO(t1, system_clock::now() + seconds(3));
BOOST_AUTO(t2, system_clock::now() + nanoseconds(3));
BOOST_AUTO(t3, (min)(t1, t2));
#endif
print_duration(std::cout, t1 - t3);
print_duration(std::cout, t2 - t3);
}
int main()
{
test_min();
return 0;
}

157
example/miscellaneous.cpp Normal file
View File

@ -0,0 +1,157 @@
// miscellaneous.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
// miscellaneous tests and demos:
#include <cassert>
#include <iostream>
using namespace boost::chrono;
void physics_function(duration<double> d)
{
std::cout << "d = " << d.count() << '\n';
}
void drive_physics_function()
{
physics_function(nanoseconds(3));
physics_function(hours(3));
physics_function(duration<double>(2./3));
std::cout.precision(16);
physics_function( hours(3) + nanoseconds(-3) );
}
void test_range()
{
using namespace boost::chrono;
hours h1 = hours(24 * ( 365 * 292 + 292/4));
nanoseconds n1 = h1 + nanoseconds(1);
nanoseconds delta = n1 - h1;
std::cout << "292 years of hours = " << h1.count() << "hr\n";
std::cout << "Add a nanosecond = " << n1.count() << "ns\n";
std::cout << "Find the difference = " << delta.count() << "ns\n";
}
void test_extended_range()
{
using namespace boost::chrono;
hours h1 = hours(24 * ( 365 * 244000 + 244000/4));
/*auto*/ microseconds u1 = h1 + microseconds(1);
/*auto*/ microseconds delta = u1 - h1;
std::cout << "244,000 years of hours = " << h1.count() << "hr\n";
std::cout << "Add a microsecond = " << u1.count() << "us\n";
std::cout << "Find the difference = " << delta.count() << "us\n";
}
template <class Rep, class Period>
void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name)
{
typedef boost::chrono::duration<Rep, Period> Duration;
std::cout << "********* " << name << " *********\n";
std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n";
std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n";
std::cout << "The representation is ";
if (boost::is_floating_point<Rep>::value)
{
std::cout << "floating point\n";
std::cout << "The precision is the most significant ";
std::cout << std::numeric_limits<Rep>::digits10 << " decimal digits.\n";
}
else if (boost::is_integral<Rep>::value)
{
std::cout << "integral\n";
d = Duration(Rep(1));
boost::chrono::duration<double> dsec = d;
std::cout << "The precision is " << dsec.count() << " seconds.\n";
}
else
{
std::cout << "a class type\n";
d = Duration(Rep(1));
boost::chrono::duration<double> dsec = d;
std::cout << "The precision is " << dsec.count() << " seconds.\n";
}
d = Duration((std::numeric_limits<Rep>::max)());
using namespace boost::chrono;
using namespace std;
typedef duration<double, boost::ratio_multiply<boost::ratio<24*3652425,10000>, hours::period>::type> Years;
Years years = d;
std::cout << "The range is +/- " << years.count() << " years.\n";
std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n';
}
void inspect_all()
{
using namespace boost::chrono;
std::cout.precision(6);
inspect_duration(nanoseconds(), "nanoseconds");
inspect_duration(microseconds(), "microseconds");
inspect_duration(milliseconds(), "milliseconds");
inspect_duration(seconds(), "seconds");
inspect_duration(minutes(), "minutes");
inspect_duration(hours(), "hours");
inspect_duration(duration<double>(), "duration<double>");
}
void test_milliseconds()
{
using namespace boost::chrono;
milliseconds ms(250);
ms += milliseconds(1);
milliseconds ms2(150);
milliseconds msdiff = ms - ms2;
if (msdiff == milliseconds(101))
std::cout << "success\n";
else
std::cout << "failure: " << msdiff.count() << '\n';
}
int main()
{
using namespace boost;
drive_physics_function();
test_range();
test_extended_range();
inspect_all();
test_milliseconds();
inspect_duration(common_type<duration<double>, hours, microseconds>::type(),
"common_type<duration<double>, hours, microseconds>::type");
duration<double, boost::milli> d = milliseconds(3) * 2.5;
inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5");
std::cout << d.count() << '\n';
// milliseconds ms(3.5); // doesn't compile
// std::cout << "milliseconds ms(3.5) doesn't compile\n";
return 0;
}

View File

@ -0,0 +1,22 @@
// run_timer_example.cpp ---------------------------------------------------//
// Copyright Beman Dawes 2006, 2008
// Copyright 2009/2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#include <boost/chrono/process_times.hpp>
#include <cmath>
int main()
{
boost::chrono::run_timer t;
for ( long i = 0; i < 10000; ++i )
std::sqrt( 123.456L ); // burn some time
return 0;
}

View File

@ -0,0 +1,24 @@
// run_timer_example.cpp ---------------------------------------------------//
// Copyright Beman Dawes 2006, 2008
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#include <boost/chrono/process_times.hpp>
#include <cmath>
int main( int argc, char * argv[] )
{
const char * format = argc > 1 ? argv[1] : "%t cpu seconds\n";
int places = argc > 2 ? std::atoi( argv[2] ) : 2;
boost::chrono::run_timer t( format, places );
for ( long i = 0; i < 10000; ++i )
std::sqrt( 123.456L ); // burn some time
return 0;
}

View File

@ -0,0 +1,227 @@
// runtime_resolution.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#if defined(BOOST_CHRONO_WINDOWS_API)
#include <windows.h>
namespace
{
//struct timeval {
// long tv_sec; /* seconds */
// long tv_usec; /* and microseconds */
//};
int gettimeofday(struct timeval * tp, void *)
{
FILETIME ft;
::GetSystemTimeAsFileTime( &ft ); // never fails
long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
# if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0
t -= 116444736000000000LL;
# else
t -= 116444736000000000;
# endif
t /= 10; // microseconds
tp->tv_sec = static_cast<long>( t / 1000000UL);
tp->tv_usec = static_cast<long>( t % 1000000UL);
return 0;
}
} // unnamed namespace
#endif
// Handle duration with resolution not known until run time
using namespace boost::chrono;
namespace runtime_resolution
{
class duration
{
public:
typedef long long rep;
private:
rep rep_;
static const double ticks_per_nanosecond;
public:
typedef boost::chrono::duration<double, boost::nano> tonanosec;
duration() {} // = default;
explicit duration(const rep& r) : rep_(r) {}
// conversions
explicit duration(const tonanosec& d)
: rep_(static_cast<rep>(d.count() * ticks_per_nanosecond)) {}
// explicit
operator tonanosec() const {return tonanosec(rep_/ticks_per_nanosecond);}
// observer
rep count() const {return rep_;}
// arithmetic
duration& operator+=(const duration& d) {rep_ += d.rep_; return *this;}
duration& operator-=(const duration& d) {rep_ += d.rep_; return *this;}
duration& operator*=(rep rhs) {rep_ *= rhs; return *this;}
duration& operator/=(rep rhs) {rep_ /= rhs; return *this;}
duration operator+() const {return *this;}
duration operator-() const {return duration(-rep_);}
duration& operator++() {++rep_; return *this;}
duration operator++(int) {return duration(rep_++);}
duration& operator--() {--rep_; return *this;}
duration operator--(int) {return duration(rep_--);}
friend duration operator+(duration x, duration y) {return x += y;}
friend duration operator-(duration x, duration y) {return x -= y;}
friend duration operator*(duration x, rep y) {return x *= y;}
friend duration operator*(rep x, duration y) {return y *= x;}
friend duration operator/(duration x, rep y) {return x /= y;}
friend bool operator==(duration x, duration y) {return x.rep_ == y.rep_;}
friend bool operator!=(duration x, duration y) {return !(x == y);}
friend bool operator< (duration x, duration y) {return x.rep_ < y.rep_;}
friend bool operator<=(duration x, duration y) {return !(y < x);}
friend bool operator> (duration x, duration y) {return y < x;}
friend bool operator>=(duration x, duration y) {return !(x < y);}
};
static
double
init_duration()
{
#if defined(BOOST_CHRONO_WINDOWS_API)
return static_cast<double>(1) / 1000; // Windows FILETIME is 1 per microsec
#elif defined(BOOST_CHRONO_MAC_API)
mach_timebase_info_data_t MachInfo;
mach_timebase_info(&MachInfo);
return static_cast<double>(MachInfo.denom) / MachInfo.numer;
#elif defined(BOOST_CHRONO_POSIX_API)
return static_cast<double>(1) / 1000;
#endif
}
const double duration::ticks_per_nanosecond = init_duration();
class clock;
class time_point
{
public:
typedef runtime_resolution::clock clock;
typedef long long rep;
private:
rep rep_;
rep count() const {return rep_;}
public:
time_point() : rep_(0) {}
explicit time_point(const duration& d)
: rep_(d.count()) {}
// arithmetic
time_point& operator+=(const duration& d) {rep_ += d.count(); return *this;}
time_point& operator-=(const duration& d) {rep_ -= d.count(); return *this;}
friend time_point operator+(time_point x, duration y) {return x += y;}
friend time_point operator+(duration x, time_point y) {return y += x;}
friend time_point operator-(time_point x, duration y) {return x -= y;}
friend duration operator-(time_point x, time_point y) {return duration(x.rep_ - y.rep_);}
};
class clock
{
public:
typedef runtime_resolution::duration::rep rep;
typedef runtime_resolution::duration duration;
typedef runtime_resolution::time_point time_point;
static time_point now()
{
#if defined(BOOST_CHRONO_WINDOWS_API)
timeval tv;
gettimeofday( &tv, 0 );
return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec));
#elif defined(BOOST_CHRONO_MAC_API)
timeval tv;
gettimeofday( &tv, 0 );
return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec));
#elif defined(BOOST_CHRONO_POSIX_API)
timespec ts;
::clock_gettime( CLOCK_REALTIME, &ts );
return time_point(duration((static_cast<rep>(ts.tv_sec)<<32) | ts.tv_nsec/1000));
#endif // POSIX
}
};
void test()
{
using namespace boost::chrono;
std::cout << "runtime_resolution test\n";
clock::duration delay(boost::chrono::milliseconds(5));
clock::time_point start = clock::now();
while (clock::now() - start <= delay)
;
clock::time_point stop = clock::now();
clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(duration_cast<nanoseconds>(duration::tonanosec(elapsed))).count()
<< " nanoseconds\n";
}
} // runtime_resolution
int main()
{
runtime_resolution::test();
return 0;
}

505
example/saturating.cpp Normal file
View File

@ -0,0 +1,505 @@
// saturating.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
//////////////////////////////////////////////////////////
//////////////////// User2 Example ///////////////////////
//////////////////////////////////////////////////////////
// Demonstrate User2:
// A "saturating" signed integral type is developed. This type has +/- infinity and a nan
// (like IEEE floating point) but otherwise obeys signed integral arithmetic.
// This class is subsequently used as the rep in boost::chrono::duration to demonstrate a
// duration class that does not silently ignore overflow.
#include <ostream>
#include <stdexcept>
#include <climits>
namespace User2
{
template <class I>
class saturate
{
public:
typedef I int_type;
static const int_type nan = int_type(int_type(1) << (sizeof(int_type) * CHAR_BIT - 1));
static const int_type neg_inf = nan + 1;
static const int_type pos_inf = -neg_inf;
private:
int_type i_;
// static_assert(std::is_integral<int_type>::value && std::is_signed<int_type>::value,
// "saturate only accepts signed integral types");
// static_assert(nan == -nan && neg_inf < pos_inf,
// "saturate assumes two's complement hardware for signed integrals");
public:
saturate() : i_(nan) {}
explicit saturate(int_type i) : i_(i) {}
// explicit
operator int_type() const;
saturate& operator+=(saturate x);
saturate& operator-=(saturate x) {return *this += -x;}
saturate& operator*=(saturate x);
saturate& operator/=(saturate x);
saturate& operator%=(saturate x);
saturate operator- () const {return saturate(-i_);}
saturate& operator++() {*this += saturate(int_type(1)); return *this;}
saturate operator++(int) {saturate tmp(*this); ++(*this); return tmp;}
saturate& operator--() {*this -= saturate(int_type(1)); return *this;}
saturate operator--(int) {saturate tmp(*this); --(*this); return tmp;}
friend saturate operator+(saturate x, saturate y) {return x += y;}
friend saturate operator-(saturate x, saturate y) {return x -= y;}
friend saturate operator*(saturate x, saturate y) {return x *= y;}
friend saturate operator/(saturate x, saturate y) {return x /= y;}
friend saturate operator%(saturate x, saturate y) {return x %= y;}
friend bool operator==(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ == y.i_;
}
friend bool operator!=(saturate x, saturate y) {return !(x == y);}
friend bool operator<(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ < y.i_;
}
friend bool operator<=(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ <= y.i_;
}
friend bool operator>(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ > y.i_;
}
friend bool operator>=(saturate x, saturate y)
{
if (x.i_ == nan || y.i_ == nan)
return false;
return x.i_ >= y.i_;
}
friend std::ostream& operator<<(std::ostream& os, saturate s)
{
switch (s.i_)
{
case pos_inf:
return os << "inf";
case nan:
return os << "nan";
case neg_inf:
return os << "-inf";
};
return os << s.i_;
}
};
template <class I>
saturate<I>::operator int_type() const
{
switch (i_)
{
case nan:
case neg_inf:
case pos_inf:
throw std::out_of_range("saturate special value can not convert to int_type");
}
return i_;
}
template <class I>
saturate<I>&
saturate<I>::operator+=(saturate x)
{
switch (i_)
{
case pos_inf:
switch (x.i_)
{
case neg_inf:
case nan:
i_ = nan;
}
return *this;
case nan:
return *this;
case neg_inf:
switch (x.i_)
{
case pos_inf:
case nan:
i_ = nan;
}
return *this;
}
switch (x.i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = x.i_;
return *this;
}
if (x.i_ >= 0)
{
if (i_ < pos_inf - x.i_)
i_ += x.i_;
else
i_ = pos_inf;
return *this;
}
if (i_ > neg_inf - x.i_)
i_ += x.i_;
else
i_ = neg_inf;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator*=(saturate x)
{
switch (i_)
{
case 0:
switch (x.i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = nan;
}
return *this;
case pos_inf:
switch (x.i_)
{
case nan:
case 0:
i_ = nan;
return *this;
}
if (x.i_ < 0)
i_ = neg_inf;
return *this;
case nan:
return *this;
case neg_inf:
switch (x.i_)
{
case nan:
case 0:
i_ = nan;
return *this;
}
if (x.i_ < 0)
i_ = pos_inf;
return *this;
}
switch (x.i_)
{
case 0:
i_ = 0;
return *this;
case nan:
i_ = nan;
return *this;
case pos_inf:
if (i_ < 0)
i_ = neg_inf;
else
i_ = pos_inf;
return *this;
case neg_inf:
if (i_ < 0)
i_ = pos_inf;
else
i_ = neg_inf;
return *this;
}
int s = (i_ < 0 ? -1 : 1) * (x.i_ < 0 ? -1 : 1);
i_ = i_ < 0 ? -i_ : i_;
int_type x_i_ = x.i_ < 0 ? -x.i_ : x.i_;
if (i_ <= pos_inf / x_i_)
i_ *= x_i_;
else
i_ = pos_inf;
i_ *= s;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator/=(saturate x)
{
switch (x.i_)
{
case pos_inf:
case neg_inf:
switch (i_)
{
case pos_inf:
case neg_inf:
case nan:
i_ = nan;
break;
default:
i_ = 0;
break;
}
return *this;
case nan:
i_ = nan;
return *this;
case 0:
switch (i_)
{
case pos_inf:
case neg_inf:
case nan:
return *this;
case 0:
i_ = nan;
return *this;
}
if (i_ > 0)
i_ = pos_inf;
else
i_ = neg_inf;
return *this;
}
switch (i_)
{
case 0:
case nan:
return *this;
case pos_inf:
case neg_inf:
if (x.i_ < 0)
i_ = -i_;
return *this;
}
i_ /= x.i_;
return *this;
}
template <class I>
saturate<I>&
saturate<I>::operator%=(saturate x)
{
// *this -= *this / x * x; // definition
switch (x.i_)
{
case nan:
case neg_inf:
case 0:
case pos_inf:
i_ = nan;
return *this;
}
switch (i_)
{
case neg_inf:
case pos_inf:
i_ = nan;
case nan:
return *this;
}
i_ %= x.i_;
return *this;
}
// Demo overflow-safe integral durations ranging from picoseconds resolution to millennium resolution
typedef boost::chrono::duration<saturate<long long>, boost::pico > picoseconds;
typedef boost::chrono::duration<saturate<long long>, boost::nano > nanoseconds;
typedef boost::chrono::duration<saturate<long long>, boost::micro > microseconds;
typedef boost::chrono::duration<saturate<long long>, boost::milli > milliseconds;
typedef boost::chrono::duration<saturate<long long> > seconds;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 60LL> > minutes;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 3600LL> > hours;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 86400LL> > days;
typedef boost::chrono::duration<saturate<long long>, boost::ratio< 31556952LL> > years;
typedef boost::chrono::duration<saturate<long long>, boost::ratio<31556952000LL> > millennium;
} // User2
// Demonstrate custom promotion rules (needed only if there are no implicit conversions)
namespace User2 { namespace detail {
template <class T1, class T2, bool = boost::is_integral<T1>::value>
struct promote_helper;
template <class T1, class T2>
struct promote_helper<T1, saturate<T2>, true> // integral
{
typedef typename boost::common_type<T1, T2>::type rep;
typedef User2::saturate<rep> type;
};
template <class T1, class T2>
struct promote_helper<T1, saturate<T2>, false> // floating
{
typedef T1 type;
};
} }
namespace boost
{
template <class T1, class T2>
struct common_type<User2::saturate<T1>, User2::saturate<T2> >
{
typedef typename common_type<T1, T2>::type rep;
typedef User2::saturate<rep> type;
};
template <class T1, class T2>
struct common_type<T1, User2::saturate<T2> >
: User2::detail::promote_helper<T1, User2::saturate<T2> > {};
template <class T1, class T2>
struct common_type<User2::saturate<T1>, T2>
: User2::detail::promote_helper<T2, User2::saturate<T1> > {};
// Demonstrate specialization of duration_values:
namespace chrono {
template <class I>
struct duration_values<User2::saturate<I> >
{
typedef User2::saturate<I> Rep;
public:
static Rep zero() {return Rep(0);}
static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return Rep(Rep::pos_inf-1);}
static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return -(max)();}
};
} // namespace chrono
} // namespace boost
#include <iostream>
void testUser2()
{
std::cout << "*************\n";
std::cout << "* testUser2 *\n";
std::cout << "*************\n";
using namespace User2;
typedef seconds::rep sat;
years yr(sat(100));
std::cout << "100 years expressed as years = " << yr.count() << '\n';
nanoseconds ns = yr;
std::cout << "100 years expressed as nanoseconds = " << ns.count() << '\n';
ns += yr;
std::cout << "200 years expressed as nanoseconds = " << ns.count() << '\n';
ns += yr;
std::cout << "300 years expressed as nanoseconds = " << ns.count() << '\n';
// yr = ns; // does not compile
std::cout << "yr = ns; // does not compile\n";
// picoseconds ps1 = yr; // does not compile, compile-time overflow in ratio arithmetic
std::cout << "ps = yr; // does not compile\n";
ns = yr;
picoseconds ps = ns;
std::cout << "100 years expressed as picoseconds = " << ps.count() << '\n';
ps = ns / sat(1000);
std::cout << "0.1 years expressed as picoseconds = " << ps.count() << '\n';
yr = years(sat(-200000000));
std::cout << "200 million years ago encoded in years: " << yr.count() << '\n';
days d = boost::chrono::duration_cast<days>(yr);
std::cout << "200 million years ago encoded in days: " << d.count() << '\n';
millennium c = boost::chrono::duration_cast<millennium>(yr);
std::cout << "200 million years ago encoded in millennium: " << c.count() << '\n';
std::cout << "Demonstrate \"uninitialized protection\" behavior:\n";
seconds sec;
for (++sec; sec < seconds(sat(10)); ++sec)
;
std::cout << sec.count() << '\n';
std::cout << "\n";
}
void testStdUser()
{
std::cout << "***************\n";
std::cout << "* testStdUser *\n";
std::cout << "***************\n";
using namespace boost::chrono;
hours hr = hours(100);
std::cout << "100 hours expressed as hours = " << hr.count() << '\n';
nanoseconds ns = hr;
std::cout << "100 hours expressed as nanoseconds = " << ns.count() << '\n';
ns += hr;
std::cout << "200 hours expressed as nanoseconds = " << ns.count() << '\n';
ns += hr;
std::cout << "300 hours expressed as nanoseconds = " << ns.count() << '\n';
// hr = ns; // does not compile
std::cout << "hr = ns; // does not compile\n";
// hr * ns; // does not compile
std::cout << "hr * ns; // does not compile\n";
duration<double> fs(2.5);
std::cout << "duration<double> has count() = " << fs.count() << '\n';
// seconds sec = fs; // does not compile
std::cout << "seconds sec = duration<double> won't compile\n";
seconds sec = duration_cast<seconds>(fs);
std::cout << "seconds has count() = " << sec.count() << '\n';
std::cout << "\n";
}
int main()
{
testStdUser();
testUser2();
return 0;
}

View File

@ -0,0 +1,198 @@
// simulated_thread_interface_demo.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <climits>
//////////////////////////////////////////////////////////
///////////// simulated thread interface /////////////////
//////////////////////////////////////////////////////////
namespace boost {
namespace detail {
void print_time(boost::chrono::system_clock::time_point t)
{
using namespace boost::chrono;
time_t c_time = system_clock::to_time_t(t);
std::tm* tmptr = std::localtime(&c_time);
system_clock::duration d = t.time_since_epoch();
std::cout << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec
<< '.' << (d - duration_cast<seconds>(d)).count();
}
}
namespace this_thread {
template <class Rep, class Period>
void sleep_for(const boost::chrono::duration<Rep, Period>& d)
{
boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d);
if (t < d)
++t;
if (t > boost::chrono::microseconds(0))
std::cout << "sleep_for " << t.count() << " microseconds\n";
}
template <class Clock, class Duration>
void sleep_until(const boost::chrono::time_point<Clock, Duration>& t)
{
using namespace boost::chrono;
typedef time_point<Clock, Duration> Time;
typedef system_clock::time_point SysTime;
if (t > Clock::now())
{
typedef typename boost::common_type<typename Time::duration,
typename SysTime::duration>::type D;
/* auto */ D d = t - Clock::now();
microseconds us = duration_cast<microseconds>(d);
if (us < d)
++us;
SysTime st = system_clock::now() + us;
std::cout << "sleep_until ";
boost::detail::print_time(st);
std::cout << " which is " << (st - system_clock::now()).count() << " microseconds away\n";
}
}
} // this_thread
struct mutex {};
struct timed_mutex
{
bool try_lock() {std::cout << "timed_mutex::try_lock()\n"; return true;}
template <class Rep, class Period>
bool try_lock_for(const boost::chrono::duration<Rep, Period>& d)
{
boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d);
if (t <= boost::chrono::microseconds(0))
return try_lock();
std::cout << "try_lock_for " << t.count() << " microseconds\n";
return true;
}
template <class Clock, class Duration>
bool try_lock_until(const boost::chrono::time_point<Clock, Duration>& t)
{
using namespace boost::chrono;
typedef time_point<Clock, Duration> Time;
typedef system_clock::time_point SysTime;
if (t <= Clock::now())
return try_lock();
typedef typename boost::common_type<typename Time::duration,
typename Clock::duration>::type D;
/* auto */ D d = t - Clock::now();
microseconds us = duration_cast<microseconds>(d);
SysTime st = system_clock::now() + us;
std::cout << "try_lock_until ";
boost::detail::print_time(st);
std::cout << " which is " << (st - system_clock::now()).count()
<< " microseconds away\n";
return true;
}
};
struct condition_variable
{
template <class Rep, class Period>
bool wait_for(mutex&, const boost::chrono::duration<Rep, Period>& d)
{
boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d);
std::cout << "wait_for " << t.count() << " microseconds\n";
return true;
}
template <class Clock, class Duration>
bool wait_until(mutex&, const boost::chrono::time_point<Clock, Duration>& t)
{
using namespace boost::chrono;
typedef time_point<Clock, Duration> Time;
typedef system_clock::time_point SysTime;
if (t <= Clock::now())
return false;
typedef typename boost::common_type<typename Time::duration,
typename Clock::duration>::type D;
/* auto */ D d = t - Clock::now();
microseconds us = duration_cast<microseconds>(d);
SysTime st = system_clock::now() + us;
std::cout << "wait_until ";
boost::detail::print_time(st);
std::cout << " which is " << (st - system_clock::now()).count()
<< " microseconds away\n";
return true;
}
};
}
//////////////////////////////////////////////////////////
//////////// Simple sleep and wait examples //////////////
//////////////////////////////////////////////////////////
boost::mutex m;
boost::timed_mutex mut;
boost::condition_variable cv;
void basic_examples()
{
std::cout << "Running basic examples\n";
using namespace boost;
using namespace boost::chrono;
system_clock::time_point time_limit = system_clock::now() + seconds(4) + milliseconds(500);
this_thread::sleep_for(seconds(3));
this_thread::sleep_for(nanoseconds(300));
this_thread::sleep_until(time_limit);
// this_thread::sleep_for(time_limit); // desired compile-time error
// this_thread::sleep_until(seconds(3)); // desired compile-time error
mut.try_lock_for(milliseconds(30));
mut.try_lock_until(time_limit);
// mut.try_lock_for(time_limit); // desired compile-time error
// mut.try_lock_until(milliseconds(30)); // desired compile-time error
cv.wait_for(m, minutes(1)); // real code would put this in a loop
cv.wait_until(m, time_limit); // real code would put this in a loop
// For those who prefer floating point
this_thread::sleep_for(duration<double>(0.25));
this_thread::sleep_until(system_clock::now() + duration<double>(1.5));
}
int main()
{
basic_examples();
return 0;
}

182
example/test_clock.cpp Normal file
View File

@ -0,0 +1,182 @@
// test_system_clock.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#include "clock_name.hpp"
#if defined(BOOST_NO_CONSTEXPR)
using namespace boost::chrono;
template <typename Clock>
void test_clock()
{
std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl;
{
typename Clock::duration delay = milliseconds(5);
typename Clock::time_point start = Clock::now();
while (Clock::now() - start <= delay)
;
typename Clock::time_point stop = Clock::now();
//typename Clock::duration elapsed = stop - start;
std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n";
}
{
typename Clock::time_point start = Clock::now();
typename Clock::time_point stop;
std::size_t count=1;
while ((stop=Clock::now()) == start) {
++count;
}
//typename Clock::duration elapsed = stop - start;
std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
start = Clock::now();
for (std::size_t c=count; c>0; --c) {
stop=Clock::now();;
}
std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
}
{
typename Clock::time_point start = Clock::now();
typename Clock::time_point stop = Clock::now();
std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
}
void test_system_clock()
{
std::cout << "system_clock test" << std::endl;
system_clock::duration delay = milliseconds(5);
system_clock::time_point start = system_clock::now();
while (system_clock::now() - start <= delay)
;
system_clock::time_point stop = system_clock::now();
system_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = system_clock::now();
stop = system_clock::now();
std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
void test_monotonic_clock()
{
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
std::cout << "monotonic_clock test" << std::endl;
monotonic_clock::duration delay = milliseconds(5);
monotonic_clock::time_point start = monotonic_clock::now();
while (monotonic_clock::now() - start <= delay)
;
monotonic_clock::time_point stop = monotonic_clock::now();
monotonic_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = monotonic_clock::now();
stop = monotonic_clock::now();
std::cout << "monotonic_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
#endif
}
void test_hi_resolution_clock()
{
std::cout << "high_resolution_clock test" << std::endl;
high_resolution_clock::duration delay = milliseconds(5);
high_resolution_clock::time_point start = high_resolution_clock::now();
while (high_resolution_clock::now() - start <= delay)
;
high_resolution_clock::time_point stop = high_resolution_clock::now();
high_resolution_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = high_resolution_clock::now();
stop = high_resolution_clock::now();
std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
//void test_mixed_clock()
//{
// std::cout << "mixed clock test" << std::endl;
// high_resolution_clock::time_point hstart = high_resolution_clock::now();
// std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n";
// monotonic_clock::time_point mend = hstart + milliseconds(5);
// bool b = hstart == mend;
// system_clock::time_point sstart = system_clock::now();
// std::cout << "Subtracting system_clock::time_point from monotonic_clock::time_point doesn't compile\n";
//// mend - sstart; // doesn't compile
// std::cout << "subtract high_resolution_clock::time_point from monotonic_clock::time_point"
// " and add that to a system_clock::time_point\n";
// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart);
// std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n";
// microseconds ms = send - sstart;
// std::cout << ms.count() << " microseconds\n";
//}
//
//void test_c_mapping()
//{
// std::cout << "C map test\n";
// using namespace boost::chrono;
// system_clock::time_point t1 = system_clock::now();
// std::time_t c_time = system_clock::to_time_t(t1);
// std::tm* tmptr = std::localtime(&c_time);
// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' '
// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n';
// c_time = std::mktime(tmptr);
// system_clock::time_point t2 = system_clock::from_time_t(c_time);
// microseconds ms = t1 - t2;
// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n";
//}
int main()
{
test_system_clock();
test_monotonic_clock();
test_hi_resolution_clock();
//test_mixed_clock();
test_clock<system_clock>();
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
test_clock<monotonic_clock>();
#endif
test_clock<high_resolution_clock>();
return 0;
}
#else
int main()
{
return 0;
}
#endif

210
example/test_clock2.cpp Normal file
View File

@ -0,0 +1,210 @@
// test_system_clock.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#include "clock_name.hpp"
namespace boost {
namespace detail_chrono {
class monotonic_clock {};
class system_clock {};
}
namespace chrono {
namespace chrono_detail {
using namespace detail_chrono;
struct has_monotonic_clock {
template< class T > static char sfinae( typename T::rep );
template< class > static int sfinae( ... );
enum { value = sizeof sfinae< monotonic_clock >( 0 ) == sizeof(char) };
};
struct has_system_clock {
template< class T > static char sfinae( typename T::rep );
template< class > static int sfinae( ... );
enum { value = sizeof sfinae< system_clock >( 0 ) == sizeof(char) };
};
}
struct has_monotonic_clock
: integral_constant<bool, chrono_detail::has_monotonic_clock::value> {};
struct has_system_clock
: integral_constant<bool, chrono_detail::has_system_clock::value> {};
}
}
BOOST_STATIC_ASSERT(boost::chrono::has_system_clock::value);
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
BOOST_STATIC_ASSERT(boost::chrono::has_monotonic_clock::value);
#else
BOOST_STATIC_ASSERT(!boost::chrono::has_monotonic_clock::value);
#endif
using namespace boost::chrono;
using namespace boost;
template <typename Clock>
void test_clock()
{
std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl;
{
typename Clock::duration delay = milliseconds(5);
typename Clock::time_point start = Clock::now();
while (Clock::now() - start <= delay)
;
typename Clock::time_point stop = Clock::now();
//typename Clock::duration elapsed = stop - start;
std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n";
}
{
typename Clock::time_point start = Clock::now();
typename Clock::time_point stop;
std::size_t count=1;
while ((stop=Clock::now()) == start) {
++count;
}
//typename Clock::duration elapsed = stop - start;
std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
start = Clock::now();
for (std::size_t c=count; c>0; --c) {
stop=Clock::now();;
}
std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
}
{
typename Clock::time_point start = Clock::now();
typename Clock::time_point stop = Clock::now();
std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
}
void test_system_clock()
{
std::cout << "system_clock test" << std::endl;
//~ system_clock clk;
chrono::system_clock::duration delay = milliseconds(5);
chrono::system_clock::time_point start = system_clock::now();
while (chrono::system_clock::now() - start <= delay)
;
chrono::system_clock::time_point stop = system_clock::now();
chrono::system_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = chrono::system_clock::now();
stop = chrono::system_clock::now();
std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
void test_monotonic_clock()
{
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
std::cout << "monotonic_clock test" << std::endl;
monotonic_clock::duration delay = milliseconds(5);
monotonic_clock::time_point start = monotonic_clock::now();
while (monotonic_clock::now() - start <= delay)
;
monotonic_clock::time_point stop = monotonic_clock::now();
monotonic_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = monotonic_clock::now();
stop = monotonic_clock::now();
std::cout << "monotonic_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
#endif
}
void test_hi_resolution_clock()
{
std::cout << "high_resolution_clock test" << std::endl;
high_resolution_clock::duration delay = milliseconds(5);
high_resolution_clock::time_point start = high_resolution_clock::now();
while (high_resolution_clock::now() - start <= delay)
;
high_resolution_clock::time_point stop = high_resolution_clock::now();
high_resolution_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = high_resolution_clock::now();
stop = high_resolution_clock::now();
std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
}
//void test_mixed_clock()
//{
// std::cout << "mixed clock test" << std::endl;
// high_resolution_clock::time_point hstart = high_resolution_clock::now();
// std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n";
// monotonic_clock::time_point mend = hstart + milliseconds(5);
// bool b = hstart == mend;
// system_clock::time_point sstart = system_clock::now();
// std::cout << "Subtracting system_clock::time_point from monotonic_clock::time_point doesn't compile\n";
//// mend - sstart; // doesn't compile
// std::cout << "subtract high_resolution_clock::time_point from monotonic_clock::time_point"
// " and add that to a system_clock::time_point\n";
// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart);
// std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n";
// microseconds ms = send - sstart;
// std::cout << ms.count() << " microseconds\n";
//}
//
//void test_c_mapping()
//{
// std::cout << "C map test\n";
// using namespace boost::chrono;
// system_clock::time_point t1 = system_clock::now();
// std::time_t c_time = system_clock::to_time_t(t1);
// std::tm* tmptr = std::localtime(&c_time);
// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' '
// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n';
// c_time = std::mktime(tmptr);
// system_clock::time_point t2 = system_clock::from_time_t(c_time);
// microseconds ms = t1 - t2;
// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n";
//}
int main()
{
test_system_clock();
test_monotonic_clock();
test_hi_resolution_clock();
//test_mixed_clock();
test_clock<system_clock>();
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
test_clock<monotonic_clock>();
#endif
test_clock<high_resolution_clock>();
return 0;
}

199
example/test_duration.cpp Normal file
View File

@ -0,0 +1,199 @@
// test_duration.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/assert.hpp>
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
template <class Rep, class Period>
void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name)
{
typedef boost::chrono::duration<Rep, Period> Duration;
std::cout << "********* " << name << " *********\n";
std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n";
std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n";
std::cout << "The representation is ";
if (boost::is_floating_point<Rep>::value)
{
std::cout << "floating point\n";
std::cout << "The precision is the most significant ";
std::cout << std::numeric_limits<Rep>::digits10 << " decimal digits.\n";
}
else if (boost::is_integral<Rep>::value)
{
std::cout << "integral\n";
d = Duration(Rep(1));
boost::chrono::duration<double> dsec = d;
std::cout << "The precision is " << dsec.count() << " seconds.\n";
}
else
{
std::cout << "a class type\n";
d = Duration(Rep(1));
boost::chrono::duration<double> dsec = d;
std::cout << "The precision is " << dsec.count() << " seconds.\n";
}
d = Duration((std::numeric_limits<Rep>::max)());
using namespace boost::chrono;
using namespace std;
typedef duration<double, boost::ratio_multiply<boost::ratio<24*3652425,10000>, hours::period>::type> Years;
Years years = d;
std::cout << "The range is +/- " << years.count() << " years.\n";
std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n';
}
void inspect_all()
{
using namespace boost::chrono;
std::cout.precision(6);
inspect_duration(nanoseconds(), "nanoseconds");
inspect_duration(microseconds(), "microseconds");
inspect_duration(milliseconds(), "milliseconds");
inspect_duration(seconds(), "seconds");
inspect_duration(minutes(), "minutes");
inspect_duration(hours(), "hours");
inspect_duration(duration<double>(), "duration<double>");
}
using namespace boost::chrono;
void test_duration_division()
{
typedef boost::common_type<boost::chrono::hours::rep, boost::chrono::minutes::rep>::type h_min_rep;
h_min_rep r3 = hours(3) / minutes(5);
std::cout << r3 << '\n';
std::cout << hours(3) / minutes(5) << '\n';
std::cout << hours(3) / milliseconds(5) << '\n';
std::cout << milliseconds(5) / hours(3) << '\n';
std::cout << hours(1) / milliseconds(1) << '\n';
}
void test_duration_multiply()
{
hours h15= 5 * hours(3);
hours h6= hours(3) *2;
}
void f(duration<double> d, double res) // accept floating point seconds
{
// d.count() == 3.e-6 when passed microseconds(3)
BOOST_ASSERT(d.count()==res);
}
void g(nanoseconds d, boost::intmax_t res)
{
// d.count() == 3000 when passed microseconds(3)
std::cout << d.count() << " " <<res << std::endl;
BOOST_ASSERT(d.count()==res);
}
template <class Rep, class Period>
void tmpl(duration<Rep, Period> d, boost::intmax_t res)
{
// convert d to nanoseconds, rounding up if it is not an exact conversion
nanoseconds ns = duration_cast<nanoseconds>(d);
if (ns < d)
++ns;
// ns.count() == 333333334 when passed 1/3 of a floating point second
BOOST_ASSERT(ns.count()==res);
}
template <class Period>
void tmpl2(duration<long long, Period> d, boost::intmax_t res)
{
// convert d to nanoseconds, rounding up if it is not an exact conversion
nanoseconds ns = duration_cast<nanoseconds>(d);
if (ns < d)
++ns;
// ns.count() == 333333334 when passed 333333333333 picoseconds
BOOST_ASSERT(ns.count()==res);
}
int main()
{
minutes m1(3); // m1 stores 3
minutes m2(2); // m2 stores 2
minutes m3 = m1 + m2; // m3 stores 5
BOOST_ASSERT(m3.count()==5);
microseconds us1(3); // us1 stores 3
microseconds us2(2); // us2 stores 2
microseconds us3 = us1 + us2; // us3 stores 5
BOOST_ASSERT(us3.count()==5);
microseconds us4 = m3 + us3; // us4 stores 300000005
BOOST_ASSERT(us4.count()==300000005);
microseconds us5 = m3; // us4 stores 300000000
BOOST_ASSERT(us5.count()==300000000);
//minutes m4 = m3 + us3; // won't compile
minutes m4 = duration_cast<minutes>(m3 + us3); // m4.count() == 5
BOOST_ASSERT(m4.count()==5);
typedef duration<double, boost::ratio<60> > dminutes;
dminutes dm4 = m3 + us3; // dm4.count() == 5.000000083333333
BOOST_ASSERT(dm4.count()==5.000000083333333);
f(microseconds(3), 0.000003);
g(microseconds(3), 3000);
duration<double> s(1./3); // 1/3 of a second
g(duration_cast<nanoseconds>(s), 333333333); // round towards zero in conversion to nanoseconds
//f(s); // does not compile
tmpl(duration<double>(1./3), 333333334);
tmpl2(duration<long long, boost::pico>(333333333333LL), 333333334); // About 1/3 of a second worth of picoseconds
//f(3,3); // Will not compile, 3 is not implicitly convertible to any `duration`
//g(3,3); // Will not compile, 3 is not implicitly convertible to any `duration`
//tmpl(3,3); // Will not compile, 3 is not implicitly convertible to any `duration`
//tmpl2(3,3); // Will not compile, 3 is not implicitly convertible to any `duration`
{
double r = double(milliseconds(3) / milliseconds(3));
std::cout << r << '\n';
duration<double, boost::milli> d = milliseconds(3) * 2.5;
duration<double, boost::milli> d2 = 2.5 * milliseconds(3) ;
duration<double, boost::milli> d3 = milliseconds(3) / 2.5;
duration<double, boost::milli> d4 = milliseconds(3) + milliseconds(5) ;
inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5");
std::cout << d.count() << '\n';
// milliseconds ms(3.5); // doesn't compile
std::cout << "milliseconds ms(3.5) doesn't compile\n";
}
test_duration_division();
test_duration_multiply();
return 0;
}

17
example/test_minmax.cpp Normal file
View File

@ -0,0 +1,17 @@
// test_duration.cpp ----------------------------------------------------------//
// Copyright 2009 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#if !defined(__GNUC__)
#define min(A,B) ((A)<(B)?(A):(B))
#define max(A,B) ((A)>(B)?(A):(B))
#include <boost/chrono/chrono.hpp>
#endif

View File

@ -0,0 +1,51 @@
// test_special_values.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
void test_special_values()
{
std::cout << "duration<unsigned>::min().count() = " << ((duration<unsigned>::min)()).count() << '\n';
std::cout << "duration<unsigned>::zero().count() = " << duration<unsigned>::zero().count() << '\n';
std::cout << "duration<unsigned>::max().count() = " << ((duration<unsigned>::max)()).count() << '\n';
std::cout << "duration<int>::min().count() = " << ((duration<int>::min)()).count() << '\n';
std::cout << "duration<int>::zero().count() = " << duration<int>::zero().count() << '\n';
std::cout << "duration<int>::max().count() = " << ((duration<int>::max)()).count() << '\n';
}
int main()
{
test_special_values();
return 0;
}

View File

@ -0,0 +1,41 @@
// test_thread_clock.cpp ----------------------------------------------------------//
// Copyright 2009 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/thread_clock.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
void test_thread_clock()
{
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
std::cout << "thread_clock test" << std::endl;
thread_clock::duration delay = milliseconds(5);
thread_clock::time_point start = thread_clock::now();
while (thread_clock::now() - start <= delay)
;
thread_clock::time_point stop = thread_clock::now();
thread_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
start = thread_clock::now();
stop = thread_clock::now();
std::cout << "thread_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
#else
std::cout << "thread_clock not available\n";
#endif
}
int main()
{
test_thread_clock();
return 0;
}

1655
example/time2_demo.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,191 @@
// time2_demo.output ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
Running basic examples
sleep_for 3000000 microseconds
sleep_for 1 microseconds
sleep_until 10:47:17.728293 which is 4499340 microseconds away
try_lock_for 30000 microseconds
try_lock_until 10:47:17.728285 which is 4499303 microseconds away
wait_for 60000000 microseconds
wait_until 10:47:17.728285 which is 4499264 microseconds away
sleep_for 250000 microseconds
sleep_until 10:47:14.729077 which is 1499979 microseconds away
***************
* testStdUser *
***************
100 hours expressed as hours = 100
100 hours expressed as nanoseconds = 360000000000000
200 hours expressed as nanoseconds = 720000000000000
300 hours expressed as nanoseconds = 1080000000000000
hr = ns; // does not compile
hr * ns; // does not compile
duration<double> has count() = 2.5
seconds sec = duration<double> won't compile
seconds has count() = 2
*************
* testUser1 *
*************
Speed = 24.5872 meters/sec
Acceleration = 9.81456 meters/sec^2
Distance = 13.5204 meters
There are 125/201168 miles/meter which is approximately 0.000621371
There are 201168/125 meters/mile which is approximately 1609.34
1 attosecond is 1e-18 seconds
sec = as; // compiles
1 second is 1e+18 attoseconds
as = sec; // compiles
*************
* testUser2 *
*************
100 years expressed as years = 100
100 years expressed as nanoseconds = 3155695200000000000
200 years expressed as nanoseconds = 6311390400000000000
300 years expressed as nanoseconds = inf
yr = ns; // does not compile
ps = yr; // does not compile
100 years expressed as picoseconds = inf
0.1 years expressed as picoseconds = 3155695200000000000
200 million years ago encoded in years: -200000000
200 million years ago encoded in days: -73048500000
200 million years ago encoded in millennium: -200000
Demonstrate "uninitialized protection" behavior:
nan
d = 3e-09
d = 10800
d = 0.666667
d = 10799.999999997
292 years of hours = 2559672hr
Add a nanosecond = 9214819200000000001ns
Find the difference = 1ns
244,000 years of hours = 2138904000hr
Add a microsecond = 7700054400000000001us
Find the difference = 1us
********* nanoseconds *********
The period of nanoseconds is 1e-09 seconds.
The frequency of nanoseconds is 1e+09 Hz.
The representation is integral
The precision is 1e-09 seconds.
The range is +/- 292.277 years.
sizeof(nanoseconds) = 8
********* microseconds *********
The period of microseconds is 1e-06 seconds.
The frequency of microseconds is 1e+06 Hz.
The representation is integral
The precision is 1e-06 seconds.
The range is +/- 292277 years.
sizeof(microseconds) = 8
********* milliseconds *********
The period of milliseconds is 0.001 seconds.
The frequency of milliseconds is 1000 Hz.
The representation is integral
The precision is 0.001 seconds.
The range is +/- 2.92277e+08 years.
sizeof(milliseconds) = 8
********* seconds *********
The period of seconds is 1 seconds.
The frequency of seconds is 1 Hz.
The representation is integral
The precision is 1 seconds.
The range is +/- 2.92277e+11 years.
sizeof(seconds) = 8
********* minutes *********
The period of minutes is 60 seconds.
The frequency of minutes is 0.0166667 Hz.
The representation is integral
The precision is 60 seconds.
The range is +/- 4083.06 years.
sizeof(minutes) = 4
********* hours *********
The period of hours is 3600 seconds.
The frequency of hours is 0.000277778 Hz.
The representation is integral
The precision is 3600 seconds.
The range is +/- 244984 years.
sizeof(hours) = 4
********* duration<double> *********
The period of duration<double> is 1 seconds.
The frequency of duration<double> is 1 Hz.
The representation is floating point
The precision is the most significant 15 decimal digits.
The range is +/- 5.69666e+300 years.
sizeof(duration<double>) = 8
success
test_with_xtime
{3,251000}
3251 milliseconds
{3,251000}
{3,0}
{3,1}
system_clock test
paused 5001000 nanoseconds
system_clock resolution estimate: 0 nanoseconds
monotonic_clock test
paused 5000181 nanoseconds
monotonic_clock resolution estimate: 97 nanoseconds
high_resolution_clock test
paused 5000277 nanoseconds
high_resolution_clock resolution estimate: 96 nanoseconds
mixed clock test
Add 5 milliseconds to a high_resolution_clock::time_point
Subtracting system_clock::time_point from monotonic_clock::time_point doesn't compile
subtract high_resolution_clock::time_point from monotonic_clock::time_point and add that to a system_clock::time_point
subtract two system_clock::time_point's and output that in microseconds:
5000 microseconds
timeval_demo system clock test
sizeof xtime_clock::time_point = 8
sizeof xtime_clock::duration = 8
sizeof xtime_clock::rep = 8
paused 5001000 nanoseconds
runtime_resolution test
paused 5000205 nanoseconds
C map test
It is now 10:47:13 2008-4-22
Round-tripping through the C interface truncated the precision by 255445 microseconds
2160000
0
3600000
0
2999998997 * 1/1000000000 seconds
0 * 1/1000000000 seconds
15778476000000000 microseconds
paused 5001000 nanoseconds
********* milliseconds(3) * 2.5 *********
The period of milliseconds(3) * 2.5 is 0.001 seconds.
The frequency of milliseconds(3) * 2.5 is 1000 Hz.
The representation is floating point
The precision is the most significant 15 decimal digits.
The range is +/- 5.69666e+297 years.
sizeof(milliseconds(3) * 2.5) = 8
7.5
milliseconds ms(3.5) doesn't compile
Simulated 400MHz clock which has a tick period of 2.5 nanoseconds
delay = 500 nanoseconds which is 200 cycles
paused 201 cycles which is 502 nanoseconds
Simulated 400MHz clock modeled with nanoseconds
delay = 500 nanoseconds
paused 503 nanoseconds
Simulated 1500MHz clock which has a tick period of 0.666667 nanoseconds
delay = 500 nanoseconds which is 750 cycles
paused 751 cycles which is 500 nanoseconds
Simulated 1500MHz clock modeled with nanoseconds
delay = 500 nanoseconds
paused 500 nanoseconds
duration<unsigned>::min().count() = 0
duration<unsigned>::zero().count() = 0
duration<unsigned>::max().count() = 4294967295
duration<int>::min().count() = -2147483647
duration<int>::zero().count() = 0
duration<int>::max().count() = 2147483647

221
example/timeval_demo.cpp Normal file
View File

@ -0,0 +1,221 @@
// timeval_demo.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
#if defined(BOOST_CHRONO_WINDOWS_API)
# include <windows.h>
#endif
#if defined(BOOST_CHRONO_WINDOWS_API)
namespace
{
//struct timeval {
// long tv_sec; /* seconds */
// long tv_usec; /* and microseconds */
//};
int gettimeofday(struct timeval * tp, void *)
{
FILETIME ft;
::GetSystemTimeAsFileTime( &ft ); // never fails
long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
# if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0
t -= 116444736000000000LL;
# else
t -= 116444736000000000;
# endif
t /= 10; // microseconds
tp->tv_sec = static_cast<long>( t / 1000000UL);
tp->tv_usec = static_cast<long>( t % 1000000UL);
return 0;
}
} // unnamed namespace
#endif
// timeval clock demo
// Demonstrate the use of a timeval-like struct to be used as the representation
// type for both duraiton and time_point.
namespace timeval_demo
{
class xtime {
private:
long tv_sec;
long tv_usec;
void fixup() {
if (tv_usec < 0) {
tv_usec += 1000000;
--tv_sec;
}
}
public:
explicit xtime(long sec, long usec) {
tv_sec = sec;
tv_usec = usec;
if (tv_usec < 0 || tv_usec >= 1000000) {
tv_sec += tv_usec / 1000000;
tv_usec %= 1000000;
fixup();
}
}
explicit xtime(long long usec)
{
tv_usec = static_cast<long>(usec % 1000000);
tv_sec = static_cast<long>(usec / 1000000);
fixup();
}
// explicit
operator long long() const {return static_cast<long long>(tv_sec) * 1000000 + tv_usec;}
xtime& operator += (xtime rhs) {
tv_sec += rhs.tv_sec;
tv_usec += rhs.tv_usec;
if (tv_usec >= 1000000) {
tv_usec -= 1000000;
++tv_sec;
}
return *this;
}
xtime& operator -= (xtime rhs) {
tv_sec -= rhs.tv_sec;
tv_usec -= rhs.tv_usec;
fixup();
return *this;
}
xtime& operator %= (xtime rhs) {
long long t = tv_sec * 1000000 + tv_usec;
long long r = rhs.tv_sec * 1000000 + rhs.tv_usec;
t %= r;
tv_sec = static_cast<long>(t / 1000000);
tv_usec = static_cast<long>(t % 1000000);
fixup();
return *this;
}
friend xtime operator+(xtime x, xtime y) {return x += y;}
friend xtime operator-(xtime x, xtime y) {return x -= y;}
friend xtime operator%(xtime x, xtime y) {return x %= y;}
friend bool operator==(xtime x, xtime y)
{ return (x.tv_sec == y.tv_sec && x.tv_usec == y.tv_usec); }
friend bool operator<(xtime x, xtime y) {
if (x.tv_sec == y.tv_sec)
return (x.tv_usec < y.tv_usec);
return (x.tv_sec < y.tv_sec);
}
friend bool operator!=(xtime x, xtime y) { return !(x == y); }
friend bool operator> (xtime x, xtime y) { return y < x; }
friend bool operator<=(xtime x, xtime y) { return !(y < x); }
friend bool operator>=(xtime x, xtime y) { return !(x < y); }
friend std::ostream& operator<<(std::ostream& os, xtime x)
{return os << '{' << x.tv_sec << ',' << x.tv_usec << '}';}
};
class xtime_clock
{
public:
typedef xtime rep;
typedef boost::micro period;
typedef boost::chrono::duration<rep, period> duration;
typedef boost::chrono::time_point<xtime_clock> time_point;
static time_point now();
};
xtime_clock::time_point
xtime_clock::now()
{
#if defined(BOOST_CHRONO_WINDOWS_API)
timeval tv;
gettimeofday(&tv, 0);
xtime xt( tv.tv_sec, tv.tv_usec);
return time_point(duration(xt));
#elif defined(BOOST_CHRONO_MAC_API)
timeval tv;
gettimeofday(&tv, 0);
xtime xt( tv.tv_sec, tv.tv_usec);
return time_point(duration(xt));
#elif defined(BOOST_CHRONO_POSIX_API)
//time_point t(0,0);
timespec ts;
::clock_gettime( CLOCK_REALTIME, &ts );
xtime xt( ts.tv_sec, ts.tv_nsec/1000);
return time_point(duration(xt));
#endif // POSIX
}
void test_xtime_clock()
{
using namespace boost::chrono;
std::cout << "timeval_demo system clock test\n";
std::cout << "sizeof xtime_clock::time_point = " << sizeof(xtime_clock::time_point) << '\n';
std::cout << "sizeof xtime_clock::duration = " << sizeof(xtime_clock::duration) << '\n';
std::cout << "sizeof xtime_clock::rep = " << sizeof(xtime_clock::rep) << '\n';
xtime_clock::duration delay(milliseconds(5));
xtime_clock::time_point start = xtime_clock::now();
while (xtime_clock::now() - start <= delay)
{
}
xtime_clock::time_point stop = xtime_clock::now();
xtime_clock::duration elapsed = stop - start;
std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
}
} // timeval_demo
int main()
{
timeval_demo::test_xtime_clock();
return 0;
}

108
example/xtime.cpp Normal file
View File

@ -0,0 +1,108 @@
// xtime.cpp ----------------------------------------------------------//
// Copyright 2008 Howard Hinnant
// Copyright 2008 Beman Dawes
// Copyright 2009 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 extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Many thanks to Howard for making his code available under the Boost license.
The original code was modified to conform to Boost conventions and to section
20.9 Time utilities [time] of the C++ committee's working paper N2798.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
time2_demo contained this comment:
Much thanks to Andrei Alexandrescu,
Walter Brown,
Peter Dimov,
Jeff Garland,
Terry Golubiewski,
Daniel Krugler,
Anthony Williams.
*/
#include <boost/chrono/chrono.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
using namespace boost::chrono;
// Example round_up utility: converts d to To, rounding up for inexact conversions
// Being able to *easily* write this function is a major feature!
template <class To, class Rep, class Period>
To
round_up(duration<Rep, Period> d)
{
To result = duration_cast<To>(d);
if (result < d)
++result;
return result;
}
// demonstrate interaction with xtime-like facility:
struct xtime
{
long sec;
unsigned long usec;
};
template <class Rep, class Period>
xtime
to_xtime_truncate(duration<Rep, Period> d)
{
xtime xt;
xt.sec = static_cast<long>(duration_cast<seconds>(d).count());
xt.usec = static_cast<long>(duration_cast<microseconds>(d - seconds(xt.sec)).count());
return xt;
}
template <class Rep, class Period>
xtime
to_xtime_round_up(duration<Rep, Period> d)
{
xtime xt;
xt.sec = static_cast<long>(duration_cast<seconds>(d).count());
xt.usec = static_cast<unsigned long>(round_up<microseconds>(d - seconds(xt.sec)).count());
return xt;
}
microseconds
from_xtime(xtime xt)
{
return seconds(xt.sec) + microseconds(xt.usec);
}
void print(xtime xt)
{
std::cout << '{' << xt.sec << ',' << xt.usec << "}\n";
}
void test_with_xtime()
{
std::cout << "test_with_xtime\n";
xtime xt = to_xtime_truncate(seconds(3) + milliseconds(251));
print(xt);
milliseconds ms = duration_cast<milliseconds>(from_xtime(xt));
std::cout << ms.count() << " milliseconds\n";
xt = to_xtime_round_up(ms);
print(xt);
xt = to_xtime_truncate(seconds(3) + nanoseconds(999));
print(xt);
xt = to_xtime_round_up(seconds(3) + nanoseconds(999));
print(xt);
}
int main()
{
test_with_xtime();
return 0;
}

15
src/chrono.cpp Normal file
View File

@ -0,0 +1,15 @@
// chrono.cpp --------------------------------------------------------------//
// Copyright Beman Dawes 2008
// Copyright Vicente J. Botet Escriba 2009-2010
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// define BOOST_CHRONO_SOURCE so that <boost/filesystem/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/chrono.hpp>

19
src/process_clock.cpp Normal file
View File

@ -0,0 +1,19 @@
// boost process_timer.cpp -----------------------------------------------------------//
// Copyright Beman Dawes 1994, 2006, 2008
// Copyright 2009 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//--------------------------------------------------------------------------------------//
// define BOOST_CHRONO_SOURCE so that <boost/chrono/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/process_clock.hpp>

View File

@ -0,0 +1,18 @@
// boost process_cpu_clocks.cpp -----------------------------------------------------------//
// Copyright 2009-2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//--------------------------------------------------------------------------------------//
// define BOOST_CHRONO_SOURCE so that <boost/chrono/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/process_cpu_clocks.hpp>

19
src/run_timer.cpp Normal file
View File

@ -0,0 +1,19 @@
// boost run_timer.cpp ---------------------------------------------------------------//
// Copyright Beman Dawes 1994, 2006, 2008
// Copyright 2009-2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//--------------------------------------------------------------------------------------//
// define BOOST_CHRONO_SOURCE so that <boost/chrono/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/run_timer.hpp>

26
src/run_timer_static.cpp Normal file
View File

@ -0,0 +1,26 @@
// boost run_timer_static.cpp --------------------------------------------------------//
// Copyright Beman Dawes 2008
// Copyright 2009-2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//--------------------------------------------------------------------------------------//
// This function is defined in a separate translation so that it will not be linked
// in except if actually used. This is more efficient because header <iostream> is
// required, and it incurs the cost of the standard stream objects even if they are
// not actually used.
//--------------------------------------------------------------------------------------//
// define BOOST_CHRONO_SOURCE so that <boost/chrono/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/run_timer_static.hpp>

19
src/thread_clock.cpp Normal file
View File

@ -0,0 +1,19 @@
// boost thread_clock.cpp -----------------------------------------------------------//
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
//--------------------------------------------------------------------------------------//
// define BOOST_CHRONO_SOURCE so that <boost/chrono/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_CHRONO_SOURCE
#include <boost/chrono/detail/inlined/thread_clock.hpp>

254
test/Jamfile.v2 Normal file
View File

@ -0,0 +1,254 @@
# Boost Chrono Library test Jamfile
# Copyright Beman Dawes 2008
# Copyright Vicente J. Botet Escriba 2009-2010
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt
# See library home page at http://www.boost.org/libs/chrono
# uncomment one if the above lines if you build outside the Boost release
#local BOOST_ROOT = /boost_1_41_0 ;
#local BOOST_ROOT = c:/cygwin/boost_1_41_0 ;
import os ;
import feature ;
if ! $(BOOST_ROOT)
{
BOOST_ROOT = [ modules.peek : BOOST_ROOT ] ;
}
#feature.feature chrono_level : sh st hd;
#feature.set-default chrono_level : [ hd ] ;
#feature.feature chrono_stub : yes no;
#feature.set-default chrono_stub : [ no ] ;
project
: requirements
<os>LINUX:<threading>multi
#<library>/boost/system//boost_system
#<define>BOOST_SYSTEM_INLINED
# uncomment the line above if you build outside the Boost release
#<include>$(BOOST_ROOT)
# uncomment the line above if you build outside the Boost release
#<include>../../..
<toolset>msvc:<asynch-exceptions>on
<define>BOOST_CHRONO_USES_MPL_ASSERT
<define>BOOST_SYSTEM_NO_DEPRECATED
<warnings>all
<toolset>gcc:<cxxflags>-Wextra
<toolset>gcc:<cxxflags>-pedantic
<toolset>gcc:<cxxflags>-Wno-long-long
<toolset>darwin:<cxxflags>-Wextra
<toolset>darwin:<cxxflags>-pedantic
<toolset>darwin:<cxxflags>-Wno-long-long
<toolset>gcc-mingw-4.5.0:<cxxflags>-Wno-missing-field-initializers
<toolset>gcc-mingw-4.5.0:<cxxflags>-fdiagnostics-show-option
<toolset>msvc:<cxxflags>/wd4127
;
rule chrono-run ( sources )
{
return
[ run $(sources) ../build//boost_chrono
: :
: <define>BOOST_USE_WINDOWS_H
<library>/boost/system//boost_system
: $(sources[1]:B)_shared ]
[ run $(sources) ../build//boost_chrono/<link>static
: :
: <library>/boost/system//boost_system
: $(sources[1]:B)_static ]
# [ run $(sources)
# : :
# : <define>BOOST_CHRONO_INLINED
# # comment one of the following lines
# #<define>BOOST_SYSTEM_INLINED
# <library>/boost/system//boost_system
# : $(sources[1]:B)_header ]
# [ run $(sources)
# : :
# : <define>BOOST_CHRONO_INLINED
# <define>BOOST_USE_WINDOWS_H
# # comment one of the following lines
# #<define>BOOST_SYSTEM_INLINED
# <library>/boost/system//boost_system
# : $(sources[1]:B)_header_win ]
;
}
rule chrono-run2 ( sources : name )
{
return
[ run $(sources) ../build//boost_chrono
: :
: <define>BOOST_USE_WINDOWS_H
<library>/boost/system//boost_system
: $(name)_shared ]
[ run $(sources) ../build//boost_chrono/<link>static
: :
: <library>/boost/system//boost_system
: $(name)_static ]
# [ run $(sources)
# : :
# : <define>BOOST_CHRONO_INLINED
# # comment one of the following lines
# #<define>BOOST_SYSTEM_INLINED
# <library>/boost/system//boost_system
# : $(name)_header ]
# [ run $(sources)
# : :
# : <define>BOOST_CHRONO_INLINED
# <define>BOOST_USE_WINDOWS_H
# # comment one of the following lines
# #<define>BOOST_SYSTEM_INLINED
# <library>/boost/system//boost_system
# : $(name)_header_win ]
;
}
rule chrono-compile ( sources )
{
return
[ compile $(sources)
:
: $(sources[1]:B)_lib ]
# [ compile $(sources)
# : <define>BOOST_CHRONO_INLINED
# # comment the following line
# <define>BOOST_SYSTEM_INLINED
# : $(sources[1]:B)_header ]
# [ compile $(sources)
# : <define>BOOST_CHRONO_INLINED
# <define>BOOST_USE_WINDOWS_H
# # comment the following line
# <define>BOOST_SYSTEM_INLINED
# : $(sources[1]:B)_header_win ]
;
}
rule chrono-compile2 ( sources : name )
{
return
[ compile $(sources)
:
: $(name)_lib ]
# [ compile $(sources)
# : <define>BOOST_CHRONO_INLINED
# # comment the following line
# <define>BOOST_SYSTEM_INLINED
# : $(name)_header ]
# [ compile $(sources)
# : <define>BOOST_CHRONO_INLINED
# <define>BOOST_USE_WINDOWS_H
# # comment the following line
# <define>BOOST_SYSTEM_INLINED
# : $(name)_header_win ]
;
}
test-suite "examples"
:
[ chrono-run ../example/cycle_count.cpp ]
[ chrono-run ../example/runtime_resolution.cpp ]
[ chrono-run ../example/xtime.cpp ]
[ chrono-run ../example/saturating.cpp ]
[ chrono-run ../example/min_time_point.cpp ]
[ chrono-run ../example/i_dont_like_the_default_duration_behavior.cpp ]
[ chrono-run ../example/simulated_thread_interface_demo.cpp ]
[ chrono-run ../example/timeval_demo.cpp ]
[ chrono-run ../example/chrono_unit_test.cpp ]
[ chrono-run ../example/explore_limits.cpp ]
[ chrono-run ../example/test_duration.cpp ]
[ chrono-run ../example/test_clock.cpp ]
[ chrono-run ../example/miscellaneous.cpp ]
[ chrono-run ../example/test_special_values.cpp ]
[ chrono-run ../example/manipulate_clock_object.cpp ]
[ chrono-run ../example/chrono_accuracy_test.cpp ]
[ chrono-run ../example/test_thread_clock.cpp ]
;
# test-suite "timer"
# :
# [ chrono-run ../example/run_timer_example.cpp ]
# [ chrono-run ../example/run_timer_example2.cpp ]
# [ chrono-run run_timer_test.cpp ]
# ;
test-suite "traits"
:
[ chrono-compile2 traits/common_type_duration_pass.cpp : traits_common_type_duration_pass ]
[ chrono-compile2 traits/common_type_time_point_pass.cpp : traits_common_type_time_point_pass ]
[ chrono-compile2 traits/treat_as_floating_point_pass.cpp : traits_treat_as_floating_point_pass ]
[ chrono-run2 traits/duration_values_pass.cpp : traits_duration_values_pass ]
;
test-suite "duration"
:
[ compile-fail duration/duration_duration_fail.cpp ]
[ compile-fail duration/ratio_fail.cpp ]
[ compile-fail duration/positive_num_fail.cpp ]
[ chrono-compile duration/default_ratio_pass.cpp ]
[ chrono-compile duration/types_pass.cpp ]
[ chrono-compile duration/ratio_alias_pass.cpp ]
[ chrono-compile duration/typedefs_pass.cpp ]
[ chrono-run duration/arithmetic_pass.cpp ]
[ chrono-run duration/duration_cast_pass.cpp ]
[ compile-fail duration/duration_cast_int_fail.cpp ]
[ chrono-run duration/comparisons_pass.cpp ]
[ chrono-run duration/constructor_pass.cpp ]
[ compile-fail duration/cons/convert_float_to_int_fail.cpp ]
[ compile-fail duration/cons/convert_inexact_fail.cpp ]
[ compile-fail duration/cons/implicit_constructot_fail.cpp ]
[ compile-fail duration/cons/non_implicit_convertible_rep_fail.cpp ]
[ compile-fail duration/cons/treat_as_floating_point_Rep2_true_fail.cpp ]
[ compile-fail duration/nonmember/divide_rep2_fail.cpp ]
[ compile-fail duration/nonmember/modulus_rep2_fail.cpp ]
[ compile-fail duration/nonmember/times_rep2_lhs_fail.cpp ]
[ compile-fail duration/nonmember/times_rep2_rhs_fail.cpp ]
[ chrono-run duration/duration_values_pass.cpp ]
;
test-suite "time_point"
:
[ chrono-compile2 time_point/default_duration_pass.cpp : time_point.default_duration_pass ]
[ compile-fail time_point/not_duration_fail.cpp : : time_point_not_duration_fail ]
[ chrono-run2 time_point/arithmetic_pass.cpp : time_point_arithmetic_pass ]
[ chrono-run2 time_point/time_point_cast_pass.cpp : time_point_time_point_cast_pass ]
[ compile-fail time_point/time_point_cast_int_fail.cpp : : time_point_time_point_cast_int_fail ]
[ chrono-run2 time_point/comparisons_pass.cpp : time_point_comparisons_pass ]
[ compile-fail time_point/comparisons/equal_fail.cpp : : time_point_equal_fail ]
[ compile-fail time_point/comparisons/less_fail.cpp : : time_point_less_fail ]
[ chrono-run2 time_point/constructor_pass.cpp : time_point_constructor_pass ]
[ compile-fail time_point/cons/implicit_fail.cpp : : time_point_implicit_fail ]
[ compile-fail time_point/cons/non_implicit_convertible_duration_fail.cpp : : time_point_non_implicit_convertible_duration_fail ]
[ chrono-run2 time_point/min_max_pass.cpp : time_point_min_max_pass ]
;
test-suite "clock"
:
[ chrono-run2 clock/clock_pass.cpp : clock_clock_pass_ ]
;
test-suite "io"
:
[ chrono-run ../example/io_ex1.cpp ]
[ chrono-run ../example/io_ex2.cpp ]
[ chrono-run ../example/io_ex3.cpp ]
[ chrono-run ../example/io_ex4.cpp ]
[ chrono-run ../example/io_ex5.cpp ]
;
test-suite "win32"
:
[ chrono-run win32_test.cpp ]
;
# test-suite "static_string"
# :
# [ chrono-run io/match_pass.cpp ]
# ;

30
test/clock.h Normal file
View File

@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#ifndef CLOCK_H
#define CLOCK_H
#include <boost/chrono.hpp>
class Clock
{
typedef boost::chrono::nanoseconds duration;
typedef duration::rep rep;
typedef duration::period period;
typedef boost::chrono::time_point<Clock, duration> time_point;
static const bool is_monotonic = false;
static time_point now();
};
#endif // CLOCK_H

165
test/clock/clock_pass.cpp Normal file
View File

@ -0,0 +1,165 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#include <boost/type_traits/is_same.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono/thread_clock.hpp>
#include <boost/system/system_error.hpp>
#include <boost/detail/lightweight_test.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <typename Clock>
void check_clock_invariants()
{
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::rep, typename Clock::duration::rep>::value), NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::period, typename Clock::duration::period>::value), NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::duration, typename Clock::time_point::duration>::value), NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT(Clock::is_monotonic || !Clock::is_monotonic, NOTHING, ());
// to be replaced by has static member bool is_monotonic
}
template <typename Clock>
void check_clock_now()
{
typename Clock::time_point t1 = Clock::now();
}
template <typename Clock>
void check_clock_now_ec()
{
boost::system::error_code ec;
typename Clock::time_point t1 = Clock::now(ec);
BOOST_TEST(ec.value()==0);
}
template <typename Clock>
void check_clock_now_throws()
{
typename Clock::time_point t1 = Clock::now(boost::throws());
}
template <typename Clock>
void check_clock_now_err(int err)
{
Clock::set_errno(err);
try {
typename Clock::time_point t1 = Clock::now();
} catch (boost::system::system_error& ex) {
BOOST_TEST(ex.code().value()==err);
// BOOST_TEST(ex.code().category() == BOOST_CHRONO_SYSTEM_CATEGORY);
// BOOST_TEST(std::string(ex.what()) == std::string("errored_clock"));
}
Clock::set_errno(0);
}
template <typename Clock>
void check_clock_now_ec_err(int err)
{
Clock::set_errno(err);
boost::system::error_code ec;
typename Clock::time_point t1 = Clock::now(ec);
BOOST_TEST(ec.value()==err);
// BOOST_TEST(ec.category() == BOOST_CHRONO_SYSTEM_CATEGORY);
Clock::set_errno(0);
}
template <typename Clock>
void check_clock_now_throws_err(int err)
{
Clock::set_errno(err);
try {
typename Clock::time_point t1 = Clock::now(boost::throws());
BOOST_TEST(0&&"exception not thown");
} catch (boost::system::system_error& ex) {
BOOST_TEST(ex.code().value()==err);
// BOOST_TEST(ex.code().category() == BOOST_CHRONO_SYSTEM_CATEGORY);
// BOOST_TEST(std::string(ex.what()) == std::string("errored_clock"));
}
Clock::set_errno(0);
}
int main()
{
check_clock_invariants<boost::chrono::high_resolution_clock>();
check_clock_now<boost::chrono::high_resolution_clock>();
check_clock_now_ec<boost::chrono::high_resolution_clock>();
check_clock_now_throws<boost::chrono::high_resolution_clock>();
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
check_clock_invariants<boost::chrono::monotonic_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::monotonic_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::monotonic_clock>();
check_clock_now_ec<boost::chrono::monotonic_clock>();
check_clock_now_throws<boost::chrono::monotonic_clock>();
#endif
check_clock_invariants<boost::chrono::system_clock>();
BOOST_CHRONO_STATIC_ASSERT(!boost::chrono::system_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::system_clock>();
check_clock_now_ec<boost::chrono::system_clock>();
check_clock_now_throws<boost::chrono::system_clock>();
{
typedef boost::chrono::system_clock C;
C::time_point t1 = C::from_time_t(C::to_time_t(C::now()));
(void)t1;
}
{
typedef boost::chrono::system_clock C;
std::time_t t1 = C::to_time_t(C::now());
(void)t1;
}
{
BOOST_TEST((boost::chrono::system_clock::duration::min)() <
boost::chrono::system_clock::duration::zero());
}
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
check_clock_invariants<boost::chrono::thread_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::thread_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::thread_clock>();
check_clock_now_ec<boost::chrono::thread_clock>();
check_clock_now_throws<boost::chrono::thread_clock>();
#endif
check_clock_invariants<boost::chrono::process_real_cpu_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_real_cpu_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::process_real_cpu_clock>();
check_clock_now_ec<boost::chrono::process_real_cpu_clock>();
check_clock_now_throws<boost::chrono::process_real_cpu_clock>();
check_clock_invariants<boost::chrono::process_user_cpu_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_user_cpu_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::process_user_cpu_clock>();
check_clock_now_ec<boost::chrono::process_user_cpu_clock>();
check_clock_now_throws<boost::chrono::process_user_cpu_clock>();
check_clock_invariants<boost::chrono::process_system_cpu_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_system_cpu_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::process_system_cpu_clock>();
check_clock_now_ec<boost::chrono::process_system_cpu_clock>();
check_clock_now_throws<boost::chrono::process_system_cpu_clock>();
check_clock_invariants<boost::chrono::process_cpu_clock>();
BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_cpu_clock::is_monotonic, NOTHING, ());
check_clock_now<boost::chrono::process_cpu_clock>();
check_clock_now_ec<boost::chrono::process_cpu_clock>();
check_clock_now_throws<boost::chrono::process_cpu_clock>();
return boost::report_errors();
}

View File

@ -0,0 +1,62 @@
// errored_clock.hpp --------------------------------------------------------------//
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_CHRONO_ERRORED_CLOCKS_HPP
#define BOOST_CHRONO_ERRORED_CLOCKS_HPP
#include <boost/chrono/config.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/chrono/time_point.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <boost/chrono/detail/system.hpp>
class errored_clock
{
public:
typedef boost::chrono::nanoseconds duration;
typedef duration::rep rep;
typedef duration::period period;
typedef boost::chrono::time_point<errored_clock> time_point;
static const bool is_monotonic = true;
static int errno_;
static void set_errno(int err) {
errno_=err;
}
// throws on error
static time_point now() {
boost::throw_exception(
boost::system::system_error(
errno_,
BOOST_CHRONO_SYSTEM_CATEGORY,
"errored_clock"
)
);
return time_point();
}
// never throws and set ec
static time_point now(boost::system::error_code & ec) {
if (BOOST_CHRONO_IS_THROWS(ec))
{
boost::throw_exception(
boost::system::system_error(
errno_,
BOOST_CHRONO_SYSTEM_CATEGORY,
"errored_clock"
)
);
}
ec.assign( errno_, BOOST_CHRONO_SYSTEM_CATEGORY );
return time_point();
};
};
int errored_clock::errno_;
#endif

View File

@ -0,0 +1,225 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/chrono/typeof/boost/chrono/chrono.hpp>
#include <boost/detail/lightweight_test.hpp>
int main()
{
// UNARY PLUS
{
const boost::chrono::minutes m(3);
boost::chrono::minutes m2 = +m;
BOOST_TEST(m.count() == m2.count());
}
// UNARY MINUS
{
const boost::chrono::minutes m(3);
boost::chrono::minutes m2 = -m;
BOOST_TEST(m2.count() == -m.count());
}
// PRE INCREMENT
{
boost::chrono::hours h(3);
boost::chrono::hours& href = ++h;
BOOST_TEST(&href == &h);
BOOST_TEST(h.count() == 4);
}
// POST INCREMENT
{
boost::chrono::hours h(3);
boost::chrono::hours h2 = h++;
BOOST_TEST(h.count() == 4);
BOOST_TEST(h2.count() == 3);
}
// PRE DECREMENT
{
boost::chrono::hours h(3);
boost::chrono::hours& href = --h;
BOOST_TEST(&href == &h);
BOOST_TEST(h.count() == 2);
}
// POST DECREMENT
{
boost::chrono::hours h(3);
boost::chrono::hours h2 = h--;
BOOST_TEST(h.count() == 2);
BOOST_TEST(h2.count() == 3);
}
// PLUS ASSIGN
{
boost::chrono::seconds s(3);
s += boost::chrono::seconds(2);
BOOST_TEST(s.count() == 5);
s += boost::chrono::minutes(2);
BOOST_TEST(s.count() == 125);
}
// MINUS ASSIGN
{
boost::chrono::seconds s(3);
s -= boost::chrono::seconds(2);
BOOST_TEST(s.count() == 1);
s -= boost::chrono::minutes(2);
BOOST_TEST(s.count() == -119);
}
// TIMES ASSIGN
{
boost::chrono::nanoseconds ns(3);
ns *= 5;
BOOST_TEST(ns.count() == 15);
}
// DIVIDE ASSIGN
{
boost::chrono::nanoseconds ns(15);
ns /= 5;
BOOST_TEST(ns.count() == 3);
}
// MODULUS ASSIGN duration
{
boost::chrono::microseconds us(11);
boost::chrono::microseconds us2(3);
us %= us2;
BOOST_TEST(us.count() == 2);
us %= boost::chrono::milliseconds(3);
BOOST_TEST(us.count() == 2);
}
// MODULUS ASSIGN Rep
{
boost::chrono::microseconds us(11);
us %= 3;
BOOST_TEST(us.count() == 2);
}
// PLUS
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(5);
boost::chrono::seconds r = s1 + s2;
BOOST_TEST(r.count() == 8);
}
{
boost::chrono::seconds s1(3);
boost::chrono::microseconds s2(5);
boost::chrono::microseconds r = s1 + s2;
BOOST_TEST(r.count() == 3000005);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(3);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(5);
boost::chrono::duration<int, boost::ratio<1, 15> > r = s1 + s2;
BOOST_TEST(r.count() == 75);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(3);
boost::chrono::duration<double, boost::ratio<3, 5> > s2(5);
boost::chrono::duration<double, boost::ratio<1, 15> > r = s1 + s2;
BOOST_TEST(r.count() == 75);
}
// MINUS
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(5);
boost::chrono::seconds r = s1 - s2;
BOOST_TEST(r.count() == -2);
}
{
boost::chrono::seconds s1(3);
boost::chrono::microseconds s2(5);
boost::chrono::microseconds r = s1 - s2;
BOOST_TEST(r.count() == 2999995);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(3);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(5);
boost::chrono::duration<int, boost::ratio<1, 15> > r = s1 - s2;
BOOST_TEST(r.count() == -15);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(3);
boost::chrono::duration<double, boost::ratio<3, 5> > s2(5);
boost::chrono::duration<double, boost::ratio<1, 15> > r = s1 - s2;
BOOST_TEST(r.count() == -15);
}
// TIMES rep
{
boost::chrono::nanoseconds ns(3);
ns = ns * 5;
BOOST_TEST(ns.count() == 15);
ns = 6 * ns;
BOOST_TEST(ns.count() == 90);
}
// DIVIDE duration
{
boost::chrono::nanoseconds ns1(15);
boost::chrono::nanoseconds ns2(5);
BOOST_TEST(ns1 / ns2 == 3);
}
{
boost::chrono::microseconds us1(15);
boost::chrono::nanoseconds ns2(5);
BOOST_TEST(us1 / ns2 == 3000);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(30);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(5);
BOOST_TEST(s1 / s2 == 6);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(30);
boost::chrono::duration<double, boost::ratio<3, 5> > s2(5);
BOOST_TEST(s1 / s2 == 20./3);
}
// DIVIDE rep
{
boost::chrono::nanoseconds ns(15);
ns = ns / 5;
BOOST_TEST(ns.count() == 3);
}
// MODULUS duration
{
boost::chrono::nanoseconds ns1(15);
boost::chrono::nanoseconds ns2(6);
boost::chrono::nanoseconds r = ns1 % ns2;
BOOST_TEST(r.count() == 3);
}
{
boost::chrono::microseconds us1(15);
boost::chrono::nanoseconds ns2(28);
boost::chrono::nanoseconds r = us1 % ns2;
BOOST_TEST(r.count() == 20);
}
{
boost::chrono::duration<int, boost::ratio<3, 5> > s1(6);
boost::chrono::duration<int, boost::ratio<2, 3> > s2(3);
boost::chrono::duration<int, boost::ratio<1, 15> > r = s1 % s2;
BOOST_TEST(r.count() == 24);
}
// MODULUS rep
{
boost::chrono::nanoseconds ns(15);
ns = ns % 6;
BOOST_TEST(ns.count() == 3);
}
return boost::report_errors();
}

View File

@ -0,0 +1,118 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
int main()
{
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(3);
BOOST_TEST(s1 == s2);
BOOST_TEST(!(s1 != s2));
}
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(4);
BOOST_TEST(!(s1 == s2));
BOOST_TEST(s1 != s2);
}
{
boost::chrono::milliseconds s1(3);
boost::chrono::microseconds s2(3000);
BOOST_TEST(s1 == s2);
BOOST_TEST(!(s1 != s2));
}
{
boost::chrono::milliseconds s1(3);
boost::chrono::microseconds s2(4000);
BOOST_TEST(!(s1 == s2));
BOOST_TEST(s1 != s2);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(9);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(10);
BOOST_TEST(s1 == s2);
BOOST_TEST(!(s1 != s2));
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(10);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(9);
BOOST_TEST(!(s1 == s2));
BOOST_TEST(s1 != s2);
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(9);
boost::chrono::duration<double, boost::ratio<3, 5> > s2(10);
BOOST_TEST(s1 == s2);
BOOST_TEST(!(s1 != s2));
}
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(3);
BOOST_TEST(!(s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST( (s1 >= s2));
}
{
boost::chrono::seconds s1(3);
boost::chrono::seconds s2(4);
BOOST_TEST( (s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST(!(s1 >= s2));
}
{
boost::chrono::milliseconds s1(3);
boost::chrono::microseconds s2(3000);
BOOST_TEST(!(s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST( (s1 >= s2));
}
{
boost::chrono::milliseconds s1(3);
boost::chrono::microseconds s2(4000);
BOOST_TEST( (s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST(!(s1 >= s2));
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(9);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(10);
BOOST_TEST(!(s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST( (s1 >= s2));
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(10);
boost::chrono::duration<int, boost::ratio<3, 5> > s2(9);
BOOST_TEST(!(s1 < s2));
BOOST_TEST( (s1 > s2));
BOOST_TEST(!(s1 <= s2));
BOOST_TEST( (s1 >= s2));
}
{
boost::chrono::duration<int, boost::ratio<2, 3> > s1(9);
boost::chrono::duration<double, boost::ratio<3, 5> > s2(10);
BOOST_TEST(!(s1 < s2));
BOOST_TEST(!(s1 > s2));
BOOST_TEST( (s1 <= s2));
BOOST_TEST( (s1 >= s2));
}
return boost::report_errors();
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// conversions from floating point to integral durations disallowed
#include <boost/chrono.hpp>
void test()
{
boost::chrono::duration<double> d;
boost::chrono::duration<int> i = d;
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// inexact conversions disallowed for integral reps
#include <boost/chrono.hpp>
void test()
{
boost::chrono::microseconds us(1);
boost::chrono::milliseconds ms = us;
}

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// test for explicit
#include <boost/chrono.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<int> d = 1;
}

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Rep2 shall be implicitly convertible to rep
#include <boost/chrono.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<Rep> d(1);
}

View File

@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// treat_as_floating_point<Rep2>::value must be false
#include <boost/chrono.hpp>
void test()
{
boost::chrono::duration<int> d(1.);
}

View File

@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <libs/chrono/test/rep.h>
template <class D>
void
check_default()
{
D d;
BOOST_TEST(d.count() == typename D::rep());
}
template <class D, class R>
void
check_from_rep(R r)
{
D d(r);
BOOST_TEST(d.count() == r);
}
int main()
{
// exact conversions allowed for integral reps
{
boost::chrono::milliseconds ms(1);
boost::chrono::microseconds us = ms;
BOOST_TEST(us.count() == 1000);
}
// inexact conversions allowed for floating point reps
{
boost::chrono::duration<double, boost::micro> us(1);
boost::chrono::duration<double, boost::milli> ms = us;
BOOST_TEST(ms.count() == 1./1000);
}
// Convert int to float
{
boost::chrono::duration<int> i(3);
boost::chrono::duration<int> d = i;
BOOST_TEST(d.count() == 3);
}
// default constructor
{
check_default<boost::chrono::duration<Rep> >();
}
// constructor from rep
{
check_from_rep<boost::chrono::duration<int> >(5);
check_from_rep<boost::chrono::duration<int, boost::ratio<3, 2> > >(5);
check_from_rep<boost::chrono::duration<Rep, boost::ratio<3, 2> > >(Rep(3));
check_from_rep<boost::chrono::duration<double, boost::ratio<2, 3> > >(5.5);
}
// constructor from other rep
{
boost::chrono::duration<double> d(5);
BOOST_TEST(d.count() == 5);
return boost::report_errors();
}
return boost::report_errors();
}

View File

@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// Test default template arg:
// template <class Rep, class Period = ratio<1>>
// class duration;
#include <boost/chrono.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<
boost::chrono::duration<int, boost::ratio<1> >,
boost::chrono::duration<int>
>::value), NOTHING, ());

View File

@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// ToDuration must be an instantiation of duration.
#include <boost/chrono.hpp>
void test()
{
boost::chrono::duration_cast<int>(boost::chrono::milliseconds(3));
}

View File

@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#include <boost/detail/lightweight_test.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <class ToDuration, class FromDuration>
void
test(const FromDuration& f, const ToDuration& d)
{
#if defined(BOOST_NO_DECLTYPE)
typedef BOOST_TYPEOF_TPL(boost::chrono::duration_cast<ToDuration>(f)) R;
#else
typedef decltype(boost::chrono::duration_cast<ToDuration>(f)) R;
#endif
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<R, ToDuration>::value), NOTHING, ());
BOOST_TEST(boost::chrono::duration_cast<ToDuration>(f) == d);
}
int main()
{
test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2));
test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121));
test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265));
test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000));
test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL));
test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL));
test(boost::chrono::milliseconds(7265000),
boost::chrono::duration<double, boost::ratio<3600> >(7265./3600));
test(boost::chrono::duration<int, boost::ratio<2, 3> >(9),
boost::chrono::duration<int, boost::ratio<3, 5> >(10));
return boost::report_errors();
}

View File

@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// If a program instantiates duration with a duration type for the template
// argument Rep a diagnostic is required.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::duration<boost::chrono::milliseconds> D;
D d;
}

View File

@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <libs/chrono/test/rep.h>
template <class D>
void check_max()
{
typedef typename D::rep Rep;
Rep max_rep = (boost::chrono::duration_values<Rep>::max)();
BOOST_TEST((D::max)().count() == max_rep);
}
template <class D>
void check_min()
{
typedef typename D::rep Rep;
Rep min_rep = (boost::chrono::duration_values<Rep>::min)();
BOOST_TEST((D::min)().count() == min_rep);
}
template <class D>
void check_zero()
{
typedef typename D::rep Rep;
Rep zero_rep = boost::chrono::duration_values<Rep>::zero();
BOOST_TEST(D::zero().count() == zero_rep);
}
int main()
{
check_max<boost::chrono::duration<int> >();
check_max<boost::chrono::duration<Rep> >();
check_min<boost::chrono::duration<int> >();
check_min<boost::chrono::duration<Rep> >();
check_zero<boost::chrono::duration<int> >();
check_zero<boost::chrono::duration<Rep> >();
return boost::report_errors();
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<Rep> d(Rep(15));
d = d / 5;
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<Rep> d(Rep(15));
d = d % 5;
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<Rep> d;
d = 5 * d;
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include "../../rep.h"
void test()
{
boost::chrono::duration<Rep> d;
d = d * 5;
}

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// Period::num must be positive, diagnostic required.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::duration<int, boost::ratio<5, -1> > D;
D d;
}

View File

@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// Period shall be a specialization of ratio, diagnostic required.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::duration<int,
boost::ratio_add<
boost::ratio<1,2>,
boost::ratio<1,3>
>
> D;
D d;
}

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// Period shall be a specialization of ratio, diagnostic required.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::duration<int, int > D;
D d;
}

View File

@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#include <limits>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <typename D, int ExpectedDigits, typename ExpectedPeriod>
void check_duration()
{
typedef typename D::rep Rep;
typedef typename D::period Period;
BOOST_CHRONO_STATIC_ASSERT(boost::is_signed<Rep>::value, NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT(boost::is_integral<Rep>::value, NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT(std::numeric_limits<Rep>::digits >= ExpectedDigits, NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<Period, ExpectedPeriod >::value), NOTHING, ());
}
void test()
{
check_duration<boost::chrono::hours, 22, boost::ratio<3600> >();
check_duration<boost::chrono::minutes, 28, boost::ratio<60> >();
check_duration<boost::chrono::seconds, 34, boost::ratio<1> >();
check_duration<boost::chrono::milliseconds, 44, boost::milli >();
check_duration<boost::chrono::microseconds, 54, boost::micro >();
check_duration<boost::chrono::nanoseconds, 63, boost::nano >();
}

View File

@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// duration
// Test nested types
// typedef Rep rep;
// typedef Period period;
#include <boost/chrono.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
typedef boost::chrono::duration<long, boost::ratio<3, 2> > D;
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<D::rep, long>::value), NOTHING, ());
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<D::period, boost::ratio<3, 2> >::value), NOTHING, ());

19
test/intmax_c.cpp Normal file
View File

@ -0,0 +1,19 @@
// intmax_c.cpp --------------------------------------------------------------//
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#include <boost/cstdint.hpp>
#ifdef INTMAX_C
#define BOOST_INTMAX_C(a) INTMAX_C(a)
#else
#define BOOST_INTMAX_C(a) a##LL
#endif
boost::intmax_t i = BOOST_INTMAX_C(1000000000);
int main() {
return (i);
}

31
test/rep.h Normal file
View File

@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#ifndef REP_H
#define REP_H
class Rep
{
public:
int data_;
Rep() : data_(-1) {}
explicit Rep(int i) : data_(i) {}
bool operator==(int i) const {return data_ == i;}
bool operator==(const Rep& r) const {return data_ == r.data_;}
Rep& operator*=(Rep x) {data_ *= x.data_; return *this;}
Rep& operator/=(Rep x) {data_ /= x.data_; return *this;}
};
#endif // REP_H

226
test/run_timer_test.cpp Normal file
View File

@ -0,0 +1,226 @@
// boost run_timer_test.cpp -----------------------------------------------------//
// Copyright Beman Dawes 2006, 2008
// Copyright 2009 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#include <boost/chrono/process_times.hpp>
#include <boost/chrono/timer.hpp>
#include <cstdlib> // for atol()
#include <iostream>
#include <sstream>
#include <locale>
#include <ctime>
#include <cmath> // for sqrt(), used to burn time
using boost::chrono::run_timer;
using boost::system::error_code;
#include <boost/detail/lightweight_test.hpp>
//#define BOOST_TEST(expr) if (!(expr)) std::cout << "*****ERROR*****\n"
#define CHECK_REPORT(Timer,String_Stream,R,U,S,Expected_String) \
check_report(Timer, String_Stream, R, U, S, Expected_String, __LINE__)
namespace
{
typedef boost::chrono::nanoseconds ns;
bool check_report( run_timer & tmr, std::stringstream & ss,
run_timer::duration r, run_timer::duration u, run_timer::duration s,
const std::string & expected, int line )
{
tmr.test_report(r,u,s);
bool result(true);
if ( ss.str() != expected )
{
std::cout << "run_timer_test.cpp(" << line << ") : error: actual output \""
<< ss.str() << "\" != expected \"" << expected << "\"\n";
result = false;
}
return result;
}
void run_timer_constructor_overload_test()
{
// exercise each supported combination of constructor arguments
std::ostream & os = std::cout;
const int pl = 9;
boost::system::error_code ec;
run_timer t1;
run_timer t2( os );
run_timer t3( ec );
run_timer t4( os, ec );
run_timer t5( pl );
run_timer t6( os, pl );
run_timer t7( pl, ec );
run_timer t8( os, pl, ec );
run_timer t9( "t9, default places, r %r, c %c, p %p, u %u, s %s\n" );
run_timer t10( os, "t10, default places, r %r, c %c, p %p, u %u, s %s\n" );
run_timer t11( "t11, default places, r %r, c %c, p %p, u %u, s %s\n", ec );
run_timer t12( os, "t12, default places, r %r, c %c, p %p, u %u, s %s\n", ec );
run_timer t13( pl, "t13, explicitly code places, r %r, c %c, p %p, u %u, s %s\n" );
run_timer t14( "t14, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", pl );
run_timer t15( os, pl, "t15, explicitly code places, r %r, c %c, p %p, u %u, s %s\n" );
run_timer t16( os, "t16, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", pl );
run_timer t17( pl, "t17, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", ec );
run_timer t18( "t18, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", pl, ec );
run_timer t19( os, pl, "t19, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", ec );
run_timer t20( os, "t20, explicitly code places, r %r, c %c, p %p, u %u, s %s\n", pl, ec );
std::cout << "Burn some time so run_timers have something to report...";
boost::chrono::timer<boost::chrono::high_resolution_clock> t;
while ( t.elapsed() < boost::chrono::seconds(1) ) {}
std::cout << "\n";
std::cout << run_timer::default_places() << " default places\n";
std::cout << pl << " explicitly coded places\n";
}
// accuracy test
void accuracy_test( int argc, char * argv[] )
{
long timeout_in_secs = 1;
if ( argc > 1 ) timeout_in_secs = std::atol( argv[1] );
std::cout << "accuracy test for " << timeout_in_secs << " second(s)...";
std::clock_t timeout_in_clock_t = std::clock();
timeout_in_clock_t += (timeout_in_secs * CLOCKS_PER_SEC);
boost::chrono::system_timer sys;
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
boost::chrono::monotonic_timer mono;
#endif
boost::chrono::high_resolution_timer hires;
boost::chrono::process_timer process;
std::clock_t now;
do
{
now = std::clock();
} while ( now < timeout_in_clock_t );
boost::chrono::system_timer::duration sys_dur = sys.elapsed();
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
boost::chrono::monotonic_timer::duration mono_dur = mono.elapsed();
#endif
boost::chrono::high_resolution_timer::duration hires_dur = hires.elapsed();
boost::chrono::process_times times;
process.elapsed( times );
std::cout << std::endl;
ns timeout_in_nanoseconds( static_cast<long long>(timeout_in_secs) * 1000000000LL );
// Allow 20% leeway. Particularly on Linux, there seems to be a large discrepancy
// between std::clock() and higher resolution clocks.
ns maximum_delta ( static_cast<long long>(timeout_in_nanoseconds.count() * 0.20 ) );
std::cout << timeout_in_nanoseconds.count() << " timeout_in_nanoseconds\n";
std::cout << maximum_delta.count() << " maximum_delta\n";
std::cout << sys_dur.count() << " sys_dur\n";
BOOST_TEST( sys_dur > timeout_in_nanoseconds - maximum_delta
&& sys_dur < timeout_in_nanoseconds + maximum_delta );
#ifdef BOOST_CHRONO_HAS_CLOCK_MONOTONIC
std::cout << mono_dur.count() << " mono_dur\n";
BOOST_TEST( mono_dur > timeout_in_nanoseconds - maximum_delta
&& mono_dur < timeout_in_nanoseconds + maximum_delta );
#endif
std::cout << hires_dur.count() << " hires_dur\n";
BOOST_TEST( hires_dur > timeout_in_nanoseconds - maximum_delta
&& hires_dur < timeout_in_nanoseconds + maximum_delta );
std::cout << times.real.count() << " times.real\n";
BOOST_TEST( times.real > timeout_in_nanoseconds - maximum_delta
&& times.real < timeout_in_nanoseconds + maximum_delta );
}
// report test
void report_test()
{
{
std::stringstream ss;
run_timer t(ss);
BOOST_TEST( CHECK_REPORT(t, ss, ns(0), ns(0), ns(0),
"\nreal 0.000s, cpu 0.000s (0.0%), user 0.000s, system 0.000s\n" ) );
}
{
std::stringstream ss;
run_timer t(ss);
BOOST_TEST( CHECK_REPORT(t, ss, ns(3000000000LL), ns(2000000000LL), ns(1000000000LL),
"\nreal 3.000s, cpu 3.000s (100.0%), user 2.000s, system 1.000s\n" ) );
}
{
std::stringstream ss;
run_timer t( ss, "9 places: r %r, c %c, p %p, u %u, s %s", 9 );
BOOST_TEST( CHECK_REPORT(t, ss, ns(3000000003LL), ns(2000000002LL), ns(1000000001LL),
"9 places: "
"r 3.000000003, c 3.000000003, p 100.0, u 2.000000002, s 1.000000001" ) );
}
}
// process_timer_test
void process_timer_test()
{
std::cout << "process_timer_test..." << std::flush;
boost::chrono::process_timer t;
double res=0; // avoids optimization
for (long i = 0; i < 10000000L; ++i)
{
res+=std::sqrt( static_cast<double>(i) ); // avoids optimization
}
boost::chrono::process_times times;
times.real = times.system = times.user = ns(0);
BOOST_TEST( times.real == ns(0) );
BOOST_TEST( times.user == ns(0) );
BOOST_TEST( times.system == ns(0) );
t.elapsed( times );
std::cout << "\n";
std::cout << times.real.count() << " times.real\n";
std::cout << times.user.count() << " times.user\n";
std::cout << times.system.count() << " times.system\n";
std::cout << (times.user+times.system).count() << " times.user+system\n";
BOOST_TEST( times.real > ns(1) );
BOOST_TEST( times.user+times.system > ns(1) );
std::cout << "complete " << res << std::endl;
}
}
int main( int argc, char * argv[] )
{
std::locale loc( "" ); // test with appropriate locale
std::cout.imbue( loc );
accuracy_test( argc, argv );
run_timer_constructor_overload_test();
process_timer_test();
report_test();
return boost::report_errors();
}

View File

@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
int main()
{
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
boost::chrono::time_point<Clock, Duration> t(Duration(3));
t += Duration(2);
BOOST_TEST(t.time_since_epoch() == Duration(5));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
boost::chrono::time_point<Clock, Duration> t(Duration(3));
t -= Duration(2);
BOOST_TEST(t.time_since_epoch() == Duration(1));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
boost::chrono::time_point<Clock, Duration1> t1(Duration1(3));
boost::chrono::time_point<Clock, Duration2> t2 = t1 - Duration2(5);
BOOST_TEST(t2.time_since_epoch() == Duration2(2995));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
boost::chrono::time_point<Clock, Duration1> t1(Duration1(3));
boost::chrono::time_point<Clock, Duration2> t2(Duration2(5));
BOOST_TEST((t1 - t2) == Duration2(2995));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
boost::chrono::time_point<Clock, Duration1> t1(Duration1(3));
boost::chrono::time_point<Clock, Duration2> t2 = t1 + Duration2(5);
BOOST_TEST(t2.time_since_epoch() == Duration2(3005));
t2 = Duration2(6) + t1;
BOOST_TEST(t2.time_since_epoch() == Duration2(3006));
}
return boost::report_errors();
}

View File

@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// time_points with different clocks should not compare
#include <boost/chrono.hpp>
#include "../../clock.h"
void test()
{
typedef boost::chrono::system_clock Clock1;
typedef Clock Clock2;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
typedef boost::chrono::time_point<Clock1, Duration1> T1;
typedef boost::chrono::time_point<Clock2, Duration2> T2;
T1 t1(Duration1(3));
T2 t2(Duration2(3000));
t1 == t2;
}

View File

@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// time_points with different clocks should not compare
#include <boost/chrono.hpp>
#include "../../clock.h"
void test()
{
typedef boost::chrono::system_clock Clock1;
typedef Clock Clock2;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
typedef boost::chrono::time_point<Clock1, Duration1> T1;
typedef boost::chrono::time_point<Clock2, Duration2> T2;
T1 t1(Duration1(3));
T2 t2(Duration2(3000));
t1 < t2;
}

View File

@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
int main()
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
typedef boost::chrono::time_point<Clock, Duration1> T1;
typedef boost::chrono::time_point<Clock, Duration2> T2;
{
T1 t1(Duration1(3));
T1 t2(Duration1(3));
BOOST_TEST( (t1 == t2));
BOOST_TEST(!(t1 != t2));
}
{
T1 t1(Duration1(3));
T1 t2(Duration1(4));
BOOST_TEST(!(t1 == t2));
BOOST_TEST( (t1 != t2));
}
{
T1 t1(Duration1(3));
T2 t2(Duration2(3000));
BOOST_TEST( (t1 == t2));
BOOST_TEST(!(t1 != t2));
}
{
T1 t1(Duration1(3));
T2 t2(Duration2(3001));
BOOST_TEST(!(t1 == t2));
BOOST_TEST( (t1 != t2));
}
{
T1 t1(Duration1(3));
T1 t2(Duration1(3));
BOOST_TEST(!(t1 < t2));
BOOST_TEST(!(t1 > t2));
BOOST_TEST( (t1 <= t2));
BOOST_TEST( (t1 >= t2));
}
{
T1 t1(Duration1(3));
T1 t2(Duration1(4));
BOOST_TEST( (t1 < t2));
BOOST_TEST(!(t1 > t2));
BOOST_TEST( (t1 <= t2));
BOOST_TEST(!(t1 >= t2));
}
{
T1 t1(Duration1(3));
T2 t2(Duration2(3000));
BOOST_TEST(!(t1 < t2));
BOOST_TEST(!(t1 > t2));
BOOST_TEST( (t1 <= t2));
BOOST_TEST( (t1 >= t2));
}
{
T1 t1(Duration1(3));
T2 t2(Duration2(3001));
BOOST_TEST( (t1 < t2));
BOOST_TEST(!(t1 > t2));
BOOST_TEST( (t1 <= t2));
BOOST_TEST(!(t1 >= t2));
}
return boost::report_errors();
}

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// test for explicit
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
boost::chrono::time_point<Clock, Duration> t = Duration(3);
}

View File

@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Duration2 shall be implicitly convertible to duration.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration1;
typedef boost::chrono::microseconds Duration2;
{
boost::chrono::time_point<Clock, Duration2> t2(Duration2(3));
boost::chrono::time_point<Clock, Duration1> t1 = t2;
}
}

View File

@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <libs/chrono/test/rep.h>
int main()
{
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::microseconds Duration1;
typedef boost::chrono::milliseconds Duration2;
boost::chrono::time_point<Clock, Duration2> t2(Duration2(3));
boost::chrono::time_point<Clock, Duration1> t1 = t2;
BOOST_TEST(t1.time_since_epoch() == Duration1(3000));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::duration<Rep, boost::milli> Duration;
boost::chrono::time_point<Clock, Duration> t;
BOOST_TEST(t.time_since_epoch() == Duration::zero());
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
boost::chrono::time_point<Clock, Duration> t(Duration(3));
BOOST_TEST(t.time_since_epoch() == Duration(3));
}
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
boost::chrono::time_point<Clock, Duration> t(boost::chrono::seconds(3));
BOOST_TEST(t.time_since_epoch() == Duration(3000));
}
return boost::report_errors();
}

View File

@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
void test()
{
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<boost::chrono::system_clock::duration,
boost::chrono::time_point<boost::chrono::system_clock>::duration>::value), NOTHING, ());
}

View File

@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
void test()
{
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<boost::chrono::system_clock::duration,
boost::chrono::time_point<boost::chrono::system_clock>::duration>::value), NOTHING, ());
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Duration shall be an instance of duration.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::time_point<boost::chrono::system_clock, int> T;
T t;
}

View File

@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/detail/lightweight_test.hpp>
int main()
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::milliseconds Duration;
typedef boost::chrono::time_point<Clock, Duration> TP;
BOOST_TEST((TP::min)() == TP((Duration::min)()));
BOOST_TEST((TP::max)() == TP((Duration::max)()));
return boost::report_errors();
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Duration shall be an instance of duration.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::time_point<boost::chrono::system_clock, int> T;
T t;
}

View File

@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// ToDuration shall be an instantiation of duration.
#include <boost/chrono.hpp>
void test()
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::time_point<Clock, boost::chrono::milliseconds> FromTimePoint;
typedef boost::chrono::time_point<Clock, boost::chrono::minutes> ToTimePoint;
boost::chrono::time_point_cast<ToTimePoint>(FromTimePoint(boost::chrono::milliseconds(3)));
}

View File

@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#include <boost/detail/lightweight_test.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <class FromDuration, class ToDuration>
void
test(const FromDuration& df, const ToDuration& d)
{
typedef boost::chrono::system_clock Clock;
typedef boost::chrono::time_point<Clock, FromDuration> FromTimePoint;
typedef boost::chrono::time_point<Clock, ToDuration> ToTimePoint;
FromTimePoint f(df);
ToTimePoint t(d);
#if defined(BOOST_NO_DECLTYPE)
typedef BOOST_TYPEOF_TPL(boost::chrono::time_point_cast<ToDuration>(f)) R;
#else
typedef decltype(boost::chrono::time_point_cast<ToDuration>(f)) R;
#endif
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<R, ToTimePoint>::value), NOTHING, ());
BOOST_TEST(boost::chrono::time_point_cast<ToDuration>(f) == t);
}
int main()
{
test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2));
test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121));
test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265));
test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000));
test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL));
test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL));
test(boost::chrono::milliseconds(7265000),
boost::chrono::duration<double, boost::ratio<3600> >(7265./3600));
test(boost::chrono::duration<int, boost::ratio<2, 3> >(9),
boost::chrono::duration<int, boost::ratio<3, 5> >(10));
return boost::report_errors();
}

View File

@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits/is_same.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <class D1, class D2, class De>
void
test()
{
typedef typename boost::common_type<D1, D2>::type Dc;
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<Dc, De>::value), NOTHING, ());
}
void testall()
{
test<boost::chrono::duration<int, boost::ratio<1, 100> >,
boost::chrono::duration<long, boost::ratio<1, 1000> >,
boost::chrono::duration<long, boost::ratio<1, 1000> > >();
test<boost::chrono::duration<long, boost::ratio<1, 100> >,
boost::chrono::duration<int, boost::ratio<1, 1000> >,
boost::chrono::duration<long, boost::ratio<1, 1000> > >();
test<boost::chrono::duration<char, boost::ratio<1, 30> >,
boost::chrono::duration<short, boost::ratio<1, 1000> >,
boost::chrono::duration<int, boost::ratio<1, 3000> > >();
test<boost::chrono::duration<double, boost::ratio<21, 1> >,
boost::chrono::duration<short, boost::ratio<15, 1> >,
boost::chrono::duration<double, boost::ratio<3, 1> > >();
}

View File

@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <class D1, class D2, class De>
void
test()
{
typedef boost::chrono::system_clock C;
typedef boost::chrono::time_point<C, D1> T1;
typedef boost::chrono::time_point<C, D2> T2;
typedef boost::chrono::time_point<C, De> Te;
typedef typename boost::common_type<T1, T2>::type Tc;
BOOST_CHRONO_STATIC_ASSERT((boost::is_same<Tc, Te>::value), NOTHING, ());
}
void testall()
{
test<boost::chrono::duration<int, boost::ratio<1, 100> >,
boost::chrono::duration<long, boost::ratio<1, 1000> >,
boost::chrono::duration<long, boost::ratio<1, 1000> > >();
test<boost::chrono::duration<long, boost::ratio<1, 100> >,
boost::chrono::duration<int, boost::ratio<1, 1000> >,
boost::chrono::duration<long, boost::ratio<1, 1000> > >();
test<boost::chrono::duration<char, boost::ratio<1, 30> >,
boost::chrono::duration<short, boost::ratio<1, 1000> >,
boost::chrono::duration<int, boost::ratio<1, 3000> > >();
test<boost::chrono::duration<double, boost::ratio<21, 1> >,
boost::chrono::duration<short, boost::ratio<15, 1> >,
boost::chrono::duration<double, boost::ratio<3, 1> > >();
}

View File

@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#include <limits>
#include <boost/detail/lightweight_test.hpp>
#include <libs/chrono/test/rep.h>
int main()
{
BOOST_TEST((boost::chrono::duration_values<int>::min)() ==
(std::numeric_limits<int>::min)());
BOOST_TEST((boost::chrono::duration_values<double>::min)() ==
-(std::numeric_limits<double>::max)());
BOOST_TEST((boost::chrono::duration_values<Rep>::min)() ==
(std::numeric_limits<Rep>::min)());
BOOST_TEST((boost::chrono::duration_values<int>::max)() ==
(std::numeric_limits<int>::max)());
BOOST_TEST((boost::chrono::duration_values<double>::max)() ==
(std::numeric_limits<double>::max)());
BOOST_TEST((boost::chrono::duration_values<Rep>::max)() ==
(std::numeric_limits<Rep>::max)());
BOOST_TEST(boost::chrono::duration_values<int>::zero() == 0);
BOOST_TEST(boost::chrono::duration_values<Rep>::zero() == 0);
return boost::report_errors();
}

View File

@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Adaptation to Boost of the libcxx
// Copyright 2010 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.hpp>
#include <boost/type_traits.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
template <class T>
void
test()
{
BOOST_CHRONO_STATIC_ASSERT((boost::is_base_of<boost::is_floating_point<T>,
boost::chrono::treat_as_floating_point<T> >::value), NOTHING, ());
}
struct A {};
void testall()
{
test<int>();
test<unsigned>();
test<char>();
test<bool>();
test<float>();
test<double>();
test<long double>();
test<A>();
}

106
test/win32_test.cpp Normal file
View File

@ -0,0 +1,106 @@
// boost win32_test.cpp -----------------------------------------------------//
// Copyright 2010 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org/libs/chrono for documentation.
#include <boost/chrono/config.hpp>
#include <boost/detail/lightweight_test.hpp>
#if defined(BOOST_CHRONO_WINDOWS_API)
#include <boost/chrono/detail/static_assert.hpp>
#if !defined(BOOST_NO_STATIC_ASSERT)
#define NOTHING ""
#endif
#include <boost/detail/win/basic_types.hpp>
#include <boost/detail/win/time.hpp>
#include <windows.h>
#include <boost/type_traits.hpp>
#include <boost/typeof/typeof.hpp>
void test() {
{
boost::detail::win32::LARGE_INTEGER_ a;
LARGE_INTEGER b;
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::LARGE_INTEGER_)==sizeof(LARGE_INTEGER)
), NOTHING, (boost::detail::win32::LARGE_INTEGER_, LARGE_INTEGER));
BOOST_TEST((
sizeof(a.QuadPart)==sizeof(b.QuadPart)
));
BOOST_CHRONO_STATIC_ASSERT((
offsetof(boost::detail::win32::LARGE_INTEGER_, QuadPart)==offsetof(LARGE_INTEGER, QuadPart)
), NOTHING, (boost::detail::win32::LARGE_INTEGER_, LARGE_INTEGER));
}
// BOOST_CHRONO_STATIC_ASSERT((
// boost::is_same<
// BOOST_TYPEOF(boost::detail::win32::LARGE_INTEGER_::QuadPart),
// BOOST_TYPEOF(LARGE_INTEGER::QuadPart)
// >::value
// ), NOTHING, (boost::detail::win32::LARGE_INTEGER_, LARGE_INTEGER));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::BOOL_)==sizeof(BOOL)
), NOTHING, (boost::detail::win32::BOOL_, BOOL));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::DWORD_)==sizeof(DWORD)
), NOTHING, (boost::detail::win32::DWORD_, DWORD));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::HANDLE_)==sizeof(HANDLE)
), NOTHING, (boost::detail::win32::HANDLE_, HANDLE));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::LONG_)==sizeof(LONG)
), NOTHING, (boost::detail::win32::LONG_, LONG));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::LONGLONG_)==sizeof(LONGLONG)
), NOTHING, (boost::detail::win32::LONGLONG_, LONGLONG));
BOOST_CHRONO_STATIC_ASSERT((
boost::is_same<boost::detail::win32::LONGLONG_,LONGLONG>::value
), NOTHING, (boost::detail::win32::LONGLONG_, LONGLONG));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::PLARGE_INTEGER_)==sizeof(PLARGE_INTEGER)
), NOTHING, (boost::detail::win32::PLARGE_INTEGER_, PLARGE_INTEGER));
{
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::FILETIME_)==sizeof(FILETIME)
), NOTHING, (boost::detail::win32::FILETIME_, FILETIME));
BOOST_CHRONO_STATIC_ASSERT((
sizeof(boost::detail::win32::PFILETIME_)==sizeof(PFILETIME)
), NOTHING, (boost::detail::win32::PFILETIME_, PFILETIME));
boost::detail::win32::FILETIME_ a;
FILETIME b;
BOOST_TEST((
sizeof(a.dwLowDateTime)==sizeof(b.dwLowDateTime)
));
BOOST_TEST((
sizeof(a.dwHighDateTime)==sizeof(b.dwHighDateTime)
));
BOOST_CHRONO_STATIC_ASSERT((
offsetof(boost::detail::win32::FILETIME_, dwLowDateTime)==offsetof(FILETIME, dwLowDateTime)
), NOTHING, (boost::detail::win32::FILETIME_, FILETIME));
BOOST_CHRONO_STATIC_ASSERT((
offsetof(boost::detail::win32::FILETIME_, dwHighDateTime)==offsetof(FILETIME, dwHighDateTime)
), NOTHING, (boost::detail::win32::FILETIME_, FILETIME));
}
// BOOST_CHRONO_STATIC_ASSERT((
// GetLastError==boost::detail::win32::::GetLastError
// ), NOTHING, ());
}
#else
void test() {
}
#endif
int main( )
{
test();
return boost::report_errors();
}