Remove old CVS $Id$ keyword
[uffi.git] / tests / uffi-c-test.c
1 /***************************************************************************
2  * FILE IDENTIFICATION
3  *
4  *  Name:         c-test-fns.c
5  *  Purpose:      Test functions in C for UFFI library
6  *  Programer:    Kevin M. Rosenberg
7  *  Date Started: Mar 2002
8  *
9  * This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
10  *
11  * These variables are correct for GCC
12  * you'll need to modify these for other compilers
13  ***************************************************************************/
14
15 #if defined(WIN32)||defined(WIN64)
16 #include <windows.h>
17
18 BOOL WINAPI DllEntryPoint(HINSTANCE hinstdll,
19                           DWORD fdwReason,
20                           LPVOID lpvReserved)
21 {
22         return 1;
23 }
24
25 #define DLLEXPORT __declspec(dllexport)
26
27 #else
28 #define DLLEXPORT
29 #endif
30
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <math.h>
34
35
36 DLLEXPORT unsigned char uchar_13 = 13;
37 DLLEXPORT signed char schar_neg_120 = -120;
38 DLLEXPORT unsigned short uword_257 = 257;
39 DLLEXPORT signed short sword_neg_321 = -321;
40 DLLEXPORT unsigned int uint_1234567 = 1234567;
41 DLLEXPORT signed int sint_neg_123456 = -123456;
42 DLLEXPORT double double_3_1 = 3.1;
43 DLLEXPORT float float_neg_4_5 = -4.5;
44
45 /* Test of constant input string */
46 DLLEXPORT
47 int
48 cs_count_upper (char* psz)
49 {
50   int count = 0;
51
52   if (psz) {
53     while (*psz) {
54       if (isupper (*psz))
55         ++count;
56       ++psz;
57     }
58     return count;
59   } else
60     return -1;
61 }
62
63 /* Test of input and output of a string */
64 DLLEXPORT
65 void
66 cs_to_upper (char* psz)
67 {
68   if (psz) {
69     while (*psz) {
70       *psz = toupper (*psz);
71       ++psz;
72     }
73   }
74 }
75
76 /* Test of an output only string */
77 DLLEXPORT
78 void
79 cs_make_random (int size, char* buffer)
80 {
81   int i;
82   for (i = 0; i < size; i++)
83     buffer[i] = 'A' + (rand() % 26);
84 }
85
86
87 /* Test of input/output vector */
88 DLLEXPORT
89 void
90 half_double_vector (int size, double* vec)
91 {
92   int i;
93   for (i = 0; i < size; i++)
94     vec[i] /= 2.;
95 }
96
97
98
99 DLLEXPORT
100 void *
101 cast_test_int () {
102   int *x = (int *) malloc(sizeof(int));
103   *x = 23;
104   return x;
105 }
106
107 DLLEXPORT
108 void *
109 cast_test_float ()
110 {
111   double *y = (double *) malloc(sizeof(double));
112   *y = 3.21;
113   return y;
114 }
115
116 DLLEXPORT
117 long
118 return_long_negative_one ()
119 {
120   return -1;
121 }
122
123 DLLEXPORT
124 int
125 return_int_negative_one ()
126 {
127   return -1;
128 }
129
130 DLLEXPORT
131 short
132 return_short_negative_one ()
133 {
134   return -1;
135 }
136
137 DLLEXPORT int fvar_addend = 3;
138
139 typedef struct {
140   int i;
141   double d;
142 } fvar_struct_type;
143
144 fvar_struct_type fvar_struct = {42, 3.2};
145
146 DLLEXPORT
147 int fvar_struct_int () {
148   return (fvar_addend + fvar_struct.i);
149 }
150
151 DLLEXPORT
152 double fvar_struct_double () {
153   return fvar_struct.d;
154 }
155
156