2 //=============================================================================
4 * @file Time_Value_Test.cpp
6 * This is a simple test of ACE_Time_Value. No command line arguments
7 * are needed to run the test.
9 * @author Prashant Jain <pjain@cs.wustl.edu> and David Levine <levine@cs.wustl.edu>
11 //=============================================================================
13 // Note, for this test the config.h file *must* come first!
14 #include "ace/config-all.h"
16 #include "test_config.h"
18 #include "ace/Time_Value.h"
20 #include "ace/Numeric_Limits.h"
25 run_main (int, ACE_TCHAR
*[])
29 ACE_START_TEST (ACE_TEXT ("Time_Value_Test"));
32 ACE_Time_Value
tv2 (2);
33 ACE_Time_Value
tv3 (100U);
34 ACE_Time_Value
tv4 (1, 1000000);
35 ACE_Time_Value
tv5 (2UL);
36 ACE_Time_Value
tv6 (1, -1000000);
37 ACE_Time_Value
tv7 (static_cast<long> (2.0));
39 // Beware! 2.5 gets truncated to 2!
40 // NOTE: this is intended to show what happens with
41 // ACE_Time_Value (2.5). Some compilers, such as g++ 2.7.2.3,
42 // actually warn about it without the case.
43 ACE_Time_Value
tv8 (static_cast <long> (2.5));
46 ACE_Time_Value
last(ACE_Time_Value::max_time
);
48 ACE_TEST_ASSERT (first
== last
);
50 // Test assignment operator, tv9 and tv6 must be the same after this
54 ACE_TEST_ASSERT (tv1
== ACE_Time_Value (0));
55 ACE_TEST_ASSERT (tv2
< tv3
);
56 ACE_TEST_ASSERT (tv2
<= tv2
);
57 ACE_TEST_ASSERT (tv2
>= tv4
);
58 ACE_TEST_ASSERT (tv5
>= tv6
);
59 ACE_TEST_ASSERT (tv2
== ACE_Time_Value (1, 1000000));
60 ACE_TEST_ASSERT (tv5
== tv4
);
61 ACE_TEST_ASSERT (tv2
== tv4
);
62 ACE_TEST_ASSERT (tv1
!= tv2
);
63 ACE_TEST_ASSERT (tv6
== tv1
);
64 ACE_TEST_ASSERT (tv5
== tv7
);
65 ACE_TEST_ASSERT (tv7
== tv8
); // That's right! See above . . .
66 ACE_TEST_ASSERT (tv9
== tv6
);
68 ACE_Time_Value
tv10 (1);
70 ACE_TEST_ASSERT (tv10
.sec() == 1);
72 // test multiplication by double
73 // test simple multiplication
77 ACE_TEST_ASSERT (tv1
== tv2
);
79 tv2
.set (static_cast<time_t> (-2), static_cast<suseconds_t
> (-2));
81 ACE_TEST_ASSERT (tv1
== tv2
);
87 ACE_TEST_ASSERT ( tv1
== tv2
);
89 tv2
.set (static_cast<time_t> (-19), -999990);
91 ACE_TEST_ASSERT (tv1
== tv2
);
93 // Test correct msec() convert; also checks for compile error reported in
95 ACE_Time_Value
msec_test (42, 555000);
100 ACE_TEXT ("msec test failed: %Q should be 42555\n"),
103 ms
= msec_test
.get_msec ();
105 ACE_ERROR ((LM_ERROR
,
106 ACE_TEXT ("get_msec test failed: %Q should be 42555\n"),
108 ACE_Time_Value
const msec_test2 (42, 555000);
110 msec_test2
.msec (ms
);
112 ACE_ERROR ((LM_ERROR
,
113 ACE_TEXT ("msec const test failed: %Q should be 42555\n"),
116 // Test setting double values
117 ACE_Time_Value
d1(10, 500000);
121 ACE_Time_Value
d3(-10, -500000);
125 ACE_TEST_ASSERT (d1
== d2
);
126 ACE_TEST_ASSERT (d3
== d4
);
127 ACE_TEST_ASSERT (d1
+ d3
== ACE_Time_Value::zero
);
128 ACE_TEST_ASSERT (d3
+ d1
== ACE_Time_Value::zero
);
129 ACE_TEST_ASSERT (d2
+ d4
== ACE_Time_Value::zero
);
130 ACE_TEST_ASSERT (d4
+ d2
== ACE_Time_Value::zero
);
131 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d1
= d3
);
132 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d3
= d1
);
133 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d2
= d4
);
134 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d4
= d2
);
138 ACE_TEST_ASSERT (d5
== ACE_Time_Value::max_time
);
139 ACE_TEST_ASSERT (ACE_Time_Value::max_time
.sec () != 0);
140 ACE_TEST_ASSERT (ACE_Time_Value::max_time
.usec () != -1);
142 // Test performance of normalize()
143 ACE_Time_Value
v1(ACE_Numeric_Limits
<time_t>::max(),
144 ACE_Numeric_Limits
<suseconds_t
>::max());
145 ACE_Time_Value
v2(ACE_Numeric_Limits
<time_t>::min(),
146 ACE_Numeric_Limits
<suseconds_t
>::min());
147 ACE_Time_Value
v3(ACE_Numeric_Limits
<time_t>::max(),
148 ACE_Numeric_Limits
<suseconds_t
>::min());
149 ACE_Time_Value
v4(ACE_Numeric_Limits
<time_t>::min(),
150 ACE_Numeric_Limits
<suseconds_t
>::max());
152 v1
.set(ACE_Numeric_Limits
<time_t>::max(),
153 ACE_Numeric_Limits
<suseconds_t
>::max());
154 v2
.set(ACE_Numeric_Limits
<time_t>::min(),
155 ACE_Numeric_Limits
<suseconds_t
>::min());
156 v3
.set(ACE_Numeric_Limits
<time_t>::max(),
157 ACE_Numeric_Limits
<suseconds_t
>::min());
158 v4
.set(ACE_Numeric_Limits
<time_t>::min(),
159 ACE_Numeric_Limits
<suseconds_t
>::max());
163 // Test setting from ACE_UINT64
165 ACE_Time_Value msec_test3
;
166 msec_test3
.set_msec (ms
);
167 if (msec_test3
.sec () != 42)
168 ACE_ERROR ((LM_ERROR
,
169 ACE_TEXT ("set_msec test failed: %d secs should be 42\n"),
171 if (msec_test3
.usec () != 555000)
172 ACE_ERROR ((LM_ERROR
,
173 ACE_TEXT ("set_msec test failed: %d usecs should be 555000\n"),
174 msec_test3
.usec ()));
176 std::ostringstream ost
;
177 ost
<< ACE_Time_Value(1);
178 ACE_TEST_ASSERT( ost
.str() == "1" );
180 ost
<< ACE_Time_Value(1,1);
181 ACE_TEST_ASSERT( ost
.str() == "1.000001" );
183 ost
<< ACE_Time_Value(static_cast<time_t>(-1),-1);
184 ACE_TEST_ASSERT( ost
.str() == "-1.000001" );
186 ost
<< ACE_Time_Value(0,1);
187 ACE_TEST_ASSERT( ost
.str() == "0.000001" );
189 ost
<< ACE_Time_Value(0,-1);
190 ACE_TEST_ASSERT( ost
.str() == "-0.000001" );
192 ost
<< ACE_Time_Value();
193 ACE_TEST_ASSERT( ost
.str() == "0" );