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