]>
git.scottworley.com Git - overonion/blob - reverse_test.c
fe35bcc303a04330c55e7bbd890f5fde8b590efb
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 reverse_file(input_file
, fd
);
17 if (close(fd
) == -1) err(EX_IOERR
, "Couldn't close temporary file");
21 void test_reverse_twice_is_the_same() {
22 char* intermediate
= reverse_to_temp_file(test_file
);
23 char* back_to_normal
= reverse_to_temp_file(intermediate
);
25 FILE* f1
= fopen(test_file
, "r");
26 if (f1
== NULL
) err(EX_IOERR
, "Couldn't open test file");
27 FILE* f2
= fopen(back_to_normal
, "r");
28 if (f2
== NULL
) err(EX_IOERR
, "Couldn't open twice-reversed temp file");
30 for (int pos
= 0; ; pos
++) {
33 if (c1
== EOF
&& ferror(f1
)) err(EX_IOERR
, "Error reading test file");
34 if (c2
== EOF
&& ferror(f2
)) err(EX_IOERR
, "Error reading twice-reversed temp file");
36 errx(EX_SOFTWARE
, "Unexpected difference found at offset %d after reversing twice. "
37 "Expected %d but found %d", pos
, c1
, c2
);
42 if (fclose(f1
) != 0) err(EX_IOERR
, "Couldn't close test file");
43 if (fclose(f2
) != 0) err(EX_IOERR
, "Couldn't close twice-reversed temp file");
44 if (unlink(intermediate
) == -1) err(EX_IOERR
, "Couldn't remove intermediate temp file");
45 if (unlink(back_to_normal
) == -1) err(EX_IOERR
, "Couldn't remove twice-reversed temp file");
51 test_reverse_twice_is_the_same();