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 : /* allows to use some internal functions */
21 : #undef NDEBUG
22 : #include "../query.c"
23 :
24 : #include "common.h"
25 :
26 : #include <freetds/data.h>
27 :
28 : static void
29 310 : test_generic(const char *s, int expected_pos, bool comment, int line)
30 : {
31 : const char *next;
32 : size_t len, n;
33 : char *buf;
34 :
35 310 : tdsdump_log(TDS_DBG_FUNC, "test line %d\n", line);
36 :
37 : /* multi byte */
38 310 : if (comment)
39 80 : next = tds_skip_comment(s);
40 : else
41 : next = tds_skip_quoted(s);
42 310 : tdsdump_log(TDS_DBG_INFO1, "returned ptr %p diff %ld\n", next, (long int) (next - s));
43 310 : assert(next >= s);
44 310 : assert(next - s == expected_pos);
45 :
46 : /* ucs2/utf16 */
47 310 : len = strlen(s);
48 310 : buf = tds_new(char, len * 2); /* use malloc to help memory debuggers */
49 1650 : for (n = 0; n < len; ++n) {
50 1340 : buf[n*2] = s[n];
51 1340 : buf[n*2 + 1] = 0;
52 : }
53 310 : s = buf;
54 310 : if (comment)
55 80 : next = tds_skip_comment_ucs2le(s, s + len*2);
56 : else
57 230 : next = tds_skip_quoted_ucs2le(s, s + len*2);
58 310 : tdsdump_log(TDS_DBG_INFO1, "returned ptr %p diff %ld\n", next, (long int) (next - s));
59 310 : assert(next >= s);
60 310 : assert((next - s) % 2 == 0);
61 310 : assert((next - s) / 2 == expected_pos);
62 310 : free(buf);
63 310 : }
64 :
65 : #define test_comment(s, e) test_generic(s, e, true, __LINE__)
66 : #define test_quote(s, e) test_generic(s, e, false, __LINE__)
67 :
68 10 : TEST_MAIN()
69 : {
70 10 : tdsdump_open(tds_dir_getenv(TDS_DIR("TDSDUMP")));
71 :
72 : /* test comment skipping */
73 10 : test_comment("--", 2);
74 10 : test_comment("-- aa", 6);
75 10 : test_comment("--\nx", 3);
76 10 : test_comment("/*", 2);
77 10 : test_comment("/*/", 3);
78 10 : test_comment("/* a", 8);
79 10 : test_comment("/**/v", 4);
80 10 : test_comment("/* */x", 5);
81 :
82 : /* test quoted strings */
83 10 : test_quote("''", 2);
84 10 : test_quote("'a'", 3);
85 10 : test_quote("''''", 4);
86 10 : test_quote("'a'''", 5);
87 10 : test_quote("'''a'", 5);
88 10 : test_quote("'' ", 2);
89 10 : test_quote("'a'x", 3);
90 10 : test_quote("'''' ", 4);
91 10 : test_quote("'a'''x", 5);
92 10 : test_quote("'''a' ", 5);
93 :
94 : /* test quoted identifiers */
95 10 : test_quote("[]", 2);
96 10 : test_quote("[a]", 3);
97 10 : test_quote("[]]]", 4);
98 10 : test_quote("[a]]]", 5);
99 10 : test_quote("[]]a]", 5);
100 10 : test_quote("[]x", 2);
101 10 : test_quote("[a] ", 3);
102 10 : test_quote("[]]]x", 4);
103 10 : test_quote("[a]]] ", 5);
104 10 : test_quote("[]]a]x", 5);
105 10 : test_quote("[[]", 3);
106 10 : test_quote("[[[]", 4);
107 10 : test_quote("[[x[]", 5);
108 :
109 10 : return 0;
110 : }
|