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