1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 // This is a test of helperfunctions
28
29 #include <osl/time.h>
30 #include <osl/thread.hxx>
31
32 #include "stringhelper.hxx"
33
34 #include "gtest/gtest.h"
35
36 // void isJaBloed()
37 // {
38 // printf("Ist ja echt bloed.\n");
39 // }
40
t_abs64(sal_Int64 _nValue)41 inline sal_Int64 t_abs64(sal_Int64 _nValue)
42 {
43 // std::abs() seems to have some ambiguity problems (so-texas)
44 // return abs(_nValue);
45 printf("t_abs64(%" SAL_PRIdINT64 ")\n", _nValue);
46 // ASSERT_TRUE(_nValue < 2147483647);
47
48 if (_nValue < 0)
49 {
50 _nValue = -_nValue;
51 }
52 return _nValue;
53 }
54
printf64(sal_Int64 n)55 void printf64(sal_Int64 n)
56 {
57 if (n < 0)
58 {
59 // negativ
60 printf("-");
61 n = -n;
62 }
63 if (n > 2147483647)
64 {
65 sal_Int64 n64 = n >> 32;
66 sal_uInt32 n32 = n64 & 0xffffffff;
67 printf("0x%.8x ", n32);
68 n32 = n & 0xffffffff;
69 printf("%.8x (64bit)", n32);
70 }
71 else
72 {
73 sal_uInt32 n32 = n & 0xffffffff;
74 printf("0x%.8x (32bit) ", n32);
75 }
76 printf("\n");
77 }
78
79 // -----------------------------------------------------------------------------
80 namespace testOfHelperFunctions
81 {
82 class test_t_abs64 : public ::testing::Test
83 {
84 };
85
TEST_F(test_t_abs64,test0)86 TEST_F(test_t_abs64, test0)
87 {
88 // this values has an overrun!
89 sal_Int32 n32 = 2147483648;
90 printf("n32 should be -2^31 is: %d\n", n32);
91 ASSERT_TRUE(n32 == -2147483648 ) << "n32!=2147483648";
92 }
93
94
TEST_F(test_t_abs64,test1_0)95 TEST_F(test_t_abs64,test1_0)
96 {
97 sal_Int64 n;
98 n = 1073741824;
99 n <<= 9;
100 printf("Value of n is ");
101 printf64(n);
102 ASSERT_TRUE(t_abs64(n) > 0) << "n=2^30 << 9";
103 }
104
TEST_F(test_t_abs64,test1)105 TEST_F(test_t_abs64, test1)
106 {
107 sal_Int64 n;
108 // The shift must happen in 64-bit: a bare 2147483648 literal is at most
109 // 32 bits wide on ILP32/LLP64 platforms (e.g. win32), so the high bit
110 // would be shifted out before the result is widened to sal_Int64.
111 // Force a 64-bit operand, exactly as test1_1 does.
112 n = sal_Int64(2147483648) << 8;
113 printf("Value of n is ");
114 printf64(n);
115 ASSERT_TRUE(t_abs64(n) > 0) << "n=2^31 << 8";
116 // 2^31 << 8 == 2^39 == 549755813888; written independently to catch a
117 // regression back to 32-bit arithmetic (which would yield 0).
118 ASSERT_TRUE(n == SAL_CONST_INT64(549755813888)) << "n=2^31 << 8 exact value";
119 }
TEST_F(test_t_abs64,test1_1)120 TEST_F(test_t_abs64, test1_1)
121 {
122 sal_Int64 n;
123 n = sal_Int64(2147483648) << 8;
124 printf("Value of n is ");
125 printf64(n);
126 ASSERT_TRUE(t_abs64(n) > 0) << "n=2^31 << 8";
127 }
128
TEST_F(test_t_abs64,test2)129 TEST_F(test_t_abs64, test2)
130 {
131 sal_Int64 n;
132 // 2^31 << 1, 2^31 * 2 and 2^32 must all equal 4294967296. Each operand
133 // is forced to 64 bits; with a bare 2147483648 literal the shift and the
134 // multiplication would overflow in 32-bit arithmetic and yield 0 on
135 // ILP32/LLP64 platforms (the original test bug, latent on LP64).
136 n = sal_Int64(2147483648) << 1;
137 printf("Value of n is ");
138 printf64(n);
139
140 ASSERT_TRUE(n != 0) << "(2^31 << 1) is != 0";
141
142 sal_Int64 n2 = sal_Int64(2147483648) * 2;
143 ASSERT_TRUE(n2 != 0) << "2^31 * 2 is != 0";
144
145 sal_Int64 n3 = 4294967296LL;
146 ASSERT_TRUE(n3 != 0) << "4294967296 is != 0";
147
148 ASSERT_TRUE(n == n2 && n == n3) << "n=2^31 << 1, n2 = 2^31 * 2, n3 = 2^32, all should equal!";
149 ASSERT_TRUE(n == SAL_CONST_INT64(4294967296)) << "2^31 << 1 == 2^32 exact value";
150 }
151
152
TEST_F(test_t_abs64,test3)153 TEST_F(test_t_abs64, test3)
154 {
155 sal_Int64 n = 0;
156 ASSERT_TRUE(t_abs64(n) == 0) << "n=0";
157
158 n = 1;
159 ASSERT_TRUE(t_abs64(n) > 0) << "n=1";
160
161 n = 2147483647;
162 ASSERT_TRUE(t_abs64(n) == 2147483647) << "n=2^31 - 1";
163
164 n = SAL_CONST_INT64(2147483648);
165 ASSERT_TRUE(t_abs64(n) == SAL_CONST_INT64(2147483648)) << "n=2^31";
166 }
167
TEST_F(test_t_abs64,test4)168 TEST_F(test_t_abs64, test4)
169 {
170 sal_Int64 n = 0;
171 n = -1;
172 printf("Value of n is -1 : ");
173 printf64(n);
174 ASSERT_TRUE(t_abs64(n) == 1) << "n=-1";
175
176 n = -SAL_CONST_INT64(2147483648);
177 printf("Value of n is -2^31 : ");
178 printf64(n);
179 ASSERT_TRUE(t_abs64(n) == SAL_CONST_INT64(2147483648)) << "n=-2^31";
180
181 n = -8589934592LL;
182 printf("Value of n is -2^33 : ");
183 printf64(n);
184 ASSERT_TRUE(t_abs64(n) == SAL_CONST_INT64(8589934592)) << "n=-2^33";
185 }
186
187 // Exercise t_abs64 across the full 64-bit range, including the boundaries
188 // that the original > 0 / != 0 assertions never really checked.
TEST_F(test_t_abs64,test_abs64_range)189 TEST_F(test_t_abs64, test_abs64_range)
190 {
191 ASSERT_TRUE(t_abs64(0) == 0) << "abs(0)";
192 ASSERT_TRUE(t_abs64(SAL_CONST_INT64(1)) == 1) << "abs(1)";
193 ASSERT_TRUE(t_abs64(SAL_CONST_INT64(-1)) == 1) << "abs(-1)";
194 ASSERT_TRUE(t_abs64(SAL_MAX_INT64) == SAL_MAX_INT64) << "abs(SAL_MAX_INT64)";
195 ASSERT_TRUE(t_abs64(-SAL_MAX_INT64) == SAL_MAX_INT64) << "abs(-SAL_MAX_INT64)";
196
197 // Known limitation: SAL_MIN_INT64 has no representable positive
198 // counterpart in two's complement, so -SAL_MIN_INT64 overflows and
199 // t_abs64 cannot return a positive value here. Document the boundary so
200 // any future change to t_abs64's contract is a deliberate, visible one.
201 ASSERT_TRUE(SAL_MIN_INT64 < 0) << "SAL_MIN_INT64 is negative";
202 ASSERT_TRUE(SAL_MIN_INT64 != -SAL_MAX_INT64) << "min/max are not symmetric";
203 }
204
205
206 // -----------------------------------------------------------------------------
207 class test_printf : public ::testing::Test
208 {
209 };
210
TEST_F(test_printf,printf_001)211 TEST_F(test_printf, printf_001)
212 {
213 printf("This is only a test of some helper functions\n");
214 sal_Int32 nValue = 12345;
215 printf("a value %d (should be 12345)\n", nValue);
216
217 rtl::OString sValue("foo bar");
218 printf("a String '%s' (should be 'foo bar')\n", sValue.getStr());
219
220 rtl::OUString suValue(rtl::OUString::createFromAscii("a unicode string"));
221 sValue <<= suValue;
222 printf("a String '%s'\n", sValue.getStr());
223 }
224
225
226 class StopWatch
227 {
228 protected:
229 TimeValue m_aStartTime;
230 TimeValue m_aEndTime;
231 bool m_bStarted;
232 public:
StopWatch()233 StopWatch()
234 :m_bStarted(false)
235 {
236 }
237
start()238 void start()
239 {
240 m_bStarted = true;
241 osl_getSystemTime(&m_aStartTime);
242 }
stop()243 void stop()
244 {
245 osl_getSystemTime(&m_aEndTime);
246 OSL_ENSURE(m_bStarted, "Not Started.");
247 m_bStarted = false;
248 }
makeTwoDigits(rtl::OString const & _sStr)249 rtl::OString makeTwoDigits(rtl::OString const& _sStr)
250 {
251 rtl::OString sBack;
252 if (_sStr.getLength() == 0)
253 {
254 sBack = "00";
255 }
256 else
257 {
258 if (_sStr.getLength() == 1)
259 {
260 sBack = "0" + _sStr;
261 }
262 else
263 {
264 sBack = _sStr;
265 }
266 }
267 return sBack;
268 }
makeThreeDigits(rtl::OString const & _sStr)269 rtl::OString makeThreeDigits(rtl::OString const& _sStr)
270 {
271 rtl::OString sBack;
272 if (_sStr.getLength() == 0)
273 {
274 sBack = "000";
275 }
276 else
277 {
278 if (_sStr.getLength() == 1)
279 {
280 sBack = "00" + _sStr;
281 }
282 else
283 {
284 if (_sStr.getLength() == 2)
285 {
286 sBack = "0" + _sStr;
287 }
288 else
289 {
290 sBack = _sStr;
291 }
292 }
293 }
294 return sBack;
295 }
296
showTime(const rtl::OString & aWhatStr)297 void showTime(const rtl::OString & aWhatStr)
298 {
299 OSL_ENSURE(!m_bStarted, "Not Stopped.");
300
301 sal_Int32 nSeconds = m_aEndTime.Seconds - m_aStartTime.Seconds;
302 sal_Int32 nNanoSec = sal_Int32(m_aEndTime.Nanosec) - sal_Int32(m_aStartTime.Nanosec);
303 // printf("Seconds: %d Nanosec: %d ", nSeconds, nNanoSec);
304 if (nNanoSec < 0)
305 {
306 nNanoSec = 1000000000 + nNanoSec;
307 nSeconds--;
308 // printf(" NEW Seconds: %d Nanosec: %d\n", nSeconds, nNanoSec);
309 }
310
311 rtl::OString aStr = "Time for ";
312 aStr += aWhatStr;
313 aStr += " ";
314 aStr += makeTwoDigits(rtl::OString::valueOf(nSeconds / 3600));
315 aStr += ":";
316 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 3600) / 60));
317 aStr += ":";
318 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 60)));
319 aStr += ":";
320 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000000) / 1000000));
321 aStr += ":";
322 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000) / 1000));
323 aStr += ":";
324 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000)));
325
326 printf("%s\n", aStr.getStr());
327 // cout << aStr.getStr() << endl;
328 }
329
330 };
331
isEqualTimeValue(const TimeValue * time1,const TimeValue * time2)332 static sal_Bool isEqualTimeValue ( const TimeValue* time1, const TimeValue* time2)
333 {
334 if( time1->Seconds == time2->Seconds &&
335 time1->Nanosec == time2->Nanosec)
336 return sal_True;
337 else
338 return sal_False;
339 }
340
isGreaterTimeValue(const TimeValue * time1,const TimeValue * time2)341 static sal_Bool isGreaterTimeValue( const TimeValue* time1, const TimeValue* time2)
342 {
343 sal_Bool retval= sal_False;
344 if ( time1->Seconds > time2->Seconds)
345 retval= sal_True;
346 else if ( time1->Seconds == time2->Seconds)
347 {
348 if( time1->Nanosec > time2->Nanosec)
349 retval= sal_True;
350 }
351 return retval;
352 }
353
isGreaterEqualTimeValue(const TimeValue * time1,const TimeValue * time2)354 static sal_Bool isGreaterEqualTimeValue( const TimeValue* time1, const TimeValue* time2)
355 {
356 if( isEqualTimeValue( time1, time2) )
357 return sal_True;
358 else if( isGreaterTimeValue( time1, time2))
359 return sal_True;
360 else
361 return sal_False;
362 }
363
isBTimeGreaterATime(TimeValue const & A,TimeValue const & B)364 bool isBTimeGreaterATime(TimeValue const& A, TimeValue const& B)
365 {
366 if (B.Seconds > A.Seconds) return true;
367 if (B.Nanosec > A.Nanosec) return true;
368
369 // lower or equal
370 return false;
371 }
372 // -----------------------------------------------------------------------------
373
374
375 class test_TimeValues : public ::testing::Test
376 {
377 };
378
TEST_F(test_TimeValues,t_time1)379 TEST_F(test_TimeValues, t_time1)
380 {
381 StopWatch aWatch;
382 aWatch.start();
383 TimeValue aTimeValue={3,0};
384 osl::Thread::wait(aTimeValue);
385 aWatch.stop();
386 aWatch.showTime("Wait for 3 seconds");
387 }
388
TEST_F(test_TimeValues,t_time2)389 TEST_F(test_TimeValues, t_time2)
390 {
391 printf("Wait repeats 20 times.\n");
392 int i=0;
393 while(i++<20)
394 {
395 StopWatch aWatch;
396 aWatch.start();
397 TimeValue aTimeValue={0,1000 * 1000 * 500};
398 osl::Thread::wait(aTimeValue);
399 aWatch.stop();
400 aWatch.showTime("wait for 500msec");
401 }
402 }
403
TEST_F(test_TimeValues,t_time3)404 TEST_F(test_TimeValues, t_time3)
405 {
406 printf("Wait repeats 100 times.\n");
407 int i=0;
408 while(i++<20)
409 {
410 StopWatch aWatch;
411 aWatch.start();
412 TimeValue aTimeValue={0,1000*1000*100};
413 osl::Thread::wait(aTimeValue);
414 aWatch.stop();
415 aWatch.showTime("wait for 100msec");
416 }
417 }
418
419 // void demoTimeValue()
420 // {
421 // TimeValue aStartTime, aEndTime;
422 // osl_getSystemTime(&aStartTime);
423 // // testSession(xORB, false);
424 // osl_getSystemTime(&aEndTime);
425 //
426 // sal_Int32 nSeconds = aEndTime.Seconds - aStartTime.Seconds;
427 // sal_Int32 nNanoSec = aEndTime.Nanosec - aStartTime.Nanosec;
428 // if (nNanoSec < 0)
429 // {
430 // nNanoSec = 1000000000 - nNanoSec;
431 // nSeconds++;
432 // }
433 //
434 // // cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
435 // }
436
437
438 } // namespace testOfHelperFunctions
439
main(int argc,char ** argv)440 int main(int argc, char **argv)
441 {
442 ::testing::InitGoogleTest(&argc, argv);
443 return RUN_ALL_TESTS();
444 }
445