]>
git.scottworley.com Git - overonion/blob - reverse_test.c
1 #include "reverse_lib.h"
10 static const char test_file
[] = "reverse.c";
12 char* reverse_to_temp_file(const char* input_file
) {
13 char* temp_filename
= strdup("/tmp/reverse_test.XXXXXX");
14 int fd
= mkstemp(temp_filename
);
15 if (fd
== -1) err(EX_IOERR
, "Couldn't make a temporary file");
16 FILE* f
= fdopen(fd
, "w");
17 if (f
== NULL
) err(EX_IOERR
, "Couldn't open temporary file");
18 reverse_file(input_file
, f
);
19 if (fclose(f
) == EOF
) err(EX_IOERR
, "Couldn't close temporary file");
23 void test_reverse_twice_is_the_same() {
24 char* intermediate
= reverse_to_temp_file(test_file
);
25 char* back_to_normal
= reverse_to_temp_file(intermediate
);
27 FILE* f1
= fopen(test_file
, "r");
28 if (f1
== NULL
) err(EX_IOERR
, "Couldn't open test file");
29 FILE* f2
= fopen(back_to_normal
, "r");
30 if (f2
== NULL
) err(EX_IOERR
, "Couldn't open twice-reversed temp file");
32 for (int pos
= 0; ; pos
++) {
35 if (c1
== EOF
&& ferror(f1
)) err(EX_IOERR
, "Error reading test file");
36 if (c2
== EOF
&& ferror(f2
)) err(EX_IOERR
, "Error reading twice-reversed temp file");
38 errx(EX_SOFTWARE
, "Unexpected difference found at offset %d after reversing twice. "
39 "Expected %d but found %d", pos
, c1
, c2
);
44 if (fclose(f1
) != 0) err(EX_IOERR
, "Couldn't close test file");
45 if (fclose(f2
) != 0) err(EX_IOERR
, "Couldn't close twice-reversed temp file");
46 if (unlink(intermediate
) == -1) err(EX_IOERR
, "Couldn't remove intermediate temp file");
47 if (unlink(back_to_normal
) == -1) err(EX_IOERR
, "Couldn't remove twice-reversed temp file");
53 test_reverse_twice_is_the_same();