From d303b5b0eb7c1ff43ac1d3ba01ed93430db1dfc8 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Fri, 14 Feb 2020 03:13:49 +0100 Subject: [PATCH] slicelistmodel: Fix two wrong computations Both of those are thinkos during math. Found by Mohammed Sadiq. Testcases triggering both have been added. --- gtk/gtkslicelistmodel.c | 4 ++-- testsuite/gtk/slicelistmodel.c | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/gtk/gtkslicelistmodel.c b/gtk/gtkslicelistmodel.c index a6aa4f3b7f..b8ceb0d0c5 100644 --- a/gtk/gtkslicelistmodel.c +++ b/gtk/gtkslicelistmodel.c @@ -133,7 +133,7 @@ gtk_slice_list_model_items_changed_cb (GListModel *model, if (position < self->offset) { guint skip = MIN (removed, added); - skip = MIN (skip, position - self->offset); + skip = MIN (skip, self->offset - position); position += skip; removed -= skip; @@ -149,7 +149,7 @@ gtk_slice_list_model_items_changed_cb (GListModel *model, g_assert (position >= self->offset); position -= self->offset; - changed = MIN (changed, self->size) - position; + changed = MIN (changed, self->size - position); g_list_model_items_changed (G_LIST_MODEL (self), position, changed, changed); } diff --git a/testsuite/gtk/slicelistmodel.c b/testsuite/gtk/slicelistmodel.c index 48619c4f82..c3ae685c59 100644 --- a/testsuite/gtk/slicelistmodel.c +++ b/testsuite/gtk/slicelistmodel.c @@ -322,6 +322,44 @@ test_changes (void) g_object_unref (slice); } +static void +test_bug_added_equals_removed (void) +{ + GtkSliceListModel *slice; + GListStore *store; + + store = new_store (1, 10, 1); + slice = new_model (store, 0, 10); + assert_model (slice, "1 2 3 4 5 6 7 8 9 10"); + assert_changes (slice, ""); + + splice (store, 9, 1, (guint[]) { 11 }, 1); + assert_model (slice, "1 2 3 4 5 6 7 8 9 11"); + assert_changes (slice, "9-1+1"); + + g_object_unref (store); + g_object_unref (slice); +} + +static void +test_bug_skip_amount (void) +{ + GtkSliceListModel *slice; + GListStore *store; + + store = new_store (1, 5, 1); + slice = new_model (store, 2, 2); + assert_model (slice, "3 4"); + assert_changes (slice, ""); + + splice (store, 0, 5, (guint[]) { 11, 12, 13, 14, 15 }, 5); + assert_model (slice, "13 14"); + assert_changes (slice, "0-2+2"); + + g_object_unref (store); + g_object_unref (slice); +} + int main (int argc, char *argv[]) { @@ -338,6 +376,8 @@ main (int argc, char *argv[]) #if GLIB_CHECK_VERSION (2, 58, 0) /* g_list_store_splice() is broken before 2.58 */ g_test_add_func ("/slicelistmodel/changes", test_changes); #endif + g_test_add_func ("/slicelistmodel/bug/added_equals_removed", test_bug_added_equals_removed); + g_test_add_func ("/slicelistmodel/bug/skip_amount", test_bug_skip_amount); return g_test_run (); }