• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 Krzysztof Czainski
2 // Copyright 2013 Vicente J. Botet Escriba
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 /*
8  * @file chrono_rounding.cpp
9  *
10  * @since 2013-11-22
11  * @author  Krzysztof Czainski <1czajnik@gmail.com>
12  */
13 
14 #include <boost/cstdint.hpp>
15 #include <boost/chrono/ceil.hpp>
16 #include <boost/chrono/floor.hpp>
17 #include <boost/chrono/round.hpp>
18 
19 #include <boost/chrono/chrono_io.hpp>
20 #include <boost/detail/lightweight_test.hpp>
21 
22 using namespace boost::chrono;
23 
test_floor()24 void test_floor()
25 {
26     BOOST_TEST_EQ( seconds(-2), floor<seconds>( milliseconds(-2000) ) );
27     BOOST_TEST_EQ( seconds(-2), floor<seconds>( milliseconds(-1999) ) );
28     BOOST_TEST_EQ( seconds(-2), floor<seconds>( milliseconds(-1001) ) );
29     BOOST_TEST_EQ( seconds(-1), floor<seconds>( milliseconds(-1000) ) );
30     BOOST_TEST_EQ( seconds(-1), floor<seconds>( milliseconds(-999) ) );
31     BOOST_TEST_EQ( seconds(-1), floor<seconds>( milliseconds(-1) ) );
32     BOOST_TEST_EQ( seconds(0), floor<seconds>( milliseconds(0) ) );
33     BOOST_TEST_EQ( seconds(0), floor<seconds>( milliseconds(1) ) );
34     BOOST_TEST_EQ( seconds(0), floor<seconds>( milliseconds(999) ) );
35     BOOST_TEST_EQ( seconds(1), floor<seconds>( milliseconds(1000) ) );
36     BOOST_TEST_EQ( seconds(1), floor<seconds>( milliseconds(1001) ) );
37     BOOST_TEST_EQ( seconds(1), floor<seconds>( milliseconds(1999) ) );
38     BOOST_TEST_EQ( seconds(2), floor<seconds>( milliseconds(2000) ) );
39 
40     {
41       // check that int32 isn't overflowed in intermediate calculations:
42         typedef duration<boost::int32_t> sec32;
43         typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms;
44         BOOST_TEST_EQ( sec32(-999000000), floor<sec32>( sec32_m1ms(-1000000000) ) );
45         BOOST_TEST_EQ( sec32( 999000000), floor<sec32>( sec32_m1ms( 1000000000) ) );
46     }
47 }
48 
test_ceil()49 void test_ceil()
50 {
51     BOOST_TEST_EQ( seconds(-2), ceil<seconds>( milliseconds(-2000) ) );
52     BOOST_TEST_EQ( seconds(-1), ceil<seconds>( milliseconds(-1999) ) );
53     BOOST_TEST_EQ( seconds(-1), ceil<seconds>( milliseconds(-1001) ) );
54     BOOST_TEST_EQ( seconds(-1), ceil<seconds>( milliseconds(-1000) ) );
55     BOOST_TEST_EQ( seconds(0), ceil<seconds>( milliseconds(-999) ) );
56     BOOST_TEST_EQ( seconds(0), ceil<seconds>( milliseconds(-1) ) );
57     BOOST_TEST_EQ( seconds(0), ceil<seconds>( milliseconds(0) ) );
58     BOOST_TEST_EQ( seconds(1), ceil<seconds>( milliseconds(1) ) );
59     BOOST_TEST_EQ( seconds(1), ceil<seconds>( milliseconds(999) ) );
60     BOOST_TEST_EQ( seconds(1), ceil<seconds>( milliseconds(1000) ) );
61     BOOST_TEST_EQ( seconds(2), ceil<seconds>( milliseconds(1001) ) );
62     BOOST_TEST_EQ( seconds(2), ceil<seconds>( milliseconds(1999) ) );
63     BOOST_TEST_EQ( seconds(2), ceil<seconds>( milliseconds(2000) ) );
64     {
65       // check that int32 isn't overflowed in intermediate calculations:
66         typedef duration<boost::int32_t> sec32;
67         typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms;
68         BOOST_TEST_EQ( sec32(-999000000), ceil<sec32>( sec32_m1ms(-1000000000) ) );
69         BOOST_TEST_EQ( sec32( 999000000), ceil<sec32>( sec32_m1ms( 1000000000) ) );
70     }
71 }
72 
test_round()73 void test_round()
74 {
75     // to even on tie
76     BOOST_TEST_EQ( seconds(-2), round<seconds>( milliseconds(-2000) ) );
77     BOOST_TEST_EQ( seconds(-2), round<seconds>( milliseconds(-1500) ) );
78     BOOST_TEST_EQ( seconds(-1), round<seconds>( milliseconds(-1499) ) );
79     BOOST_TEST_EQ( seconds(-1), round<seconds>( milliseconds(-1000) ) );
80     BOOST_TEST_EQ( seconds(0), round<seconds>( milliseconds(-500) ) );
81     BOOST_TEST_EQ( seconds(0), round<seconds>( milliseconds(-499) ) );
82     BOOST_TEST_EQ( seconds(0), round<seconds>( milliseconds(0) ) );
83     BOOST_TEST_EQ( seconds(0), round<seconds>( milliseconds(499) ) );
84     BOOST_TEST_EQ( seconds(0), round<seconds>( milliseconds(500) ) );
85     BOOST_TEST_EQ( seconds(1), round<seconds>( milliseconds(1000) ) );
86     BOOST_TEST_EQ( seconds(1), round<seconds>( milliseconds(1499) ) );
87     BOOST_TEST_EQ( seconds(2), round<seconds>( milliseconds(1500) ) );
88     BOOST_TEST_EQ( seconds(2), round<seconds>( milliseconds(2000) ) );
89     {
90       // check that int32 isn't overflowed in intermediate calculations:
91         typedef duration<boost::int32_t> sec32;
92         typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms;
93         BOOST_TEST_EQ( sec32(-999000000), round<sec32>( sec32_m1ms(-1000000000) ) );
94         BOOST_TEST_EQ( sec32( 999000000), round<sec32>( sec32_m1ms( 1000000000) ) );
95     }
96 }
97 
main()98 int main()
99 {
100   test_floor();
101   test_ceil();
102   test_round();
103   return boost::report_errors();
104 
105 }
106