[gcalctool] Integrate unittests into build process



commit aaeb94066a70e1898d0b3a63988a90c9b8472a75
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Feb 24 17:26:19 2011 +1100

    Integrate unittests into build process

 NEWS                                     |    4 +
 po/POTFILES.in                           |    1 -
 src/Makefile.am                          |   54 ++++++++-
 src/gcalctool.c                          |    6 -
 src/{unittest.c => test-mp-serializer.c} |  182 +-----------------------------
 src/unittest.h                           |   24 ----
 6 files changed, 59 insertions(+), 212 deletions(-)
---
diff --git a/NEWS b/NEWS
index 9625991..f75d19c 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,9 @@
 Overview of changes in gcalctool 5.91.90
 
+    * Integrate unittests into build process
+
+Overview of changes in gcalctool 5.91.90
+
     * Fix grammar mistakes
     * Fix translated conversions (Bug #641166)
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 50d6598..0401271 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -30,4 +30,3 @@ src/mp-trigonometric.c
 src/unit.c
 src/unit-category.c
 src/unit-manager.c
-src/unittest.c
diff --git a/src/Makefile.am b/src/Makefile.am
index f0f79ef..bbcc34f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,7 @@
 bin_PROGRAMS = gcalctool gcalccmd
+noinst_PROGRAMS = test-mp test-mp-serializer
+
+TESTS = test-mp test-mp-serializer
 
 INCLUDES = \
 	-DUI_DIR=\""$(datadir)/gcalctool"\" \
@@ -54,12 +57,10 @@ gcalctool_SOURCES = \
 	unit-category.c \
 	unit-category.h \
 	unit-manager.c \
-	unit-manager.h \
-	unittest.c \
-	unittest.h
+	unit-manager.h
 
 gcalctool_LDADD = \
-	$(GCALCTOOL_LIBS)
+	$(GCALCTOOL_LIBS)        
 
 gcalccmd_SOURCES = \
 	gcalccmd.c \
@@ -67,6 +68,8 @@ gcalccmd_SOURCES = \
 	currency.h \
 	currency-manager.c \
 	currency-manager.h \
+	math-enums.c \
+	math-enums.h \
 	mp.c \
 	mp-convert.c \
 	mp-binary.c \
@@ -76,8 +79,49 @@ gcalccmd_SOURCES = \
 	mp-equation-lexer.c \
 	mp-serializer.c \
 	mp-serializer.h\
+	unit.c \
+	unit.h \
+	unit-category.c \
+	unit-category.h \
+	unit-manager.c \
+	unit-manager.h
+
+gcalccmd_LDADD = \
+	$(GCALCCMD_LIBS) \
+	-lm
+
+test_mp_SOURCES = \
+	mp.c \
+	mp-convert.c \
+	mp-binary.c \
 	math-enums.c \
 	math-enums.h \
+	mp-serializer.c \
+	mp-serializer.h \
+	mp-trigonometric.c \
+	test-mp.c
+
+test_mp_LDADD = \
+	$(GCALCCMD_LIBS) \
+	-lm
+
+test_mp_serializer_SOURCES = \
+	currency.c \
+	currency.h \
+	currency-manager.c \
+	currency-manager.h \
+	math-enums.c \
+	math-enums.h \
+	mp.c \
+	mp-convert.c \
+	mp-binary.c \
+	mp-trigonometric.c \
+	mp-equation.c \
+	mp-equation-parser.c \
+	mp-equation-lexer.c \
+	mp-serializer.c \
+	mp-serializer.h \
+	test-mp-serializer.c \
 	unit.c \
 	unit.h \
 	unit-category.c \
@@ -85,7 +129,7 @@ gcalccmd_SOURCES = \
 	unit-manager.c \
 	unit-manager.h
 
-gcalccmd_LDADD = \
+test_mp_serializer_LDADD = \
 	$(GCALCCMD_LIBS) \
 	-lm
 
diff --git a/src/gcalctool.c b/src/gcalctool.c
index 5aa1a0a..35760ee 100644
--- a/src/gcalctool.c
+++ b/src/gcalctool.c
@@ -23,7 +23,6 @@
 #include <locale.h>
 #include <glib/gi18n.h>
 
-#include "unittest.h"
 #include "math-window.h"
 #include "mp-equation.h"
 #include "unit-manager.h"
@@ -117,7 +116,6 @@ usage(const gchar *progname, gboolean show_application, gboolean show_gtk)
         fprintf(stderr,
                 /* Description on gcalctool application options displayed on command-line */
                 _("Application Options:\n"
-                  "  -u, --unittest                  Perform unit tests\n"
                   "  -s, --solve <equation>          Solve the given equation"));
         fprintf(stderr,
                 "\n\n");
@@ -168,10 +166,6 @@ get_options(int argc, char *argv[])
             else
                 solve(argv[i]);
         }
-        else if (strcmp(arg, "-u") == 0 ||
-            strcmp(arg, "--unittest") == 0) {
-            unittest();
-        }
         else {
             fprintf(stderr,
                     /* Error printed to stderr when user provides an unknown command-line argument */
diff --git a/src/unittest.c b/src/test-mp-serializer.c
similarity index 75%
rename from src/unittest.c
rename to src/test-mp-serializer.c
index 68e35c6..8f3f80d 100644
--- a/src/unittest.c
+++ b/src/test-mp-serializer.c
@@ -22,7 +22,6 @@
 #include <stdarg.h>
 #include <locale.h>
 
-#include "unittest.h"
 #include "mp-equation.h"
 #include "mp-serializer.h"
 #include "unit-manager.h"
@@ -617,185 +616,16 @@ test_equations()
 }
 
 
-static void
-print_number(MPNumber *x)
-{
-    int i, j;
-
-    printf("sign=%d exponent=%d fraction=%d", x->sign, x->exponent, x->fraction[0]);
-    for (i = 1; i < MP_SIZE; i++) {
-        for (j = i; j < MP_SIZE && x->fraction[j] == 0; j++);
-        if (j == MP_SIZE) {
-            printf(",...");
-            break;
-        }
-        printf(",%d", x->fraction[i]);
-    }
-}
-
-static void
-test_string(const char *number)
-{
-    MPNumber t;
-
-    mp_set_from_string(number, 10, &t);
-
-    printf("MPNumber(%s) -> {", number);
-    print_number(&t);
-    printf("}\n");
-}
-
-static void
-test_integer(int number)
-{
-    MPNumber t;
-
-    mp_set_from_integer(number, &t);
-
-    printf("MPNumber(%d) -> {", number);
-    print_number(&t);
-    printf("}\n");
-}
-
-#include "mp-private.h"
-static void
-test_numbers()
-{
-    printf("base=%d\n", MP_BASE);
-    test_integer(0);
-    test_integer(1);
-    test_integer(-1);
-    test_integer(2);
-    test_integer(9999);
-    test_integer(10000);
-    test_integer(10001);
-    test_integer(2147483647);
-
-    test_string("0");
-    test_string("1");
-    test_string("-1");
-    test_string("16383");
-    test_string("16384");
-    test_string("16385");
-    test_string("268435456");
-
-    test_string("0.1");
-    test_string("0.5");
-    test_string("0.25");
-    test_string("0.125");
-    test_string("0.0625");
-    test_string("0.00006103515625");
-    test_string("0.000030517578125");
-
-    test_string("1.00006103515625");
-    test_string("16384.00006103515625");
-}
-
-
-static void
-try(const char *string, bool result, bool expected)
-{
-    if ((result && !expected) || (!result && expected))
-        fail("%s -> %s, expected %s", string, expected ? "true" : "false", result ? "true" : "false");
-    else
-        pass("%s -> %s", string, result ? "true" : "false");
-}
-
-
-static void
-test_mp()
-{
-    MPNumber zero, one, minus_one;
-  
-    mp_set_from_integer(0, &zero);
-    mp_set_from_integer(1, &one);
-    mp_set_from_integer(-1, &minus_one);
-  
-    try("mp_is_zero(-1)", mp_is_zero(&minus_one), false);
-    try("mp_is_zero(0)", mp_is_zero(&zero), true);
-    try("mp_is_zero(1)", mp_is_zero(&one), false);
-
-    try("mp_is_negative(-1)", mp_is_negative(&minus_one), true);
-    try("mp_is_negative(0)", mp_is_negative(&zero), false);
-    try("mp_is_negative(1)", mp_is_negative(&one), false);
-
-    try("mp_is_integer(-1)", mp_is_integer(&minus_one), true);
-    try("mp_is_integer(0)", mp_is_integer(&zero), true);
-    try("mp_is_integer(1)", mp_is_integer(&one), true);
-
-    try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one), false);
-    try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero), true);
-    try("mp_is_positive_integer(1)", mp_is_positive_integer(&one), true);
-
-    try("mp_is_natural(-1)", mp_is_natural(&minus_one), false);
-    try("mp_is_natural(0)", mp_is_natural(&zero), false);
-    try("mp_is_natural(1)", mp_is_natural(&one), true);
-
-    try("mp_is_complex(-1)", mp_is_complex(&minus_one), false);
-    try("mp_is_complex(0)", mp_is_complex(&zero), false);
-    try("mp_is_complex(1)", mp_is_complex(&one), false);
-
-    try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one, &minus_one), true);
-    try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one, &zero), false);
-    try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one, &one), false);
-    try("mp_is_equal(0, -1)", mp_is_equal(&zero, &minus_one), false);
-    try("mp_is_equal(0, 0)", mp_is_equal(&zero, &zero), true);
-    try("mp_is_equal(0, 1)", mp_is_equal(&zero, &one), false);
-    try("mp_is_equal(1, -1)", mp_is_equal(&one, &minus_one), false);
-    try("mp_is_equal(1, 0)", mp_is_equal(&one, &zero), false);
-    try("mp_is_equal(1, 1)", mp_is_equal(&one, &one), true);
-
-    try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero, &minus_one), true);  
-    try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero, &zero), false);
-    try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero, &one), false);
-    try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one, &minus_one), true);  
-    try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one, &zero), true);
-    try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one, &one), false);
-    try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one, &minus_one), false);  
-    try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one, &zero), false);
-    try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one, &one), false);
-
-    try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero, &minus_one), true);  
-    try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero, &zero), true);
-    try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero, &one), false);
-    try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one, &minus_one), true);
-    try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one, &zero), true);
-    try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one, &one), true);
-    try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one, &minus_one), true);
-    try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one, &zero), false);
-    try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one, &one), false);
-
-    try("mp_is_less_than(0, -1)", mp_is_less_than (&zero, &minus_one), false);  
-    try("mp_is_less_than(0, 0)", mp_is_less_than (&zero, &zero), false);
-    try("mp_is_less_than(0, 1)", mp_is_less_than (&zero, &one), true);
-    try("mp_is_less_than(1, -1)", mp_is_less_than (&one, &minus_one), false);  
-    try("mp_is_less_than(1, 0)", mp_is_less_than (&one, &zero), false);
-    try("mp_is_less_than(1, 1)", mp_is_less_than (&one, &one), false);
-    try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one, &minus_one), false);  
-    try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one, &zero), true);
-    try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one, &one), true);
-
-    try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero, &minus_one), false);  
-    try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero, &zero), true);
-    try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero, &one), true);
-    try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one, &minus_one), false);  
-    try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one, &zero), false);
-    try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one, &one), true);
-    try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one, &minus_one), true);  
-    try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one, &zero), true);
-    try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one, &one), true);
-}
-
-
-void
-unittest()
+int
+main (int argc, char **argv)
 {
     setlocale(LC_ALL, "C");
-    test_mp();
-    test_numbers();
+    g_type_init ();
+
     test_conversions();
     test_equations();
     if (fails == 0)
         printf("Passed all %i tests\n", passes);
-    exit(fails > 0 ? 1 : 0);
+
+    return fails;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]