1 /*
2  Copyright (c) 2010, 2021, Oracle and/or its affiliates.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation.  The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program 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 General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 /*
25  * myapi_test.cpp
26  */
27 
28 #include <my_config.h>
29 #include <assert.h> // not using namespaces yet
30 #include <stdio.h> // not using namespaces yet
31 #include <stdlib.h> // not using namespaces yet
32 
33 #include "myapi.hpp"
34 #include "helpers.hpp"
35 
36 void
test0()37 test0()
38 {
39     printf("\ntesting basic function: f0() ...\n");
40 
41     f0();
42 }
43 
44 // primitive types, by value in, out
45 void
test1()46 test1()
47 {
48     printf("\ntesting primitive type functions: fxx(0) ...\n");
49 
50 
51     f31(0);
52     f32(0);
53     f33(0);
54     f34(0);
55     f35(0);
56     f36(0);
57     f37(0);
58     f38(0);
59     f39(0);
60     f40(0);
61     f41(0);
62     f42(0);
63     f43(0);
64     f44(0);
65     f45(0);
66 }
67 
68 void
test2()69 test2()
70 {
71     printf("\ntesting basic functions: f1xx(f0xx()) ...\n");
72 
73     for (int i = 0; i < 2; i++) {
74 
75         f131(f031());
76         f132(f032());
77         f133(f033());
78         f134(f034());
79         f135(f035());
80         f136(f036());
81         f137(f037());
82         f138(f038());
83         f141(f041());
84         f142(f042());
85         f143(f043());
86         f144(f044());
87     }
88 }
89 
90 void
test3()91 test3()
92 {
93     printf("\ntesting basic functions: f3xx(f2xx()) ...\n");
94 
95     for (int i = 0; i < 2; i++) {
96         f311(f211());
97         f312(f212());
98         f313(f213());
99         f314(f214());
100         f315(f215());
101         f316(f216());
102         f317(f217());
103         f318(f218());
104         f321(f221());
105         f322(f222());
106         f323(f223());
107         f324(f224());
108 
109         f331(f231());
110         f332(f232());
111         f333(f233());
112         f334(f234());
113         f335(f235());
114         f336(f236());
115         f337(f237());
116         f338(f238());
117         f341(f241());
118         f342(f242());
119         f343(f243());
120         f344(f244());
121     }
122 }
123 
124 void
test4()125 test4()
126 {
127     printf("\ntesting basic functions: f5xx(f4xx()) ...\n");
128 
129     for (int i = 0; i < 2; i++) {
130         f511(f411());
131         f512(f412());
132         f513(f413());
133         f514(f414());
134         f515(f415());
135         f516(f416());
136         f517(f417());
137         f518(f418());
138         f521(f421());
139         f522(f422());
140         f523(f423());
141         f524(f424());
142 
143         f531(f431());
144         f532(f432());
145         f533(f433());
146         f534(f434());
147         f535(f435());
148         f536(f436());
149         f537(f437());
150         f538(f438());
151         f541(f441());
152         f542(f442());
153         f543(f443());
154         f544(f444());
155 
156 
157     }
158 }
159 
160 void
test5()161 test5()
162 {
163     printf("\ntesting basic functions: f7xx(f6xx()) ...\n");
164 
165     for (int i = 0; i < 2; i++) {
166         f711(f611());
167         f712(f612());
168         f713(f613());
169         f714(f614());
170         f715(f615());
171         f716(f616());
172         f717(f617());
173         f718(f618());
174         f721(f621());
175         f722(f622());
176         f723(f623());
177         f724(f624());
178 
179         f731(f631());
180         f732(f632());
181         f733(f633());
182         f734(f634());
183         f735(f635());
184         f736(f636());
185         f737(f637());
186         f738(f638());
187         f741(f641());
188         f742(f642());
189         f743(f643());
190         f744(f644());
191 
192 
193     }
194 }
195 
196 void
test6()197 test6()
198 {
199     printf("\ntesting instance wrappers: ...\n");
200     int n;
201 
202     printf("\ncalling A...\n");
203     A * a = new A();
204     printf("... new A() = %p\n", &a);
205     delete new A(5);
206     printf("... delete new A(int)\n");
207     n = A::f0s();
208     printf("... A::f0s() = %d\n", n);
209     assert(n == 10);
210     n = a->f0s();
211     printf("... a->f0s() = %d\n", n);
212     assert(n == 10);
213     n = a->f0n();
214     printf("... a->f0n() = %d\n", n);
215     assert(n == 11);
216     n = a->f0v();
217     printf("... a->f0v() = %d\n", n);
218     assert(n == 12);
219 
220     printf("\nA::take_ptr(A::deliver_ptr())...\n");
221     A::take_ptr(A::deliver_ptr());
222 
223     printf("\nA::take_null_ptr(A::deliver_null_ptr())...\n");
224     A::take_null_ptr(A::deliver_null_ptr());
225 
226     printf("\nA::take_ref(A::deliver_ref())...\n");
227     A::take_ref(A::deliver_ref());
228 
229     printf("\nA::take_null_ref(A::deliver_null_ref())...\n");
230     A::take_null_ref(A::deliver_null_ref());
231 
232     printf("\nA::print(A *)...\n");
233     A::print(a);
234 
235 
236     printf("\naccessing A...\n");
237     n = ++A::d0s;
238     printf("... ++A::d0s = %d\n", n);
239     assert(n == 11);
240     n = A::d0sc;
241     printf("... A::d0sc = %d\n", n);
242     assert(n == -10);
243     n = ++a->d0s;
244     printf("... ++a->d0s = %d\n", n);
245     assert(n == 12);
246     n = a->d0sc;
247     printf("... a->d0sc = %d\n", n);
248     assert(n == -10);
249     n = ++a->d0;
250     printf("... ++a->d0 = %d\n", n);
251     assert(n == 12);
252     n = a->d0c;
253     printf("... a->d0c = %d\n", n);
254     assert(n == -11);
255 
256     printf("\ncalling B0...\n");
257     B0 & b0b0 = *a->newB0();
258     printf("... a->newB0() = %p\n", &b0b0);
259     n = B0::f0s();
260     printf("... B0::f0s() = %d\n", n);
261     assert(n == 20);
262     n = b0b0.f0s();
263     printf("... b0b0.f0s() = %d\n", n);
264     assert(n == 20);
265     n = b0b0.f0n();
266     printf("... b0b0.f0n() = %d\n", n);
267     assert(n == 21);
268     n = b0b0.f0v();
269     printf("... b0b0.f0v() = %d\n", n);
270     assert(n == 22);
271     a->del(b0b0);
272     printf("... a->del(b0b0)\n");
273 
274     printf("\naccessing B0...\n");
275     B0 & b0 = *a->newB0();
276     printf("... a->newB0() = %p\n", &b0);
277     n = ++B0::d0s;
278     printf("... ++B0::d0s = %d\n", n);
279     assert(n == 21);
280     n = B0::d0sc;
281     printf("... B0::d0sc = %d\n", n);
282     assert(n == -20);
283     n = ++b0.d0s;
284     printf("... ++b0.d0s = %d\n", n);
285     assert(n == 22);
286     n = b0.d0sc;
287     printf("... b0.d0sc = %d\n", n);
288     assert(n == -20);
289     n = ++b0.d0;
290     printf("... ++b0.d0 = %d\n", n);
291     assert(n == 22);
292     n = b0.d0c;
293     printf("... b0.d0c = %d\n", n);
294     assert(n == -21);
295     a->del(b0);
296     printf("... a->del(b0)\n");
297 
298     printf("\ncalling B1...\n");
299     B1 & b1b1 = *a->newB1();
300     B0 & b0b1 = b1b1;
301     printf("... a->newB1() = %p\n", &b0b1);
302     n = B1::f0s();
303     printf("... B1::f0s() = %d\n", n);
304     assert(n == 30);
305     n = b0b1.f0s();
306     printf("... b0b1.f0s() = %d\n", n);
307     assert(n == 20);
308     n = b0b1.f0n();
309     printf("... b0b1.f0n() = %d\n", n);
310     assert(n == 21);
311     n = b0b1.f0v();
312     printf("... b0b1.f0v() = %d\n", n);
313     assert(n == 32);
314     a->del(b1b1);
315     printf("... a->del(b1b1)\n");
316 
317     printf("\naccessing B1...\n");
318     B1 & b1 = *a->newB1();
319     printf("... a->newB1() = %p\n", &b1);
320     n = ++B1::d0s;
321     printf("... ++B1::d0s = %d\n", n);
322     assert(n == 31);
323     n = B1::d0sc;
324     printf("... B1::d0sc = %d\n", n);
325     assert(n == -30);
326     n = ++b1.d0s;
327     printf("... ++b1.d0s = %d\n", n);
328     assert(n == 32);
329     n = b1.d0sc;
330     printf("... b1.d0sc = %d\n", n);
331     assert(n == -30);
332     n = ++b1.d0;
333     printf("... ++b1.d0 = %d\n", n);
334     assert(n == 32);
335     n = b1.d0c;
336     printf("... b1.d0c = %d\n", n);
337     assert(n == -31);
338     a->del(b1);
339     printf("... a->del(b1)\n");
340 
341     printf("\ndelete A...\n");
342     delete a;
343 };
344 
345 void
test7()346 test7()
347 {
348     printf("\ntesting string/byte array functions: sxxx(sxxx) ...\n");
349 
350     s110(s010());
351     s110(s012());
352     s112(s012());
353 
354     s110(s030());
355     s110(s032());
356     s112(s032());
357 
358     s130(s030());
359     s130(s032());
360     s132(s032());
361 
362 
363 
364 
365     s310(s210());
366     s310(s212());
367     s312(s212());
368 
369     s310(s230());
370     s310(s232());
371     s312(s232());
372 
373     s330(s230());
374     s330(s232());
375     s332(s232());
376 
377 
378 
379 }
380 
381 void
test8()382 test8()
383 {
384     printf("\ntesting n-ary array functions: g(), h() ...\n");
385     int32_t n;
386 
387     printf("\ncreating A...\n");
388     A * a = new A();
389     const A * ac = a;
390 
391     h0();
392 
393     h1(1);
394 
395     h2(1, 2);
396 
397     h3(1, 2, 3);
398 
399     n = h0r();
400     assert(n == 0);
401 
402     n = h1r(1);
403     assert(n == 1);
404 
405     n = h2r(1, 2);
406     assert(n == 3);
407 
408     n = h3r(1, 2, 3);
409     assert(n == 6);
410 
411     ac->g0c();
412 
413     ac->g1c(1);
414 
415     ac->g2c(1, 2);
416 
417     ac->g3c(1, 2, 3);
418 
419     a->g0();
420 
421     a->g1(1);
422 
423     a->g2(1, 2);
424 
425     a->g3(1, 2, 3);
426 
427     n = ac->g0rc();
428     assert(n == 0);
429 
430     n = ac->g1rc(1);
431     assert(n == 1);
432 
433     n = ac->g2rc(1, 2);
434     assert(n == 3);
435 
436     n = ac->g3rc(1, 2, 3);
437     assert(n == 6);
438 
439     n = a->g0r();
440     assert(n == 0);
441 
442     n = a->g1r(1);
443     assert(n == 1);
444 
445     n = a->g2r(1, 2);
446     assert(n == 3);
447 
448     n = a->g3r(1, 2, 3);
449     assert(n == 6);
450 
451     printf("delete A...\n");
452     delete a;
453 }
454 
455 void
test9()456 test9()
457 {
458     printf("\ntesting const & inheritance: ...\n");
459 
460     C0 & c0 = *C0::c;
461     const C0 & c0c = *C0::cc;
462     C1 & c1 = *C1::c;
463     const C1 & c1c = *C1::cc;
464 
465     // for debugging
466     if (false) {
467         printf("\nc0.print()... c0c.print()...\n");
468         c0.print();
469         c1.print();
470 
471         printf("\nc1.print()... c1c.print()...\n");
472         c0c.print();
473         c1c.print();
474     }
475 
476     printf("\nc0.check(c0.id);\n");
477     c0.check(c0.id);
478 
479     printf("\nc0c.check(c0c.id);\n");
480     c0c.check(c0c.id);
481 
482     printf("\nc1.check(c1.id);\n");
483     c1.check(c1.id);
484 
485     printf("\nc1c.check(c1c.id);\n");
486     c1c.check(c1c.id);
487 
488     // C0 -> C0
489     printf("\nc0c.take_C0Cp(c0c.deliver_C0Cp())...\n");
490     c0c.take_C0Cp(c0c.deliver_C0Cp());
491 
492     printf("\nc0c.take_C0Cr(c0c.deliver_C0Cr())...\n");
493     c0c.take_C0Cr(c0c.deliver_C0Cr());
494 
495     printf("\nc0c.take_C0Cp(c0.deliver_C0p())...\n");
496     c0c.take_C0Cp(c0.deliver_C0p());
497 
498     printf("\nc0c.take_C0Cr(c0.deliver_C0r())...\n");
499     c0c.take_C0Cr(c0.deliver_C0r());
500 
501     printf("\nc0.take_C0p(c0.deliver_C0p())...\n");
502     c0.take_C0p(c0.deliver_C0p());
503 
504     printf("\nc0.take_C0r(c0.deliver_C0r())...\n");
505     c0.take_C0r(c0.deliver_C0r());
506 
507     // C1 -> C0
508     printf("\nc0c.take_C0Cp(c1c.deliver_C1Cp())...\n");
509     c0c.take_C0Cp(c1c.deliver_C1Cp());
510 
511     printf("\nc0c.take_C0Cr(c1c.deliver_C1Cr())...\n");
512     c0c.take_C0Cr(c1c.deliver_C1Cr());
513 
514     printf("\nc0c.take_C0Cp(c1.deliver_C1p())...\n");
515     c0c.take_C0Cp(c1.deliver_C1p());
516 
517     printf("\nc0c.take_C0Cr(c1.deliver_C1r())...\n");
518     c0c.take_C0Cr(c1.deliver_C1r());
519 
520     printf("\nc0.take_C0p(c1.deliver_C1p())...\n");
521     c0.take_C0p(c1.deliver_C1p());
522 
523     printf("\nc0.take_C0r(c1.deliver_C1r())...\n");
524     c0.take_C0r(c1.deliver_C1r());
525 
526     // C0 -> C1
527     printf("\nc1c.take_C1Cp(c1c.deliver_C1Cp())...\n");
528     c1c.take_C1Cp(c1c.deliver_C1Cp());
529 
530     printf("\nc1c.take_C1Cr(c1c.deliver_C1Cr())...\n");
531     c1c.take_C1Cr(c1c.deliver_C1Cr());
532 
533     printf("\nc1c.take_C1Cp(c1.deliver_C1p())...\n");
534     c1c.take_C1Cp(c1.deliver_C1p());
535 
536     printf("\nc1c.take_C1Cr(c1.deliver_C1r())...\n");
537     c1c.take_C1Cr(c1.deliver_C1r());
538 
539     printf("\nc1.take_C1p(c1.deliver_C1p())...\n");
540     c1.take_C1p(c1.deliver_C1p());
541 
542     printf("\nc1.take_C1r(c1.deliver_C1r())...\n");
543     c1.take_C1r(c1.deliver_C1r());
544 }
545 
546 void
test10()547 test10()
548 {
549     printf("\ntesting object array functions ...\n\n");
550 
551     printf("\ndelete[] (new C0[0])\n");
552     C0 * c0a0 = new C0[0];
553 #if __GNUC__ == 4 && __GNUC_MINOR__ == 7 && __GNUC_PATCHLEVEL__ >= 0
554     // workaround for GCC 4.7.x bug
555     // no initialization of var for const zero-length obj array expr;
556     // compile warning: "'c0a0' is used uninitialized in this function"
557     // see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53330
558     c0a0 = new C0[atoi("0")];
559 #endif
560 
561     // check that zero-length array new worked
562     assert(c0a0);
563     delete[] c0a0;
564 
565     const int n = 3;
566     C0 * c0a = new C0[n];
567     const C0 * c0ca = c0a;
568     C1 * c1a = new C1[n];
569     const C1 * c1ca = c1a;
570 
571     for (int i = 0; i < 0; i++) {
572         printf("\nc0a[i].print()\n");
573         c0a[i].print();
574 
575         printf("\nc0ca[i].print()\n");
576         c0ca[i].print();
577 
578         printf("\nc1a[i].print()\n");
579         c1a[i].print();
580 
581         printf("\nc1ca[i].print()\n");
582         c1ca[i].print();
583     }
584 
585     for (int i = 0; i < n; i++) {
586         printf("\nc0a[i].check(c0a[i].id)\n");
587         c0a[i].check(c0a[i].id);
588 
589         printf("\nc0ca[i].check(c0ca[i].id)\n");
590         c0ca[i].check(c0ca[i].id);
591 
592         printf("\nc1a[i].check(c1a[i].id)\n");
593         c1a[i].check(c1a[i].id);
594 
595         printf("\nc1ca[i].check(c1ca[i].id)\n");
596         c1ca[i].check(c1ca[i].id);
597     }
598 
599     printf("\nC0::hash(c0a, n) == C0::hash(C0::pass(c0a), n)...\n");
600     assert(C0::hash(c0a, n) == C0::hash(C0::pass(c0a), n));
601 
602     printf("\nC0::hash(c0ca, n) == C0::hash(C0::pass(c0ca), n)...\n");
603     assert(C0::hash(c0ca, n) == C0::hash(C0::pass(c0ca), n));
604 
605     printf("\nC1::hash(c1a, n) == C1::hash(C1::pass(c1a), n)...\n");
606     assert(C1::hash(c1a, n) == C1::hash(C1::pass(c1a), n));
607 
608     printf("\nC1::hash(c1ca, n) == C1::hash(C1::pass(c1ca), n)...\n");
609     assert(C1::hash(c1ca, n) == C1::hash(C1::pass(c1ca), n));
610 
611     delete[] c1a;
612     delete[] c0a;
613 }
614 
615 void
test11()616 test11()
617 {
618     printf("\ntesting function dispatch ...\n\n");
619 
620     assert(D0::sub()->f_d0() == 20);
621     assert(D0::sub()->f_nv() == 31);
622     assert(D0::sub()->f_v() == 32);
623 
624     assert(D1::sub()->f_d0() == 20);
625     assert(D1::sub()->f_d1() == 30);
626     assert(D1::sub()->f_nv() == 31);
627     assert(D1::sub()->f_v() == 42);
628 
629     assert(D2::sub() == NULL);
630 }
631 
632 template< typename E, void (F)(E) >
call(E e)633 inline void call(E e)
634 {
635     (F)(e);
636 }
637 
638 template< typename E, E (F)() >
call()639 inline E call()
640 {
641     return (F)();
642 }
643 
644 void
test12()645 test12()
646 {
647     printf("\ntesting enums: ...\n");
648 
649     // EE enums
650     printf("\nE::take_EE1(E::deliver_EE1())...\n");
651     E::take_EE1(E::deliver_EE1());
652 
653     printf("\ncall< E::EE, E::deliver_EE1 >()...\n");
654     E::EE e = call< E::EE, E::deliver_EE1 >();
655     assert(e == E::EE1);
656 
657     printf("\ncall< E::EE, E::take_EE1 >(e)...\n");
658     call< E::EE, E::take_EE1 >(e);
659 
660     printf("\nE::take_EE1c(E::deliver_EE1())...\n");
661     E::take_EE1c(E::deliver_EE1());
662 
663     printf("\ncall< E::EE, E::deliver_EE1 >()...\n");
664     const E::EE ec = call< E::EE, E::deliver_EE1 >();
665     assert(ec == E::EE1);
666 
667     printf("\ncall< E::EE, E::take_EE1c >(e)...\n");
668     call< const E::EE, E::take_EE1c >(ec);
669 }
670 
671 int
main(int argc,const char * argv[])672 main(int argc, const char* argv[])
673 {
674     printf("\n--> main()\n");
675     (void)argc; (void)argv;
676 
677     myapi_init();
678 
679     if (true) {
680         test0();
681         test1();
682         test2();
683         test3();
684         test4();
685         test5();
686         test6();
687         test7();
688         test8();
689         test9();
690         test10();
691         test11();
692         test12();
693     } else {
694         test12();
695     }
696 
697     myapi_finit();
698 
699     printf("\n<-- main()\n");
700     return 0;
701 }
702