Line data Source code
1 : #include "common.h"
2 :
3 : #ifdef CS_TDS_74
4 : # define FREETDS 1
5 : #else
6 : # define FREETDS 0
7 : #endif
8 :
9 : static CS_CONTEXT *ctx;
10 : static int allSuccess = 1;
11 :
12 : typedef const char *STR;
13 :
14 : static CS_INT dest_format = CS_FMT_UNUSED;
15 :
16 : static int
17 464 : DoTest(
18 : /* source information */
19 : CS_INT fromtype, void *fromdata, CS_INT fromlen,
20 : /* to information */
21 : CS_INT totype, CS_INT tomaxlen,
22 : /* expected result */
23 : CS_RETCODE tores, void *todata, CS_INT tolen,
24 : /* fields in string format */
25 : STR sdecl,
26 : STR sfromtype, STR sfromdata, STR sfromlen, STR stotype, STR stomaxlen, STR stores, STR stodata, STR stolen,
27 : /* source line number for error reporting */
28 : int line)
29 : {
30 : CS_DATAFMT destfmt, srcfmt;
31 : CS_INT reslen;
32 : CS_RETCODE retcode;
33 : int i;
34 : char buffer[1024];
35 464 : const char *err = "";
36 :
37 464 : assert(tolen >= 0);
38 :
39 464 : memset(&destfmt, 0, sizeof(destfmt));
40 464 : destfmt.datatype = totype;
41 464 : destfmt.maxlength = tomaxlen;
42 464 : destfmt.format = dest_format;
43 :
44 464 : memset(&srcfmt, 0, sizeof(srcfmt));
45 464 : srcfmt.datatype = fromtype;
46 464 : srcfmt.maxlength = fromlen;
47 :
48 : /*
49 : * FIXME this fix some thing but if error cs_convert should return
50 : * CS_UNUSED; note that this is defined 5.. a valid result ...
51 : */
52 464 : reslen = 0;
53 :
54 : /*
55 : * TODO: add special case for CS_CHAR_TYPE and give different
56 : * flags and len
57 : */
58 :
59 : /* do convert */
60 464 : memset(buffer, 23, sizeof(buffer));
61 464 : retcode = cs_convert(ctx, &srcfmt, fromdata, &destfmt, buffer, &reslen);
62 :
63 : /* test result of convert */
64 464 : if (tores != retcode) {
65 : err = "result";
66 : goto Failed;
67 : }
68 :
69 : /* test buffer */
70 464 : if (todata && (reslen != tolen || memcmp(todata, buffer, tolen) != 0)) {
71 : int n;
72 0 : printf("exp");
73 0 : for (n = 0; n < tolen; ++n)
74 0 : printf(" %02x", ((unsigned char*)todata)[n]);
75 0 : printf("\ngot");
76 0 : for (n = 0; n < reslen; ++n)
77 0 : printf(" %02x", ((unsigned char*)buffer)[n]);
78 0 : printf("\n");
79 :
80 0 : err = "result data";
81 0 : goto Failed;
82 : }
83 :
84 : /* test result len */
85 464 : if (reslen != tolen) {
86 : int n;
87 0 : printf("got");
88 0 : for (n = 0; n < reslen; ++n)
89 0 : printf(" %02x", ((unsigned char*)buffer)[n]);
90 0 : printf("\n");
91 :
92 0 : err = "result length";
93 0 : goto Failed;
94 : }
95 :
96 : /* test other part of buffer */
97 464 : if (todata)
98 400 : memset(buffer, 23, tolen);
99 475600 : for (i = 0; i < sizeof(buffer); ++i)
100 475136 : if (buffer[i] != 23) {
101 : err = "buffer left";
102 : goto Failed;
103 : }
104 :
105 : /* success */
106 : return 0;
107 0 : Failed:
108 0 : fprintf(stderr, "Test %s failed (got ret=%d len=%d)\n", err, (int) retcode, (int) reslen);
109 0 : fprintf(stderr, "line: %d\n DO_TEST(decl=%s,\n"
110 : "\t fromtype=%s,fromdata=%s,fromlen=%s,\n"
111 : "\t totype=%s,tomaxlen=%s,\n"
112 : "\t tores=%s,todata=%s,tolen=%s);\n",
113 : line, sdecl, sfromtype, sfromdata, sfromlen, stotype, stomaxlen, stores, stodata, stolen);
114 0 : allSuccess = 0;
115 0 : return 1;
116 : }
117 :
118 : #define DO_TEST(decl,fromtype,fromdata,fromlen,totype,tomaxlen,tores,todata,tolen) { \
119 : decl; \
120 : DoTest(fromtype,fromdata,fromlen,totype,tomaxlen,tores,todata,tolen,\
121 : #decl,#fromtype,#fromdata,#fromlen,#totype,#tomaxlen,#tores,#todata,#tolen,\
122 : __LINE__);\
123 : }
124 :
125 : int
126 8 : main(void)
127 : {
128 8 : volatile CS_BIGINT one = 1;
129 8 : int verbose = 1;
130 :
131 8 : printf("%s: Testing conversion\n", __FILE__);
132 :
133 8 : check_call(cs_ctx_alloc, (CS_VERSION_100, &ctx));
134 :
135 : /* TODO For each conversion test different values of fromlen and tolen */
136 :
137 : /*
138 : * * INT to everybody
139 : */
140 8 : DO_TEST(CS_INT test = 12345;
141 : CS_INT test2 = 12345,
142 : CS_INT_TYPE, &test, sizeof(test), CS_INT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
143 8 : DO_TEST(CS_INT test = 12345;
144 : CS_INT test2 = 12345,
145 : CS_INT_TYPE, &test, sizeof(test), CS_INT_TYPE, sizeof(test2) * 2, CS_SUCCEED, &test2, sizeof(test2));
146 : /* FIXME: correct ?? */
147 8 : DO_TEST(CS_INT test = 12345;
148 : CS_INT test2 = 12345,
149 : CS_INT_TYPE, &test, sizeof(test), CS_INT_TYPE, sizeof(CS_SMALLINT), CS_SUCCEED, &test2, sizeof(test2));
150 :
151 8 : DO_TEST(CS_INT test = 1234;
152 : CS_SMALLINT test2 = 1234, CS_INT_TYPE, &test, sizeof(test), CS_SMALLINT_TYPE, 1, CS_SUCCEED, &test2, sizeof(test2));
153 : /* biggest and smallest SMALLINT */
154 8 : DO_TEST(CS_INT test = 32767;
155 : CS_SMALLINT test2 = 32767,
156 : CS_INT_TYPE, &test, sizeof(test), CS_SMALLINT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
157 8 : DO_TEST(CS_INT test = -32768;
158 : CS_SMALLINT test2 = -32768,
159 : CS_INT_TYPE, &test, sizeof(test), CS_SMALLINT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
160 : /* overflow */
161 : #if FREETDS
162 8 : DO_TEST(CS_INT test = 32768;
163 : CS_SMALLINT test2 = 12345, CS_INT_TYPE, &test, sizeof(test), CS_SMALLINT_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
164 8 : DO_TEST(CS_INT test = -32769;
165 : CS_SMALLINT test2 = 12345, CS_INT_TYPE, &test, sizeof(test), CS_SMALLINT_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
166 : #endif
167 :
168 : /* biggest and smallest TINYINT */
169 8 : DO_TEST(CS_INT test = 255;
170 : CS_TINYINT test2 = 255,
171 : CS_INT_TYPE, &test, sizeof(test), CS_TINYINT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
172 8 : DO_TEST(CS_INT test = 0;
173 : CS_TINYINT test2 = 0,
174 : CS_INT_TYPE, &test, sizeof(test), CS_TINYINT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
175 : /* overflow */
176 : #if FREETDS
177 8 : DO_TEST(CS_INT test = 256;
178 : CS_TINYINT test2 = 1, CS_INT_TYPE, &test, sizeof(test), CS_TINYINT_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
179 8 : DO_TEST(CS_INT test = -1;
180 : CS_TINYINT test2 = 1, CS_INT_TYPE, &test, sizeof(test), CS_TINYINT_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
181 : #endif
182 :
183 : /* biggest and smallest BIT */
184 8 : DO_TEST(CS_INT test = 1;
185 : CS_BYTE test2 = 1, CS_INT_TYPE, &test, sizeof(test), CS_BIT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
186 8 : DO_TEST(CS_INT test = 0;
187 : CS_BYTE test2 = 0, CS_INT_TYPE, &test, sizeof(test), CS_BIT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
188 : /* overflow FIXME: or 1 if != 0 ?? */
189 8 : DO_TEST(CS_INT test = 2;
190 : CS_BYTE test2 = 1, CS_INT_TYPE, &test, sizeof(test), CS_BIT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
191 8 : DO_TEST(CS_INT test = -1;
192 : CS_BYTE test2 = 1, CS_INT_TYPE, &test, sizeof(test), CS_BIT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
193 :
194 8 : DO_TEST(CS_INT test = 1234;
195 : CS_REAL test2 = 1234.0,
196 : CS_INT_TYPE, &test, sizeof(test), CS_REAL_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
197 8 : DO_TEST(CS_INT test = -8765;
198 : CS_REAL test2 = -8765.0,
199 : CS_INT_TYPE, &test, sizeof(test), CS_REAL_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
200 :
201 8 : DO_TEST(CS_INT test = 1234;
202 : CS_FLOAT test2 = 1234.0,
203 : CS_INT_TYPE, &test, sizeof(test), CS_FLOAT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
204 8 : DO_TEST(CS_INT test = -8765;
205 : CS_FLOAT test2 = -8765.0,
206 : CS_INT_TYPE, &test, sizeof(test), CS_FLOAT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
207 :
208 : #if FREETDS
209 8 : DO_TEST(CS_INT test = 1234678; CS_MONEY4 test2 = {
210 : 1234678}
211 : , CS_INT_TYPE, &test, sizeof(test), CS_MONEY4_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
212 : #endif
213 8 : DO_TEST(CS_INT test = -8765; CS_MONEY4 test2 = {
214 : -8765 * 10000}
215 : , CS_INT_TYPE, &test, sizeof(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
216 :
217 : /* strange money formatting */
218 8 : DO_TEST(CS_CHAR test[] = ""; CS_MONEY4 test2 = {
219 : 0}
220 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
221 8 : DO_TEST(CS_CHAR test[] = "."; CS_MONEY4 test2 = {
222 : 0}
223 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
224 8 : DO_TEST(CS_CHAR test[] = ".12"; CS_MONEY4 test2 = {
225 : 1200}
226 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
227 8 : DO_TEST(CS_CHAR test[] = "++++-123"; CS_MONEY4 test2 = {
228 : -123 * 10000}
229 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
230 8 : DO_TEST(CS_CHAR test[] = " -123"; CS_MONEY4 test2 = {
231 : -123 * 10000}
232 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
233 8 : DO_TEST(CS_CHAR test[] = " +123"; CS_MONEY4 test2 = {
234 : 123 * 10000}
235 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
236 8 : DO_TEST(CS_CHAR test[] = "+123.1234"; CS_MONEY4 test2 = {
237 : 1231234}
238 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
239 8 : DO_TEST(CS_CHAR test[] = "+123.123411"; CS_MONEY4 test2 = {
240 : 1231234}
241 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
242 8 : DO_TEST(CS_CHAR test[] = "+123.12.3411"; CS_MONEY4 test2 = {
243 : 1231234}
244 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
245 8 : DO_TEST(CS_CHAR test[] = "pippo"; CS_MONEY4 test2 = {
246 : 0}
247 : , CS_CHAR_TYPE, test, strlen(test), CS_MONEY4_TYPE, sizeof(test2), CS_FAIL, NULL, 0);
248 :
249 : /* not terminated money */
250 8 : DO_TEST(CS_CHAR test[] = "-1234567"; CS_MONEY4 test2 = {
251 : -1230000}
252 : , CS_CHAR_TYPE, test, 4, CS_MONEY4_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
253 :
254 8 : DO_TEST(CS_INT test = 1234678;
255 : CS_MONEY test2;
256 : test2.mnyhigh = ((one * 1234678) * 10000) >> 32;
257 : test2.mnylow = (CS_UINT) ((one * 1234678) * 10000),
258 : CS_INT_TYPE, &test, sizeof(test), CS_MONEY_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
259 8 : DO_TEST(CS_INT test = -8765;
260 : CS_MONEY test2;
261 : test2.mnyhigh = ((one * -8765) * 10000) >> 32;
262 : test2.mnylow = (CS_UINT) ((one * -8765) * 10000),
263 : CS_INT_TYPE, &test, sizeof(test), CS_MONEY_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
264 :
265 8 : DO_TEST(CS_INT test = 12345;
266 : CS_CHAR test2[] = "12345",
267 : CS_INT_TYPE, &test, sizeof(test), CS_CHAR_TYPE, sizeof(test2), CS_SUCCEED, test2, sizeof(test2) - 1);
268 :
269 : #if FREETDS
270 8 : { CS_VARCHAR test2 = { 5, "12345"};
271 8 : memset(test2.str+5, 23, 251);
272 8 : DO_TEST(CS_INT test = 12345,
273 : CS_INT_TYPE,&test,sizeof(test),
274 : CS_VARCHAR_TYPE,sizeof(test2),
275 : CS_SUCCEED,&test2,sizeof(test2));
276 : }
277 : #endif
278 :
279 8 : DO_TEST(CS_CHAR test[] = "12345";
280 : CS_INT test2 = 12345, CS_CHAR_TYPE, test, 5, CS_INT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
281 :
282 : /* unterminated number */
283 8 : DO_TEST(CS_CHAR test[] = " - 12345";
284 : CS_INT test2 = -12, CS_CHAR_TYPE, test, 5, CS_INT_TYPE, sizeof(test2), CS_SUCCEED, &test2, sizeof(test2));
285 :
286 : /* to binary */
287 8 : DO_TEST(CS_CHAR test[] = "abc";
288 : CS_CHAR test2[] = "abc", CS_BINARY_TYPE, test, 3, CS_BINARY_TYPE, 3, CS_SUCCEED, test2, 3);
289 : #if 0
290 : DO_TEST(CS_CHAR test[] = "abcdef";
291 : CS_CHAR test2[] = "ab", CS_BINARY_TYPE, test, 6, CS_BINARY_TYPE, 2, CS_FAIL, test2, 2);
292 : DO_TEST(CS_CHAR test[] = "abc";
293 : CS_CHAR test2[] = "ab", CS_BINARY_TYPE, test, 3, CS_BINARY_TYPE, 2, CS_FAIL, test2, 2);
294 : #endif
295 8 : DO_TEST(CS_CHAR test[] = "616263";
296 : CS_CHAR test2[] = "abc", CS_CHAR_TYPE, test, 6, CS_BINARY_TYPE, 3, CS_SUCCEED, test2, 3);
297 8 : DO_TEST(CS_CHAR test[] = "616263646566";
298 : CS_CHAR test2[] = "abc", CS_CHAR_TYPE, test, 12, CS_BINARY_TYPE, 3, CS_FAIL, test2, 3);
299 8 : DO_TEST(CS_CHAR test[] = "hello";
300 : CS_CHAR test2[] = "abc", CS_CHAR_TYPE, test, 12, CS_BINARY_TYPE, 10, CS_FAIL, test2, 0);
301 :
302 : /* to char */
303 8 : DO_TEST(CS_INT test = 1234567;
304 : CS_CHAR test2[] = "1234567", CS_INT_TYPE, &test, sizeof(test), CS_CHAR_TYPE, 7, CS_SUCCEED, test2, 7);
305 8 : DO_TEST(CS_CHAR test[] = "abc";
306 : CS_CHAR test2[] = "ab", CS_CHAR_TYPE, test, 3, CS_CHAR_TYPE, 2, CS_FAIL, test2, 2);
307 8 : DO_TEST(CS_CHAR test[] = "abc";
308 : CS_CHAR test2[] = "616263", CS_BINARY_TYPE, test, 3, CS_CHAR_TYPE, 6, CS_SUCCEED, test2, 6);
309 8 : DO_TEST(CS_CHAR test[] = "abcdef";
310 : CS_CHAR test2[] = "616263", CS_BINARY_TYPE, test, 6, CS_CHAR_TYPE, 6, CS_FAIL, test2, 6);
311 :
312 : /* conversion to various binaries */
313 8 : DO_TEST(CS_CHAR test[] = "616263646566";
314 : CS_CHAR test2[12] = "abcdef", CS_CHAR_TYPE, test, 12, CS_IMAGE_TYPE, 12, CS_SUCCEED, test2, 6);
315 8 : DO_TEST(CS_CHAR test[] = "616263646566";
316 : CS_CHAR test2[12] = "abcdef", CS_CHAR_TYPE, test, 12, CS_BINARY_TYPE, 12, CS_SUCCEED, test2, 6);
317 8 : DO_TEST(CS_CHAR test[] = "616263646566yyy";
318 : CS_CHAR test2[] = "616263646566xxx", CS_BINARY_TYPE, test, 12, CS_BINARY_TYPE, 12, CS_SUCCEED, test2, 12);
319 8 : DO_TEST(CS_CHAR test[] = "6162636465yyyyy";
320 : CS_CHAR test2[] = "616263646566xxx", CS_BINARY_TYPE, test, 12, CS_BINARY_TYPE, 10, CS_FAIL, test2, 10);
321 8 : DO_TEST(CS_CHAR test[] = "6162636465yyyyy";
322 : CS_CHAR test2[] = "616263646566xxx", CS_IMAGE_TYPE, test, 12, CS_IMAGE_TYPE, 10, CS_FAIL, test2, 10);
323 8 : DO_TEST(CS_CHAR test[] = "6162636465yyyyy";
324 : CS_CHAR test2[] = "616263646566xxx", CS_BINARY_TYPE, test, 12, CS_IMAGE_TYPE, 10, CS_FAIL, test2, 10);
325 8 : DO_TEST(CS_CHAR test[] = "6162636465yyyyy";
326 : CS_CHAR test2[] = "616263646566xxx", CS_IMAGE_TYPE, test, 12, CS_BINARY_TYPE, 10, CS_FAIL, test2, 10);
327 8 : DO_TEST(CS_CHAR test[] = "616263646566";
328 : CS_VARBINARY test2; test2.len = 6; memset(test2.array, 23, 256); memcpy(test2.array, "abcdef", 6),
329 : CS_CHAR_TYPE, test, 12, CS_VARBINARY_TYPE, 12, CS_SUCCEED, &test2, 258);
330 :
331 : /* conversion to various binaries, same as above but with zero padding */
332 8 : dest_format = CS_FMT_PADNULL;
333 8 : DO_TEST(CS_CHAR test[] = "616263646566";
334 : CS_CHAR test2[12] = "abcdef", CS_CHAR_TYPE, test, 12, CS_IMAGE_TYPE, 12, CS_SUCCEED, test2, 12);
335 8 : DO_TEST(CS_CHAR test[] = "616263646566";
336 : CS_CHAR test2[12] = "abcdef", CS_CHAR_TYPE, test, 12, CS_BINARY_TYPE, 12, CS_SUCCEED, test2, 12);
337 8 : DO_TEST(CS_CHAR test[] = "616263646566";
338 : CS_VARBINARY test2; test2.len = 6; memset(test2.array, 23, 256); memcpy(test2.array, "abcdef", 6),
339 : CS_CHAR_TYPE, test, 12, CS_VARBINARY_TYPE, 12, CS_SUCCEED, &test2, 258);
340 8 : dest_format = CS_FMT_UNUSED;
341 :
342 8 : check_call(ct_exit, (ctx, CS_UNUSED));
343 8 : check_call(cs_ctx_drop, (ctx));
344 :
345 8 : if (verbose && allSuccess) {
346 8 : printf("Test succeded\n");
347 : }
348 8 : return allSuccess ? 0 : 1;
349 : }
|