Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / Time_Value_Test.cpp
blob98bf4cab5eb7c3d55ccf8fb657429538ac104c17
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)
100 ACE_ERROR ((LM_ERROR,
101 ACE_TEXT ("msec test failed: %Q should be 42555\n"),
102 ms));
103 ++ret;
106 ms = 0;
107 ms = msec_test.get_msec ();
108 if (ms != 42555)
110 ACE_ERROR ((LM_ERROR,
111 ACE_TEXT ("get_msec test failed: %Q should be 42555\n"),
112 ms));
113 ++ret;
115 ACE_Time_Value const msec_test2 (42, 555000);
116 ms = 0;
117 msec_test2.msec (ms);
118 if (ms != 42555)
120 ACE_ERROR ((LM_ERROR,
121 ACE_TEXT ("msec const test failed: %Q should be 42555\n"),
122 ms));
123 ++ret;
126 // Test setting double values
127 ACE_Time_Value d1(10, 500000);
128 ACE_Time_Value d2;
129 d2.set(10.5);
131 ACE_Time_Value d3(-10, -500000);
132 ACE_Time_Value d4;
133 d4.set(-10.5);
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);
146 ACE_Time_Value d5;
147 d5.set(DBL_MAX);
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());
171 v1.set(DBL_MAX);
173 // Test setting from ACE_UINT64
174 ms = 42555;
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"),
181 msec_test3.sec ()));
182 ++ret;
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 ()));
189 ++ret;
192 std::ostringstream ost;
193 ost << ACE_Time_Value(1);
194 ACE_TEST_ASSERT( ost.str() == "1" );
195 ost.str("");
196 ost << ACE_Time_Value(1,1);
197 ACE_TEST_ASSERT( ost.str() == "1.000001" );
198 ost.str("");
199 ost << ACE_Time_Value(static_cast<time_t>(-1),-1);
200 ACE_TEST_ASSERT( ost.str() == "-1.000001" );
201 ost.str("");
202 ost << ACE_Time_Value(0,1);
203 ACE_TEST_ASSERT( ost.str() == "0.000001" );
204 ost.str("");
205 ost << ACE_Time_Value(0,-1);
206 ACE_TEST_ASSERT( ost.str() == "-0.000001" );
207 ost.str("");
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")));
215 ++ret;
217 else
219 ACE_DEBUG ((LM_DEBUG,
220 ACE_TEXT ("time_t is at least 64bit, this platform will not have problems after 2038\n")));
223 ACE_END_TEST;
225 return ret;