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_ERROR ((LM_ERROR
,
101 ACE_TEXT ("msec test failed: %Q should be 42555\n"),
107 ms
= msec_test
.get_msec ();
110 ACE_ERROR ((LM_ERROR
,
111 ACE_TEXT ("get_msec test failed: %Q should be 42555\n"),
115 ACE_Time_Value
const msec_test2 (42, 555000);
117 msec_test2
.msec (ms
);
120 ACE_ERROR ((LM_ERROR
,
121 ACE_TEXT ("msec const test failed: %Q should be 42555\n"),
126 // Test setting double values
127 ACE_Time_Value
d1(10, 500000);
131 ACE_Time_Value
d3(-10, -500000);
135 ACE_TEST_ASSERT (d1
== d2
);
136 ACE_TEST_ASSERT (d3
== d4
);
137 ACE_TEST_ASSERT (d1
+ d3
== ACE_Time_Value::zero
);
138 ACE_TEST_ASSERT (d3
+ d1
== ACE_Time_Value::zero
);
139 ACE_TEST_ASSERT (d2
+ d4
== ACE_Time_Value::zero
);
140 ACE_TEST_ASSERT (d4
+ d2
== ACE_Time_Value::zero
);
141 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d1
= d3
);
142 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d3
= d1
);
143 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d2
= d4
);
144 ACE_TEST_ASSERT (ACE_Time_Value::zero
- d4
= d2
);
148 ACE_TEST_ASSERT (d5
== ACE_Time_Value::max_time
);
149 ACE_TEST_ASSERT (ACE_Time_Value::max_time
.sec () != 0);
150 ACE_TEST_ASSERT (ACE_Time_Value::max_time
.usec () != -1);
152 // Test performance of normalize()
153 ACE_Time_Value
v1(ACE_Numeric_Limits
<time_t>::max(),
154 ACE_Numeric_Limits
<suseconds_t
>::max());
155 ACE_Time_Value
v2(ACE_Numeric_Limits
<time_t>::min(),
156 ACE_Numeric_Limits
<suseconds_t
>::min());
157 ACE_Time_Value
v3(ACE_Numeric_Limits
<time_t>::max(),
158 ACE_Numeric_Limits
<suseconds_t
>::min());
159 ACE_Time_Value
v4(ACE_Numeric_Limits
<time_t>::min(),
160 ACE_Numeric_Limits
<suseconds_t
>::max());
162 v1
.set(ACE_Numeric_Limits
<time_t>::max(),
163 ACE_Numeric_Limits
<suseconds_t
>::max());
164 v2
.set(ACE_Numeric_Limits
<time_t>::min(),
165 ACE_Numeric_Limits
<suseconds_t
>::min());
166 v3
.set(ACE_Numeric_Limits
<time_t>::max(),
167 ACE_Numeric_Limits
<suseconds_t
>::min());
168 v4
.set(ACE_Numeric_Limits
<time_t>::min(),
169 ACE_Numeric_Limits
<suseconds_t
>::max());
173 // Test setting from ACE_UINT64
175 ACE_Time_Value msec_test3
;
176 msec_test3
.set_msec (ms
);
177 if (msec_test3
.sec () != 42)
179 ACE_ERROR ((LM_ERROR
,
180 ACE_TEXT ("set_msec test failed: %d secs should be 42\n"),
184 if (msec_test3
.usec () != 555000)
186 ACE_ERROR ((LM_ERROR
,
187 ACE_TEXT ("set_msec test failed: %d usecs should be 555000\n"),
188 msec_test3
.usec ()));
192 std::ostringstream ost
;
193 ost
<< ACE_Time_Value(1);
194 ACE_TEST_ASSERT( ost
.str() == "1" );
196 ost
<< ACE_Time_Value(1,1);
197 ACE_TEST_ASSERT( ost
.str() == "1.000001" );
199 ost
<< ACE_Time_Value(static_cast<time_t>(-1),-1);
200 ACE_TEST_ASSERT( ost
.str() == "-1.000001" );
202 ost
<< ACE_Time_Value(0,1);
203 ACE_TEST_ASSERT( ost
.str() == "0.000001" );
205 ost
<< ACE_Time_Value(0,-1);
206 ACE_TEST_ASSERT( ost
.str() == "-0.000001" );
208 ost
<< ACE_Time_Value();
209 ACE_TEST_ASSERT( ost
.str() == "0" );
211 if (sizeof(time_t) < 8)
213 ACE_ERROR ((LM_ERROR
,
214 ACE_TEXT ("time_t not at least 64bit, this platform will have problems after 2038\n")));
219 ACE_DEBUG ((LM_DEBUG
,
220 ACE_TEXT ("time_t is at least 64bit, this platform will not have problems after 2038\n")));