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

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2020  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #undef NDEBUG
      21             : 
      22             : /* allows to use some internal functions */
      23             : #include "../query.c"
      24             : 
      25             : #include <freetds/data.h>
      26             : 
      27             : static void
      28         248 : test_generic(const char *s, int expected_pos, bool comment, int line)
      29             : {
      30             :         const char *next;
      31             :         size_t len, n;
      32             :         char *buf;
      33             : 
      34         248 :         tdsdump_log(TDS_DBG_FUNC, "test line %d\n", line);
      35             : 
      36             :         /* multi byte */
      37         248 :         if (comment)
      38          64 :                 next = tds_skip_comment(s);
      39             :         else
      40             :                 next = tds_skip_quoted(s);
      41         248 :         tdsdump_log(TDS_DBG_INFO1, "returned ptr %p diff %ld\n", next, (long int) (next - s));
      42         248 :         assert(next >= s);
      43         248 :         assert(next - s == expected_pos);
      44             : 
      45             :         /* ucs2/utf16 */
      46         248 :         len = strlen(s);
      47         248 :         buf = tds_new(char, len * 2); /* use malloc to help memory debuggers */
      48        1320 :         for (n = 0; n < len; ++n) {
      49        1072 :                 buf[n*2] = s[n];
      50        1072 :                 buf[n*2 + 1] = 0;
      51             :         }
      52         248 :         s = buf;
      53         248 :         if (comment)
      54          64 :                 next = tds_skip_comment_ucs2le(s, s + len*2);
      55             :         else
      56         184 :                 next = tds_skip_quoted_ucs2le(s, s + len*2);
      57         248 :         tdsdump_log(TDS_DBG_INFO1, "returned ptr %p diff %ld\n", next, (long int) (next - s));
      58         248 :         assert(next >= s);
      59         248 :         assert((next - s) % 2 == 0);
      60         248 :         assert((next - s) / 2 == expected_pos);
      61         248 :         free(buf);
      62         248 : }
      63             : 
      64             : #define test_comment(s, e) test_generic(s, e, true, __LINE__)
      65             : #define test_quote(s, e) test_generic(s, e, false, __LINE__)
      66             : 
      67             : int
      68           8 : main(int argc, char **argv)
      69             : {
      70           8 :         tdsdump_open(getenv("TDSDUMP"));
      71             : 
      72             :         /* test comment skipping */
      73           8 :         test_comment("--", 2);
      74           8 :         test_comment("--  aa", 6);
      75           8 :         test_comment("--\nx", 3);
      76           8 :         test_comment("/*", 2);
      77           8 :         test_comment("/*/", 3);
      78           8 :         test_comment("/*     a", 8);
      79           8 :         test_comment("/**/v", 4);
      80           8 :         test_comment("/* */x", 5);
      81             : 
      82             :         /* test quoted strings */
      83           8 :         test_quote("''", 2);
      84           8 :         test_quote("'a'", 3);
      85           8 :         test_quote("''''", 4);
      86           8 :         test_quote("'a'''", 5);
      87           8 :         test_quote("'''a'", 5);
      88           8 :         test_quote("'' ", 2);
      89           8 :         test_quote("'a'x", 3);
      90           8 :         test_quote("'''' ", 4);
      91           8 :         test_quote("'a'''x", 5);
      92           8 :         test_quote("'''a' ", 5);
      93             : 
      94             :         /* test quoted identifiers */
      95           8 :         test_quote("[]", 2);
      96           8 :         test_quote("[a]", 3);
      97           8 :         test_quote("[]]]", 4);
      98           8 :         test_quote("[a]]]", 5);
      99           8 :         test_quote("[]]a]", 5);
     100           8 :         test_quote("[]x", 2);
     101           8 :         test_quote("[a] ", 3);
     102           8 :         test_quote("[]]]x", 4);
     103           8 :         test_quote("[a]]] ", 5);
     104           8 :         test_quote("[]]a]x", 5);
     105           8 :         test_quote("[[]", 3);
     106           8 :         test_quote("[[[]", 4);
     107           8 :         test_quote("[[x[]", 5);
     108             : 
     109             :         return 0;
     110             : }

Generated by: LCOV version 1.13