]> git.draconx.ca Git - dxcommon.git/blob - t/getline.c
Import getline helper from cdecl99.
[dxcommon.git] / t / getline.c
1 /*
2  * Copyright © 2024 Nick Bowler
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 void tap_diag(const char *fmt, ...);
19
20 #undef HAVE_GETLINE
21 #define DX_GETLINE_INITIAL_ALLOC 2
22 #include "getline.h"
23 #include "tap.h"
24
25 #include <errno.h>
26
27 #define LONGLINE "this line is pretty long and may require scaling the buffer multiple times"
28
29 static const char testdata[] =
30         "\n"
31         LONGLINE "\n"
32         LONGLINE "\n"
33         LONGLINE "\n"
34         "hellorld\n";
35
36 int main(void)
37 {
38         char *line = NULL;
39         size_t long_size;
40         size_t n = 0;
41         FILE *f;
42
43         if (!(f = tmpfile()))
44                 tap_bail_out("tmpfile failed: %s\n", strerror(errno));
45
46         if (fwrite(testdata, sizeof testdata-1, 1, f) != 1)
47                 tap_bail_out("fwrite failed: %s\n", strerror(errno));
48
49         if (fflush(f) != 0)
50                 tap_bail_out("fflush failed: %s\n", strerror(errno));
51
52         if (fseek(f, 0, SEEK_SET) != 0)
53                 tap_bail_out("fseek failed: %s\n", strerror(errno));
54
55         /* First line: empty */
56         tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
57         if (!tap_result(n == 2, "alloc size")) {
58                 tap_diag("Failed, unexpected result");
59                 tap_diag("   Received: %lu", (unsigned long)n);
60                 tap_diag("   Expected: 2");
61         }
62         if (!tap_result(line[0] == 0, "returned string")) {
63                 tap_diag("Failed, unexpected result");
64                 tap_diag("   Received: \"%.*s\"", (int)n, line);
65                 tap_diag("   Expected: \"\"");
66         }
67
68         /* Next line: long line (buffer needs expanding) */
69         tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
70         if (!tap_result(n > sizeof LONGLINE, "alloc size")) {
71                 tap_diag("Failed, unexpected result");
72                 tap_diag("   Received: %lu", (unsigned long)n);
73                 tap_diag("   Expected: >%u", (unsigned)(sizeof LONGLINE));
74         }
75         if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
76                 tap_diag("Failed, unexpected result");
77                 tap_diag("   Received: \"%.*s\"", (int)n, line);
78                 tap_diag("   Expected: \"" LONGLINE "\"");
79         }
80
81         /* Next line: long line (buffer does not need expanding) */
82         long_size = n;
83         tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
84         if (!tap_result(n == long_size, "alloc size")) {
85                 tap_diag("Failed, unexpected result");
86                 tap_diag("   Received: %lu", (unsigned long)n);
87                 tap_diag("   Expected: %lu", (unsigned long)long_size);
88         }
89         if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
90                 tap_diag("Failed, unexpected result");
91                 tap_diag("   Received: \"%.*s\"", (int)n, line);
92                 tap_diag("   Expected: \"" LONGLINE "\"");
93         }
94
95         /* Next line: long line (new buffer allocation) */
96         free(line); line = NULL; n = 0;
97         tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
98         if (!tap_result(n == long_size, "alloc size")) {
99                 tap_diag("Failed, unexpected result");
100                 tap_diag("   Received: %lu", (unsigned long)n);
101                 tap_diag("   Expected: %lu", (unsigned long)long_size);
102         }
103         if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
104                 tap_diag("Failed, unexpected result");
105                 tap_diag("   Received: \"%.*s\"", (int)n, line);
106                 tap_diag("   Expected: \"" LONGLINE "\"");
107         }
108
109         /* Next line: short line */
110         tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
111         if (!tap_result(n == long_size, "alloc size")) {
112                 tap_diag("Failed, unexpected result");
113                 tap_diag("   Received: %lu", (unsigned long)n);
114                 tap_diag("   Expected: %lu", (unsigned long)long_size);
115         }
116         if (!tap_result(!strcmp(line, "hellorld"), "returned string")) {
117                 tap_diag("Failed, unexpected result");
118                 tap_diag("   Received: \"%.*s\"", (int)n, line);
119                 tap_diag("   Expected: \"hellorld\"");
120         }
121
122         tap_done();
123 }