Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Time_Value_Test.cpp
blob2698a6994c13a079c9e57cc6036c2193835a33bd
2 //=============================================================================
3 /**
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"
17 #include "ace/ACE.h"
18 #include "ace/Time_Value.h"
20 #include "ace/Numeric_Limits.h"
22 #include <sstream>
24 int
25 run_main (int, ACE_TCHAR *[])
27 int ret = 0;
29 ACE_START_TEST (ACE_TEXT ("Time_Value_Test"));
31 ACE_Time_Value tv1;
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));
45 ACE_Time_Value first;
46 ACE_Time_Value last(ACE_Time_Value::max_time);
47 first = last;
48 ACE_TEST_ASSERT (first == last);
50 // Test assignment operator, tv9 and tv6 must be the same after this
51 ACE_Time_Value tv9;
52 tv9 = tv6;
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
74 tv1.set (1, 1);
75 tv2.set (2, 2);
76 tv1 *= 2.0;
77 ACE_TEST_ASSERT (tv1 == tv2);
78 tv1.set (1, 1);
79 tv2.set (static_cast<time_t> (-2), static_cast<suseconds_t> (-2));
80 tv1 *= -2.0;
81 ACE_TEST_ASSERT (tv1 == tv2);
83 // test usec shift
84 tv1.set (1, 999999);
85 tv2.set (19, 999990);
86 tv1 *= 10.0;
87 ACE_TEST_ASSERT ( tv1 == tv2);
88 tv1.set (1, 999999);
89 tv2.set (static_cast<time_t> (-19), -999990);
90 tv1 *= -10.0;
91 ACE_TEST_ASSERT (tv1 == tv2);
93 // Test correct msec() convert; also checks for compile error reported in
94 // Bugzilla 3336.
95 ACE_Time_Value msec_test (42, 555000);
96 ACE_UINT64 ms = 0;
97 msec_test.msec (ms);
98 if (ms != 42555)
99 ACE_ERROR ((LM_ERROR,
100 ACE_TEXT ("msec test failed: %Q should be 42555\n"),
101 ms));
102 ms = 0;
103 ms = msec_test.get_msec ();
104 if (ms != 42555)
105 ACE_ERROR ((LM_ERROR,
106 ACE_TEXT ("get_msec test failed: %Q should be 42555\n"),
107 ms));
108 ACE_Time_Value const msec_test2 (42, 555000);
109 ms = 0;
110 msec_test2.msec (ms);
111 if (ms != 42555)
112 ACE_ERROR ((LM_ERROR,
113 ACE_TEXT ("msec const test failed: %Q should be 42555\n"),
114 ms));
116 // Test setting double values
117 ACE_Time_Value d1(10, 500000);
118 ACE_Time_Value d2;
119 d2.set(10.5);
121 ACE_Time_Value d3(-10, -500000);
122 ACE_Time_Value d4;
123 d4.set(-10.5);
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);
136 ACE_Time_Value d5;
137 d5.set(DBL_MAX);
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());
161 v1.set(DBL_MAX);
163 // Test setting from ACE_UINT64
164 ms = 42555;
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"),
170 msec_test3.sec ()));
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" );
179 ost.str("");
180 ost << ACE_Time_Value(1,1);
181 ACE_TEST_ASSERT( ost.str() == "1.000001" );
182 ost.str("");
183 ost << ACE_Time_Value(static_cast<time_t>(-1),-1);
184 ACE_TEST_ASSERT( ost.str() == "-1.000001" );
185 ost.str("");
186 ost << ACE_Time_Value(0,1);
187 ACE_TEST_ASSERT( ost.str() == "0.000001" );
188 ost.str("");
189 ost << ACE_Time_Value(0,-1);
190 ACE_TEST_ASSERT( ost.str() == "-0.000001" );
191 ost.str("");
192 ost << ACE_Time_Value();
193 ACE_TEST_ASSERT( ost.str() == "0" );
195 ACE_END_TEST;
197 return ret;