]> git.saurik.com Git - wxWidgets.git/blob - tests/lltest.cpp
122a148b108e5cf2866e3752ff428780a6e30387
[wxWidgets.git] / tests / lltest.cpp
1 #include "wx/longlong.h"
2
3 #include <time.h>
4 #include <stdlib.h>
5 #if defined(__MWERKS__) && macintosh
6 #include <SIOUX.h>
7 #include <Events.h>
8 #endif
9
10 static const unsigned long long NumberOfTests = 100000ll;
11
12 //using namespace std;
13 int oddbits(unsigned int number);
14
15 int oddbits(unsigned int number)
16 {
17 int sum = 0;
18 while(number > 0)
19 {
20 sum += number & 1;
21 number >>= 1;
22 }
23
24 return sum & 1;
25 }
26 void random(long& hi, unsigned long& lo);
27
28 void random(long& hi, unsigned long& lo)
29 {
30 hi = 0;
31 lo = 0;
32
33 for (int i = 0; i < 32; i++)
34 {
35 hi |= oddbits(rand()) << i;
36 lo |= oddbits(rand()) << i;
37 }
38 }
39
40 int shifttest();
41
42 int shifttest()
43 {
44 std::cout << std::endl << "Starting shift tests." << endl;
45
46 long hi = 0;
47 unsigned long lo = 0;
48
49 unsigned wxLongLong_t counter = 0;
50
51 while (counter < NumberOfTests)
52 {
53 if ((counter % 1000) == 999)
54 {
55 std::cerr << "+";
56 #if defined(__MWERKS__) && macintosh
57 GetNextEvent(0, NULL);
58 #endif
59 }
60
61 random(hi, lo);
62 wxLongLongWx l1a(hi, lo);
63 wxLongLongWx l1c(hi, lo);
64 wxLongLongWx l1b;
65 wxLongLongNative l2a(hi, lo);
66 wxLongLongNative l2c(hi, lo);
67 wxLongLongNative l2b;
68
69 int shift = rand() % 64;
70 int leftshift = oddbits(rand());
71
72 if (leftshift)
73 {
74 l1b = l1a << shift;
75 l2b = l2a << shift;
76 l1c <<= shift;
77 l2c <<= shift;
78 }
79 else
80 {
81 l1b = l1a >> shift;
82 l2b = l2a >> shift;
83 l1c >>= shift;
84 l2c >>= shift;
85 }
86
87 void *oneb = l1b.asArray();
88 void *twob = l2b.asArray();
89 void *onec = l1c.asArray();
90 void *twoc = l2c.asArray();
91
92 if ((memcmp(oneb, twob, 8) != 0) || (memcmp(onec, twoc, 8) != 0) || (memcmp(oneb, onec, 8) != 0))
93 {
94 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
95 std::cout << l1a << std::endl;
96 std::cout << l2a << std::endl;
97 if (leftshift)
98 std::cout << "<< ";
99 else
100 std::cout << ">> ";
101 std::cout << shift << std::endl;
102 std::cout << l1b << std::endl;
103 std::cout << l2b << std::endl;
104 std::cout << l1c << std::endl;
105 std::cout << l2c << std::endl;
106 return 0;
107 }
108 counter++;
109 }
110
111 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
112
113 return 1;
114 }
115
116 int add1test();
117
118 int add1test()
119 {
120 std::cout << std::endl << "Starting first addition tests." << endl;
121
122 long hi = 0;
123 unsigned long lo = 0;
124
125 unsigned wxLongLong_t counter = 0;
126
127 while (counter < NumberOfTests)
128 {
129 if ((counter % 1000) == 999)
130 {
131 std::cerr << "+";
132 #if defined(__MWERKS__) && macintosh
133 GetNextEvent(0, NULL);
134 #endif
135 }
136
137 random(hi, lo);
138 wxLongLongWx l1a(hi, lo);
139 wxLongLongNative l2a(hi, lo);
140 wxLongLongWx l1d(hi, lo);
141 wxLongLongNative l2d(hi, lo);
142 random(hi, lo);
143 wxLongLongWx l1b(hi, lo);
144 wxLongLongNative l2b(hi, lo);
145 wxLongLongWx l1c;
146 wxLongLongNative l2c;
147
148 l1c = l1a + l1b;
149 l2c = l2a + l2b;
150 l1d += l1b;
151 l2d += l2b;
152
153 void *onec = l1c.asArray();
154 void *twoc = l2c.asArray();
155 void *oned = l1d.asArray();
156 void *twod = l2d.asArray();
157
158 if ((memcmp(onec, twoc, 8) != 0) || (memcmp(oned, twod, 8) != 0) || (memcmp(onec, oned, 8) != 0))
159 {
160 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
161 std::cout << l1a << std::endl;
162 std::cout << l2a << std::endl;
163 std::cout << l1b << std::endl;
164 std::cout << l2b << std::endl;
165 std::cout << l1c << std::endl;
166 std::cout << l2c << std::endl;
167 return 0;
168 }
169 counter++;
170 }
171
172 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
173
174 return 1;
175 }
176
177 /*
178 int add2test();
179
180 int add2test()
181 {
182 std::cout << std::endl << "Starting second addition tests." << endl;
183
184 long hi = 0;
185 unsigned long lo = 0;
186
187 unsigned wxLongLong_t counter = 0;
188
189 while (counter < NumberOfTests)
190 {
191 if ((counter % 1000) == 999)
192 {
193 std::cerr << "+";
194 #if defined(__MWERKS__) && macintosh
195 GetNextEvent(0, NULL);
196 #endif
197 }
198
199 random(hi, lo);
200 wxLongLongWx l1a(hi, lo);
201 wxLongLongNative l2a(hi, lo);
202 wxLongLongWx l1c(hi, lo);
203 wxLongLongNative l2c(hi, lo);
204 random(hi, lo);
205 wxLongLongWx l1b(hi, lo);
206 wxLongLongNative l2b(hi, lo);
207
208 l1b += l1a;
209 l2b += l2a;
210
211 void *one = l1b.asArray();
212 void *two = l2b.asArray();
213
214 if (memcmp(one, two, 8) != 0)
215 {
216 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
217 std::cout << l1c << std::endl;
218 std::cout << l2c << std::endl;
219 std::cout << l1a << std::endl;
220 std::cout << l2a << std::endl;
221 std::cout << l1b << std::endl;
222 std::cout << l2b << std::endl;
223 return 0;
224 }
225 counter++;
226 }
227
228 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
229
230 return 1;
231 }
232 */
233
234 int sub1test();
235
236 int sub1test()
237 {
238 std::cout << std::endl << "Starting first subtraction tests." << endl;
239
240 long hi = 0;
241 unsigned long lo = 0;
242
243 unsigned wxLongLong_t counter = 0;
244
245 while (counter < NumberOfTests)
246 {
247 if ((counter % 1000) == 999)
248 {
249 std::cerr << "+";
250 #if defined(__MWERKS__) && macintosh
251 GetNextEvent(0, NULL);
252 #endif
253 }
254
255 random(hi, lo);
256 wxLongLongWx l1a(hi, lo);
257 wxLongLongNative l2a(hi, lo);
258 wxLongLongWx l1d(hi, lo);
259 wxLongLongNative l2d(hi, lo);
260 random(hi, lo);
261 wxLongLongWx l1b(hi, lo);
262 wxLongLongNative l2b(hi, lo);
263 wxLongLongWx l1c;
264 wxLongLongNative l2c;
265
266 l1c = l1a - l1b;
267 l2c = l2a - l2b;
268 l1d -= l1b;
269 l2d -= l2b;
270
271 void *onec = l1c.asArray();
272 void *twoc = l2c.asArray();
273 void *oned = l1d.asArray();
274 void *twod = l2d.asArray();
275
276 if ((memcmp(onec, twoc, 8) != 0) || (memcmp(oned, twod, 8) != 0) || (memcmp(onec, oned, 8) != 0))
277 {
278 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
279 std::cout << l1a << std::endl;
280 std::cout << l2a << std::endl;
281 std::cout << l1b << std::endl;
282 std::cout << l2b << std::endl;
283 std::cout << l1c << std::endl;
284 std::cout << l2c << std::endl;
285 std::cout << l1d << std::endl;
286 std::cout << l2d << std::endl;
287 return 0;
288 }
289 counter++;
290 }
291
292 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
293
294 return 1;
295 }
296
297 /*
298 int sub2test();
299
300 int sub2test()
301 {
302 std::cout << std::endl << "Starting second subtraction tests." << endl;
303
304 long hi = 0;
305 unsigned long lo = 0;
306
307 unsigned wxLongLong_t counter = 0;
308
309 while (counter < NumberOfTests)
310 {
311 if ((counter % 1000) == 999)
312 {
313 std::cerr << "+";
314 #if defined(__MWERKS__) && macintosh
315 GetNextEvent(0, NULL);
316 #endif
317 }
318
319 random(hi, lo);
320 wxLongLongWx l1a(hi, lo);
321 wxLongLongNative l2a(hi, lo);
322 wxLongLongWx l1c(hi, lo);
323 wxLongLongNative l2c(hi, lo);
324 random(hi, lo);
325 wxLongLongWx l1b(hi, lo);
326 wxLongLongNative l2b(hi, lo);
327
328 l1b -= l1a;
329 l2b -= l2a;
330
331 void *one = l1b.asArray();
332 void *two = l2b.asArray();
333
334 if (memcmp(one, two, 8) != 0)
335 {
336 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
337 std::cout << l1c << std::endl;
338 std::cout << l2c << std::endl;
339 std::cout << l1a << std::endl;
340 std::cout << l2a << std::endl;
341 std::cout << l1b << std::endl;
342 std::cout << l2b << std::endl;
343 return 0;
344 }
345 counter++;
346 }
347
348 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
349
350 return 1;
351 }
352
353 */
354
355 int incdectest();
356
357 int incdectest()
358 {
359 std::cout << std::endl << "Starting pre/post increment/decrement tests." << endl;
360
361 long hi = 0;
362 unsigned long lo = 0;
363
364 unsigned wxLongLong_t counter = 0;
365
366 while (counter < NumberOfTests)
367 {
368 if ((counter % 1000) == 999)
369 {
370 std::cerr << "+";
371 #if defined(__MWERKS__) && macintosh
372 GetNextEvent(0, NULL);
373 #endif
374 }
375
376 random(hi, lo);
377 wxLongLongWx l1a(hi, lo);
378 wxLongLongNative l2a(hi, lo);
379 wxLongLongWx l1b(hi, lo);
380 wxLongLongNative l2b(hi, lo);
381
382 int increment = oddbits(rand());
383 int pre = oddbits(rand());
384
385 if (increment)
386 if (pre)
387 {
388 ++l1b;
389 ++l2b;
390 }
391 else
392 {
393 l1b++;
394 l2b++;
395 }
396 else
397 if (pre)
398 {
399 --l1b;
400 --l2b;
401 }
402 else
403 {
404 l1b--;
405 l2b--;
406 }
407
408 void *one = l1b.asArray();
409 void *two = l2b.asArray();
410
411 if (memcmp(one, two, 8) != 0)
412 {
413 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
414 std::cout << l1a << std::endl;
415 std::cout << l2a << std::endl;
416 std::cout << (pre ? "pre" : "post") << (increment ? "increment" : "decrement") << std::endl;
417 std::cout << l1b << std::endl;
418 std::cout << l2b << std::endl;
419 return 0;
420 }
421 counter++;
422 }
423
424 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
425
426 return 1;
427 }
428
429 int negationtest();
430
431 int negationtest()
432 {
433 std::cout << std::endl << "Starting negation tests." << endl;
434
435 long hi = 0;
436 unsigned long lo = 0;
437
438 unsigned wxLongLong_t counter = 0;
439
440 while (counter < NumberOfTests)
441 {
442 if ((counter % 1000) == 999)
443 {
444 std::cerr << "+";
445 #if defined(__MWERKS__) && macintosh
446 GetNextEvent(0, NULL);
447 #endif
448 }
449
450 random(hi, lo);
451 wxLongLongWx l1a(hi, lo);
452 wxLongLongNative l2a(hi, lo);
453 wxLongLongWx l1b(-l1a);
454 wxLongLongNative l2b(-l2a);
455
456 void *one = l1b.asArray();
457 void *two = l2b.asArray();
458
459 if (memcmp(one, two, 8) != 0)
460 {
461 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
462 std::cout << l1a << std::endl;
463 std::cout << l2a << std::endl;
464 std::cout << l1b << std::endl;
465 std::cout << l2b << std::endl;
466 return 0;
467 }
468 counter++;
469 }
470
471 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
472
473 return 1;
474 }
475
476 int multiplicationtest();
477
478 int multiplicationtest()
479 {
480 std::cout << std::endl << "Starting multiplication tests." << endl;
481
482 long hi = 0;
483 unsigned long lo = 0;
484
485 unsigned wxLongLong_t counter = 0;
486
487 while (counter < NumberOfTests)
488 {
489 if ((counter % 1000) == 999)
490 {
491 std::cerr << "+";
492 #if defined(__MWERKS__) && macintosh
493 GetNextEvent(0, NULL);
494 #endif
495 }
496
497 random(hi, lo);
498 wxLongLongWx l1a(hi, lo);
499 wxLongLongNative l2a(hi, lo);
500 wxLongLongWx l1d(hi, lo);
501 wxLongLongNative l2d(hi, lo);
502 random(hi, lo);
503 wxLongLongWx l1b(hi, lo);
504 wxLongLongNative l2b(hi, lo);
505
506 wxLongLongWx l1c(l1a * l1b);
507 wxLongLongNative l2c(l2a * l2b);
508
509 l1d *= l1b;
510 l2d *= l2b;
511
512 void *onec = l1c.asArray();
513 void *twoc = l2c.asArray();
514 void *oned = l1c.asArray();
515 void *twod = l2c.asArray();
516
517 if ((memcmp(onec, twoc, 8) != 0) || (memcmp(oned, twod, 8) != 0) || (memcmp(onec, oned, 8) != 0))
518 {
519 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
520 std::cout << l1a << std::endl;
521 std::cout << l2a << std::endl;
522 std::cout << l1b << std::endl;
523 std::cout << l2b << std::endl;
524 std::cout << l1c << std::endl;
525 std::cout << l2c << std::endl;
526 std::cout << l1d << std::endl;
527 std::cout << l2d << std::endl;
528 return 0;
529 }
530 counter++;
531 }
532
533 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
534
535 return 1;
536 }
537
538 int bitwisetest();
539
540 int bitwisetest()
541 {
542 std::cout << std::endl << "Starting bitwise tests." << endl;
543
544 long hi = 0;
545 unsigned long lo = 0;
546
547 unsigned wxLongLong_t counter = 0;
548
549 while (counter < NumberOfTests)
550 {
551 if ((counter % 1000) == 999)
552 {
553 std::cerr << "+";
554 #if defined(__MWERKS__) && macintosh
555 GetNextEvent(0, NULL);
556 #endif
557 }
558
559 random(hi, lo);
560 wxLongLongWx l1a(hi, lo);
561 wxLongLongNative l2a(hi, lo);
562 wxLongLongWx l1d(hi, lo);
563 wxLongLongNative l2d(hi, lo);
564 random(hi, lo);
565 wxLongLongWx l1b(hi, lo);
566 wxLongLongNative l2b(hi, lo);
567
568 wxLongLongWx l1c(l1a * l1b);
569 wxLongLongNative l2c(l2a * l2b);
570
571 l1d *= l1b;
572 l2d *= l2b;
573
574 void *onec = l1c.asArray();
575 void *twoc = l2c.asArray();
576 void *oned = l1c.asArray();
577 void *twod = l2c.asArray();
578
579 if ((memcmp(onec, twoc, 8) != 0) || (memcmp(oned, twod, 8) != 0) || (memcmp(onec, oned, 8) != 0))
580 {
581 std::cout << std::endl << "After " << counter << " successful trials: " << std::endl;
582 std::cout << l1a << std::endl;
583 std::cout << l2a << std::endl;
584 std::cout << l1b << std::endl;
585 std::cout << l2b << std::endl;
586 std::cout << l1c << std::endl;
587 std::cout << l2c << std::endl;
588 std::cout << l1d << std::endl;
589 std::cout << l2d << std::endl;
590 return 0;
591 }
592 counter++;
593 }
594
595 std::cout << std::endl << counter << " successful trial" << (counter == 1 ? "." : "s.") << std::endl;
596
597 return 1;
598 }
599
600 int main()
601 {
602 #if defined(__MWERKS__) && macintosh
603 SIOUXSettings.asktosaveonclose = 0;
604 SIOUXSettings.showstatusline = 1;
605 SIOUXSettings.autocloseonquit = 0;
606 #endif
607
608 std::cout << "Starting tests." << endl;
609
610 #if defined(__MWERKS__) && macintosh
611 GetNextEvent(0, NULL);
612 #endif
613
614 srand(time(NULL));
615
616 if (!multiplicationtest())
617 return 1;
618
619 if (!shifttest())
620 return 1;
621
622 if (!add1test())
623 return 1;
624
625 // if (!add2test())
626 // return 1;
627
628 if (!sub1test())
629 return 1;
630
631 // if (!sub2test())
632 // return 1;
633
634 if (!incdectest())
635 return 1;
636
637 if (!negationtest())
638 return 1;
639
640 std::cout << std::endl << "The tests are finished." << std::endl;
641
642 return 0;
643 }