From e2db5d83189f2fd874ac0d1bfd3ea698e3099ef9 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Sat, 1 Jan 2011 17:33:42 +0100
Subject: [PATCH] unittest: framework for unit-testing

This is my suggested first approach for doing fairly clean and easy to
write and debug unit tests.
---
 tests/data/test1300        |   30 ++++++++++++++++++++++++++++++
 tests/libtest/Makefile.inc |    8 +++++++-
 tests/libtest/curlcheck.h  |   31 +++++++++++++++++++++++++++++++
 tests/libtest/first.c      |    2 +-
 tests/libtest/lib1300.c    |   34 ++++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+), 2 deletions(-)
 create mode 100644 tests/data/test1300
 create mode 100644 tests/libtest/curlcheck.h
 create mode 100644 tests/libtest/lib1300.c

diff --git a/tests/data/test1300 b/tests/data/test1300
new file mode 100644
index 0000000..3dc3b90
--- /dev/null
+++ b/tests/data/test1300
@@ -0,0 +1,30 @@
+<testcase>
+<info>
+<keywords>
+unittest
+</keywords>
+</info>
+
+#
+# Client-side
+<client>
+<server>
+none
+</server>
+ <name>
+llist unit tests
+ </name>
+<tool>
+lib1300
+</tool>
+<command>
+lib1300
+</command>
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+
+</verify>
+</testcase>
diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc
index a0f1c0e..d17556e 100644
--- a/tests/libtest/Makefile.inc
+++ b/tests/libtest/Makefile.inc
@@ -4,6 +4,9 @@ TESTUTIL = testutil.c testutil.h
 # these files are used in every single test program below
 SUPPORTFILES = first.c test.h
 
+# these files are used in every single unit test program
+UNITFILES = first.c test.h curlcheck.h
+
 # These are all libcurl test programs
 noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505 lib506	\
   lib507 lib508 lib510 lib511 lib512 lib513 lib514 lib515 lib516	\
@@ -12,7 +15,8 @@ noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505 lib506	\
   lib529 lib530 lib532 lib533 lib536 lib537 lib540 lib541 lib542 lib543 \
   lib544 lib545 lib547 lib548 lib549 lib552 lib553 lib554 lib555 lib556 \
   lib539 lib557 lib558 lib559 lib560 lib562 lib564 lib565 lib566 lib567 \
-  lib568 lib569 lib570 lib571 lib572 lib573 chkhostname
+  lib568 lib569 lib570 lib571 lib572 lib573 chkhostname \
+  lib1300
 
 chkhostname_SOURCES = chkhostname.c $(top_srcdir)/lib/curl_gethostname.c
 chkhostname_LDADD = @CURL_NETWORK_LIBS@
@@ -164,3 +168,5 @@ lib573_SOURCES = lib573.c $(SUPPORTFILES) $(TESTUTIL)
 lib578_SOURCES = lib578.c $(SUPPORTFILES)
 
 lib579_SOURCES = lib579.c $(SUPPORTFILES)
+
+lib1300_SOURCES = lib1300.c $(UNITFILES)
diff --git a/tests/libtest/curlcheck.h b/tests/libtest/curlcheck.h
new file mode 100644
index 0000000..c34f990
--- /dev/null
+++ b/tests/libtest/curlcheck.h
@@ -0,0 +1,31 @@
+/*****************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ */
+
+#include "test.h"
+
+#define fail_unless(expr, msg)                          \
+  if(!(expr)) {                                         \
+    fprintf(stderr, "%s:%d Assertion '%s' failed: %s" , \
+            __FILE__, __LINE__, #expr, msg);            \
+    unitfail++;                                         \
+  }
+
+extern int unitfail;
+
+#define UNITTEST_START                          \
+  int test(char *unused)                        \
+  {                                             \
+  (void)unused;                                 \
+  unit_setup();
+
+#define UNITTEST_STOP                           \
+  unit_stop();                                  \
+  return unitfail;                              \
+  }
+
diff --git a/tests/libtest/first.c b/tests/libtest/first.c
index a0e713f..53b372b 100644
--- a/tests/libtest/first.c
+++ b/tests/libtest/first.c
@@ -37,7 +37,7 @@ char *libtest_arg2=NULL;
 char *libtest_arg3=NULL;
 int test_argc;
 char **test_argv;
-
+int unitfail; /* for unittests */
 
 int main(int argc, char **argv)
 {
diff --git a/tests/libtest/lib1300.c b/tests/libtest/lib1300.c
new file mode 100644
index 0000000..92c0a7a
--- /dev/null
+++ b/tests/libtest/lib1300.c
@@ -0,0 +1,34 @@
+#include <stdlib.h>
+#include "curl_config.h"
+#include "setup.h"
+
+#include "llist.h"
+#include "curlcheck.h"
+
+struct curl_llist *llist;
+
+static void test_curl_llist_dtor(void *key , void *value)
+{
+  /* used by the llist API, does nothing here */
+  (void)key;
+  (void)value;
+}
+
+static void unit_setup( void )
+{
+  llist = Curl_llist_alloc( test_curl_llist_dtor );
+}
+
+static void unit_stop( void )
+{
+  Curl_llist_destroy( llist, NULL );
+}
+
+UNITTEST_START
+
+  fail_unless( llist->size == 0 , "list initial size should be zero" );
+  fail_unless( llist->head == NULL , "list head should initiate to NULL" );
+  fail_unless( llist->tail == NULL , "list tail should intiate to NULL" );
+  fail_unless( llist->dtor == test_curl_llist_dtor , "list dtor shold initiate to test_curl_llist_dtor" );
+
+UNITTEST_STOP
-- 
1.7.2.3

