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 <testshl/simpleheader.hxx>
35 
36 // void isJaBloed()
37 // {
38 //     t_print("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     t_print("t_abs64(%ld)\n", _nValue);
46     // CPPUNIT_ASSERT(_nValue < 2147483647);
47 
48     if (_nValue < 0)
49     {
50         _nValue = -_nValue;
51     }
52     return _nValue;
53 }
54 
t_print64(sal_Int64 n)55 void t_print64(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 CppUnit::TestFixture
83     {
84     public:
85         void test0();
86         void test1_0();
87         void test1();
88         void test1_1();
89         void test2();
90         void test3();
91         void test4();
92 
93         CPPUNIT_TEST_SUITE( test_t_abs64 );
94         CPPUNIT_TEST( test0 );
95         CPPUNIT_TEST( test1_0 );
96         CPPUNIT_TEST( test1 );
97         CPPUNIT_TEST( test1_1 );
98         CPPUNIT_TEST( test2 );
99         CPPUNIT_TEST( test3 );
100         CPPUNIT_TEST( test4 );
101         CPPUNIT_TEST_SUITE_END( );
102     };
103 
test0()104     void test_t_abs64::test0()
105     {
106         // this values has an overrun!
107         sal_Int32 n32 = 2147483648;
108         t_print("n32 should be -2^31 is: %d\n", n32);
109         CPPUNIT_ASSERT_MESSAGE("n32!=2147483648", n32 == -2147483648 );
110     }
111 
112 
test1_0()113     void test_t_abs64::test1_0()
114     {
115         sal_Int64 n;
116         n = 1073741824;
117         n <<= 9;
118         t_print("Value of n is ");
119         t_print64(n);
120         CPPUNIT_ASSERT_MESSAGE("n=2^30 << 9", t_abs64(n) > 0 );
121     }
122 
test1()123     void test_t_abs64::test1()
124     {
125         sal_Int64 n;
126         n = 2147483648 << 8;
127         t_print("Value of n is ");
128         t_print64(n);
129         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
130     }
test1_1()131     void test_t_abs64::test1_1()
132     {
133         sal_Int64 n;
134         n = sal_Int64(2147483648) << 8;
135         t_print("Value of n is ");
136         t_print64(n);
137         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 8", t_abs64(n) > 0 );
138     }
139 
test2()140     void test_t_abs64::test2()
141     {
142         sal_Int64 n;
143         n = 2147483648 << 1;
144         t_print("Value of n is ");
145         t_print64(n);
146 
147         CPPUNIT_ASSERT_MESSAGE("(2147483648 << 1) is != 0", n != 0 );
148 
149         sal_Int64 n2 = 2147483648 * 2;
150         CPPUNIT_ASSERT_MESSAGE("2147483648 * 2 is != 0", n2 != 0 );
151 
152         sal_Int64 n3 = 4294967296LL;
153         CPPUNIT_ASSERT_MESSAGE("4294967296 is != 0", n3 != 0 );
154 
155         CPPUNIT_ASSERT_MESSAGE("n=2^31 << 1, n2 = 2^31 * 2, n3 = 2^32, all should equal!", n == n2 && n == n3 );
156     }
157 
158 
test3()159     void test_t_abs64::test3()
160     {
161         sal_Int64 n = 0;
162         CPPUNIT_ASSERT_MESSAGE("n=0", t_abs64(n) == 0 );
163 
164         n = 1;
165         CPPUNIT_ASSERT_MESSAGE("n=1", t_abs64(n) > 0 );
166 
167         n = 2147483647;
168         CPPUNIT_ASSERT_MESSAGE("n=2^31 - 1", t_abs64(n) > 0 );
169 
170         n = 2147483648;
171         CPPUNIT_ASSERT_MESSAGE("n=2^31", t_abs64(n) > 0 );
172     }
173 
test4()174     void test_t_abs64::test4()
175     {
176         sal_Int64 n = 0;
177         n = -1;
178         t_print("Value of n is -1 : ");
179         t_print64(n);
180         CPPUNIT_ASSERT_MESSAGE("n=-1", t_abs64(n) > 0 );
181 
182         n = -2147483648;
183         t_print("Value of n is -2^31 : ");
184         t_print64(n);
185         CPPUNIT_ASSERT_MESSAGE("n=-2^31", t_abs64(n) > 0 );
186 
187         n = -8589934592LL;
188         t_print("Value of n is -2^33 : ");
189         t_print64(n);
190         CPPUNIT_ASSERT_MESSAGE("n=-2^33", t_abs64(n) > 0 );
191     }
192 
193 
194 // -----------------------------------------------------------------------------
195     class test_t_print : public CppUnit::TestFixture
196     {
197     public:
198         void t_print_001();
199 
200         CPPUNIT_TEST_SUITE( test_t_print );
201         CPPUNIT_TEST( t_print_001 );
202         CPPUNIT_TEST_SUITE_END( );
203     };
204 
t_print_001()205     void test_t_print::t_print_001( )
206     {
207         t_print("This is only a test of some helper functions\n");
208         sal_Int32 nValue = 12345;
209         t_print("a value %d (should be 12345)\n", nValue);
210 
211         rtl::OString sValue("foo bar");
212         t_print("a String '%s' (should be 'foo bar')\n", sValue.getStr());
213 
214         rtl::OUString suValue(rtl::OUString::createFromAscii("a unicode string"));
215         sValue <<= suValue;
216         t_print("a String '%s'\n", sValue.getStr());
217     }
218 
219 
220     class StopWatch
221     {
222         TimeValue m_aStartTime;
223         TimeValue m_aEndTime;
224         bool m_bStarted;
225     public:
StopWatch()226         StopWatch()
227                 :m_bStarted(false)
228             {
229             }
230 
start()231         void start()
232             {
233                 m_bStarted = true;
234                 osl_getSystemTime(&m_aStartTime);
235             }
stop()236         void stop()
237             {
238                 osl_getSystemTime(&m_aEndTime);
239                 OSL_ENSURE(m_bStarted, "Not Started.");
240                 m_bStarted = false;
241             }
makeTwoDigits(rtl::OString const & _sStr)242         rtl::OString makeTwoDigits(rtl::OString const& _sStr)
243             {
244                 rtl::OString sBack;
245                 if (_sStr.getLength() == 0)
246                 {
247                     sBack = "00";
248                 }
249                 else
250                 {
251                     if (_sStr.getLength() == 1)
252                     {
253                         sBack = "0" + _sStr;
254                     }
255                     else
256                     {
257                         sBack = _sStr;
258                     }
259                 }
260                 return sBack;
261             }
makeThreeDigits(rtl::OString const & _sStr)262         rtl::OString makeThreeDigits(rtl::OString const& _sStr)
263             {
264                 rtl::OString sBack;
265                 if (_sStr.getLength() == 0)
266                 {
267                     sBack = "000";
268                 }
269                 else
270                 {
271                     if (_sStr.getLength() == 1)
272                     {
273                         sBack = "00" + _sStr;
274                     }
275                     else
276                     {
277                         if (_sStr.getLength() == 2)
278                         {
279                             sBack = "0" + _sStr;
280                         }
281                         else
282                         {
283                             sBack = _sStr;
284                         }
285                     }
286                 }
287                 return sBack;
288             }
289 
showTime(const rtl::OString & aWhatStr)290         void  showTime(const rtl::OString & aWhatStr)
291             {
292                 OSL_ENSURE(!m_bStarted, "Not Stopped.");
293 
294                 sal_Int32 nSeconds = m_aEndTime.Seconds - m_aStartTime.Seconds;
295                 sal_Int32 nNanoSec = sal_Int32(m_aEndTime.Nanosec) - sal_Int32(m_aStartTime.Nanosec);
296                 // printf("Seconds: %d Nanosec: %d ", nSeconds, nNanoSec);
297                 if (nNanoSec < 0)
298                 {
299                     nNanoSec = 1000000000 + nNanoSec;
300                     nSeconds--;
301                     // printf(" NEW Seconds: %d Nanosec: %d\n", nSeconds, nNanoSec);
302                 }
303 
304                 rtl::OString aStr = "Time for ";
305                 aStr += aWhatStr;
306                 aStr += " ";
307                 aStr += makeTwoDigits(rtl::OString::valueOf(nSeconds / 3600));
308                 aStr += ":";
309                 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 3600) / 60));
310                 aStr += ":";
311                 aStr += makeTwoDigits(rtl::OString::valueOf((nSeconds % 60)));
312                 aStr += ":";
313                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000000) / 1000000));
314                 aStr += ":";
315                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000000) / 1000));
316                 aStr += ":";
317                 aStr += makeThreeDigits(rtl::OString::valueOf((nNanoSec % 1000)));
318 
319                 printf("%s\n", aStr.getStr());
320                 // cout << aStr.getStr() << endl;
321             }
322 
323     };
324 
isEqualTimeValue(const TimeValue * time1,const TimeValue * time2)325 static sal_Bool isEqualTimeValue ( const TimeValue* time1,  const TimeValue* time2)
326 {
327 	if( time1->Seconds == time2->Seconds &&
328 		time1->Nanosec == time2->Nanosec)
329 		return sal_True;
330 	else
331 		return sal_False;
332 }
333 
isGreaterTimeValue(const TimeValue * time1,const TimeValue * time2)334 static sal_Bool isGreaterTimeValue(  const TimeValue* time1,  const TimeValue* time2)
335 {
336 	sal_Bool retval= sal_False;
337 	if ( time1->Seconds > time2->Seconds)
338 		retval= sal_True;
339 	else if ( time1->Seconds == time2->Seconds)
340 	{
341 		if( time1->Nanosec > time2->Nanosec)
342 			retval= sal_True;
343 	}
344 	return retval;
345 }
346 
isGreaterEqualTimeValue(const TimeValue * time1,const TimeValue * time2)347 static sal_Bool isGreaterEqualTimeValue( const TimeValue* time1, const TimeValue* time2)
348 {
349 	if( isEqualTimeValue( time1, time2) )
350 		return sal_True;
351 	else if( isGreaterTimeValue( time1, time2))
352 		return sal_True;
353 	else
354 		return sal_False;
355 }
356 
isBTimeGreaterATime(TimeValue const & A,TimeValue const & B)357 bool isBTimeGreaterATime(TimeValue const& A, TimeValue const& B)
358 {
359 	if (B.Seconds > A.Seconds) return true;
360 	if (B.Nanosec > A.Nanosec) return true;
361 
362 	// lower or equal
363 	return false;
364 }
365     // -----------------------------------------------------------------------------
366 
367 
368     class test_TimeValues : public CppUnit::TestFixture
369     {
370     public:
371 
372         void t_time1();
373         void t_time2();
374         void t_time3();
375 
376         CPPUNIT_TEST_SUITE( test_TimeValues );
377         CPPUNIT_TEST( t_time1 );
378         CPPUNIT_TEST( t_time2 );
379         CPPUNIT_TEST( t_time3 );
380         CPPUNIT_TEST_SUITE_END( );
381     };
382 
t_time1()383 void test_TimeValues::t_time1()
384 {
385     StopWatch aWatch;
386     aWatch.start();
387     TimeValue aTimeValue={3,0};
388     osl::Thread::wait(aTimeValue);
389     aWatch.stop();
390     aWatch.showTime("Wait for 3 seconds");
391 }
392 
t_time2()393 void test_TimeValues::t_time2()
394 {
395     t_print("Wait repeats 20 times.\n");
396     int i=0;
397     while(i++<20)
398     {
399         StopWatch aWatch;
400         aWatch.start();
401         TimeValue aTimeValue={0,1000 * 1000 * 500};
402         osl::Thread::wait(aTimeValue);
403         aWatch.stop();
404         aWatch.showTime("wait for 500msec");
405     }
406 }
407 
t_time3()408 void test_TimeValues::t_time3()
409 {
410     t_print("Wait repeats 100 times.\n");
411     int i=0;
412     while(i++<20)
413     {
414         StopWatch aWatch;
415         aWatch.start();
416         TimeValue aTimeValue={0,1000*1000*100};
417         osl::Thread::wait(aTimeValue);
418         aWatch.stop();
419         aWatch.showTime("wait for 100msec");
420     }
421 }
422 
423     // void demoTimeValue()
424     // {
425     //     TimeValue aStartTime, aEndTime;
426     //     osl_getSystemTime(&aStartTime);
427     //     // testSession(xORB, false);
428     //     osl_getSystemTime(&aEndTime);
429     //
430     //     sal_Int32 nSeconds = aEndTime.Seconds - aStartTime.Seconds;
431     //     sal_Int32 nNanoSec = aEndTime.Nanosec - aStartTime.Nanosec;
432     //     if (nNanoSec < 0)
433     //     {
434     //         nNanoSec = 1000000000 - nNanoSec;
435     //         nSeconds++;
436     //     }
437     //
438     //     // cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
439     // }
440 
441 
442 } // namespace testOfHelperFunctions
443 
444 // -----------------------------------------------------------------------------
445 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_print, "helperFunctions" );
446 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_t_abs64, "helperFunctions" );
447 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( testOfHelperFunctions::test_TimeValues, "helperFunctions" );
448 
449 // -----------------------------------------------------------------------------
450 NOADDITIONAL;
451