diff options
Diffstat (limited to 'lib/x1000-installer/test/test_stream.c')
-rw-r--r-- | lib/x1000-installer/test/test_stream.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/x1000-installer/test/test_stream.c b/lib/x1000-installer/test/test_stream.c new file mode 100644 index 0000000000..b4a5723b76 --- /dev/null +++ b/lib/x1000-installer/test/test_stream.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "test.h" | ||
23 | #include "xf_stream.h" | ||
24 | #include "xf_error.h" | ||
25 | #include "file.h" | ||
26 | #include <string.h> | ||
27 | #include <stdbool.h> | ||
28 | #include <stdio.h> | ||
29 | |||
30 | #define TESTDATADIR "test/data/" | ||
31 | #define TESTFILE_SHUFFLED TESTDATADIR "lines_shuffled.txt" | ||
32 | #define TESTFILE_SORTED TESTDATADIR "lines_sorted.txt" | ||
33 | |||
34 | const char line_string[] = "1234567890abcdefghijklmnopqrstuvwxyz"; | ||
35 | |||
36 | static int read_line_cb(int n, char* buf, void* arg) | ||
37 | { | ||
38 | int* max_n = (int*)arg; | ||
39 | *max_n = n; | ||
40 | |||
41 | T_ASSERT(strlen(buf) > 0); | ||
42 | T_ASSERT(strncmp(line_string, buf, strlen(buf)) == 0); | ||
43 | |||
44 | cleanup: | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | void test_stream_read_lines(void) | ||
49 | { | ||
50 | struct xf_stream s; | ||
51 | int rc; | ||
52 | char buf[100]; | ||
53 | bool s_open = false; | ||
54 | |||
55 | for(int bufsz = 38; bufsz <= 100; bufsz += 1) { | ||
56 | rc = xf_open_file(TESTFILE_SHUFFLED, O_RDONLY, &s); | ||
57 | T_ASSERT(rc == 0); | ||
58 | s_open = true; | ||
59 | |||
60 | int max_n = 0; | ||
61 | rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n); | ||
62 | T_ASSERT(rc == 0); | ||
63 | T_ASSERT(max_n+1 == 108); | ||
64 | |||
65 | xf_stream_close(&s); | ||
66 | s_open = false; | ||
67 | } | ||
68 | |||
69 | cleanup: | ||
70 | if(s_open) | ||
71 | xf_stream_close(&s); | ||
72 | return; | ||
73 | } | ||
74 | |||
75 | void test_stream_read_line_too_long(void) | ||
76 | { | ||
77 | struct xf_stream s; | ||
78 | int rc; | ||
79 | char buf[38]; | ||
80 | bool s_open = false; | ||
81 | |||
82 | for(int bufsz = 0; bufsz <= 38; bufsz += 1) { | ||
83 | rc = xf_open_file(TESTFILE_SORTED, O_RDONLY, &s); | ||
84 | T_ASSERT(rc == 0); | ||
85 | s_open = true; | ||
86 | |||
87 | int max_n = -1; | ||
88 | rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n); | ||
89 | if(bufsz == 38) { | ||
90 | T_ASSERT(rc == 0); | ||
91 | T_ASSERT(max_n+1 == 36); | ||
92 | } else { | ||
93 | T_ASSERT(rc == XF_E_LINE_TOO_LONG); | ||
94 | if(bufsz <= 1) | ||
95 | T_ASSERT(max_n == -1); | ||
96 | else | ||
97 | T_ASSERT(max_n+1 == bufsz-2); | ||
98 | } | ||
99 | |||
100 | xf_stream_close(&s); | ||
101 | s_open = false; | ||
102 | } | ||
103 | |||
104 | cleanup: | ||
105 | if(s_open) | ||
106 | xf_stream_close(&s); | ||
107 | return; | ||
108 | } | ||