]> git.scottworley.com Git - overonion/commitdiff
Test reverse
authorScott Worley <scottworley@scottworley.com>
Sat, 6 May 2017 16:44:34 +0000 (09:44 -0700)
committerScott Worley <scottworley@scottworley.com>
Fri, 20 Oct 2017 08:26:41 +0000 (01:26 -0700)
.gitignore
Makefile
reverse_test.c [new file with mode: 0644]

index df23e151bba4544a197ff7ea8e7da720f2e4175c..188a328241113b755a11273f1730f6a5261e72e9 100644 (file)
@@ -1,4 +1,5 @@
 reverse
 reverse
+reverse_test
 *.o
 deps.makefile
 deps.makefile.bak
 *.o
 deps.makefile
 deps.makefile.bak
index 71a482eb99dce17ff1cbc59db7a77768fdf48efe..fe978d38c88269922e50e5925f60db2828a7d8c7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CFLAGS = -Wall -Wextra -pedantic -Wstrict-overflow
 all: reverse
 
 clean:
 all: reverse
 
 clean:
-       -rm *.o reverse
+       -rm *.o reverse reverse_test
 
 distclean: clean
        -rm deps.makefile deps.makefile.bak
 
 distclean: clean
        -rm deps.makefile deps.makefile.bak
@@ -12,9 +12,13 @@ distclean: clean
 depend: *.c
        makedepend -f- $^ > deps.makefile
 
 depend: *.c
        makedepend -f- $^ > deps.makefile
 
-.PHONY: clean distclean depend
+test: reverse_test
+       ./reverse_test
+
+.PHONY: clean distclean depend test
 
 reverse: reverse.o reverse_lib.o
 
 reverse: reverse.o reverse_lib.o
+reverse_test: reverse_test.o reverse_lib.o
        $(CC) -o $@ $^ $(CFLAGS)
 
 %.o: %.c
        $(CC) -o $@ $^ $(CFLAGS)
 
 %.o: %.c
diff --git a/reverse_test.c b/reverse_test.c
new file mode 100644 (file)
index 0000000..fe35bcc
--- /dev/null
@@ -0,0 +1,54 @@
+#include "reverse_lib.h"
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+static const char test_file[] = "reverse.c";
+
+char* reverse_to_temp_file(const char* input_file) {
+  char* temp_filename = strdup("/tmp/reverse_test.XXXXXX");
+  int fd = mkstemp(temp_filename);
+  if (fd == -1) err(EX_IOERR, "Couldn't make a temporary file");
+  reverse_file(input_file, fd);
+  if (close(fd) == -1) err(EX_IOERR, "Couldn't close temporary file");
+  return temp_filename;
+}
+
+void test_reverse_twice_is_the_same() {
+  char* intermediate = reverse_to_temp_file(test_file);
+  char* back_to_normal = reverse_to_temp_file(intermediate);
+
+  FILE* f1 = fopen(test_file, "r");
+  if (f1 == NULL) err(EX_IOERR, "Couldn't open test file");
+  FILE* f2 = fopen(back_to_normal, "r");
+  if (f2 == NULL) err(EX_IOERR, "Couldn't open twice-reversed temp file");
+
+  for (int pos = 0; ; pos++) {
+    int c1 = fgetc(f1);
+    int c2 = fgetc(f2);
+    if (c1 == EOF && ferror(f1)) err(EX_IOERR, "Error reading test file");
+    if (c2 == EOF && ferror(f2)) err(EX_IOERR, "Error reading twice-reversed temp file");
+    if (c1 != c2) {
+      errx(EX_SOFTWARE, "Unexpected difference found at offset %d after reversing twice.  "
+                        "Expected %d but found %d", pos, c1, c2);
+    }
+    if (c1 == EOF) break;
+  }
+
+  if (fclose(f1) != 0) err(EX_IOERR, "Couldn't close test file");
+  if (fclose(f2) != 0) err(EX_IOERR, "Couldn't close twice-reversed temp file");
+  if (unlink(intermediate) == -1) err(EX_IOERR, "Couldn't remove intermediate temp file");
+  if (unlink(back_to_normal) == -1) err(EX_IOERR, "Couldn't remove twice-reversed temp file");
+  free(intermediate);
+  free(back_to_normal);
+}
+
+int main() {
+  test_reverse_twice_is_the_same();
+  puts("PASS");
+  return 0;
+}