LCOV - code coverage report
Current view: top level - src/ctlib/unittests - will_convert.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 36 47 76.6 %
Date: 2025-01-18 12:13:41 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* Test cs_will_convert
       2             :  */
       3             : #undef NDEBUG
       4             : #include <config.h>
       5             : 
       6             : #include <stdio.h>
       7             : 
       8             : #if HAVE_STDLIB_H
       9             : #include <stdlib.h>
      10             : #endif /* HAVE_STDLIB_H */
      11             : 
      12             : #if HAVE_STRING_H
      13             : #include <string.h>
      14             : #endif /* HAVE_STRING_H */
      15             : 
      16             : #include <assert.h>
      17             : #include <ctpublic.h>
      18             : #include "common.h"
      19             : 
      20             : static CS_CONTEXT *context;
      21             : 
      22             : static void
      23           8 : check_ret(const char *name, CS_RETCODE ret)
      24             : {
      25           8 :         if (ret != CS_SUCCEED) {
      26           0 :                 fprintf(stderr, "%s(): failed\n", name);
      27           0 :                 exit(1);
      28             :         }
      29           8 : }
      30             : 
      31             : typedef struct {
      32             :         const char *expected;
      33             :         CS_INT from;
      34             : } test_row;
      35             : 
      36             : /* from
      37             :  * http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ocs_12.5.1.comlib/html/comlib/X12687.htm
      38             :  * X = conversion supported
      39             :  * . = conversion not supported
      40             :  * x = should be supported, but currently it's not
      41             :  * _ = should be not supported, but currently it is
      42             :  */
      43             : static const test_row test_rows[] = {
      44             : /*
      45             :   B L V B C L V D D T S I D N F R M M B S T I U D T
      46             :   I O A I H O A A A I M N E U L E O O O E E M N A I
      47             :   N N R T A N R T T N A T C M O A N N U N X A I T M
      48             :   A G B   R G C E E Y L   I E A L E E N S T G C E E
      49             :   R B I     C H T T I L   M R T   Y Y D I   E H
      50             :   Y I N     H A I I N I   A I       4 A T     A
      51             :     N A     A R M M T N   L C         R I     R
      52             :     A R     R   E E   T               Y V
      53             :     R Y           4                     I
      54             :     Y                                   T
      55             :                                         Y
      56             : */
      57             : {"X X X x X X X x x X X X x x X X X X . . X X X x x", CS_BINARY_TYPE},
      58             : {"X X X x X X X x x X X X x x X X X X . . X X X x x", CS_LONGBINARY_TYPE},
      59             : {"X X X x X X X x x X X X x x X X X X . . X X X x x", CS_VARBINARY_TYPE},
      60             : {"X X X X X X X . . X X X X X X X _ _ . . X X X x x", CS_BIT_TYPE},
      61             : {"X X X X X X X X X X X X X X X X X X x x X X X X X", CS_CHAR_TYPE},
      62             : {"X X X X X X X X X X X X X X X X X X x x X X X X X", CS_LONGCHAR_TYPE},
      63             : {"X X X X X X X X X X X X X X X X X X x x X X X X X", CS_VARCHAR_TYPE},
      64             : {"_ _ X . X X X X X . . . . . . . . . . . X X X X X", CS_DATETIME_TYPE},
      65             : {"_ _ X . X X X X X . . . . . . . . . . . X X X X X", CS_DATETIME4_TYPE},
      66             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_TINYINT_TYPE},
      67             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_SMALLINT_TYPE},
      68             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_INT_TYPE},
      69             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_DECIMAL_TYPE},
      70             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_NUMERIC_TYPE},
      71             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_FLOAT_TYPE},
      72             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_REAL_TYPE},
      73             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_MONEY_TYPE},
      74             : {"X X X X X X X . . X X X X X X X X X . . X X _ . .", CS_MONEY4_TYPE},
      75             : {". . . . x x x . . . . . . . . . . . x . x . . . .", CS_BOUNDARY_TYPE},
      76             : {". . . . x x x . . . . . . . . . . . . x x . . . .", CS_SENSITIVITY_TYPE},
      77             : {"X X X X X X X X X X X X X X X X X X x x X X _ _ _", CS_TEXT_TYPE},
      78             : {"X X X x X X X x x X X X x x X X X X . . X X _ . .", CS_IMAGE_TYPE},
      79             : {"X X X X X X X X X _ _ _ _ _ _ _ _ _ . . _ _ _ _ _", CS_UNICHAR_TYPE},
      80             : {"_ _ X . X X X X X . . . . . . . . . . . X X X X _", CS_DATE_TYPE},
      81             : {"_ _ X . X X X X X . . . . . . . . . . . X X X _ X", CS_TIME_TYPE},
      82             : {NULL, 0}
      83             : };
      84             : 
      85             : #define TEST_ALL_TYPES \
      86             :         TEST_TYPE(BINARY) \
      87             :         TEST_TYPE(LONGBINARY) \
      88             :         TEST_TYPE(VARBINARY) \
      89             :         TEST_TYPE(BIT) \
      90             :         TEST_TYPE(CHAR) \
      91             :         TEST_TYPE(LONGCHAR) \
      92             :         TEST_TYPE(VARCHAR) \
      93             :         TEST_TYPE(DATETIME) \
      94             :         TEST_TYPE(DATETIME4) \
      95             :         TEST_TYPE(TINYINT) \
      96             :         TEST_TYPE(SMALLINT) \
      97             :         TEST_TYPE(INT) \
      98             :         TEST_TYPE(DECIMAL) \
      99             :         TEST_TYPE(NUMERIC) \
     100             :         TEST_TYPE(FLOAT) \
     101             :         TEST_TYPE(REAL) \
     102             :         TEST_TYPE(MONEY) \
     103             :         TEST_TYPE(MONEY4) \
     104             :         TEST_TYPE(BOUNDARY) \
     105             :         TEST_TYPE(SENSITIVITY) \
     106             :         TEST_TYPE(TEXT) \
     107             :         TEST_TYPE(IMAGE) \
     108             :         TEST_TYPE(UNICHAR) \
     109             :         TEST_TYPE(DATE) \
     110             :         TEST_TYPE(TIME)
     111             : 
     112             : 
     113             : static CS_INT column_types[] = {
     114             : #define TEST_TYPE(type) CS_ ## type ## _TYPE,
     115             :         TEST_ALL_TYPES
     116             : #undef TEST_TYPE
     117             :         CS_ILLEGAL_TYPE
     118             : };
     119             : 
     120             : static const char *
     121           0 : type_name(CS_INT value)
     122             : {
     123           0 :         switch (value) {
     124             : #define TEST_TYPE(type) case CS_ ## type ## _TYPE: return #type;
     125           0 :         TEST_ALL_TYPES
     126             : #undef TEST_TYPE
     127             :         }
     128           0 :         return "unknown!";
     129             : }
     130             : 
     131             : static void
     132        4320 : test0(CS_INT from, CS_INT to, CS_BOOL expected, int line)
     133             : {
     134             :         CS_BOOL res;
     135        4320 :         res = 123;
     136        4320 :         cs_will_convert(context, from, to, &res);
     137        4320 :         if (res != expected) {
     138           0 :                 fprintf(stderr, "Wrong result %d (%s) -> %d (%s) %d\n",
     139             :                         from, type_name(from),
     140             :                         to, type_name(to),
     141             :                         res);
     142           0 :                 exit(1);
     143             :         }
     144        4320 :         res = 123;
     145        4320 :         cs_will_convert(NULL, from, to, &res);
     146        4320 :         if (res != expected) {
     147           0 :                 fprintf(stderr, "Wrong result %d (%s) -> %d (%s) %d\n",
     148             :                         from, type_name(from),
     149             :                         to, type_name(to),
     150             :                         res);
     151           0 :                 exit(1);
     152             :         }
     153        4320 : }
     154             : 
     155             : #define test(from, to, expected) test0(from, to, expected, __LINE__)
     156             : 
     157             : int
     158           8 : main(int argc, char **argv)
     159             : {
     160           8 :         int verbose = 0;
     161             :         CS_COMMAND *command;
     162             :         CS_CONNECTION *connection;
     163             :         CS_INT from, to;
     164             :         const test_row *row;
     165             : 
     166           8 :         check_ret("try_ctlogin", try_ctlogin(&context, &connection, &command, verbose));
     167             : 
     168         208 :         for (row = test_rows; row->expected; ++row) {
     169             :                 const CS_INT *type;
     170             :                 const char *expected = row->expected;
     171        5000 :                 for (type = column_types; *type != CS_ILLEGAL_TYPE; ++type) {
     172        5000 :                         switch (*expected) {
     173        3056 :                         case 'X':
     174        3056 :                                 test(row->from, *type, CS_TRUE);
     175        3056 :                                 break;
     176        1264 :                         case '.':
     177        1264 :                                 test(row->from, *type, CS_FALSE);
     178        1264 :                                 break;
     179             :                         case 'x':
     180             :                         case '_':
     181             :                                 /* ignore */
     182             :                                 break;
     183             :                         default:
     184           0 :                                 assert(0);
     185             :                         }
     186        5000 :                         ++expected;
     187        5000 :                         assert(*expected == 0 || *expected == ' ');
     188        5000 :                         if (*expected)
     189        4800 :                                 ++expected;
     190             :                 }
     191             :         }
     192             : 
     193        1720 :         for (from = CS_MAX_SYBTYPE + 1; from < 256; ++from) {
     194      369800 :                 for (to = CS_MAX_SYBTYPE + 1; to < 256; ++to) {
     195             :                         CS_BOOL res;
     196      369800 :                         res = 123;
     197      369800 :                         cs_will_convert(context, from, to, &res);
     198      369800 :                         assert(res == CS_FALSE);
     199      369800 :                         res = 123;
     200      369800 :                         cs_will_convert(NULL, from, to, &res);
     201      369800 :                         assert(res == CS_FALSE);
     202             :                 }
     203             :         }
     204             : 
     205           8 :         try_ctlogout(context, connection, command, verbose);
     206             : 
     207             :         return 0;
     208             : }

Generated by: LCOV version 1.13