Line data Source code
1 : /** @file suite_csvtranscode.c
2 : * @brief Check testsuite template
3 : * @author François Cerbelle (Fanfan), francois@cerbelle.net
4 : *
5 : * @internal
6 : * Created: 20/03/2022
7 : * Revision: none
8 : * Last modified: 2024-11-11 17:26
9 : * Compiler: gcc
10 : * Organization: Cerbelle.net
11 : * Copyright: Copyright (c) 2024, François Cerbelle
12 : *
13 : * This source code is released for free distribution under the terms of the
14 : * GNU General Public License as published by the Free Software Foundation.
15 : */
16 :
17 : #ifdef HAVE_CONFIG_H
18 : #include "config.h"
19 : #endif
20 :
21 : #include <check.h>
22 : #include "checktools.inc"
23 :
24 : #include "csv.c"
25 :
26 : /** Dummy test to trigger fixtures
27 : *
28 : * This test is defined and added to all testcases in order to execute at
29 : * least one test test in each testcase of the testsuite, trigger the
30 : * fixtures and have a decent coverage report.
31 : */
32 1 : START_TEST(csvtranscode_test) {
33 : /* This test is supposed to trigger a SIGABRT(6) and will crash the
34 : * whole testsuite if not caught or not in a child process */
35 1 : forktest_only;
36 :
37 1 : abort();
38 : }
39 : END_TEST
40 :
41 23 : int csvtranscode_txt2csv_test(const char* txt, const char* exp) {
42 : char* src;
43 : char* dst;
44 : int retval;
45 23 : src=strdup(txt);
46 23 : dst = txt2csv(src);
47 23 : retval=strcmp(dst,exp);
48 23 : free(src);
49 23 : free(dst);
50 23 : return retval;
51 : }
52 :
53 1 : START_TEST(csvtranscode_txt2csv) {
54 :
55 1 : ck_assert(0==csvtranscode_txt2csv_test("",""));
56 1 : ck_assert(0==csvtranscode_txt2csv_test("ab","ab"));
57 :
58 1 : ck_assert(0==csvtranscode_txt2csv_test(",ab","\",ab\""));
59 1 : ck_assert(0==csvtranscode_txt2csv_test("a,b","\"a,b\""));
60 1 : ck_assert(0==csvtranscode_txt2csv_test("ab,","\"ab,\""));
61 :
62 1 : ck_assert(0==csvtranscode_txt2csv_test(" ab"," ab"));
63 1 : ck_assert(0==csvtranscode_txt2csv_test("a b","a b"));
64 1 : ck_assert(0==csvtranscode_txt2csv_test("ab ","ab "));
65 :
66 1 : ck_assert(0==csvtranscode_txt2csv_test("\rab","\"\rab\""));
67 1 : ck_assert(0==csvtranscode_txt2csv_test("a\rb","\"a\rb\""));
68 1 : ck_assert(0==csvtranscode_txt2csv_test("ab\r","\"ab\r\""));
69 :
70 1 : ck_assert(0==csvtranscode_txt2csv_test("\nab","\"\nab\""));
71 1 : ck_assert(0==csvtranscode_txt2csv_test("a\nb","\"a\nb\""));
72 1 : ck_assert(0==csvtranscode_txt2csv_test("ab\n","\"ab\n\""));
73 :
74 1 : ck_assert(0==csvtranscode_txt2csv_test("\r\nab","\"\r\nab\""));
75 1 : ck_assert(0==csvtranscode_txt2csv_test("a\r\nb","\"a\r\nb\""));
76 1 : ck_assert(0==csvtranscode_txt2csv_test("ab\r\n","\"ab\r\n\""));
77 :
78 1 : ck_assert(0==csvtranscode_txt2csv_test("\n\rab","\"\n\rab\""));
79 1 : ck_assert(0==csvtranscode_txt2csv_test("a\n\rb","\"a\n\rb\""));
80 1 : ck_assert(0==csvtranscode_txt2csv_test("ab\n\r","\"ab\n\r\""));
81 :
82 1 : ck_assert(0==csvtranscode_txt2csv_test("\"ab","\"\"\"ab\""));
83 1 : ck_assert(0==csvtranscode_txt2csv_test("a\"b","\"a\"\"b\""));
84 1 : ck_assert(0==csvtranscode_txt2csv_test("ab\"","\"ab\"\"\""));
85 1 : }
86 : END_TEST
87 :
88 48 : int csvtranscode_csvtok_test(char* csv, const char* expected) {
89 48 : char* txt = csvtok(csv);
90 48 : ck_assert(((NULL!=expected)&&(NULL!=txt))||
91 : ((NULL==expected)&&(NULL==txt))) ;
92 48 : int retval = (NULL==txt?0:strcmp(txt,expected));
93 48 : free(txt);
94 48 : return retval;
95 : }
96 :
97 1 : START_TEST(csvtranscode_csvtok_ok) {
98 1 : char* csv = strdup(
99 : " ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", \r\n"
100 : " ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", ");
101 :
102 1 : ck_assert(0==csvtranscode_csvtok_test(csv," "));
103 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
104 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
105 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
106 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
107 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
108 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
109 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," "));
110 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
111 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
112 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
113 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
114 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
115 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
116 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
117 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
118 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," "));
119 :
120 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," "));
121 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
122 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
123 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
124 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
125 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
126 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
127 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," "));
128 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
129 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
130 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
131 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
132 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
133 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
134 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
135 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
136 1 : ck_assert(0==csvtranscode_csvtok_test(NULL," "));
137 :
138 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
139 :
140 1 : free(csv);
141 1 : }
142 : END_TEST
143 :
144 1 : START_TEST(csvtranscode_csvtok_empty) {
145 1 : char* csv = strdup("");
146 1 : ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
147 1 : free(csv);
148 1 : }
149 : END_TEST
150 :
151 1 : START_TEST(csvtranscode_csvtok_commazero) {
152 1 : char* csv = strdup("a,");
153 1 : ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
154 1 : free(csv);
155 1 : }
156 : END_TEST
157 :
158 1 : START_TEST(csvtranscode_csvtok_commaCR) {
159 1 : char* csv = strdup("a,\r");
160 : /* Accepted because CR is not a separator by itself, alone, but a contents */
161 1 : ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
162 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,"\r"));
163 1 : free(csv);
164 1 : }
165 : END_TEST
166 :
167 1 : START_TEST(csvtranscode_csvtok_commaLF) {
168 1 : char* csv = strdup("a,\n");
169 1 : ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
170 1 : free(csv);
171 1 : }
172 : END_TEST
173 :
174 1 : START_TEST(csvtranscode_csvtok_commaCRLF) {
175 1 : char* csv = strdup("a,\r\n");
176 1 : ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
177 1 : free(csv);
178 1 : }
179 : END_TEST
180 :
181 1 : START_TEST(csvtranscode_csvtok_commaLFCR) {
182 1 : char* csv = strdup("a,\n\r");
183 1 : ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
184 1 : free(csv);
185 1 : }
186 : END_TEST
187 :
188 1 : START_TEST(csvtranscode_csvtok_spacebeforequote) {
189 1 : char* csv = strdup("a, \"b\",c");
190 1 : ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
191 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
192 1 : free(csv);
193 1 : }
194 : END_TEST
195 :
196 1 : START_TEST(csvtranscode_csvtok_spaceafterquote) {
197 1 : char* csv = strdup("a,\"b\" ,c");
198 1 : ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
199 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
200 1 : free(csv);
201 1 : }
202 : END_TEST
203 :
204 1 : START_TEST(csvtranscode_csvtok_unbalancedquotes) {
205 1 : char* csv = strdup("a,\"b,c");
206 1 : ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
207 1 : ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
208 1 : free(csv);
209 1 : }
210 : END_TEST
211 :
212 : /************************************************************************/
213 : /* Begining of test (potentially in child) */
214 12 : void csvtranscode_checked_uninitialized_setup() {
215 12 : signals_catch();
216 12 : forktest_gprofdir();
217 12 : }
218 :
219 : /* End of test (potentially in child) */
220 11 : void csvtranscode_checked_uninitialized_teardown() {
221 11 : signals_release();
222 11 : }
223 : /************************************************************************/
224 13 : void csvtranscode_unchecked_common_setup() {
225 13 : forktest_init();
226 13 : }
227 1 : void csvtranscode_unchecked_common_teardown() {
228 1 : }
229 : /************************************************************************/
230 13 : Suite* csvtranscode_suite() {
231 : Suite *s;
232 : TCase *tc;
233 :
234 13 : s = suite_create("CSVTranscode");
235 :
236 13 : tc = tcase_create("csvtok");
237 13 : tcase_set_tags(tc,"CSVTranscodeTag CSVTranscodeTag2");
238 : /* tcase_set_timeout(tc,5); */ /* seconds */
239 13 : tcase_add_checked_fixture(tc, csvtranscode_checked_uninitialized_setup, csvtranscode_checked_uninitialized_teardown);
240 13 : tcase_add_unchecked_fixture(tc, csvtranscode_unchecked_common_setup, csvtranscode_unchecked_common_teardown);
241 13 : suite_add_tcase(s, tc);
242 13 : tcase_add_test_raise_signal(tc, csvtranscode_test,6);
243 13 : tcase_add_test(tc, csvtranscode_txt2csv);
244 13 : tcase_add_test(tc, csvtranscode_csvtok_empty);
245 13 : tcase_add_test(tc, csvtranscode_csvtok_ok);
246 13 : tcase_add_test(tc, csvtranscode_csvtok_commazero);
247 13 : tcase_add_test(tc, csvtranscode_csvtok_commaLF);
248 13 : tcase_add_test(tc, csvtranscode_csvtok_commaCR);
249 13 : tcase_add_test(tc, csvtranscode_csvtok_commaCRLF);
250 13 : tcase_add_test(tc, csvtranscode_csvtok_commaLFCR);
251 13 : tcase_add_test(tc, csvtranscode_csvtok_spacebeforequote);
252 13 : tcase_add_test(tc, csvtranscode_csvtok_spaceafterquote);
253 13 : tcase_add_test(tc, csvtranscode_csvtok_unbalancedquotes);
254 :
255 13 : return s;
256 : }
257 : /* vim: set tw=80: */
|