[libgdata/libgdata-0-14: 7/18] gd: Fix comparison of GDataGDReminders with non-equal relative times



commit b9889a5984785986072545ef8e61f33db9b28aae
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sat Aug 31 22:44:02 2013 -0600

    gd: Fix comparison of GDataGDReminders with non-equal relative times
    
    Two GDataGDReminders would previously compare as equal if their methods were
    equal but their relative times were not. Now they only compare equal if both
    their methods and their relative (or absolute) times are equal.
    
    This includes a test case.
    
    Spotted by Miao Yu <will yu yahoo com>.

 gdata/gd/gdata-gd-reminder.c |   13 +++++++------
 gdata/tests/general.c        |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 6 deletions(-)
---
diff --git a/gdata/gd/gdata-gd-reminder.c b/gdata/gd/gdata-gd-reminder.c
index 8ed7344..fab5f23 100644
--- a/gdata/gd/gdata-gd-reminder.c
+++ b/gdata/gd/gdata-gd-reminder.c
@@ -148,7 +148,7 @@ gdata_gd_reminder_class_init (GDataGDReminderClass *klass)
 static gint
 compare_with (GDataComparable *self, GDataComparable *other)
 {
-       gint method_cmp;
+       gint method_cmp, time_cmp;
        GDataGDReminder *a = (GDataGDReminder*) self, *b = (GDataGDReminder*) other;
 
        if (gdata_gd_reminder_is_absolute_time (a) != gdata_gd_reminder_is_absolute_time (b))
@@ -156,14 +156,15 @@ compare_with (GDataComparable *self, GDataComparable *other)
 
        method_cmp = g_strcmp0 (a->priv->method, b->priv->method);
        if (gdata_gd_reminder_is_absolute_time (a) == TRUE) {
-               if (method_cmp == 0 && a->priv->absolute_time == b->priv->absolute_time)
-                       return 0;
+               time_cmp = a->priv->absolute_time - b->priv->absolute_time;
        } else {
-               if (method_cmp == 0 && a->priv->relative_time == b->priv->relative_time)
-                       return 0;
+               time_cmp = a->priv->relative_time - b->priv->relative_time;
        }
 
-       return method_cmp;
+       if (method_cmp == 0)
+               return time_cmp;
+       else
+               return method_cmp;
 }
 
 static void
diff --git a/gdata/tests/general.c b/gdata/tests/general.c
index 51dd12d..74a71ba 100644
--- a/gdata/tests/general.c
+++ b/gdata/tests/general.c
@@ -2730,6 +2730,40 @@ test_gd_reminder_escaping (void)
 }
 
 static void
+test_gd_reminder_comparison (void)
+{
+       GDataGDReminder *reminder1, *reminder2;
+
+#define ASSERT_COMPARISON(op) \
+       g_assert_cmpint (gdata_comparable_compare (GDATA_COMPARABLE (reminder1), GDATA_COMPARABLE 
(reminder2)), op, 0)
+
+       /* Check for equality. */
+       reminder1 = gdata_gd_reminder_new (GDATA_GD_REMINDER_ALERT, -1, 15);
+       reminder2 = gdata_gd_reminder_new (GDATA_GD_REMINDER_ALERT, -1, 15);
+       ASSERT_COMPARISON(==);
+
+       /* Different methods, same time type, same time. */
+       gdata_gd_reminder_set_method (reminder1, GDATA_GD_REMINDER_SMS);
+       ASSERT_COMPARISON(>);
+
+       /* Same method, different time type, same time. */
+       gdata_gd_reminder_set_method (reminder1, GDATA_GD_REMINDER_ALERT);
+       gdata_gd_reminder_set_relative_time (reminder1, -1);
+       gdata_gd_reminder_set_absolute_time (reminder1, 5);
+       ASSERT_COMPARISON(>);
+
+       /* Same method, same time type, different time. */
+       gdata_gd_reminder_set_absolute_time (reminder1, -1);
+       gdata_gd_reminder_set_relative_time (reminder1, 20);
+       ASSERT_COMPARISON(>);
+
+       g_object_unref (reminder2);
+       g_object_unref (reminder1);
+
+#undef ASSERT_COMPARISON
+}
+
+static void
 test_gd_when (void)
 {
        GDataGDWhen *when, *when2;
@@ -4090,6 +4124,7 @@ main (int argc, char *argv[])
        g_test_add_func ("/gd/postal_address/escaping", test_gd_postal_address_escaping);
        g_test_add_func ("/gd/reminder", test_gd_reminder);
        g_test_add_func ("/gd/reminder/escaping", test_gd_reminder_escaping);
+       g_test_add_func ("/gd/reminder/comparison", test_gd_reminder_comparison);
        g_test_add_func ("/gd/when", test_gd_when);
        g_test_add_func ("/gd/when/escaping", test_gd_when_escaping);
        g_test_add_func ("/gd/where", test_gd_where);


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