-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
181 lines (141 loc) · 4.53 KB
/
tests.c
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
/*
MIT License
Copyright (c) 2022 Baldwin, Josiah
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// For reference
// https://www.gnu.org/software/libc/manual/html_mono/libc.html#Line-Input
#include <acutest.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "getline.h"
#define LINE_LEN 85
void
getline_basic(void) {
char exp[] = "Tiwlers\n";
size_t line_len = LINE_LEN;
ssize_t chars_read;
char* out = malloc(line_len);
FILE* tmp_out = tmpfile();
fputs(exp, tmp_out);
rewind(tmp_out);
chars_read = getline(&out, &line_len, tmp_out);
TEST_CHECK(strcmp(exp, out) == 0);
TEST_DUMP("Expected:", exp, sizeof exp);
TEST_DUMP("Produced:", out, strlen(out));
TEST_CHECK(chars_read == (sizeof exp - 1));
TEST_MSG("getline returned %ld, rather than %ld", chars_read, (sizeof exp - 1));
fclose(tmp_out);
free(out);
}
void
getline_fails_with_no_input(void) {
size_t line_len = LINE_LEN;
char* out = malloc(line_len);
FILE* tmp_out = tmpfile();
ssize_t ret_code = getline(&out, &line_len, tmp_out);
TEST_CHECK(ret_code == -1);
TEST_MSG("getline did not return -1 for empty input.\n"
"returned %ld.", ret_code);
fclose(tmp_out);
free(out);
}
void
getline_reallocates(void) {
char original[] = "aaaaaaaaaaaaaaaaaaaaaaa";
size_t line_len = 5;
char* out = malloc(line_len);
FILE* file = tmpfile();
fputs(original, file);
rewind(file);
getline(&out, &line_len, file);
TEST_CHECK(line_len >= sizeof original);
TEST_MSG("Orig %zd, dest %zd", sizeof original, line_len);
fclose(file);
free(out);
}
void
getline_larger_then_intern_buffer(void) {
char source[200];
for (unsigned i = 0; i < sizeof source; i++)
source[i] = 'c';
source[sizeof source - 2] = '\n';
source[sizeof source - 1] = '\0';
size_t line_len = LINE_LEN;
char* out = malloc(line_len);
FILE* file = tmpfile();
fputs(source, file);
rewind(file);
getline(&out, &line_len, file);
TEST_CHECK(strcmp(source, out) == 0);
TEST_MSG("Src: %s : dest: %s", source, out);
fclose(file);
free(out);
}
void
getline_allocates_on_null(void) {
char source[] = "pizza pizza pizza pizzzzzzzzzzzaeps";
size_t line_len = 0;
char* out = NULL;
FILE* file = tmpfile();
fputs(source, file);
rewind(file);
getline(&out, &line_len, file);
TEST_CHECK(strcmp(source, out) == 0);
TEST_MSG("Src: %s : dest: %s", source, out);
fclose(file);
free(out);
}
void
getline_gives_error_lineptr_null_but_n_not_0(void) {
char source[] = "mike";
size_t line_len = 666;
char* out = NULL;
FILE* file = tmpfile();
fputs(source, file);
rewind(file);
intmax_t result = getline(&out, &line_len, file);
TEST_CHECK(result < 0);
TEST_MSG("result %p (%" PRIdMAX ")", out, result);
fclose(file);
}
void
getline_gives_error_n_0_but_lineptr_not_null(void) {
char source[] = "mike";
size_t line_len = 0;
char* out = (char*)0x1;
FILE* file = tmpfile();
fputs(source, file);
rewind(file);
intmax_t result = getline(&out, &line_len, file);
TEST_CHECK(result < 0);
TEST_MSG("result %p (%" PRIdMAX ")", out, result);
fclose(file);
}
TEST_LIST = {
{ "Basic usage", getline_basic },
{ "getline fails for empty file input", getline_fails_with_no_input },
{ "getline reallocs if dest buffer to small", getline_reallocates },
{ "getline works for string larger than internal buffer", getline_larger_then_intern_buffer },
{ "Allocates when passed null + 0", getline_allocates_on_null },
{ "getline fails when passed null but !0", getline_gives_error_lineptr_null_but_n_not_0 },
{ "getline fails when passed 0 but not null", getline_gives_error_n_0_but_lineptr_not_null },
{ NULL, NULL },
};