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