2 * Copyright © 2024 Nick Bowler
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.
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.
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/>.
18 void tap_diag(const char *fmt, ...);
21 #define DX_GETLINE_INITIAL_ALLOC 2
27 #define LONGLINE "this line is pretty long and may require scaling the buffer multiple times"
29 static const char testdata[] =
44 tap_bail_out("tmpfile failed: %s\n", strerror(errno));
46 if (fwrite(testdata, sizeof testdata-1, 1, f) != 1)
47 tap_bail_out("fwrite failed: %s\n", strerror(errno));
50 tap_bail_out("fflush failed: %s\n", strerror(errno));
52 if (fseek(f, 0, SEEK_SET) != 0)
53 tap_bail_out("fseek failed: %s\n", strerror(errno));
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");
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: \"\"");
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));
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 "\"");
81 /* Next line: long line (buffer does not need expanding) */
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);
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 "\"");
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);
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 "\"");
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);
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\"");