]>
git.scottworley.com Git - overonion/blob - reverse_test.c
1 #include "reverse_lib.h"
11 /* Reverse input_file. Dump the result in a temporary file. Return the temp
12 * file's name. The caller must free the filename. */
13 char* reverse_to_temp_file(const char* input_file
) {
16 make_temporary_file(&temp_filename
, &f
);
17 reverse_file(input_file
, f
);
18 if (fclose(f
) == EOF
) err(EX_IOERR
, "Couldn't close temporary file");
22 /* Compare the contents of two files. Terminate with a diagnostic if they
24 void compare(const char* filename1
, const char* filename2
) {
25 FILE* f1
= fopen(filename1
, "r");
26 if (f1
== NULL
) err(EX_IOERR
, "Couldn't open file %s", filename1
);
27 FILE* f2
= fopen(filename2
, "r");
28 if (f2
== NULL
) err(EX_IOERR
, "Couldn't open file %s", filename2
);
30 for (int pos
= 0; ; pos
++) {
33 if (c1
== EOF
&& ferror(f1
)) err(EX_IOERR
, "Error reading file %s", filename1
);
34 if (c2
== EOF
&& ferror(f2
)) err(EX_IOERR
, "Error reading file %s", filename2
);
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 file %s", filename1
);
43 if (fclose(f2
) != 0) err(EX_IOERR
, "Couldn't close file %s", filename2
);
46 void test_reverse_twice_from_files_is_the_same(const char* test_filename
) {
47 char* intermediate
= reverse_to_temp_file(test_filename
);
48 char* back_to_normal
= reverse_to_temp_file(intermediate
);
50 compare(test_filename
, back_to_normal
);
52 if (unlink(intermediate
) == -1) err(EX_IOERR
, "Couldn't remove intermediate temp file");
53 if (unlink(back_to_normal
) == -1) err(EX_IOERR
, "Couldn't remove twice-reversed temp file");
58 void test_reverse_from_file_then_from_stream_is_the_same(const char* test_filename
) {
60 if (pipe(pipefd
) == -1) err(EX_OSERR
, "Couldn't create pipe");
63 if (pid
== -1) err(EX_OSERR
, "Couldn't fork");
65 if (close(pipefd
[0]) == -1) err(EX_OSERR
, "Couldn't close unneeded pipe descriptor");
66 FILE* to_second
= fdopen(pipefd
[1], "w");
67 if (to_second
== NULL
) err(EX_IOERR
, "Couldn't open pipe for writing");
68 reverse_file(test_filename
, to_second
);
71 if (close(pipefd
[1]) == -1) err(EX_OSERR
, "Couldn't close unneeded pipe descriptor");
72 FILE* from_first
= fdopen(pipefd
[0], "r");
73 if (from_first
== NULL
) err(EX_IOERR
, "Couldn't open pipe for reading");
74 char* out_temp_filename
;
76 make_temporary_file(&out_temp_filename
, &out_file
);
77 reverse_stream(from_first
, out_file
);
78 if (fclose(out_file
) == EOF
) err(EX_IOERR
, "Couldn't close temporary file");
80 compare(test_filename
, out_temp_filename
);
82 if (unlink(out_temp_filename
) == -1) err(EX_IOERR
, "Couldn't remove temp output file");
83 free(out_temp_filename
);
86 int main(int argc
, char** argv
) {
87 char* test_filename
= "reverse.c";
89 errx(EX_USAGE
, "usage: reverse_test [datafile]");
90 } else if (argc
== 2) {
91 test_filename
= argv
[1];
94 test_reverse_twice_from_files_is_the_same(test_filename
);
95 test_reverse_from_file_then_from_stream_is_the_same(test_filename
);