1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testHelperFunctions.cxx,v $
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
37 #include <osl/thread.hxx>
39 #include "stringhelper.hxx"
41 #include <cppunit/simpleheader.hxx>
45 // t_print("Ist ja echt bloed.\n");
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);
62 void t_print64(sal_Int64 n
)
72 sal_Int64 n64
= n
>> 32;
73 sal_uInt32 n32
= n64
& 0xffffffff;
74 printf("0x%.8x ", n32
);
76 printf("%.8x (64bit)", n32
);
80 sal_uInt32 n32
= n
& 0xffffffff;
81 printf("0x%.8x (32bit) ", n32
);
86 // -----------------------------------------------------------------------------
87 namespace testOfHelperFunctions
89 class test_t_abs64
: public CppUnit::TestFixture
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()
125 t_print("Value of n is ");
127 CPPUNIT_ASSERT_MESSAGE("n=2^30 << 9", t_abs64(n
) > 0 );
130 void test_t_abs64::test1()
134 t_print("Value of n is ");
136 CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n
) > 0 );
138 void test_t_abs64::test1_1()
141 n
= sal_Int64(2147483648) << 8;
142 t_print("Value of n is ");
144 CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n
) > 0 );
147 void test_t_abs64::test2()
151 t_print("Value of n is ");
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()
169 CPPUNIT_ASSERT_MESSAGE("n=0", t_abs64(n
) == 0 );
172 CPPUNIT_ASSERT_MESSAGE("n=1", t_abs64(n
) > 0 );
175 CPPUNIT_ASSERT_MESSAGE("n=2^31 - 1", t_abs64(n
) > 0 );
178 CPPUNIT_ASSERT_MESSAGE("n=2^31", t_abs64(n
) > 0 );
181 void test_t_abs64::test4()
185 t_print("Value of n is -1 : ");
187 CPPUNIT_ASSERT_MESSAGE("n=-1", t_abs64(n
) > 0 );
190 t_print("Value of n is -2^31 : ");
192 CPPUNIT_ASSERT_MESSAGE("n=-2^31", t_abs64(n
) > 0 );
195 t_print("Value of n is -2^33 : ");
197 CPPUNIT_ASSERT_MESSAGE("n=-2^33", t_abs64(n
) > 0 );
201 // -----------------------------------------------------------------------------
202 class test_t_print
: public CppUnit::TestFixture
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"));
223 t_print("a String '%s'\n", sValue
.getStr());
229 TimeValue m_aStartTime
;
230 TimeValue m_aEndTime
;
241 osl_getSystemTime(&m_aStartTime
);
245 osl_getSystemTime(&m_aEndTime
);
246 OSL_ENSURE(m_bStarted
, "Not Started.");
249 rtl::OString
makeTwoDigits(rtl::OString
const& _sStr
)
252 if (_sStr
.getLength() == 0)
258 if (_sStr
.getLength() == 1)
269 rtl::OString
makeThreeDigits(rtl::OString
const& _sStr
)
272 if (_sStr
.getLength() == 0)
278 if (_sStr
.getLength() == 1)
280 sBack
= "00" + _sStr
;
284 if (_sStr
.getLength() == 2)
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);
306 nNanoSec
= 1000000000 + nNanoSec
;
308 // printf(" NEW Seconds: %d Nanosec: %d\n", nSeconds, nNanoSec);
311 rtl::OString aStr
= "Time for ";
314 aStr
+= makeTwoDigits(rtl::OString::valueOf(nSeconds
/ 3600));
316 aStr
+= makeTwoDigits(rtl::OString::valueOf((nSeconds
% 3600) / 60));
318 aStr
+= makeTwoDigits(rtl::OString::valueOf((nSeconds
% 60)));
320 aStr
+= makeThreeDigits(rtl::OString::valueOf((nNanoSec
% 1000000000) / 1000000));
322 aStr
+= makeThreeDigits(rtl::OString::valueOf((nNanoSec
% 1000000) / 1000));
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
)
341 static sal_Bool
isGreaterTimeValue( const TimeValue
* time1
, const TimeValue
* time2
)
343 sal_Bool retval
= sal_False
;
344 if ( time1
->Seconds
> time2
->Seconds
)
346 else if ( time1
->Seconds
== time2
->Seconds
)
348 if( time1
->Nanosec
> time2
->Nanosec
)
354 static sal_Bool
isGreaterEqualTimeValue( const TimeValue
* time1
, const TimeValue
* time2
)
356 if( isEqualTimeValue( time1
, time2
) )
358 else if( isGreaterTimeValue( time1
, time2
))
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;
372 // -----------------------------------------------------------------------------
375 class test_TimeValues
: public CppUnit::TestFixture
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()
394 TimeValue aTimeValue
={3,0};
395 osl::Thread::wait(aTimeValue
);
397 aWatch
.showTime("Wait for 3 seconds");
400 void test_TimeValues::t_time2()
402 t_print("Wait repeats 20 times.\n");
408 TimeValue aTimeValue
={0,1000 * 1000 * 500};
409 osl::Thread::wait(aTimeValue
);
411 aWatch
.showTime("wait for 500msec");
415 void test_TimeValues::t_time3()
417 t_print("Wait repeats 100 times.\n");
423 TimeValue aTimeValue
={0,1000*1000*100};
424 osl::Thread::wait(aTimeValue
);
426 aWatch
.showTime("wait for 100msec");
430 // void demoTimeValue()
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;
441 // nNanoSec = 1000000000 - nNanoSec;
445 // // cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
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 // -----------------------------------------------------------------------------