tracker r1685 - in branches/indexer-split: . src/libtracker-common tests tests/common tests/libtracker-common



Author: ifrade
Date: Mon Jun 16 13:52:45 2008
New Revision: 1685
URL: http://svn.gnome.org/viewvc/tracker?rev=1685&view=rev

Log:
Added tracker-type-utils tests. Created library to common code for all the tests

Added:
   branches/indexer-split/tests/common/
   branches/indexer-split/tests/common/Makefile.am
   branches/indexer-split/tests/common/tracker-test-helpers.c
   branches/indexer-split/tests/common/tracker-test-helpers.h
   branches/indexer-split/tests/libtracker-common/tracker-type-utils-test.c
Modified:
   branches/indexer-split/ChangeLog
   branches/indexer-split/configure.ac
   branches/indexer-split/src/libtracker-common/tracker-type-utils.c
   branches/indexer-split/tests/Makefile.am
   branches/indexer-split/tests/libtracker-common/Makefile.am
   branches/indexer-split/tests/libtracker-common/tracker-ontology-test.c

Modified: branches/indexer-split/configure.ac
==============================================================================
--- branches/indexer-split/configure.ac	(original)
+++ branches/indexer-split/configure.ac	Mon Jun 16 13:52:45 2008
@@ -735,6 +735,7 @@
 	src/tracker-preferences/tracker-preferences.desktop.in	
 	src/trackerd/Makefile
 	tests/Makefile
+	tests/common/Makefile
 	tests/libtracker-common/Makefile
 	tests/trackerd/Makefile
 	tests/trackerd/xesam/Makefile

Modified: branches/indexer-split/src/libtracker-common/tracker-type-utils.c
==============================================================================
--- branches/indexer-split/src/libtracker-common/tracker-type-utils.c	(original)
+++ branches/indexer-split/src/libtracker-common/tracker-type-utils.c	Mon Jun 16 13:52:45 2008
@@ -552,10 +552,16 @@
 			guint       *value)
 {
 	unsigned long int n;
+	char *end;
 
-	g_return_val_if_fail (s, FALSE);
+	g_return_val_if_fail (s != NULL && value != NULL, FALSE);
 
-	n = strtoul (s, NULL, 10);
+	n = (guint) strtoul (s, &end, 10);
+
+	if (end == s) {
+		*value = 0;
+		return FALSE;
+	}
 
 	if (n > G_MAXUINT) {
 		*value = 0;
@@ -574,6 +580,8 @@
 	gchar **p;
 	gint    i = 0;
 
+	g_return_val_if_fail (str != NULL && strv != NULL, -1);
+
 	for (p = strv; *p; p++, i++) {
 		if (strcasecmp (*p, str) == 0) {
 			return i;
@@ -590,6 +598,8 @@
 	gchar  **strv;
 	gint     i = 0;
 
+	g_return_val_if_fail (list != NULL, NULL);
+
 	strv = g_new0 (gchar*, g_slist_length (list) + 1);
 
 	for (l = list; l; l = l->next) {
@@ -613,6 +623,12 @@
 	GString *string;
 	gint     i;
 
+	g_return_val_if_fail (strv != NULL, NULL);
+
+	if (length == -1) {
+		length = g_strv_length (strv);
+	}
+
 	string = g_string_new ("");
 
 	for (i = 0; i < length; i++) {
@@ -633,6 +649,9 @@
 gchar *  
 tracker_boolean_as_text_to_number  (const gchar *value)
 {
+
+	g_return_val_if_fail (value != NULL, NULL);
+
 	if (strcasecmp (value, "true") == 0) {
 		return g_strdup ("1");
 

Modified: branches/indexer-split/tests/Makefile.am
==============================================================================
--- branches/indexer-split/tests/Makefile.am	(original)
+++ branches/indexer-split/tests/Makefile.am	Mon Jun 16 13:52:45 2008
@@ -1,4 +1,4 @@
 include $(top_srcdir)/Makefile.decl
 
-SUBDIRS = libtracker-common trackerd
+SUBDIRS = libtracker-common trackerd common
 

Added: branches/indexer-split/tests/common/Makefile.am
==============================================================================
--- (empty file)
+++ branches/indexer-split/tests/common/Makefile.am	Mon Jun 16 13:52:45 2008
@@ -0,0 +1,8 @@
+noinst_LTLIBRARIES = libtracker-testcommon.la
+
+INCLUDES = $(GLIB2_CFLAGS)
+
+libtracker_testcommon_la_SOURCES = tracker-test-helpers.c tracker-test-helpers.h
+
+libtracker_testcommon_la_LIBDADD = $(GLIB2_LIBS)
+

Added: branches/indexer-split/tests/common/tracker-test-helpers.c
==============================================================================
--- (empty file)
+++ branches/indexer-split/tests/common/tracker-test-helpers.c	Mon Jun 16 13:52:45 2008
@@ -0,0 +1,23 @@
+#include "tracker-test-helpers.h"
+
+gboolean
+tracker_test_helpers_cmpstr_equal (const gchar *obtained, const gchar *expected) 
+{
+        gboolean result;
+
+	// NULL pointers are equals at the eyes of Godpiler
+	if ( expected == obtained ) { 
+		return TRUE;
+	}
+
+	if ( expected && obtained ) {
+		result = !g_utf8_collate (expected, obtained);
+                if (!result) {
+                        g_warning ("Expected %s - obtained %s", expected, obtained);
+                }
+                return result;
+	} else {
+                g_warning ("\n Only one of the strings is NULL\n");
+		return FALSE;
+	}
+}

Added: branches/indexer-split/tests/common/tracker-test-helpers.h
==============================================================================
--- (empty file)
+++ branches/indexer-split/tests/common/tracker-test-helpers.h	Mon Jun 16 13:52:45 2008
@@ -0,0 +1,9 @@
+#ifndef __TRACKER_TEST_HELPERS_H__
+#define __TRACKER_TEST_HELPERS_H__
+
+#include <glib.h>
+
+gboolean tracker_test_helpers_cmpstr_equal (const gchar *obtained, 
+                                            const gchar *expected);
+
+#endif

Modified: branches/indexer-split/tests/libtracker-common/Makefile.am
==============================================================================
--- branches/indexer-split/tests/libtracker-common/Makefile.am	(original)
+++ branches/indexer-split/tests/libtracker-common/Makefile.am	Mon Jun 16 13:52:45 2008
@@ -2,12 +2,13 @@
 
 noinst_PROGRAMS = $(TEST_PROGS)
 
-TEST_PROGS += tracker-ontology tracker-dbus
+TEST_PROGS += tracker-ontology tracker-dbus tracker-type-utils
 
 INCLUDES = 				\
 	-g 				\
 	-DG_LOG_DOMAIN=\"Tracker\"	\
 	-I$(top_srcdir)/src		\
+	-I$(top_srcdir)/tests/common	\
 	$(DBUS_CFLAGS)			\
 	$(GMODULE_CFLAGS)		\
 	$(GTHREAD_CFLAGS)		\
@@ -16,10 +17,11 @@
 
 
 tracker_ontology_SOURCES = \
-	tracker-ontology-test.c
+	tracker-ontology-test.c 
 
 tracker_ontology_LDADD =						\
 	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
+	$(top_builddir)/tests/common/libtracker-testcommon.la 		\
 	$(GMODULE_LIBS)							\
 	$(GTHREAD_LIBS)							\
 	$(GLIB2_LIBS)							
@@ -34,3 +36,13 @@
 	$(GMODULE_LIBS)							\
 	$(GTHREAD_LIBS)							\
 	$(GLIB2_LIBS)							
+
+tracker_type_utils_SOURCES = \
+	tracker-type-utils-test.c 
+
+tracker_type_utils_LDADD =						\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
+	$(top_builddir)/tests/common/libtracker-testcommon.la 		\
+	$(GMODULE_LIBS)							\
+	$(GTHREAD_LIBS)							\
+	$(GLIB2_LIBS)							

Modified: branches/indexer-split/tests/libtracker-common/tracker-ontology-test.c
==============================================================================
--- branches/indexer-split/tests/libtracker-common/tracker-ontology-test.c	(original)
+++ branches/indexer-split/tests/libtracker-common/tracker-ontology-test.c	Mon Jun 16 13:52:45 2008
@@ -3,22 +3,7 @@
 #include <libtracker-common/tracker-ontology.h>
 #include <glib.h>
 #include <glib/gtestutils.h>
-
-gboolean
-test_cmp_str_equal (const gchar *one, const gchar *two) 
-{
-	// Two NULL pointers are equals at the eyes of Godpiler
-	if ( one == two ) { 
-		return TRUE;
-	}
-
-	if ( one && two ) {
-		return g_utf8_collate (one, two);
-	} else {
-                g_warning ("\n Only one of the strings is NULL\n");
-		return FALSE;
-	}
-}
+#include <tracker-test-helpers.h>
 
 gboolean
 test_cmp_servicedef_equals (TrackerService *one, 
@@ -38,9 +23,9 @@
 	}
 
 	return ( tracker_service_get_id (one) == tracker_service_get_id (two) 
-		 && test_cmp_str_equal ( tracker_service_get_name (one), 
-                                         tracker_service_get_name (two))
-                 && test_cmp_str_equal ( tracker_service_get_parent (one),
+		 && tracker_test_helpers_cmpstr_equal ( tracker_service_get_name (one), 
+                                               tracker_service_get_name (two))
+                 && tracker_test_helpers_cmpstr_equal ( tracker_service_get_parent (one),
                                          tracker_service_get_parent (two))
 		 && tracker_service_get_db_type (one) == tracker_service_get_db_type (two)
 		 && tracker_service_get_embedded (one) == tracker_service_get_embedded (two));

Added: branches/indexer-split/tests/libtracker-common/tracker-type-utils-test.c
==============================================================================
--- (empty file)
+++ branches/indexer-split/tests/libtracker-common/tracker-type-utils-test.c	Mon Jun 16 13:52:45 2008
@@ -0,0 +1,403 @@
+#include <glib.h>
+#include <glib/gtestutils.h>
+
+#include <time.h>
+
+#include <libtracker-common/tracker-type-utils.h>
+#include <tracker-test-helpers.h>
+
+static void
+test_date_format ()
+{
+        gchar *result;
+
+        result = tracker_date_format ("");
+        g_assert (result == NULL);
+
+
+        /* Fails 
+        result = tracker_date_format ("1978"); //Audio.ReleaseDate
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "1978-01-01T00:00:00"));
+        */
+
+        result = tracker_date_format ("2008-06-14");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-14T00:00:00"));
+        g_free (result);
+
+        result = tracker_date_format ("20080614000000");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-14T00:00:00"));
+        g_free (result);
+
+        result = tracker_date_format ("20080614000000Z");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-14T00:00:00Z"));
+        g_free (result);
+
+        result = tracker_date_format ("Mon Jun 14 04:20:20 2008"); //MS Office
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-14T04:20:20"));
+        g_free (result);
+
+        result = tracker_date_format ("2008:06:14 04:20:20"); //Exif style
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-14T04:20:20"));
+        g_free (result);
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                result = tracker_date_format (NULL);
+        }
+        g_test_trap_assert_failed ();
+
+}
+
+static void
+test_string_to_date ()
+{
+        gchar *input = "2008-06-16T23:53:10+0300";
+        struct tm *original;
+        time_t     expected, result;
+
+        original = g_new0 (struct tm, 1);
+        original->tm_sec = 10;
+        original->tm_min = 53;
+        original->tm_hour = 23;
+        original->tm_mday = 16;
+        original->tm_mon = 5;
+        original->tm_year = 108;
+        original->tm_isdst = 1;
+        
+        expected = mktime (original);
+        
+        result = tracker_string_to_date (input);
+        g_assert_cmpint (expected, ==, result);
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                result = tracker_string_to_date (NULL);
+        }
+        g_test_trap_assert_failed ();
+
+        result = tracker_string_to_date ("");
+        g_assert_cmpint (result, ==, -1);
+
+        result = tracker_string_to_date ("i am not a date");
+        g_assert_cmpint (result, ==, -1);
+        
+        /* Fails! Check the code
+        result = tracker_string_to_date ("2008-06-32T04:23:10+0000");
+        g_assert_cmpint (result, ==, -1);
+        */
+}
+
+static void
+test_date_to_string ()
+{
+        struct tm *original;
+        time_t     input;
+        gchar     *result;
+
+        original = g_new0 (struct tm, 1);
+        original->tm_sec = 10;
+        original->tm_min = 53;
+        original->tm_hour = 23;
+        original->tm_mday = 16;
+        original->tm_mon = 5;
+        original->tm_year = 108;
+        original->tm_isdst = 1;
+        
+        input = mktime (original);
+
+        result = tracker_date_to_string (input);
+        // Maybe this test fails in a different time zone!
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "2008-06-16T23:53:10+0300"));
+}
+
+
+static void
+test_long_to_string ()
+{
+        glong n;
+        gchar *result;
+
+        n = 10050;
+        result = tracker_long_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "10050"));
+        g_free (result);
+
+        n = -9950;
+        result = tracker_long_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "-9950"));
+        g_free (result);
+}
+
+static void
+test_int_to_string ()
+{
+        gint n;
+        gchar *result;
+
+        n = 654;
+        result = tracker_int_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "654"));
+        g_free (result);
+
+        n = -963;
+        result = tracker_int_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "-963"));
+        g_free (result);
+
+}
+
+
+static void
+test_uint_to_string ()
+{
+        guint n;
+        gchar *result;
+
+        n = 100;
+        result = tracker_uint_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "100"));
+        g_free (result);
+}
+
+static void
+test_gint32_to_string ()
+{
+        gint32 n;
+        gchar *result;
+
+        n = 100;
+        result = tracker_gint32_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "100"));
+        g_free (result);
+
+        n = -96;
+        result = tracker_gint32_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "-96"));
+        g_free (result);
+
+}
+
+
+static void
+test_guint32_to_string ()
+{
+        guint32 n;
+        gchar *result;
+
+        n = 100;
+        result = tracker_guint32_to_string (n);
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "100"));
+        g_free (result);
+
+}
+
+
+static void
+test_string_to_uint ()
+{
+        guint num_result, rc;
+
+        rc = tracker_string_to_uint ("10", &num_result);
+        
+        g_assert (rc);
+        g_assert_cmpint (num_result, ==, 10);
+
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                rc = tracker_string_to_uint (NULL, &num_result);
+        }
+        g_test_trap_assert_failed ();
+
+        // ???? FIXME
+        rc = tracker_string_to_uint ("-20", &num_result);
+        // ????
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                tracker_string_to_uint (NULL, &num_result);
+        }
+        g_test_trap_assert_failed ();
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                tracker_string_to_uint ("199", NULL);
+        }
+        g_test_trap_assert_failed ();
+
+        rc = tracker_string_to_uint ("i am not a number", &num_result);
+        g_assert (!rc);
+        g_assert_cmpint (rc, ==, 0);
+}
+
+
+static void
+test_string_in_string_list ()
+{
+        gchar *complete = "This is an extract of text with different terms an props like Audio:Title ...";
+        gchar **pieces;
+
+        pieces = g_strsplit (complete, " ", -1);
+
+        g_assert_cmpint (tracker_string_in_string_list ("is", pieces), ==, 1);
+        g_assert_cmpint (tracker_string_in_string_list ("Audio:Title", pieces), ==, 12);
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                g_assert_cmpint (tracker_string_in_string_list (NULL, pieces), ==, -1);
+        }
+        g_test_trap_assert_failed ();
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                g_assert_cmpint (tracker_string_in_string_list ("terms", NULL), ==, -1);
+        }
+        g_test_trap_assert_failed ();
+}
+
+static void
+test_gslist_to_string_list () 
+{
+        GSList *input = NULL;
+        gchar **result;
+
+        input = g_slist_prepend (input, "four");
+        input = g_slist_prepend (input, "three");
+        input = g_slist_prepend (input, "two");
+        input = g_slist_prepend (input, "one");
+
+        result = tracker_gslist_to_string_list (input);
+
+        g_assert (tracker_test_helpers_cmpstr_equal (result[0], "one")
+                  && tracker_test_helpers_cmpstr_equal (result[1], "two")
+                  && tracker_test_helpers_cmpstr_equal (result[2], "three")
+                  && tracker_test_helpers_cmpstr_equal (result[3], "four"));
+
+        g_strfreev (result);
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                result = tracker_gslist_to_string_list (NULL);
+        }
+        g_test_trap_assert_failed ();
+}
+
+
+static void
+test_string_list_to_string () 
+{
+        gchar *input = "one two three four";
+        gchar **pieces;
+        gchar *result;
+
+        pieces = g_strsplit (input, " ", 4);
+        
+        result = tracker_string_list_to_string (pieces, 4, ' ');
+        g_assert (tracker_test_helpers_cmpstr_equal (input, result));
+        g_free (result);
+
+        result = tracker_string_list_to_string (pieces, 3, '_');
+        g_assert (tracker_test_helpers_cmpstr_equal ("one_two_three", result));
+        g_free (result);
+
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                result = tracker_string_list_to_string (NULL, 6, 'x');
+        }
+        g_test_trap_assert_failed ();
+
+        result = tracker_string_list_to_string (pieces, -1, ' ');
+        g_assert (tracker_test_helpers_cmpstr_equal (input, result));
+        g_free (result);
+
+        result = tracker_string_list_to_string (pieces, 6, ' ');
+        g_assert (tracker_test_helpers_cmpstr_equal (input, result));
+        g_free (result);
+
+        g_strfreev (pieces);
+}
+
+
+static void
+test_boolean_as_text_to_number ()
+{
+        gchar *result;
+
+        /* Correct true values */
+        result = tracker_boolean_as_text_to_number ("True");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "1"));
+        g_free (result);
+
+
+        result = tracker_boolean_as_text_to_number ("TRUE");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "1"));
+        g_free (result);
+
+        result = tracker_boolean_as_text_to_number ("true");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "1"));
+        g_free (result);
+
+        /* Correct false values */
+        result = tracker_boolean_as_text_to_number ("False");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "0"));
+        g_free (result);
+
+        result = tracker_boolean_as_text_to_number ("FALSE");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "0"));
+        g_free (result);
+
+        result = tracker_boolean_as_text_to_number ("false");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "0"));
+        g_free (result);
+
+        /* Invalid values */
+        result = tracker_boolean_as_text_to_number ("Thrue");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "Thrue"));
+        g_free (result);
+
+        result = tracker_boolean_as_text_to_number ("Falsez");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "Falsez"));
+        g_free (result);
+
+        result = tracker_boolean_as_text_to_number ("Other invalid value");
+        g_assert (tracker_test_helpers_cmpstr_equal (result, "Other invalid value"));
+        g_free (result);
+
+        
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                result = tracker_boolean_as_text_to_number (NULL);
+        }
+        g_test_trap_assert_failed ();
+}
+
+
+int
+main (int argc, char **argv) {
+
+        int result;
+
+	g_type_init ();
+	g_test_init (&argc, &argv, NULL);
+
+        g_test_add_func ("/libtracker-common/tracker-type-utils/boolean_as_text_to_number",
+                         test_boolean_as_text_to_number);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_as_list",
+                         test_string_list_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/gslist_to_string_list",
+                         test_gslist_to_string_list);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_string_list",
+                         test_string_in_string_list);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/string_to_uint",
+                         test_string_to_uint);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/guint32_to_string",
+                         test_guint32_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/gint32_to_string",
+                         test_gint32_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/uint_to_string",
+                         test_uint_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/int_to_string",
+                         test_int_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/long_to_string",
+                         test_long_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/date_format",
+                         test_date_format);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/date_to_string",
+                         test_date_to_string);
+        g_test_add_func ("/libtracker-common/tracker-type-utils/string_to_date",
+                         test_string_to_date);
+        result = g_test_run ();
+        
+        return result;
+}



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