update dev300-m58
[ooovba.git] / sal / qa / testHelperFunctions / testHelperFunctions.cxx
blob2a86f45b187167c118cd50c4fe93b7cbb8d1e531
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testHelperFunctions.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // This is a test of helperfunctions
36 #include <osl/time.h>
37 #include <osl/thread.hxx>
39 #include "stringhelper.hxx"
41 #include <cppunit/simpleheader.hxx>
43 // void isJaBloed()
44 // {
45 // t_print("Ist ja echt bloed.\n");
46 // }
48 inline sal_Int64 t_abs64(sal_Int64 _nValue)
50 // std::abs() seems to have some ambiguity problems (so-texas)
51 // return abs(_nValue);
52 t_print("t_abs64(%ld)\n", _nValue);
53 // CPPUNIT_ASSERT(_nValue < 2147483647);
55 if (_nValue < 0)
57 _nValue = -_nValue;
59 return _nValue;
62 void t_print64(sal_Int64 n)
64 if (n < 0)
66 // negativ
67 printf("-");
68 n = -n;
70 if (n > 2147483647)
72 sal_Int64 n64 = n >> 32;
73 sal_uInt32 n32 = n64 & 0xffffffff;
74 printf("0x%.8x ", n32);
75 n32 = n & 0xffffffff;
76 printf("%.8x (64bit)", n32);
78 else
80 sal_uInt32 n32 = n & 0xffffffff;
81 printf("0x%.8x (32bit) ", n32);
83 printf("\n");
86 // -----------------------------------------------------------------------------
87 namespace testOfHelperFunctions
89 class test_t_abs64 : public CppUnit::TestFixture
91 public:
92 void test0();
93 void test1_0();
94 void test1();
95 void test1_1();
96 void test2();
97 void test3();
98 void test4();
100 CPPUNIT_TEST_SUITE( test_t_abs64 );
101 CPPUNIT_TEST( test0 );
102 CPPUNIT_TEST( test1_0 );
103 CPPUNIT_TEST( test1 );
104 CPPUNIT_TEST( test1_1 );
105 CPPUNIT_TEST( test2 );
106 CPPUNIT_TEST( test3 );
107 CPPUNIT_TEST( test4 );
108 CPPUNIT_TEST_SUITE_END( );
111 void test_t_abs64::test0()
113 // this values has an overrun!
114 sal_Int32 n32 = 2147483648;
115 t_print("n32 should be -2^31 is: %d\n", n32);
116 CPPUNIT_ASSERT_MESSAGE("n32!=2147483648", n32 == -2147483648 );
120 void test_t_abs64::test1_0()
122 sal_Int64 n;
123 n = 1073741824;
124 n <<= 9;
125 t_print("Value of n is ");
126 t_print64(n);
127 CPPUNIT_ASSERT_MESSAGE("n=2^30 << 9", t_abs64(n) > 0 );
130 void test_t_abs64::test1()
132 sal_Int64 n;
133 n = 2147483648 << 8;
134 t_print("Value of n is ");
135 t_print64(n);
136 CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
138 void test_t_abs64::test1_1()
140 sal_Int64 n;
141 n = sal_Int64(2147483648) << 8;
142 t_print("Value of n is ");
143 t_print64(n);
144 CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
147 void test_t_abs64::test2()
149 sal_Int64 n;
150 n = 2147483648 << 1;
151 t_print("Value of n is ");
152 t_print64(n);
154 CPPUNIT_ASSERT_MESSAGE("(2147483648 << 1) is != 0", n != 0 );
156 sal_Int64 n2 = 2147483648 * 2;
157 CPPUNIT_ASSERT_MESSAGE("2147483648 * 2 is != 0", n2 != 0 );
159 sal_Int64 n3 = 4294967296LL;
160 CPPUNIT_ASSERT_MESSAGE("4294967296 is != 0", n3 != 0 );
162 CPPUNIT_ASSERT_MESSAGE("n=2^31 << 1, n2 = 2^31 * 2, n3 = 2^32, all should equal!", n == n2 && n == n3 );
166 void test_t_abs64::test3()
168 sal_Int64 n = 0;
169 CPPUNIT_ASSERT_MESSAGE("n=0", t_abs64(n) == 0 );
171 n = 1;
172 CPPUNIT_ASSERT_MESSAGE("n=1", t_abs64(n) > 0 );
174 n = 2147483647;
175 CPPUNIT_ASSERT_MESSAGE("n=2^31 - 1", t_abs64(n) > 0 );
177 n = 2147483648;
178 CPPUNIT_ASSERT_MESSAGE("n=2^31", t_abs64(n) > 0 );
181 void test_t_abs64::test4()
183 sal_Int64 n = 0;
184 n = -1;
185 t_print("Value of n is -1 : ");
186 t_print64(n);
187 CPPUNIT_ASSERT_MESSAGE("n=-1", t_abs64(n) > 0 );
189 n = -2147483648;
190 t_print("Value of n is -2^31 : ");
191 t_print64(n);
192 CPPUNIT_ASSERT_MESSAGE("n=-2^31", t_abs64(n) > 0 );
194 n = -8589934592LL;
195 t_print("Value of n is -2^33 : ");
196 t_print64(n);
197 CPPUNIT_ASSERT_MESSAGE("n=-2^33", t_abs64(n) > 0 );
201 // -----------------------------------------------------------------------------
202 class test_t_print : public CppUnit::TestFixture
204 public:
205 void t_print_001();
207 CPPUNIT_TEST_SUITE( test_t_print );
208 CPPUNIT_TEST( t_print_001 );
209 CPPUNIT_TEST_SUITE_END( );
212 void test_t_print::t_print_001( )
214 t_print("This is only a test of some helper functions\n");
215 sal_Int32 nValue = 12345;
216 t_print("a value %d (should be 12345)\n", nValue);
218 rtl::OString sValue("foo bar");
219 t_print("a String '%s' (should be 'foo bar')\n", sValue.getStr());
221 rtl::OUString suValue(rtl::OUString::createFromAscii("a unicode string"));
222 sValue <<= suValue;
223 t_print("a String '%s'\n", sValue.getStr());
227 class StopWatch
229 TimeValue m_aStartTime;
230 TimeValue m_aEndTime;
231 bool m_bStarted;
232 public:
233 StopWatch()
234 :m_bStarted(false)
238 void start()
240 m_bStarted = true;
241 osl_getSystemTime(&m_aStartTime);
243 void stop()
245 osl_getSystemTime(&m_aEndTime);
246 OSL_ENSURE(m_bStarted, "Not Started.");
247 m_bStarted = false;
249 rtl::OString makeTwoDigits(rtl::OString const& _sStr)
251 rtl::OString sBack;
252 if (_sStr.getLength() == 0)
254 sBack = "00";
256 else
258 if (_sStr.getLength() == 1)
260 sBack = "0" + _sStr;
262 else
264 sBack = _sStr;
267 return sBack;
269 rtl::OString makeThreeDigits(rtl::OString const& _sStr)
271 rtl::OString sBack;
272 if (_sStr.getLength() == 0)
274 sBack = "000";
276 else
278 if (_sStr.getLength() == 1)
280 sBack = "00" + _sStr;
282 else
284 if (_sStr.getLength() == 2)
286 sBack = "0" + _sStr;
288 else
290 sBack = _sStr;
294 return sBack;
297 void showTime(const rtl::OString & aWhatStr)
299 OSL_ENSURE(!m_bStarted, "Not Stopped.");
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)
306 nNanoSec = 1000000000 + nNanoSec;
307 nSeconds--;
308 // printf(" NEW Seconds: %d Nanosec: %d\n", nSeconds, nNanoSec);
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)));
326 printf("%s\n", aStr.getStr());
327 // cout << aStr.getStr() << endl;
332 static sal_Bool isEqualTimeValue ( const TimeValue* time1, const TimeValue* time2)
334 if( time1->Seconds == time2->Seconds &&
335 time1->Nanosec == time2->Nanosec)
336 return sal_True;
337 else
338 return sal_False;
341 static sal_Bool isGreaterTimeValue( const TimeValue* time1, const TimeValue* time2)
343 sal_Bool retval= sal_False;
344 if ( time1->Seconds > time2->Seconds)
345 retval= sal_True;
346 else if ( time1->Seconds == time2->Seconds)
348 if( time1->Nanosec > time2->Nanosec)
349 retval= sal_True;
351 return retval;
354 static sal_Bool isGreaterEqualTimeValue( const TimeValue* time1, const TimeValue* time2)
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;
364 bool isBTimeGreaterATime(TimeValue const& A, TimeValue const& B)
366 if (B.Seconds > A.Seconds) return true;
367 if (B.Nanosec > A.Nanosec) return true;
369 // lower or equal
370 return false;
372 // -----------------------------------------------------------------------------
375 class test_TimeValues : public CppUnit::TestFixture
377 public:
379 void t_time1();
380 void t_time2();
381 void t_time3();
383 CPPUNIT_TEST_SUITE( test_TimeValues );
384 CPPUNIT_TEST( t_time1 );
385 CPPUNIT_TEST( t_time2 );
386 CPPUNIT_TEST( t_time3 );
387 CPPUNIT_TEST_SUITE_END( );
390 void test_TimeValues::t_time1()
392 StopWatch aWatch;
393 aWatch.start();
394 TimeValue aTimeValue={3,0};
395 osl::Thread::wait(aTimeValue);
396 aWatch.stop();
397 aWatch.showTime("Wait for 3 seconds");
400 void test_TimeValues::t_time2()
402 t_print("Wait repeats 20 times.\n");
403 int i=0;
404 while(i++<20)
406 StopWatch aWatch;
407 aWatch.start();
408 TimeValue aTimeValue={0,1000 * 1000 * 500};
409 osl::Thread::wait(aTimeValue);
410 aWatch.stop();
411 aWatch.showTime("wait for 500msec");
415 void test_TimeValues::t_time3()
417 t_print("Wait repeats 100 times.\n");
418 int i=0;
419 while(i++<20)
421 StopWatch aWatch;
422 aWatch.start();
423 TimeValue aTimeValue={0,1000*1000*100};
424 osl::Thread::wait(aTimeValue);
425 aWatch.stop();
426 aWatch.showTime("wait for 100msec");
430 // void demoTimeValue()
431 // {
432 // TimeValue aStartTime, aEndTime;
433 // osl_getSystemTime(&aStartTime);
434 // // testSession(xORB, false);
435 // osl_getSystemTime(&aEndTime);
437 // sal_Int32 nSeconds = aEndTime.Seconds - aStartTime.Seconds;
438 // sal_Int32 nNanoSec = aEndTime.Nanosec - aStartTime.Nanosec;
439 // if (nNanoSec < 0)
440 // {
441 // nNanoSec = 1000000000 - nNanoSec;
442 // nSeconds++;
443 // }
445 // // cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
446 // }
449 } // namespace testOfHelperFunctions
451 // -----------------------------------------------------------------------------
452 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_print, "helperFunctions" );
453 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_abs64, "helperFunctions" );
454 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_TimeValues, "helperFunctions" );
456 // -----------------------------------------------------------------------------
457 NOADDITIONAL;