LCOV - code coverage report
Current view: top level - src/ctlib/unittests - cs_convert.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 79 96 82.3 %
Date: 2025-01-18 12:13:41 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.13