[tracker/tracker-0.12] libtracker-common: Fixed tracker_filename_casecmp_without_extension() test case failed



commit 222a232bf792749e5a76591b3a6e585844ea80f4
Author: Martyn Russell <martyn lanedo com>
Date:   Thu Oct 6 16:26:32 2011 +0100

    libtracker-common: Fixed tracker_filename_casecmp_without_extension() test case failed

 src/libtracker-common/tracker-file-utils.c        |   83 ++++++++++-----------
 tests/libtracker-common/tracker-file-utils-test.c |   32 ++++++++-
 tests/libtracker-common/tracker-utils-test.c      |   31 --------
 3 files changed, 69 insertions(+), 77 deletions(-)
---
diff --git a/src/libtracker-common/tracker-file-utils.c b/src/libtracker-common/tracker-file-utils.c
index 0682943..bc650b6 100644
--- a/src/libtracker-common/tracker-file-utils.c
+++ b/src/libtracker-common/tracker-file-utils.c
@@ -856,60 +856,55 @@ gboolean
 tracker_filename_casecmp_without_extension (const gchar *a,
                                             const gchar *b)
 {
-	const gchar *ca = a;
-	const gchar *cb = b;
-	gboolean period_a = FALSE;
-	gboolean period_b = FALSE;
-	gboolean match = TRUE;
+	gchar *pa;
+	gchar *pb;
+	gint len_a;
+	gint len_b;
 
 	g_return_val_if_fail (a != NULL, FALSE);
 	g_return_val_if_fail (b != NULL, FALSE);
 
-	while (1) {
-		if (*ca == '\0' && *cb == '\0')
-			break;
+	pa = strrchr (a, '.');
+	pb = strrchr (b, '.');
 
-		if (g_ascii_tolower (*ca) != g_ascii_tolower (*cb)) {
-			match = FALSE;
-			break;
-		}
-
-		if (*ca == '.')
-			period_a = TRUE;
-
-		if (*cb == '.')
-			period_b = TRUE;
-
-		if (*ca == '\0' || *cb == '\0') {
-			match = FALSE;
-			break;
-		}
-
-		ca ++; cb ++;
+	/* Did we find a "." */
+	if (pa) {
+		len_a = pa - a;
+	} else {
+		len_a = -1;
 	}
 
-	if (!match) {
-		/* If the mismatch was past the last '.' then forgive it. */
-		if (*ca != '\0' && period_a) {
-			match = TRUE;
+	if (pb) {
+		len_b = pb - b;
+	} else {
+		len_b = -1;
+	}
 
-			while (*(ca ++) != '\0') {
-				if (*ca == '.') {
-					match = FALSE;
-				}
-			}
-		}
+	/* If one has a "." and the other doesn't, we do length
+	 * comparison with strlen() which is less optimal but this is
+	 * not a case we consider common operation.
+	 */
+	if (len_a == -1 && len_b > -1) {
+		len_a = strlen (a);
+	} else if (len_b == -1 && len_a > -1) {
+		len_b = strlen (b);
+	}
 
-		if (*cb != '\0' && period_b) {
-			match = TRUE;
+	/* If we have length for both and it's different then these
+	 * strings are not the same. If we have no length for the
+	 * strings then it's a simple -1 != -1 comparison.
+	 */
+	if (len_a != len_b) {
+		return FALSE;
+	}
 
-			while (*(cb ++) != '\0') {
-				if (*cb == '.') {
-					match = FALSE;
-				}
-			}
-		}
+	/* Now we know we either have the same length string or no
+	 * extension in a and b, meaning it's a strcmp() of the
+	 * string only. We test only len_a or len_b here for that:
+	 */
+	if (G_UNLIKELY (len_a == -1)) {
+		return g_ascii_strcasecmp (a, b) == 0;
 	}
 
-	return match;
+	return g_ascii_strncasecmp (a, b, len_a) == 0;
 }
diff --git a/tests/libtracker-common/tracker-file-utils-test.c b/tests/libtracker-common/tracker-file-utils-test.c
index e8268f0..3e53525 100644
--- a/tests/libtracker-common/tracker-file-utils-test.c
+++ b/tests/libtracker-common/tracker-file-utils-test.c
@@ -181,6 +181,34 @@ test_file_get_mime_type (void)
 	g_free (dir_name);
 }
 
+#define assert_filename_match(a, b) { \
+	g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, TRUE); \
+	g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, TRUE); }
+
+#define assert_no_filename_match(a, b) { \
+	g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, FALSE); \
+	g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, FALSE); }
+
+static void
+test_case_match_filename_without_extension ()
+{
+	assert_filename_match ("test.mp3", "test.mp3");
+	assert_filename_match ("test.mp3", "test.wav");
+	assert_filename_match ("test.mp3", "test.mp");
+	assert_filename_match ("test.mp3", "test.");
+	assert_filename_match ("test.mp3", "test");
+	assert_filename_match ("01 - Song 1 (Remix).wav", "01 - Song 1 (Remix).flac");
+
+	assert_no_filename_match ("test.mp3", "bacon.mp3");
+
+	/* Pathological cases, mainly testing that nothing crashes */
+	assert_no_filename_match (".", "\n");
+	assert_no_filename_match ("as", "as..");
+	assert_no_filename_match ("...as", "...as..");
+	assert_no_filename_match (".", "test.");
+	assert_filename_match ("", ".");
+}
+
 int
 main (int argc, char **argv)
 {
@@ -193,12 +221,12 @@ main (int argc, char **argv)
 
 	g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_evaluate_name",
 	                 test_path_evaluate_name);
-
 	g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_list_filter_duplicates",
 	                 test_path_list_filter_duplicates);
-
 	g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/file_get_mime_type",
 	                 test_file_get_mime_type);
+	g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/case_match_filename_without_extension",
+	                 test_case_match_filename_without_extension);
 
 	result = g_test_run ();
 
diff --git a/tests/libtracker-common/tracker-utils-test.c b/tests/libtracker-common/tracker-utils-test.c
index 2ed2524..ac68c6c 100644
--- a/tests/libtracker-common/tracker-utils-test.c
+++ b/tests/libtracker-common/tracker-utils-test.c
@@ -75,34 +75,6 @@ test_seconds_estimate_to_string ()
 	g_print ("%s\n", result);
 }
 
-#define assert_filename_match(a, b) { \
-	g_assert (tracker_filename_casecmp_without_extension (a, b) == TRUE); \
-	g_assert (tracker_filename_casecmp_without_extension (b, a) == TRUE); }
-
-#define assert_no_filename_match(a, b) { \
-	g_assert (tracker_filename_casecmp_without_extension (a, b) == FALSE); \
-	g_assert (tracker_filename_casecmp_without_extension (b, a) == FALSE); }
-
-static void
-test_case_match_filename_without_extension ()
-{
-	assert_filename_match ("test.mp3", "test.mp3");
-	assert_filename_match ("test.mp3", "test.wav");
-	assert_filename_match ("test.mp3", "test.mp");
-	assert_filename_match ("test.mp3", "test.");
-	assert_filename_match ("test.mp3", "test");
-	assert_filename_match ("01 - Song 1 (Remix).wav", "01 - Song 1 (Remix).flac");
-
-	assert_no_filename_match ("test.mp3", "bacon.mp3");
-
-	/* Pathological cases, mainly testing that nothing crashes */
-	assert_no_filename_match (".", "\n");
-	assert_no_filename_match ("as", "as..");
-	assert_no_filename_match ("...as", "...as..");
-	assert_no_filename_match (".", "test.");
-	assert_filename_match ("", ".");
-}
-
 int
 main (int argc, char **argv)
 {
@@ -119,9 +91,6 @@ main (int argc, char **argv)
 	g_test_add_func ("/libtracker-common/tracker-utils/seconds_estimate_to_string",
 	                 test_seconds_estimate_to_string);
 
-	g_test_add_func ("/libtracker-common/tracker-utils/case_match_filename_without_extension",
-	                 test_case_match_filename_without_extension);
-
 	ret = g_test_run ();
 
 	tracker_locale_shutdown ();



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