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

Generated by: LCOV version 1.13