Skip to content

Instantly share code, notes, and snippets.

@jackowayed
Created April 12, 2011 21:07
Show Gist options
  • Save jackowayed/916414 to your computer and use it in GitHub Desktop.
Save jackowayed/916414 to your computer and use it in GitHub Desktop.
C unit test
diff --git a/Makefile b/Makefile
index e381341..1bd4487 100644
--- a/Makefile
+++ b/Makefile
@@ -44,6 +44,12 @@ default: $(TARGET)
$(TARGET) : $(OBJECTS) Makefile.dependencies libcvecmap.a
$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LDFLAGS) $(LIBRARIES)
+test : test.o Makefile.dependencies libcvecmap.a
+ $(CC) $(CFLAGS) -o $@ test.o $(LDFLAGS) $(LIBRARIES)
+
+test.o : $(OBJECTS)
+
+
# In make's default rules, a .o automatically depends on its .c file
# (so editing the .c will cause recompilation into its .o file).
# The line below creates additional dependencies, most notably that it
diff --git a/spellcheck.c b/spellcheck.c
index 7d155a3..cfd859a 100644
--- a/spellcheck.c
+++ b/spellcheck.c
@@ -1,8 +1,11 @@
#include "cvector.h"
#include "cmap.h"
+int foo(){
+ return 2;
+}
+
int main(int argc, const char *argv[] )
{
return 0;
}
-
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..ba05364
--- /dev/null
+++ b/test.c
@@ -0,0 +1,16 @@
+#define main their_main
+#include "spellcheck.c"
+#undef main
+#include <stdio.h>
+
+#define test(expr) do { \
+ if (!(expr)) printf("FAILED: %s\n", #expr); \
+ else printf(" ok: %s\n", #expr); \
+ } while (0)
+
+int main(int argc, const char *argv[])
+{
+ test(foo()==5);
+ test(foo()==2);
+ return 0;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment