1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // This is a test of helperfunctions
32 
33 #include <osl/time.h>
34 #include <osl/thread.hxx>
35 
36 #include "stringhelper.hxx"
37 
38 #include <testshl/simpleheader.hxx>
39 
40 // void isJaBloed()
41 // {
42 //     t_print("Ist ja echt bloed.\n");
43 // }
44 
45 inline sal_Int64 t_abs64(sal_Int64 _nValue)
46 {
47     // std::abs() seems to have some ambiguity problems (so-texas)
48     // return abs(_nValue);
49     t_print("t_abs64(%ld)\n", _nValue);
50     // CPPUNIT_ASSERT(_nValue < 2147483647);
51 
52     if (_nValue < 0)
53     {
54         _nValue = -_nValue;
55     }
56     return _nValue;
57 }
58 
59 void t_print64(sal_Int64 n)
60 {
61     if (n < 0)
62     {
63         // negativ
64         printf("-");
65         n = -n;
66     }
67     if (n > 2147483647)
68     {
69         sal_Int64 n64 = n >> 32;
70         sal_uInt32 n32 = n64 & 0xffffffff;
71         printf("0x%.8x ", n32);
72         n32 = n & 0xffffffff;
73         printf("%.8x (64bit)", n32);
74     }
75     else
76     {
77         sal_uInt32 n32 = n & 0xffffffff;
78         printf("0x%.8x (32bit) ", n32);
79     }
80     printf("\n");
81 }
82 
83 // -----------------------------------------------------------------------------
84 namespace testOfHelperFunctions
85 {
86     class test_t_abs64 : public CppUnit::TestFixture
87     {
88     public:
89         void test0();
90         void test1_0();
91         void test1();
92         void test1_1();
93         void test2();
94         void test3();
95         void test4();
96 
97         CPPUNIT_TEST_SUITE( test_t_abs64 );
98         CPPUNIT_TEST( test0 );
99         CPPUNIT_TEST( test1_0 );
100         CPPUNIT_TEST( test1 );
101         CPPUNIT_TEST( test1_1 );
102         CPPUNIT_TEST( test2 );
103         CPPUNIT_TEST( test3 );
104         CPPUNIT_TEST( test4 );
105         CPPUNIT_TEST_SUITE_END( );
106     };
107 
108     void test_t_abs64::test0()
109     {
110         // this values has an overrun!
111         sal_Int32 n32 = 2147483648;
112         t_print("n32 should be -2^31 is: %d\n", n32);
113         CPPUNIT_ASSERT_MESSAGE("n32!=2147483648", n32 == -2147483648 );
114     }
115 
116 
117     void test_t_abs64::test1_0()
118     {
119         sal_Int64 n;
120         n = 1073741824;
121         n <<= 9;
122         t_print("Value of n is ");
123         t_print64(n);
124         CPPUNIT_ASSERT_MESSAGE("n=2^30 << 9", t_abs64(n) > 0 );
125     }
126 
127     void test_t_abs64::test1()
128     {
129         sal_Int64 n;
130         n = 2147483648 << 8;
131         t_print("Value of n is ");
132         t_print64(n);
133         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
134     }
135     void test_t_abs64::test1_1()
136     {
137         sal_Int64 n;
138         n = sal_Int64(2147483648) << 8;
139         t_print("Value of n is ");
140         t_print64(n);
141         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
142     }
143 
144     void test_t_abs64::test2()
145     {
146         sal_Int64 n;
147         n = 2147483648 << 1;
148         t_print("Value of n is ");
149         t_print64(n);
150 
151         CPPUNIT_ASSERT_MESSAGE("(2147483648 << 1) is != 0", n != 0 );
152 
153         sal_Int64 n2 = 2147483648 * 2;
154         CPPUNIT_ASSERT_MESSAGE("2147483648 * 2 is != 0", n2 != 0 );
155 
156         sal_Int64 n3 = 4294967296LL;
157         CPPUNIT_ASSERT_MESSAGE("4294967296 is != 0", n3 != 0 );
158 
159         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 1, n2 = 2^31 * 2, n3 = 2^32, all should equal!", n == n2 && n == n3 );
160     }
161 
162 
163     void test_t_abs64::test3()
164     {
165         sal_Int64 n = 0;
166         CPPUNIT_ASSERT_MESSAGE("n=0", t_abs64(n) == 0 );
167 
168         n = 1;
169         CPPUNIT_ASSERT_MESSAGE("n=1", t_abs64(n) > 0 );
170 
171         n = 2147483647;
172         CPPUNIT_ASSERT_MESSAGE("n=2^31 - 1", t_abs64(n) > 0 );
173 
174         n = 2147483648;
175         CPPUNIT_ASSERT_MESSAGE("n=2^31", t_abs64(n) > 0 );
176     }
177 
178     void test_t_abs64::test4()
179     {
180         sal_Int64 n = 0;
181         n = -1;
182         t_print("Value of n is -1 : ");
183         t_print64(n);
184         CPPUNIT_ASSERT_MESSAGE("n=-1", t_abs64(n) > 0 );
185 
186         n = -2147483648;
187         t_print("Value of n is -2^31 : ");
188         t_print64(n);
189         CPPUNIT_ASSERT_MESSAGE("n=-2^31", t_abs64(n) > 0 );
190 
191         n = -8589934592LL;
192         t_print("Value of n is -2^33 : ");
193         t_print64(n);
194         CPPUNIT_ASSERT_MESSAGE("n=-2^33", t_abs64(n) > 0 );
195     }
196 
197 
198 // -----------------------------------------------------------------------------
199     class test_t_print : public CppUnit::TestFixture
200     {
201     public:
202         void t_print_001();
203 
204         CPPUNIT_TEST_SUITE( test_t_print );
205         CPPUNIT_TEST( t_print_001 );
206         CPPUNIT_TEST_SUITE_END( );
207     };
208 
209     void test_t_print::t_print_001( )
210     {
211         t_print("This is only a test of some helper functions\n");
212         sal_Int32 nValue = 12345;
213         t_print("a value %d (should be 12345)\n", nValue);
214 
215         rtl::OString sValue("foo bar");
216         t_print("a String '%s' (should be 'foo bar')\n", sValue.getStr());
217 
218         rtl::OUString suValue(rtl::OUString::createFromAscii("a unicode string"));
219         sValue <<= suValue;
220         t_print("a String '%s'\n", sValue.getStr());
221     }
222 
223 
224     class StopWatch
225     {
226         TimeValue m_aStartTime;
227         TimeValue m_aEndTime;
228         bool m_bStarted;
229     public:
230         StopWatch()
231                 :m_bStarted(false)
232             {
233             }
234 
235         void start()
236             {
237                 m_bStarted = true;
238                 osl_getSystemTime(&m_aStartTime);
239             }
240         void stop()
241             {
242                 osl_getSystemTime(&m_aEndTime);
243                 OSL_ENSURE(m_bStarted, "Not Started.");
244                 m_bStarted = false;
245             }
246         rtl::OString makeTwoDigits(rtl::OString const& _sStr)
247             {
248                 rtl::OString sBack;
249                 if (_sStr.getLength() == 0)
250                 {
251                     sBack = "00";
252                 }
253                 else
254                 {
255                     if (_sStr.getLength() == 1)
256                     {
257                         sBack = "0" + _sStr;
258                     }
259                     else
260                     {
261                         sBack = _sStr;
262                     }
263                 }
264                 return sBack;
265             }
266         rtl::OString makeThreeDigits(rtl::OString const& _sStr)
267             {
268                 rtl::OString sBack;
269                 if (_sStr.getLength() == 0)
270                 {
271                     sBack = "000";
272                 }
273                 else
274                 {
275                     if (_sStr.getLength() == 1)
276                     {
277                         sBack = "00" + _sStr;
278                     }
279                     else
280                     {
281                         if (_sStr.getLength() == 2)
282                         {
283                             sBack = "0" + _sStr;
284                         }
285                         else
286                         {
287                             sBack = _sStr;
288                         }
289                     }
290                 }
291                 return sBack;
292             }
293 
294         void  showTime(const rtl::OString & aWhatStr)
295             {
296                 OSL_ENSURE(!m_bStarted, "Not Stopped.");
297 
298                 sal_Int32 nSeconds = m_aEndTime.Seconds - m_aStartTime.Seconds;
299                 sal_Int32 nNanoSec = sal_Int32(m_aEndTime.Nanosec) - sal_Int32(m_aStartTime.Nanosec);
300                 // printf("Seconds: %d Nanosec: %d ", nSeconds, nNanoSec);
301                 if (nNanoSec < 0)
302                 {
303                     nNanoSec = 1000000000 + nNanoSec;
304                     nSeconds--;
305                     // printf(" NEW Seconds: %d Nanosec: %d\n", nSeconds, nNanoSec);
306                 }
307 
308                 rtl::OString aStr = "Time for ";
309                 aStr += aWhatStr;
310                 aStr += " ";
311                 aStr += makeTwoDigits(rtl::OString::valueOf(nSeconds / 3600));
312                 aStr += ":";
313                 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 3600) / 60));
314                 aStr += ":";
315                 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 60)));
316                 aStr += ":";
317                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000000) / 1000000));
318                 aStr += ":";
319                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000) / 1000));
320                 aStr += ":";
321                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000)));
322 
323                 printf("%s\n", aStr.getStr());
324                 // cout << aStr.getStr() << endl;
325             }
326 
327     };
328 
329 static sal_Bool isEqualTimeValue ( const TimeValue* time1,  const TimeValue* time2)
330 {
331 	if( time1->Seconds == time2->Seconds &&
332 		time1->Nanosec == time2->Nanosec)
333 		return sal_True;
334 	else
335 		return sal_False;
336 }
337 
338 static sal_Bool isGreaterTimeValue(  const TimeValue* time1,  const TimeValue* time2)
339 {
340 	sal_Bool retval= sal_False;
341 	if ( time1->Seconds > time2->Seconds)
342 		retval= sal_True;
343 	else if ( time1->Seconds == time2->Seconds)
344 	{
345 		if( time1->Nanosec > time2->Nanosec)
346 			retval= sal_True;
347 	}
348 	return retval;
349 }
350 
351 static sal_Bool isGreaterEqualTimeValue( const TimeValue* time1, const TimeValue* time2)
352 {
353 	if( isEqualTimeValue( time1, time2) )
354 		return sal_True;
355 	else if( isGreaterTimeValue( time1, time2))
356 		return sal_True;
357 	else
358 		return sal_False;
359 }
360 
361 bool isBTimeGreaterATime(TimeValue const& A, TimeValue const& B)
362 {
363 	if (B.Seconds > A.Seconds) return true;
364 	if (B.Nanosec > A.Nanosec) return true;
365 
366 	// lower or equal
367 	return false;
368 }
369     // -----------------------------------------------------------------------------
370 
371 
372     class test_TimeValues : public CppUnit::TestFixture
373     {
374     public:
375 
376         void t_time1();
377         void t_time2();
378         void t_time3();
379 
380         CPPUNIT_TEST_SUITE( test_TimeValues );
381         CPPUNIT_TEST( t_time1 );
382         CPPUNIT_TEST( t_time2 );
383         CPPUNIT_TEST( t_time3 );
384         CPPUNIT_TEST_SUITE_END( );
385     };
386 
387 void test_TimeValues::t_time1()
388 {
389     StopWatch aWatch;
390     aWatch.start();
391     TimeValue aTimeValue={3,0};
392     osl::Thread::wait(aTimeValue);
393     aWatch.stop();
394     aWatch.showTime("Wait for 3 seconds");
395 }
396 
397 void test_TimeValues::t_time2()
398 {
399     t_print("Wait repeats 20 times.\n");
400     int i=0;
401     while(i++<20)
402     {
403         StopWatch aWatch;
404         aWatch.start();
405         TimeValue aTimeValue={0,1000 * 1000 * 500};
406         osl::Thread::wait(aTimeValue);
407         aWatch.stop();
408         aWatch.showTime("wait for 500msec");
409     }
410 }
411 
412 void test_TimeValues::t_time3()
413 {
414     t_print("Wait repeats 100 times.\n");
415     int i=0;
416     while(i++<20)
417     {
418         StopWatch aWatch;
419         aWatch.start();
420         TimeValue aTimeValue={0,1000*1000*100};
421         osl::Thread::wait(aTimeValue);
422         aWatch.stop();
423         aWatch.showTime("wait for 100msec");
424     }
425 }
426 
427     // void demoTimeValue()
428     // {
429     //     TimeValue aStartTime, aEndTime;
430     //     osl_getSystemTime(&aStartTime);
431     //     // testSession(xORB, false);
432     //     osl_getSystemTime(&aEndTime);
433     //
434     //     sal_Int32 nSeconds = aEndTime.Seconds - aStartTime.Seconds;
435     //     sal_Int32 nNanoSec = aEndTime.Nanosec - aStartTime.Nanosec;
436     //     if (nNanoSec < 0)
437     //     {
438     //         nNanoSec = 1000000000 - nNanoSec;
439     //         nSeconds++;
440     //     }
441     //
442     //     // cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
443     // }
444 
445 
446 } // namespace testOfHelperFunctions
447 
448 // -----------------------------------------------------------------------------
449 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_print, "helperFunctions" );
450 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_abs64, "helperFunctions" );
451 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_TimeValues, "helperFunctions" );
452 
453 // -----------------------------------------------------------------------------
454 NOADDITIONAL;
455