From 71781896ed840cc46015dcefa1ad9834231b2b07 Mon Sep 17 00:00:00 2001
From: Pat Thoyts <patthoyts@users.sourceforge.net>
Date: Sat, 19 Jun 2010 09:35:02 +0100
Subject: [PATCH] Checked the tests using valgrind and eliminated misused
 calls.

Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
---
 makefile.linux | 16 +++++++++++++++-
 src/ag_test.c  | 39 ++++++++++-----------------------------
 2 files changed, 25 insertions(+), 30 deletions(-)

diff --git a/makefile.linux b/makefile.linux
index 228568f..ae02e3c 100644
--- a/makefile.linux
+++ b/makefile.linux
@@ -1,7 +1,9 @@
+# -*- Makefile -*-
+
 CC=gcc
 LD=gcc
 CFLAGS  =-g -Wall `sdl-config --cflags`
-CFLAGS +=-O9 -funroll-loops -fomit-frame-pointer
+#CFLAGS +=-O9 -funroll-loops -fomit-frame-pointer
 LDFLAGS=`sdl-config --libs` -lSDL_mixer
 
 C_FILES=src/dlb.c src/linked.c src/sprite.c src/ag.c
@@ -29,5 +31,17 @@ src/sprite.o: src/sprite.c
 src/ag.o: src/ag.c
 	$(CC) $(CFLAGS) -c -o $@ $^
 
+src/ag_test.o: src/ag_test.c
+	$(CC) $(CFLAGS) -DUNIT_TEST -c -o $@ $^
+
+tests: test_ag test_linked
+
+test_ag: ag $(TEST_OBJS) src/unittest.h
+	$(LD) $(LDFLAGS) -o $@ $(TEST_OBJS)
+
+test_linked: src/linked.c src/unittest.h
+	$(CC) $(CFLAGS) -Isrc -DUNIT_TEST $< -o $@
+
 clean:
 	rm -f src/*.o
+	rm -f test_linked test_ag
diff --git a/src/ag_test.c b/src/ag_test.c
index 6bea8a4..7ef3464 100644
--- a/src/ag_test.c
+++ b/src/ag_test.c
@@ -1,5 +1,8 @@
+#ifndef UNIT_TEST
 #define UNIT_TEST
+#endif
 #include "unittest.h"
+
 #define main ag_main
 #include "ag.c"
 #undef main
@@ -17,8 +20,12 @@ static int test_nextBlank()
 static int test_shiftLeftKill()
 {
     char a[7] = { 'a','b','c','d','e','f', 0 };
-    test_equals_str("shiftLeftKill", "bcdef", shiftLeftKill(a));
-    test_equals_str("shiftLeftKill const str", "bcdef", shiftLeftKill("abcdef"));
+    char *s = shiftLeftKill(a);
+    test_equals_str("shiftLeftKill", "bcdef", s);
+    free(s);
+    s = shiftLeftKill("abcdef");
+    test_equals_str("shiftLeftKill const str", "bcdef", s);
+    free(s);
     return 0;
 }
 
@@ -31,26 +38,6 @@ static int test_shiftLeft()
     return 0;
 }
 
-static int test_swapChars()
-{
-    char a[7] = { 'a','b','c','d','e','f', 0 };
-    const char *p = a, *q = NULL;
-    test_equals_str("swapChars end", "fbcdea", swapChars(0, 5, a));
-    q = swapChars(2, 3, a);
-    test_equals_str("swapChars inner", "fbdcea", q);
-    test_equals_ptr("swapChars ptr equiv", p, q);
-    return 0;
-}
-
-static int test_revFirstNonSpace()
-{
-    char a[7] = { 'a','b','c','d','e','f', 0 };
-    char b[7] = { 'a','b',SPACE_CHAR,'d','e',SPACE_CHAR, 0 };
-    test_equals_int("rev no space", 6, revFirstNonSpace(a));
-    test_equals_int("rev find space", 5, revFirstNonSpace(b));
-    return 0;
-}
- 
 static int test_whereinstr()
 {
     test_equals_int("where is b", 1, whereinstr("abcdef",'b'));
@@ -63,18 +50,12 @@ struct unit_test_t unit_tests[] = {
     {NULL, test_shiftLeftKill, NULL},
     {NULL, test_shiftLeft, NULL},
     {NULL, test_nextBlank, NULL},
-    {NULL, test_swapChars, NULL},
-    {NULL, test_revFirstNonSpace, NULL},
     {NULL, test_whereinstr, NULL},
 };
 
 int
 main(void)
 {
-    size_t n;
-    for (n = 0; n < sizeof(unit_tests)/sizeof(unit_tests[0]); ++n) {
-        run_tests(&unit_tests[n]);
-    }
-    test_print_results();
+    run_tests(unit_tests);
     return 0;
 }
-- 
2.23.0