LCOV - code coverage report
Current view: top level - src/odbc/unittests - utf8_3.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 33 38 86.8 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : #include <assert.h>
       3             : 
       4             : /* test conversion using SQLGetData */
       5             : 
       6             : int
       7           8 : main(void)
       8             : {
       9             :         SQLLEN len;
      10             :         unsigned char buf[30];
      11             :         static const char expected[] = "\xf0\x9f\x8e\x84";
      12             :         int i;
      13             : 
      14           8 :         odbc_use_version3 = 1;
      15           8 :         odbc_conn_additional_params = "ClientCharset=UTF-8;";
      16             : 
      17           8 :         odbc_connect();
      18           8 :         if (!odbc_driver_is_freetds()) {
      19           0 :                 odbc_disconnect();
      20           0 :                 printf("Driver is not FreeTDS, exiting\n");
      21           0 :                 odbc_test_skipped();
      22           0 :                 return 0;
      23             :         }
      24             : 
      25           8 :         if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x700) {
      26           2 :                 odbc_disconnect();
      27             :                 /* we need NVARCHAR */
      28           2 :                 printf("Test for MSSQL only using protocol 7.0\n");
      29           2 :                 odbc_test_skipped();
      30           0 :                 return 0;
      31             :         }
      32             : 
      33           6 :         CHKAllocStmt(&odbc_stmt, "S");
      34             : 
      35             :         /* a Christmas tree */
      36           6 :         odbc_command("SELECT CONVERT(NVARCHAR(10), CONVERT(VARBINARY(20), 0x3CD884DF))");
      37             : 
      38           6 :         CHKFetch("S");
      39             : 
      40             :         /* read one byte at a time and test it */
      41          30 :         for (i = 0; i < 4; ++i) {
      42          24 :                 memset(buf, '-', sizeof(buf));
      43          24 :                 CHKGetData(1, SQL_C_CHAR, buf, 2, &len, i < 3 ? "I" : "S");
      44          24 :                 printf("res %ld buf { 0x%02x, 0x%02x }\n", (long int) len, buf[0], buf[1]);
      45          24 :                 assert(len == SQL_NO_TOTAL || len == 4 - i);
      46          24 :                 assert(buf[0] == (unsigned char) expected[i]);
      47          24 :                 assert(buf[1] == 0);
      48             :         }
      49           6 :         CHKGetData(1, SQL_C_CHAR, buf, 2, &len, "No");
      50             : 
      51           6 :         odbc_reset_statement();
      52             : 
      53             : #define CN_STRING \
      54             :         "202020202052656974657261746520486f6c642028324829207261a174696e6720a15820" \
      55             :         "a14ea16fa172a173a174a161a172a1207265706f72746564206120736574206f6620756e" \
      56             :         "6578636974696e6720726573756c74732077697468206d6f646573742067726f77746820" \
      57             :         "696e20726576656e756520616e6420626f74746f6d206c696e652e20457870616e73696f" \
      58             :         "6e20696e746f2074686520646f6d6573"
      59             : 
      60             :         /* insert does not change as much as CONVERT so insert first into a new table */
      61           6 :         odbc_command("CREATE TABLE #tmp1(c VARCHAR(200) COLLATE Chinese_PRC_CI_AS NULL)");
      62           6 :         odbc_command("INSERT INTO #tmp1 VALUES(CONVERT(VARBINARY(200), 0x" CN_STRING "))");
      63           6 :         odbc_command("SELECT c FROM #tmp1");
      64           6 :         CHKFetch("S");
      65          36 :         for (i = 0; i < 5; ++i) {
      66          30 :                 memset(buf, 0, sizeof(buf));
      67          30 :                 CHKGetData(1, SQL_C_CHAR, buf, sizeof(buf), &len, "I");
      68          30 :                 printf("loop %d output '%s'\n", i, buf);
      69          30 :                 assert(strlen((char *) buf) == sizeof(buf) - 1);
      70             :         }
      71           6 :         CHKGetData(1, SQL_C_CHAR, buf, sizeof(buf), &len, "S");
      72             : 
      73           6 :         odbc_disconnect();
      74           6 :         return 0;
      75             : }
      76             : 

Generated by: LCOV version 1.13