| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
#include <stdio.h> |
|---|
| 15 |
#include "../z-file.h" |
|---|
| 16 |
#include "../z-tests.h" |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
static const char *test_fname = "./test"; |
|---|
| 20 |
|
|---|
| 21 |
TEST(file_open) |
|---|
| 22 |
{ |
|---|
| 23 |
ang_file *f; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
f = file_open(test_fname, MODE_WRITE, FTYPE_TEXT); |
|---|
| 27 |
if (!f) FAIL; |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
if (!file_close(f)) FAIL; |
|---|
| 31 |
} |
|---|
| 32 |
TEST_END |
|---|
| 33 |
|
|---|
| 34 |
TEST(file_exists) |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
if (!file_exists(test_fname)) FAIL; |
|---|
| 38 |
} |
|---|
| 39 |
TEST_END |
|---|
| 40 |
|
|---|
| 41 |
TEST(file_lock) |
|---|
| 42 |
{ |
|---|
| 43 |
|
|---|
| 44 |
void file_lock(ang_file *f); |
|---|
| 45 |
void file_unlock(ang_file *f); |
|---|
| 46 |
} |
|---|
| 47 |
TEST_END |
|---|
| 48 |
|
|---|
| 49 |
TEST(file_rws) |
|---|
| 50 |
{ |
|---|
| 51 |
ang_file *f; |
|---|
| 52 |
char buffer[1024]; |
|---|
| 53 |
char comparison_buffer[1024]; |
|---|
| 54 |
size_t i; |
|---|
| 55 |
u32b seek_positions[] = { 5, 18, 500, 95, 1023 }; |
|---|
| 56 |
bool success; |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
for (i = 0; i < sizeof(buffer); i++) |
|---|
| 60 |
buffer[i] = (char) i * 3; |
|---|
| 61 |
|
|---|
| 62 |
printf("opening, "); |
|---|
| 63 |
f = file_open(test_fname, MODE_WRITE, FTYPE_TEXT); |
|---|
| 64 |
if (!f) FAIL; |
|---|
| 65 |
|
|---|
| 66 |
printf("writing, "); |
|---|
| 67 |
success = file_write(f, buffer, sizeof(buffer)); |
|---|
| 68 |
file_close(f); |
|---|
| 69 |
if (!success) FAIL; |
|---|
| 70 |
|
|---|
| 71 |
printf("reopening, "); |
|---|
| 72 |
f = file_open(test_fname, MODE_READ, -1); |
|---|
| 73 |
if (!f) FAIL; |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
printf("reading, "); |
|---|
| 77 |
file_read(f, comparison_buffer, sizeof(buffer)); |
|---|
| 78 |
for (i = 0; i < sizeof(buffer); i++) |
|---|
| 79 |
{ |
|---|
| 80 |
if (buffer[i] != comparison_buffer[i]) |
|---|
| 81 |
FAIL; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
printf("seeking "); |
|---|
| 86 |
for (i = 0; i < N_ELEMENTS(seek_positions); i++) |
|---|
| 87 |
{ |
|---|
| 88 |
u32b pos = seek_positions[i]; |
|---|
| 89 |
byte b; |
|---|
| 90 |
|
|---|
| 91 |
printf("%d", i); |
|---|
| 92 |
if (!file_seek(f, pos)) |
|---|
| 93 |
{ |
|---|
| 94 |
file_close(f); |
|---|
| 95 |
FAIL; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
printf("A"); |
|---|
| 99 |
if (!file_readc(f, &b)) |
|---|
| 100 |
{ |
|---|
| 101 |
file_close(f); |
|---|
| 102 |
FAIL; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
printf("B "); |
|---|
| 106 |
if ((char)b != buffer[pos]) |
|---|
| 107 |
{ |
|---|
| 108 |
printf("was %d, should be %d", (char)b, (char)buffer[pos]); |
|---|
| 109 |
file_close(f); |
|---|
| 110 |
FAIL; |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
TEST_END |
|---|
| 115 |
|
|---|
| 116 |
TEST(file_chario) |
|---|
| 117 |
{ |
|---|
| 118 |
size_t i; |
|---|
| 119 |
ang_file *f; |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
printf("opening, "); |
|---|
| 123 |
f = file_open(test_fname, MODE_WRITE, FTYPE_TEXT); |
|---|
| 124 |
if (!f) FAIL; |
|---|
| 125 |
|
|---|
| 126 |
for (i = 0; i < 25; i++) |
|---|
| 127 |
{ |
|---|
| 128 |
if (!file_writec(f, (byte)i)) |
|---|
| 129 |
{ |
|---|
| 130 |
file_close(f); |
|---|
| 131 |
FAIL; |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
file_close(f); |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
printf("reopening, "); |
|---|
| 138 |
f = file_open(test_fname, MODE_READ, FTYPE_TEXT); |
|---|
| 139 |
if (!f) FAIL; |
|---|
| 140 |
|
|---|
| 141 |
for (i = 0; i < 25; i++) |
|---|
| 142 |
{ |
|---|
| 143 |
byte ch; |
|---|
| 144 |
|
|---|
| 145 |
if (!file_readc(f, &ch)) |
|---|
| 146 |
{ |
|---|
| 147 |
file_close(f); |
|---|
| 148 |
FAIL; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
if ((char)ch != i) |
|---|
| 152 |
{ |
|---|
| 153 |
file_close(f); |
|---|
| 154 |
FAIL; |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
TEST_END |
|---|
| 159 |
|
|---|
| 160 |
TEST(file_lineio) |
|---|
| 161 |
{ |
|---|
| 162 |
ang_file *f; |
|---|
| 163 |
size_t i; |
|---|
| 164 |
const char *lineendings[] = { "\n", "\r", "\r\n" }; |
|---|
| 165 |
const char *text[] = |
|---|
| 166 |
{ |
|---|
| 167 |
"'Twas brillig, and the slithy toves", |
|---|
| 168 |
" Did gyre and gimble in the wabe", |
|---|
| 169 |
"All mimsy were the borogoves", |
|---|
| 170 |
" And the mome raths outgrabe", |
|---|
| 171 |
"", |
|---|
| 172 |
"Beware the Jabberwock, my son!", |
|---|
| 173 |
" The jaws that bite, the claws that catch!", |
|---|
| 174 |
"Beware the Jubjub bird, and shun", |
|---|
| 175 |
" The frumious Bandersnatch!", |
|---|
| 176 |
}; |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
printf("opening, "); |
|---|
| 180 |
f = file_open(test_fname, MODE_WRITE, FTYPE_TEXT); |
|---|
| 181 |
if (!f) FAIL; |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
printf("writing, "); |
|---|
| 185 |
for (i = 0; i < N_ELEMENTS(text); i++) |
|---|
| 186 |
{ |
|---|
| 187 |
if (!file_put(f, text[i])) |
|---|
| 188 |
{ |
|---|
| 189 |
file_close(f); |
|---|
| 190 |
FAIL; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
if (!file_put(f, lineendings[i % N_ELEMENTS(lineendings)])) |
|---|
| 194 |
{ |
|---|
| 195 |
file_close(f); |
|---|
| 196 |
FAIL; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
file_close(f); |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
printf("reopening, "); |
|---|
| 203 |
f = file_open(test_fname, MODE_READ, FTYPE_TEXT); |
|---|
| 204 |
if (!f) FAIL; |
|---|
| 205 |
|
|---|
| 206 |
printf("reading, "); |
|---|
| 207 |
for (i = 0; i < N_ELEMENTS(text); i++) |
|---|
| 208 |
{ |
|---|
| 209 |
char buf[1024]; |
|---|
| 210 |
printf("%d", i); |
|---|
| 211 |
if (!file_getl(f, buf, sizeof(buf))) |
|---|
| 212 |
{ |
|---|
| 213 |
file_close(f); |
|---|
| 214 |
FAIL; |
|---|
| 215 |
} |
|---|
| 216 |
printf("B "); |
|---|
| 217 |
if (strcmp(buf, text[i])) |
|---|
| 218 |
{ |
|---|
| 219 |
printf("%s %s", buf, text[i]); |
|---|
| 220 |
file_close(f); |
|---|
| 221 |
FAIL; |
|---|
| 222 |
} |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
file_close(f); |
|---|
| 226 |
} |
|---|
| 227 |
TEST_END |
|---|
| 228 |
|
|---|
| 229 |
TEST(file_move) |
|---|
| 230 |
{ |
|---|
| 231 |
printf("move, "); |
|---|
| 232 |
if (!file_move(test_fname, "test2")) FAIL; |
|---|
| 233 |
printf("check, "); |
|---|
| 234 |
if (!file_exists("test2")) FAIL; |
|---|
| 235 |
printf("move, "); |
|---|
| 236 |
if (!file_move("test2", test_fname)) FAIL; |
|---|
| 237 |
printf("check, "); |
|---|
| 238 |
if (!file_exists(test_fname)) FAIL; |
|---|
| 239 |
} |
|---|
| 240 |
TEST_END |
|---|
| 241 |
|
|---|
| 242 |
TEST(file_newer) |
|---|
| 243 |
{ |
|---|
| 244 |
if (!file_exists("z-file.c")) |
|---|
| 245 |
{ |
|---|
| 246 |
printf("need z-file.c "); |
|---|
| 247 |
FAIL; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
if (!file_exists(test_fname)) |
|---|
| 251 |
{ |
|---|
| 252 |
printf("need %s ", test_fname); |
|---|
| 253 |
FAIL; |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
printf("testing existent files 1 "); |
|---|
| 257 |
if (file_newer("z-file.c", test_fname)) FAIL; |
|---|
| 258 |
printf("2 "); |
|---|
| 259 |
if (!file_newer(test_fname, "z-file.c")) FAIL; |
|---|
| 260 |
|
|---|
| 261 |
printf("testing nonexistent files 1 "); |
|---|
| 262 |
if (file_newer("nonexistent", "z-file.c")) FAIL; |
|---|
| 263 |
printf("2 "); |
|---|
| 264 |
if (!file_newer("z-file.c", "nonexistent")) FAIL; |
|---|
| 265 |
} |
|---|
| 266 |
TEST_END |
|---|
| 267 |
|
|---|
| 268 |
TEST(file_delete) |
|---|
| 269 |
{ |
|---|
| 270 |
if (!file_delete(test_fname)) FAIL; |
|---|
| 271 |
if (file_exists(test_fname)) FAIL; |
|---|
| 272 |
} |
|---|
| 273 |
TEST_END |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
TEST(path_build) |
|---|
| 277 |
{ |
|---|
| 278 |
char buf[1024]; |
|---|
| 279 |
|
|---|
| 280 |
path_build(buf, sizeof(buf), "some" PATH_SEP "path", "extra"); |
|---|
| 281 |
if (strcmp(buf, "some" PATH_SEP "path" PATH_SEP "extra")) FAIL; |
|---|
| 282 |
|
|---|
| 283 |
path_build(buf, sizeof(buf), "", "leaf"); |
|---|
| 284 |
if (strcmp(buf, "leaf")) FAIL; |
|---|
| 285 |
|
|---|
| 286 |
path_build(buf, sizeof(buf), "base", ""); |
|---|
| 287 |
if (strcmp(buf, "base")) FAIL; |
|---|
| 288 |
|
|---|
| 289 |
path_build(buf, sizeof(buf), "somepath", PATH_SEP "absolutepath"); |
|---|
| 290 |
if (strcmp(buf, PATH_SEP "absolutepath")) FAIL; |
|---|
| 291 |
|
|---|
| 292 |
#ifdef SET_UID |
|---|
| 293 |
path_build(buf, sizeof(buf), "~", ""); |
|---|
| 294 |
if (!strcmp(buf, "~")) FAIL; |
|---|
| 295 |
|
|---|
| 296 |
path_build(buf, sizeof(buf), "~" PATH_SEP "dull", ""); |
|---|
| 297 |
if (!strcmp(buf, "~/dull")) FAIL; |
|---|
| 298 |
if (!suffix(buf, PATH_SEP "dull")) FAIL; |
|---|
| 299 |
|
|---|
| 300 |
path_build(buf, sizeof(buf), "~ajps", ""); |
|---|
| 301 |
if (!strcmp(buf, "~ajps")) FAIL; |
|---|
| 302 |
|
|---|
| 303 |
path_build(buf, sizeof(buf), "somepath", "~"); |
|---|
| 304 |
if (!strcmp(buf, "somepath")) FAIL; |
|---|
| 305 |
#endif |
|---|
| 306 |
} |
|---|
| 307 |
TEST_END |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
int main(void) |
|---|
| 311 |
{ |
|---|
| 312 |
TEST_PLAN(test, 10); |
|---|
| 313 |
|
|---|
| 314 |
TEST_RUN(&test, file_open); |
|---|
| 315 |
TEST_RUN(&test, file_exists); |
|---|
| 316 |
TEST_RUN(&test, file_lock); |
|---|
| 317 |
TEST_RUN(&test, file_rws); |
|---|
| 318 |
TEST_RUN(&test, file_chario); |
|---|
| 319 |
TEST_RUN(&test, file_lineio); |
|---|
| 320 |
TEST_RUN(&test, file_move); |
|---|
| 321 |
TEST_RUN(&test, file_newer); |
|---|
| 322 |
TEST_RUN(&test, file_delete); |
|---|
| 323 |
|
|---|
| 324 |
TEST_RUN(&test, path_build); |
|---|
| 325 |
|
|---|
| 326 |
TESTS_COMPLETE(test); |
|---|
| 327 |
} |
|---|