+void test_reverse_from_file_then_from_stream_is_the_same() {
+ int pipefd[2];
+ if (pipe(pipefd) == -1) err(EX_OSERR, "Couldn't create pipe");
+
+ pid_t pid = fork();
+ if (pid == -1) err(EX_OSERR, "Couldn't fork");
+ if (pid == 0) {
+ if (close(pipefd[0]) == -1) err(EX_OSERR, "Couldn't close unneeded pipe descriptor");
+ FILE* to_second = fdopen(pipefd[1], "w");
+ if (to_second == NULL) err(EX_IOERR, "Couldn't open pipe for writing");
+ reverse_file(test_file, to_second);
+ exit(0);
+ }
+ if (close(pipefd[1]) == -1) err(EX_OSERR, "Couldn't close unneeded pipe descriptor");
+ FILE* from_first = fdopen(pipefd[0], "r");
+ if (from_first == NULL) err(EX_IOERR, "Couldn't open pipe for reading");
+ char* out_temp_filename = strdup("/tmp/reverse_test.XXXXXX");
+ int out_fd = mkstemp(out_temp_filename);
+ if (out_fd == -1) err(EX_IOERR, "Couldn't make a temporary file");
+ FILE* out_file = fdopen(out_fd, "w");
+ if (out_file == NULL) err(EX_IOERR, "Couldn't open temporary file");
+ reverse_stream(from_first, out_file);
+ if (fclose(out_file) == EOF) err(EX_IOERR, "Couldn't close temporary file");
+
+ compare(test_file, out_temp_filename);
+
+ if (unlink(out_temp_filename) == -1) err(EX_IOERR, "Couldn't remove temp output file");
+ free(out_temp_filename);
+}
+