test/duration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Open Source Robotics Foundation, Inc..
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <limits>
31 
32 #include <gtest/gtest.h>
33 #include <ros/duration.h>
34 #include <ros/time.h>
35 
36 using namespace ros;
37 
38 TEST(Duration, sleepWithSimTime)
39 {
41 
42  Time start = Time::now();
43  start -= Duration(2.0);
44  Time::setNow(start);
45 
47 
48  Duration d(1.0);
49  bool rc = d.sleep();
50  ASSERT_FALSE(rc);
51 }
52 
53 TEST(Duration, castFromDoubleExceptions)
54 {
56 
57  Duration d1, d2, d3, d4, d5, d6, d7, d8, d9;
58  // Valid values to cast, must not throw exceptions
59  EXPECT_NO_THROW(d1.fromSec(-2147483648.0));
60  EXPECT_NO_THROW(d2.fromSec(-2147483647.999999));
61  EXPECT_NO_THROW(d3.fromSec(2147483647.0));
62  EXPECT_NO_THROW(d4.fromSec(2147483647.999999));
63 
64  // The next casts all incorrect.
65  EXPECT_THROW(d1.fromSec(2147483648.0), std::runtime_error);
66  EXPECT_THROW(d2.fromSec(6442450943.0), std::runtime_error); // It's 2^31 - 1 + 2^32, and it could pass the test.
67  EXPECT_THROW(d3.fromSec(-2147483648.001), std::runtime_error);
68  EXPECT_THROW(d4.fromSec(-6442450943.0), std::runtime_error);
69  EXPECT_THROW(d5.fromSec(std::numeric_limits<double>::infinity()), std::runtime_error);
70  EXPECT_THROW(d6.fromSec(-std::numeric_limits<double>::infinity()), std::runtime_error);
71  EXPECT_THROW(d7.fromSec(std::numeric_limits<double>::quiet_NaN()), std::runtime_error);
72  // max int64 value is 9223372036854775807
73  EXPECT_THROW(d8.fromSec(9223372036854775808.0), std::runtime_error);
74  EXPECT_THROW(d9.fromSec(-9223372036854775809.0), std::runtime_error);
75 }
76 
77 TEST(Duration, castFromInt64Exceptions)
78 {
80 
81  Duration d1, d2, d3, d4;
82  // Valid values to cast, must not throw exceptions
83  EXPECT_NO_THROW(d1.fromNSec(-2147483648000000000));
84  EXPECT_NO_THROW(d2.fromNSec(-2147483647999999999));
85  EXPECT_NO_THROW(d3.fromNSec(2147483647000000000));
86  EXPECT_NO_THROW(d4.fromNSec(2147483647999999999));
87 
88  // The next casts all incorrect.
89  EXPECT_THROW(d1.fromSec(2147483648000000000), std::runtime_error);
90  EXPECT_THROW(d2.fromSec(4294967296000000000), std::runtime_error);
91  EXPECT_THROW(d3.fromSec(-2147483648000000001), std::runtime_error);
92  EXPECT_THROW(d4.fromSec(-6442450943000000000), std::runtime_error);
93 }
94 
95 TEST(Duration, arithmeticExceptions)
96 {
98 
99  Duration d1(2147483647, 0);
100  Duration d2(2147483647, 999999999);
101  EXPECT_THROW(d1 + d2, std::runtime_error);
102 
103  Duration d3(-2147483648, 0);
104  Duration d4(2147483647, 0);
105  EXPECT_THROW(d3 - d4, std::runtime_error);
106  EXPECT_THROW(d4 - d3, std::runtime_error);
107 
108  Duration d5(-2147483647, 1);
109  Duration d6(-2, 999999999);
110  Duration d7;
111  EXPECT_NO_THROW(d7 = d5 + d6);
112  EXPECT_EQ(-2147483648000000000, d7.toNSec());
113 }
114 
115 TEST(Duration, negativeSignExceptions)
116 {
117  ros::Time::init();
118 
119  Duration d1(2147483647, 0);
120  Duration d2(2147483647, 999999999);
121  Duration d3;
122  EXPECT_NO_THROW(d3 = -d1);
123  EXPECT_EQ(-2147483647000000000, d3.toNSec());
124  EXPECT_NO_THROW(d3 = -d2);
125  EXPECT_EQ(-2147483647999999999, d3.toNSec());
126 
127  Duration d4(-2147483647, 0);
128  Duration d5(-2147483648, 999999999);
129  Duration d6(-2147483648, 2);
130  Duration d7;
131  EXPECT_NO_THROW(d7 = -d4);
132  EXPECT_EQ(2147483647000000000, d7.toNSec());
133  EXPECT_NO_THROW(d7 = -d5);
134  EXPECT_EQ(2147483647000000001, d7.toNSec());
135  EXPECT_NO_THROW(d7 = -d6);
136  EXPECT_EQ(2147483647999999998, d7.toNSec());
137 }
138 
139 TEST(Duration, rounding)
140 {
141  ros::Time::init();
142 
143  Duration d1(49.0000000004);
144  EXPECT_EQ(49, d1.sec);
145  EXPECT_EQ(0, d1.nsec);
146  Duration d2(-49.0000000004);
147  EXPECT_EQ(-49, d2.sec);
148  EXPECT_EQ(0, d2.nsec);
149 
150  Duration d3(49.0000000006);
151  EXPECT_EQ(49, d3.sec);
152  EXPECT_EQ(1, d3.nsec);
153  Duration d4(-49.0000000006);
154  EXPECT_EQ(-50, d4.sec);
155  EXPECT_EQ(999999999, d4.nsec);
156 
157  Duration d5(49.9999999994);
158  EXPECT_EQ(49, d5.sec);
159  EXPECT_EQ(999999999, d5.nsec);
160  Duration d6(-49.9999999994);
161  EXPECT_EQ(-50, d6.sec);
162  EXPECT_EQ(1, d6.nsec);
163 
164  Duration d7(49.9999999996);
165  EXPECT_EQ(50, d7.sec);
166  EXPECT_EQ(0, d7.nsec);
167  Duration d8(-49.9999999996);
168  EXPECT_EQ(-50, d8.sec);
169  EXPECT_EQ(0, d8.nsec);
170 }
171 
172 int main(int argc, char **argv){
173  testing::InitGoogleTest(&argc, argv);
174  return RUN_ALL_TESTS();
175 }
main
int main(int argc, char **argv)
Definition: test/duration.cpp:172
ros::DurationBase::fromSec
T & fromSec(double t)
Definition: impl/duration.h:89
ros
TEST
TEST(Duration, sleepWithSimTime)
Definition: test/duration.cpp:38
ros::DurationBase::nsec
int32_t nsec
Definition: duration.h:75
ros::Time::setNow
static void setNow(const Time &new_now)
Definition: src/time.cpp:280
ros::Time::shutdown
static void shutdown()
Definition: src/time.cpp:295
time.h
duration.h
ros::Time::init
static void init()
Definition: src/time.cpp:288
ros::Time
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:174
ros::DurationBase::toNSec
int64_t toNSec() const
Definition: duration.h:93
ros::Duration::sleep
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep,...
Definition: src/time.cpp:423
ros::Duration
Duration representation for use with the Time class.
Definition: duration.h:117
ros::DurationBase::sec
int32_t sec
Definition: duration.h:75
ros::Time::now
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
Definition: src/time.cpp:260
ros::DurationBase::fromNSec
T & fromNSec(int64_t t)
Definition: impl/duration.h:109


rostime
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:37