File: csv_parser_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (326 lines) | stat: -rw-r--r-- 9,472 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <test.h>

#include <sequence.h>
#include <alloc.h>
#include <csv_parser.h>
#include <writer.h>

static void test_new_csv_reader_empty_string()
{
    char line[]=",Null is not empty string,,\"\",\"\"";

    Seq *list = SeqParseCsvString(line);
    assert_true(list != NULL);
    assert_int_equal(list->length, 5);
    assert_true(list->data[0] != NULL);
    assert_true(list->data[1] != NULL);
    assert_true(list->data[2] != NULL);
    assert_true(list->data[3] != NULL);
    assert_true(list->data[4] != NULL);
    assert_string_equal(list->data[0], "");
    assert_string_equal(list->data[1], "Null is not empty string");
    assert_string_equal(list->data[2], "");
    assert_string_equal(list->data[3], "");
    assert_string_equal(list->data[4], "");
    assert_string_equal(list->data[2], list->data[3]);
    SeqDestroy(list);
}

static void test_new_csv_reader_basic()
{
    Seq *list = NULL;
    char *lines[5] = {
        "aaaa,bbb,ccccc",
        "\"aaaa\",\"bbb\",\"ccccc\"",
        "\"aaaa\",bbb,\"ccccc\"",
        "aaaa,\"bbb\",ccccc",
        ",\"bbb\",ccccc",
    };


    for (int i=0; i<5; i++)
    {
        list = SeqParseCsvString(lines[i]);
        assert_int_equal(list->length, 3);
        assert_string_equal(list->data[2], "ccccc");
        if (list != NULL)
        {
            SeqDestroy(list);
        } else {
            assert_true(false);
        }
    }
}

static void test_new_csv_reader()
{
    Seq *list = NULL;
    char line[]="  Type    ,\"walo1\",  \"walo2\"   ,  \"wal,o \"\" 3\",  \"  ab,cd  \", walo solo ,, \"walo\"";


    list = SeqParseCsvString(line);

    assert_int_equal(list->length, 8);
    assert_string_equal(list->data[3], "wal,o \" 3");
    assert_string_equal(list->data[6], "");
    assert_string_equal(list->data[7], "walo");
    if (list != NULL)
    {
        SeqDestroy(list);
    }
    else
    {
        assert_true(false);
    }
}

static void test_new_csv_reader_lfln()
{
    Seq *list = NULL;
    char line[]="  Type    ,\"walo1\",  \"walo2\"   ,  \"wa\r\nl,o\n \"\"\r 3\",  \"  ab,cd  \", walo solo ,, \"walo\" ";


    list = SeqParseCsvString(line);

    assert_int_equal(list->length, 8);
    assert_string_equal(list->data[3], "wa\r\nl,o\n \"\r 3");
    if (list != NULL)
    {
        SeqDestroy(list);
    }
    else
    {
        assert_true(false);
    }
}

static void test_new_csv_reader_lfln_at_end()
{
    Seq *list = NULL;
    char line[]="  Type    ,\"walo1\",  \"walo2\"   ,  \"wal\r\n,o \"\" 3\",  \"  ab,cd  \", walo solo , , \"walo\"       \r\n           ";


    list = SeqParseCsvString(line);

    assert_int_equal(list->length, 8);
    assert_string_equal(list->data[3], "wal\r\n,o \" 3");
    assert_string_equal(list->data[6], " ");
    assert_string_equal(list->data[7], "walo");
    if (list != NULL)
    {
        SeqDestroy(list);
    }
    else
    {
        assert_true(false);
    }
}

static void test_new_csv_reader_lfln_at_end2()
{
    Seq *list = NULL;
    char line[]="  Type    ,\"walo1\",  \"walo2\"   ,  \"wal\r\n,o \"\" 3\",  \"  ab,cd  \", walo solo , ,walo\r\n";

    list = SeqParseCsvString(line);

    assert_int_equal(list->length, 8);
    assert_string_equal(list->data[7], "walo");
    if (list != NULL)
    {
        SeqDestroy(list);
    }
    else
    {
        assert_true(false);
    }
}

static void test_new_csv_reader_lfln_at_end3()
{
    Seq *list = NULL;
    char line[]="  Type    ,\"walo1\",  \"walo2\"   ,  \"wal\r\n,o \"\" 3\",  \"  ab,cd  \", walo solo , , \r\n";


    list = SeqParseCsvString(line);

    assert_int_equal(list->length, 8);
    assert_string_equal(list->data[7], " ");
    if (list != NULL)
    {
        SeqDestroy(list);
    }
    else
    {
        assert_true(false);
    }
}

static void test_get_next_line()
{
    FILE *fp = fopen("./data/csv_file.csv", "r");
    assert_true(fp);

    {
        char *line = GetCsvLineNext(fp);
        assert_true(line);
        assert_string_equal(line, "field_1, field_2\r\n");

        Seq *list = SeqParseCsvString(line);
        assert_true(list);
        assert_int_equal(SeqLength(list), 2);
        assert_string_equal(SeqAt(list, 0), "field_1");
        assert_string_equal(SeqAt(list, 1), " field_2");
        SeqDestroy(list);
        free(line);
    }

    {
        char *line = GetCsvLineNext(fp);
        assert_true(line);
        assert_string_equal(line, "field_1, \"value1 \nvalue2 \nvalue3\"\r\n");

        Seq *list = SeqParseCsvString(line);
        assert_true(list);
        assert_int_equal(SeqLength(list), 2);
        assert_string_equal(SeqAt(list, 0), "field_1");
        assert_string_equal(SeqAt(list, 1), "value1 \nvalue2 \nvalue3");
        SeqDestroy(list);
        free(line);
    }

    {
        char *line = GetCsvLineNext(fp);
        assert_true(line);
        assert_string_equal(line, "field_1, \"field,2\"\r\n");
        Seq *list = SeqParseCsvString(line);
        assert_true(list);
        assert_int_equal(SeqLength(list), 2);
        assert_string_equal(SeqAt(list, 0), "field_1");
        assert_string_equal(SeqAt(list, 1), "field,2");
        SeqDestroy(list);
        free(line);
    }

    fclose(fp);
}

static void test_get_next_line_edge_cases()
{
    // Generated file in python:
    // with open("tests/unit/data/csv_file_edge_cases.csv", "w") as f:
    //     f.write("Empty,Empty,One double quote,Two double quotes,LF,CRLF,CRLFCRLF,Empty\r\n")
    //     f.write('"",,"""","""""","\n","\r\n","\r\n\r\n",\r\n')


    FILE *fp = fopen("./data/csv_file_edge_cases.csv", "r");
    assert_true(fp);

    {
        char *header_string = GetCsvLineNext(fp);
        char *data_string = GetCsvLineNext(fp);

        assert_true(header_string != NULL);
        assert_true(data_string != NULL);

        assert_string_equal(header_string, "Empty,Empty,One double quote,Two double quotes,LF,CRLF,CRLFCRLF,Empty\r\n");
        assert_string_equal(data_string,   "\"\",,\"\"\"\",\"\"\"\"\"\",\"\n\",\"\r\n\",\"\r\n\r\n\",\r\n");

        Seq *header_list = SeqParseCsvString(header_string);
        Seq *data_list = SeqParseCsvString(data_string);
        assert_true(header_list != NULL);
        assert_true(data_list != NULL);
        assert_int_equal(SeqLength(header_list), 8);
        assert_int_equal(SeqLength(data_list), 8);

        assert_string_equal(SeqAt(header_list, 0), "Empty");
        assert_string_equal(SeqAt(data_list,   0), "");
        assert_string_equal(SeqAt(header_list, 1), "Empty");
        assert_string_equal(SeqAt(data_list,   1), "");
        assert_string_equal(SeqAt(header_list, 2), "One double quote");
        assert_string_equal(SeqAt(data_list,   2), "\"");
        assert_string_equal(SeqAt(header_list, 3), "Two double quotes");
        assert_string_equal(SeqAt(data_list,   3), "\"\"");
        assert_string_equal(SeqAt(header_list, 4), "LF");
        assert_string_equal(SeqAt(data_list,   4), "\n");
        assert_string_equal(SeqAt(header_list, 5), "CRLF");
        assert_string_equal(SeqAt(data_list,   5), "\r\n");
        assert_string_equal(SeqAt(header_list, 6), "CRLFCRLF");
        assert_string_equal(SeqAt(data_list,   6), "\r\n\r\n");
        assert_string_equal(SeqAt(header_list, 7), "Empty");
        assert_string_equal(SeqAt(data_list,   7), "");

        SeqDestroy(header_list);
        SeqDestroy(data_list);
        free(header_string);
        free(data_string);
    }

    fclose(fp);
}

/* A memory corruption (off-by-one write) used to occur with this string,
 * detectable by valgrind and crashing some non-linux platforms. */
static void test_new_csv_reader_zd3151_ENT3023()
{
    const char *input = "default.cfe_autorun_inventory_proc.cpuinfo_array[337][0";
    Seq *list = SeqParseCsvString(input);
    assert_int_equal(SeqLength(list), 1);
    assert_string_equal(input, SeqAt(list, 0));
    SeqDestroy(list);
}

static void test_new_csv_reader_carriage_return()
{
    const char *inputs[] =
    {
        "field_1,stuff,values are here\r\n",
        "field_2,empty,\r\n",
        "field_3,more stuff,more values are here\r\n",
        "field_4,,\r\n",
    };

    // First entry
    Seq *list = SeqParseCsvString(inputs[0]);
    assert_int_equal(SeqLength(list), 3);
    assert_string_equal(SeqAt(list, 2), "values are here");
    SeqDestroy(list);

    // Second entry
    list = SeqParseCsvString(inputs[1]);
    assert_int_equal(SeqLength(list), 3);
    assert_string_equal(SeqAt(list, 2), "");
    SeqDestroy(list);

    // Third entry
    list = SeqParseCsvString(inputs[2]);
    assert_int_equal(SeqLength(list), 3);
    assert_string_equal(SeqAt(list, 2), "more values are here");
    SeqDestroy(list);

    // Fourth entry
    list = SeqParseCsvString(inputs[3]);
    assert_int_equal(SeqLength(list), 3);
    assert_string_equal(SeqAt(list, 2), "");
    SeqDestroy(list);
}

int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_new_csv_reader_empty_string),
        unit_test(test_new_csv_reader_basic),
        unit_test(test_new_csv_reader),
        unit_test(test_new_csv_reader_lfln),
        unit_test(test_new_csv_reader_lfln_at_end),
        unit_test(test_new_csv_reader_lfln_at_end2),
        unit_test(test_new_csv_reader_lfln_at_end3),
        unit_test(test_get_next_line),
        unit_test(test_get_next_line_edge_cases),
        unit_test(test_new_csv_reader_zd3151_ENT3023),
        unit_test(test_new_csv_reader_carriage_return),
    };

    return run_tests(tests);
}