Starting release 0.7.0
[parrot.git] / src / nci_test.c
blob38f00e720f9ef9ae3dad19bdaf10bc31e561f939
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/nci_test.c - shared library used for testing the Native Call Interface
9 =head1 DESCRIPTION
11 From this code a shared library can be compiled and linked with a command like:
13 cc -shared -fpic nci_test.c -o libnci_test.so -g
15 For non-Unix platforms the above command has to be modified appropriately.
17 The resulting shared library should be copied to a location like:
19 parrot/runtime/parrot/dynext/libnci_test.so
21 At that location the shared library is loadable with the opcode 'loadlib'.
22 The functions in the library are available with the opcode 'dlfunc'.
23 The variables in the library are available with the opcode 'dlvar'.
25 =head2 Functions
27 The name of a test function is usually 'nci_<signature>'. E.g. the function
28 'nci_ip' takes a 'pointer' and returns a 'int'.
30 =over 4
32 =cut
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <parrot/config.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
45 /* Declarations of structs */
47 typedef struct Nested {
48 int y;
49 } Nested;
51 typedef struct Outer {
52 int x;
53 Nested *nested;
54 } Outer;
56 typedef struct Rect_Like {
57 int x, y;
58 int w, h;
59 } Rect_Like;
61 typedef struct Opaque {
62 int x;
63 } Opaque;
65 /* Function declarations.
67 *** If you add a new test function here,
68 *** please update src/libnci_test.def and src/call_list.txt too. ***
72 PARROT_API int call_back(const char *str);
73 PARROT_API char nci_c(void);
74 PARROT_API char nci_csc(short, char);
75 PARROT_API double nci_d(void);
76 PARROT_API double nci_dd(double);
77 PARROT_API float nci_f(void);
78 PARROT_API float nci_fff(float, float);
79 PARROT_API int nci_i(void);
80 PARROT_API int nci_ib(int *);
81 PARROT_API int nci_iiii(int, int, int);
82 PARROT_API int nci_ii3(int, int *);
83 PARROT_API int nci_ip(void *);
84 PARROT_API int nci_isc(short, char);
85 PARROT_API int nci_it(void *);
86 PARROT_API int nci_i33(int *, int *);
87 PARROT_API int nci_i4i(long *, int);
88 PARROT_API long nci_l(void);
89 PARROT_API int * nci_p(void);
90 PARROT_API void * nci_pi(int);
91 PARROT_API void * nci_pii(int, int);
92 PARROT_API void * nci_piiii(int, int, int, int);
93 PARROT_API void nci_pip(int, Rect_Like *);
94 PARROT_API void * nci_pp(void *);
95 PARROT_API short nci_s(void);
96 PARROT_API short nci_ssc(short, char);
97 PARROT_API char * nci_t(void);
98 PARROT_API char * nci_tb(void *);
99 PARROT_API char * nci_tB(void **);
100 PARROT_API char * nci_tt(void *);
101 PARROT_API void nci_v(void);
102 PARROT_API void nci_vP(void *);
103 PARROT_API void nci_vpii(Outer *, int, int);
104 PARROT_API void nci_vv(void);
105 PARROT_API void nci_vVi(Opaque**, int);
106 PARROT_API void nci_vp(Opaque*);
109 /* Declarations for callback tests */
111 typedef void (*cb_C1_func)(const char*, void*);
112 PARROT_API void nci_cb_C1(cb_C1_func, void*);
114 typedef void (*cb_C2_func)(int, void*);
115 PARROT_API void nci_cb_C2(cb_C2_func, void*);
117 typedef void (*cb_C3_func)(void*, void*);
118 PARROT_API void nci_cb_C3(cb_C3_func, void*);
120 typedef void (*cb_D1_func)(void*, const char*);
121 PARROT_API void nci_cb_D1(cb_D1_func, void*);
123 typedef void (*cb_D2_func)(void*, int);
124 PARROT_API void nci_cb_D2(cb_D2_func, void*);
126 typedef void (*cb_D3_func)(void*, void*);
127 PARROT_API void nci_cb_D3(cb_D3_func, void*);
129 typedef void (*cb_D4_func)(void*, void*);
130 PARROT_API void nci_cb_D4(cb_D4_func, void*);
132 /* Variable definitions */
134 PARROT_API int int_cb_D4 = -55555;
135 PARROT_API int nci_dlvar_char = 22;
136 PARROT_API int nci_dlvar_short = 333;
137 PARROT_API int nci_dlvar_int = -4444;
138 PARROT_API long nci_dlvar_long = -7777777;
139 PARROT_API float nci_dlvar_float = -333.0;
140 PARROT_API double nci_dlvar_double = -55555.55555;
141 PARROT_API char nci_dlvar_cstring[] = "This is a C-string.\n";
144 /* Function definitions */
148 =item C<PARROT_API char
149 nci_c(void)>
151 RT#48260: Not yet documented!!!
153 =cut
157 PARROT_API char
158 nci_c(void) {
159 return nci_dlvar_char;
164 =item C<PARROT_API char
165 nci_csc(short l1, char l2)>
167 RT#48260: Not yet documented!!!
169 =cut
173 PARROT_API char
174 nci_csc(short l1, char l2)
176 return l1 * l2;
181 =item C<PARROT_API double
182 nci_d(void)>
184 RT#48260: Not yet documented!!!
186 =cut
190 PARROT_API double
191 nci_d(void)
193 nci_dlvar_double *= 10.0;
195 return nci_dlvar_double;
200 =item C<PARROT_API double
201 nci_dd(double d)>
203 RT#48260: Not yet documented!!!
205 =cut
209 PARROT_API double
210 nci_dd(double d)
212 return d * 2.0;
217 =item C<PARROT_API float
218 nci_f(void)>
220 RT#48260: Not yet documented!!!
222 =cut
226 PARROT_API float
227 nci_f(void)
229 nci_dlvar_float *= 10.0;
231 return nci_dlvar_float;
236 =item C<PARROT_API float
237 nci_fff(float l1, float l2)>
239 RT#48260: Not yet documented!!!
241 =cut
245 PARROT_API float
246 nci_fff(float l1, float l2)
248 return l1 / l2;
253 =item C<PARROT_API int
254 nci_i(void)>
256 RT#48260: Not yet documented!!!
258 =cut
262 PARROT_API int
263 nci_i(void)
265 return nci_dlvar_int;
270 =item C<PARROT_API int
271 nci_isc(short l1, char l2)>
273 RT#48260: Not yet documented!!!
275 =cut
279 PARROT_API int
280 nci_isc(short l1, char l2)
282 return l1 * l2;
287 =item C<PARROT_API int
288 nci_ip(void *p)>
290 RT#48260: Not yet documented!!!
292 =cut
296 PARROT_API int
297 nci_ip(void *p)
299 typedef struct _dfi {
300 double d;
301 float f;
302 int i;
303 char *s;
304 } dfi;
305 dfi *sp = (dfi*) p;
306 puts(sp->s);
307 fflush(stdout);
309 return (int) (sp->d + sp->f + sp->i);
314 =item C<PARROT_API int
315 nci_it(void *p)>
317 test calls this with a string
319 =cut
323 PARROT_API int
324 nci_it(void *p)
326 fprintf(stderr, "%c%c\n", ((char*) p)[1], ((char *) p)[0]);
327 fflush(stderr);
329 return 2;
334 =item C<PARROT_API long
335 nci_l(void)>
337 RT#48260: Not yet documented!!!
339 =cut
343 PARROT_API long
344 nci_l(void)
346 return nci_dlvar_long;
351 =item C<PARROT_API int *
352 nci_p(void)>
354 RT#48260: Not yet documented!!!
356 =cut
360 PARROT_API int *
361 nci_p(void)
363 return &nci_dlvar_int;
368 =item C<PARROT_API char *
369 nci_t(void)>
371 RT#48260: Not yet documented!!!
373 =cut
377 PARROT_API char *
378 nci_t(void)
380 return nci_dlvar_cstring;
385 =item C<PARROT_API char *
386 nci_tb(void *p)>
388 RT#48260: Not yet documented!!!
390 =cut
394 static char b[] = "xx worked\n";
396 PARROT_API char *
397 nci_tb(void *p)
399 b[0] = ((char*) p)[1];
400 b[1] = ((char*) p)[0];
402 return b;
407 =item C<PARROT_API char *
408 nci_tt(void *p)>
410 RT#48260: Not yet documented!!!
412 =cut
416 static char s[] = "xx worked\n";
418 PARROT_API char *
419 nci_tt(void *p)
421 s[0] = ((char*) p)[1];
422 s[1] = ((char*) p)[0];
424 return s;
429 =item C<PARROT_API char *
430 nci_tB(void **p)>
432 RT#48260: Not yet documented!!!
434 =cut
438 static char B[] = "xx done\n";
440 PARROT_API char *
441 nci_tB(void **p)
443 B[0] = (*(char**) p)[1];
444 B[1] = (*(char**) p)[0];
446 return B;
451 =item C<PARROT_API void *
452 nci_pp(void *p)>
454 RT#48260: Not yet documented!!!
456 =cut
460 PARROT_API void *
461 nci_pp(void *p)
463 return p;
468 =item C<PARROT_API int
469 nci_iiii(int i1, int i2, int i3)>
471 RT#48260: Not yet documented!!!
473 =cut
477 PARROT_API int
478 nci_iiii(int i1, int i2, int i3)
480 fprintf(stderr, "%d %d %d\n", i1, i2, i3);
481 fflush(stderr);
483 return 2;
488 =item C<PARROT_API int
489 nci_i4i(long * l, int i)>
491 RT#48260: Not yet documented!!!
493 =cut
497 PARROT_API int
498 nci_i4i(long * l, int i)
501 return (int) (*l * i);
506 =item C<PARROT_API int
507 nci_ii3(int a, int *bp)>
509 RT#48260: Not yet documented!!!
511 =cut
515 PARROT_API int
516 nci_ii3(int a, int *bp)
518 int r = a * *bp;
519 *bp = 4711;
521 return r;
526 =item C<PARROT_API int
527 call_back(const char *str)>
529 RT#48260: Not yet documented!!!
531 =cut
535 PARROT_API int
536 call_back(const char *str)
538 puts(str);
539 fflush(stdout);
541 return 4711;
546 =item C<PARROT_API void *
547 nci_pi(int test)>
549 RT#48260: Not yet documented!!!
551 =cut
555 PARROT_API void *
556 nci_pi(int test)
558 switch (test) {
559 case 0:
561 static struct {
562 int i[2];
563 char c;
564 } t = {
565 {42, 100},
568 return &t;
570 case 1:
572 static struct {
573 float f[2];
574 double d;
575 } t = {
576 {42.0, 100.0},
577 47.11
579 return &t;
581 case 2:
583 static struct {
584 char c;
585 int i;
586 } t = {
590 return &t;
592 case 3:
594 static struct {
595 const char *c;
596 int i;
597 } t = {
598 "hello",
601 return &t;
603 case 4:
605 static struct _x {
606 int i;
607 int j;
608 double d;
609 } xx = { 100, 77, 200.0 };
610 static struct {
611 char c;
612 struct _x *x;
613 } t = {
617 return &t;
619 case 5:
621 static struct {
622 int (*f)(const char *);
623 } t = {
624 call_back
626 return &t;
628 case 6:
630 static struct xt {
631 int x;
632 struct yt {
633 int i;
634 int j;
635 } _y;
636 int z;
637 } _x = {
639 { 127, 12345 },
642 return &_x;
644 case 7:
646 static struct xt {
647 char x;
648 struct yt {
649 char i;
650 int j;
651 } _y;
652 char z;
653 } _x = {
655 { 127, 12345 },
658 return &_x;
660 case 8:
662 static struct _z {
663 int i;
664 int j;
665 } zz = { 100, 77 };
666 static struct xt {
667 int x;
668 struct yt {
669 int i;
670 int j;
671 struct _z *z;
672 } _y;
673 } _x = {
675 { 127, 12345, &zz },
677 return &_x;
679 case 9:
681 static int i = 55555;
682 return &i;
684 default:
685 fprintf(stderr, "unknown test number\n");
688 return NULL;
693 =item C<PARROT_API short
694 nci_s(void)>
696 RT#48260: Not yet documented!!!
698 =cut
702 PARROT_API short
703 nci_s(void)
705 return nci_dlvar_short;
710 =item C<PARROT_API short
711 nci_ssc(short l1, char l2)>
713 RT#48260: Not yet documented!!!
715 =cut
719 PARROT_API short
720 nci_ssc(short l1, char l2)
722 return l1 * l2;
727 =item C<PARROT_API void
728 nci_vP(void *pmc)>
730 RT#48260: Not yet documented!!!
732 =cut
736 PARROT_API void
737 nci_vP(void *pmc)
739 if (pmc)
740 puts("ok");
741 else
742 puts("got null");
748 =back
750 =head2 Functions used for pdd16 tests
752 =over 4
754 =cut
760 =item C<PARROT_API void
761 nci_cb_C1(cb_C1_func cb, void* user_data)>
763 RT#48260: Not yet documented!!!
765 =cut
769 PARROT_API void
770 nci_cb_C1(cb_C1_func cb, void* user_data)
772 const char *result = "succeeded";
773 /* call the cb synchronously */
774 (cb)(result, user_data);
776 return;
781 =item C<PARROT_API void
782 nci_cb_C2(cb_C2_func cb, void* user_data)>
784 RT#48260: Not yet documented!!!
786 =cut
790 PARROT_API void
791 nci_cb_C2(cb_C2_func cb, void* user_data)
793 /* call the cb synchronously */
794 (cb)(77, user_data);
796 return;
801 =item C<PARROT_API void
802 nci_cb_C3(cb_C3_func cb, void* user_data)>
804 RT#48260: Not yet documented!!!
806 =cut
810 static int int_cb_C3 = 99;
812 PARROT_API void
813 nci_cb_C3(cb_C3_func cb, void* user_data)
815 /* call the cb synchronously */
816 (cb)(&int_cb_C3, user_data);
818 return;
823 =item C<PARROT_API void
824 nci_cb_D1(cb_D1_func cb, void* user_data)>
826 RT#48260: Not yet documented!!!
828 =cut
832 PARROT_API void
833 nci_cb_D1(cb_D1_func cb, void* user_data)
835 const char *result = "succeeded";
836 /* call the cb synchronously */
837 (cb)(user_data, result);
839 return;
844 =item C<PARROT_API void
845 nci_cb_D2(cb_D2_func cb, void* user_data)>
847 RT#48260: Not yet documented!!!
849 =cut
853 PARROT_API void
854 nci_cb_D2(cb_D2_func cb, void* user_data)
856 /* call the cb synchronously */
857 (cb)(user_data, 88);
859 return;
864 =item C<PARROT_API void
865 nci_cb_D3(cb_D3_func cb, void* user_data)>
867 RT#48260: Not yet documented!!!
869 =cut
873 static int int_cb_D3 = 111;
875 PARROT_API void
876 nci_cb_D3(cb_D3_func cb, void* user_data)
878 /* call the cb synchronously */
879 (cb)(user_data, &int_cb_D3);
881 return;
886 =item C<PARROT_API void
887 nci_cb_D4(cb_D4_func times_ten, void* user_data)>
889 RT#48260: Not yet documented!!!
891 =cut
895 PARROT_API void
896 nci_cb_D4(cb_D4_func times_ten, void* user_data)
898 int cnt;
899 for (cnt = 0; cnt < 9; cnt++)
901 (times_ten)(user_data, &int_cb_D4);
902 int_cb_D4++;
905 return;
910 =item C<PARROT_API void
911 nci_pip(int count, Rect_Like *rects)>
913 RT#48260: Not yet documented!!!
915 =cut
919 PARROT_API void
920 nci_pip(int count, Rect_Like *rects)
922 int i;
923 printf("Count: %d\n", count);
924 for (i = 0; i < 4; ++i)
925 printf("X: %d\nY: %d\nW: %d\nH: %d\n",
926 rects[i].x, rects[i].y, rects[i].w, rects[i].h);
931 =item C<PARROT_API int
932 nci_i33(int *double_me, int *triple_me)>
934 RT#48260: Not yet documented!!!
936 =cut
940 PARROT_API int
941 nci_i33(int *double_me, int *triple_me)
943 *double_me *= 2;
944 *triple_me *= 3;
946 return (*double_me + *triple_me);
951 =item C<PARROT_API void
952 nci_vpii(Outer *my_data, int my_x, int my_y)>
954 RT#48260: Not yet documented!!!
956 =cut
960 PARROT_API void
961 nci_vpii(Outer *my_data, int my_x, int my_y)
963 my_data->x = my_x;
964 my_data->nested->y = my_y;
969 =item C<PARROT_API void *
970 nci_piiii(int alpha, int beta, int gamma, int delta)>
972 RT#48260: Not yet documented!!!
974 =cut
978 static int my_array[4];
980 PARROT_API void *
981 nci_piiii(int alpha, int beta, int gamma, int delta)
983 static struct array_container
985 int x;
986 int *array;
987 } container;
989 my_array[0] = alpha;
990 my_array[1] = beta;
991 my_array[2] = gamma;
992 my_array[3] = delta;
994 container.x = 4;
995 container.array = my_array;
997 return &container;
1002 =item C<PARROT_API void *
1003 nci_pii(int fac1, int fac2)>
1005 RT#48260: Not yet documented!!!
1007 =cut
1011 PARROT_API void *
1012 nci_pii(int fac1, int fac2)
1014 nci_dlvar_int = fac1 * fac2;
1016 return &nci_dlvar_int;
1021 =item C<PARROT_API void
1022 nci_v(void)>
1024 RT#48260: Not yet documented!!!
1026 =cut
1030 PARROT_API void
1031 nci_v(void)
1033 nci_dlvar_int *= 10;
1038 =item C<PARROT_API void
1039 nci_vv(void)>
1041 RT#48260: Not yet documented!!!
1043 =cut
1047 PARROT_API void
1048 nci_vv(void)
1050 nci_dlvar_int *= 3;
1055 =item C<PARROT_API void
1056 nci_vVi(Opaque**, int)>
1058 Test an NCI opaque struct out value.
1060 =cut
1064 PARROT_API void
1065 nci_vVi(Opaque **outOpaque, int x)
1067 static Opaque opaque;
1068 opaque.x = x;
1069 *outOpaque = &opaque;
1074 =item C<PARROT_API int
1075 nci_vp(Opaque*)>
1077 Test that a previously generated opaque struct gets passed back
1078 to an NCI function correctly.
1080 =cut
1084 PARROT_API void
1085 nci_vp(Opaque *inOpaque)
1087 if (inOpaque)
1088 printf("got %d\n", inOpaque->x);
1089 else
1090 printf("got null");
1094 #ifdef TEST
1096 char l2 = 4;
1097 float f2 = 4.0;
1101 =item C<int
1102 main(void)>
1104 RT#48260: Not yet documented!!!
1106 =cut
1111 main(void)
1113 short l1 = 3;
1114 float f, f1 = 3.0;
1115 int l = nci_ssc(l1, l2);
1116 printf("%d\n", l);
1117 f = nci_fff(f1, f2);
1118 printf("%f\n", f);
1120 return 0;
1123 #endif
1125 #ifdef __cplusplus
1127 #endif
1131 =back
1133 =head1 SEE ALSO:
1135 F<docs/pdds/pdd16_native_call.pod>
1136 F<config/gen/makefiles/root.in>
1137 F<t/pmc/nci.t>
1139 =cut
1145 * Local variables:
1146 * c-file-style: "parrot"
1147 * End:
1148 * vim: expandtab shiftwidth=4: