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 <gtest/gtest.h>
31 #include <ros/duration.h>
32 #include <ros/time.h>
33 
34 using namespace ros;
35 
36 TEST(Duration, sleepWithSimTime)
37 {
39 
40  Time start = Time::now();
41  start -= Duration(2.0);
42  Time::setNow(start);
43 
45 
46  Duration d(1.0);
47  bool rc = d.sleep();
48  ASSERT_FALSE(rc);
49 }
50 
51 TEST(Duration, castFromDoubleExceptions)
52 {
54 
55  Duration d1, d2, d3, d4;
56  // Valid values to cast, must not throw exceptions
57  EXPECT_NO_THROW(d1.fromSec(-2147483648.0));
58  EXPECT_NO_THROW(d2.fromSec(-2147483647.999999));
59  EXPECT_NO_THROW(d3.fromSec(2147483647.0));
60  EXPECT_NO_THROW(d4.fromSec(2147483647.999999));
61 
62  // The next casts all incorrect.
63  EXPECT_THROW(d1.fromSec(2147483648.0), std::runtime_error);
64  EXPECT_THROW(d2.fromSec(6442450943.0), std::runtime_error); // It's 2^31 - 1 + 2^32, and it could pass the test.
65  EXPECT_THROW(d3.fromSec(-2147483648.001), std::runtime_error);
66  EXPECT_THROW(d4.fromSec(-6442450943.0), std::runtime_error);
67 }
68 
69 TEST(Duration, castFromInt64Exceptions)
70 {
72 
73  Duration d1, d2, d3, d4;
74  // Valid values to cast, must not throw exceptions
75  EXPECT_NO_THROW(d1.fromNSec(-2147483648000000000));
76  EXPECT_NO_THROW(d2.fromNSec(-2147483647999999999));
77  EXPECT_NO_THROW(d3.fromNSec(2147483647000000000));
78  EXPECT_NO_THROW(d4.fromNSec(2147483647999999999));
79 
80  // The next casts all incorrect.
81  EXPECT_THROW(d1.fromSec(2147483648000000000), std::runtime_error);
82  EXPECT_THROW(d2.fromSec(4294967296000000000), std::runtime_error);
83  EXPECT_THROW(d3.fromSec(-2147483648000000001), std::runtime_error);
84  EXPECT_THROW(d4.fromSec(-6442450943000000000), std::runtime_error);
85 }
86 
87 TEST(Duration, arithmeticExceptions)
88 {
90 
91  Duration d1(2147483647, 0);
92  Duration d2(2147483647, 999999999);
93  EXPECT_THROW(d1 + d2, std::runtime_error);
94 
95  Duration d3(-2147483648, 0);
96  Duration d4(2147483647, 0);
97  EXPECT_THROW(d3 - d4, std::runtime_error);
98  EXPECT_THROW(d4 - d3, std::runtime_error);
99 
100  Duration d5(-2147483647, 1);
101  Duration d6(-2, 999999999);
102  Duration d7;
103  EXPECT_NO_THROW(d7 = d5 + d6);
104  EXPECT_EQ(-2147483648000000000, d7.toNSec());
105 }
106 
107 TEST(Duration, negativeSignExceptions)
108 {
109  ros::Time::init();
110 
111  Duration d1(2147483647, 0);
112  Duration d2(2147483647, 999999999);
113  Duration d3;
114  EXPECT_NO_THROW(d3 = -d1);
115  EXPECT_EQ(-2147483647000000000, d3.toNSec());
116  EXPECT_NO_THROW(d3 = -d2);
117  EXPECT_EQ(-2147483647999999999, d3.toNSec());
118 
119  Duration d4(-2147483647, 0);
120  Duration d5(-2147483648, 999999999);
121  Duration d6(-2147483648, 2);
122  Duration d7;
123  EXPECT_NO_THROW(d7 = -d4);
124  EXPECT_EQ(2147483647000000000, d7.toNSec());
125  EXPECT_NO_THROW(d7 = -d5);
126  EXPECT_EQ(2147483647000000001, d7.toNSec());
127  EXPECT_NO_THROW(d7 = -d6);
128  EXPECT_EQ(2147483647999999998, d7.toNSec());
129 }
130 
131 TEST(Duration, rounding)
132 {
133  ros::Time::init();
134 
135  Duration d1(49.0000000004);
136  EXPECT_EQ(49, d1.sec);
137  EXPECT_EQ(0, d1.nsec);
138  Duration d2(-49.0000000004);
139  EXPECT_EQ(-49, d2.sec);
140  EXPECT_EQ(0, d2.nsec);
141 
142  Duration d3(49.0000000006);
143  EXPECT_EQ(49, d3.sec);
144  EXPECT_EQ(1, d3.nsec);
145  Duration d4(-49.0000000006);
146  EXPECT_EQ(-50, d4.sec);
147  EXPECT_EQ(999999999, d4.nsec);
148 
149  Duration d5(49.9999999994);
150  EXPECT_EQ(49, d5.sec);
151  EXPECT_EQ(999999999, d5.nsec);
152  Duration d6(-49.9999999994);
153  EXPECT_EQ(-50, d6.sec);
154  EXPECT_EQ(1, d6.nsec);
155 
156  Duration d7(49.9999999996);
157  EXPECT_EQ(50, d7.sec);
158  EXPECT_EQ(0, d7.nsec);
159  Duration d8(-49.9999999996);
160  EXPECT_EQ(-50, d8.sec);
161  EXPECT_EQ(0, d8.nsec);
162 }
163 
164 int main(int argc, char **argv){
165  testing::InitGoogleTest(&argc, argv);
166  return RUN_ALL_TESTS();
167 }
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:170
TEST(Duration, sleepWithSimTime)
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Definition: src/time.cpp:447
static void setNow(const Time &new_now)
Definition: src/time.cpp:304
int32_t nsec
Definition: duration.h:75
static void shutdown()
Definition: src/time.cpp:319
T & fromSec(double t)
Definition: impl/duration.h:54
int64_t toNSec() const
Definition: duration.h:94
static void init()
Definition: src/time.cpp:312
int main(int argc, char **argv)
Duration representation for use with the Time class.
Definition: duration.h:108
T & fromNSec(int64_t t)
Definition: impl/duration.h:68
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:284


rostime
Author(s): Josh Faust
autogenerated on Sat Apr 6 2019 02:52:21