LCOV - code coverage report
Current view: top level - src/ctlib/unittests - will_convert.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 33 42 78.6 %
Date: 2025-01-18 11:50:39 Functions: 2 3 66.7 %

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

Generated by: LCOV version 1.13