[gtk/matthiasc/for-master] bitset: Fix the right-shift implementation



commit 6a6146a9e0cee92acf6204bfd4787744aae3c0ab
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jun 28 16:36:03 2020 -0400

    bitset: Fix the right-shift implementation
    
    This was not doing the right thing at all.
    
    This commit also adds tests for left- and
    right-shift.

 gtk/gtkbitset.c        |  4 +--
 testsuite/gtk/bitset.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkbitset.c b/gtk/gtkbitset.c
index 944b899de6..c4116bd87c 100644
--- a/gtk/gtkbitset.c
+++ b/gtk/gtkbitset.c
@@ -604,8 +604,8 @@ gtk_bitset_shift_right (GtkBitset *self,
   original = gtk_bitset_copy (self);
   gtk_bitset_remove_all (self);
 
-  for (loop = gtk_bitset_iter_init_at (&iter, original, amount, &value);
-       loop && value >= G_MAXUINT - amount;
+  for (loop = gtk_bitset_iter_init_first (&iter, original, &value);
+       loop && value <= G_MAXUINT - amount;
        loop = gtk_bitset_iter_next (&iter, &value))
     {
       gtk_bitset_add (self, value + amount);
diff --git a/testsuite/gtk/bitset.c b/testsuite/gtk/bitset.c
index 5c7d0d16d9..de7d4654eb 100644
--- a/testsuite/gtk/bitset.c
+++ b/testsuite/gtk/bitset.c
@@ -350,6 +350,74 @@ test_subtract (void)
     }
 }
 
+static void
+test_shift_left (void)
+{
+  guint i, j, k, min, max;
+  GtkBitset *iset, *testset;
+
+  for (i = 0; i < G_N_ELEMENTS (bitsets); i++)
+    {
+      iset = bitsets[i].create();
+
+      for (j = 1; j < 10000000; j *= 10)
+        {
+          testset = gtk_bitset_copy (iset);
+
+          gtk_bitset_shift_left (testset, j);
+
+          min = MIN (gtk_bitset_get_minimum (iset), gtk_bitset_get_minimum (testset));
+          max = MAX (gtk_bitset_get_maximum (iset), gtk_bitset_get_maximum (testset));
+
+          for (k = min; k <= max; k++)
+            {
+              if (k >= j)
+                g_assert_cmpint (gtk_bitset_contains (iset, k), ==, gtk_bitset_contains (testset, k - j));
+            }
+
+          gtk_bitset_unref (testset);
+        }
+
+       gtk_bitset_unref (iset);
+    }
+}
+
+static void
+test_shift_right (void)
+{
+  guint i, j, k, min, max;
+  GtkBitset *iset, *testset;
+
+  for (i = 0; i < G_N_ELEMENTS (bitsets); i++)
+    {
+      iset = bitsets[i].create();
+
+      for (j = 1; j < 10000000; j *= 10)
+        {
+          testset = gtk_bitset_copy (iset);
+
+          gtk_bitset_shift_right (testset, j);
+
+          min = MIN (gtk_bitset_get_minimum (iset), gtk_bitset_get_minimum (testset));
+          max = MAX (gtk_bitset_get_maximum (iset), gtk_bitset_get_maximum (testset));
+
+          for (k = min; k <= max; k++)
+            {
+              if (k <= G_MAXUINT - j)
+                {
+                if (gtk_bitset_contains (iset, k) != gtk_bitset_contains (testset, k + j))
+                  g_print ("right-shift fail set %u shift %u test %u\n", i, j, k);
+                g_assert_cmpint (gtk_bitset_contains (iset, k), ==, gtk_bitset_contains (testset, k + j));
+                }
+            }
+
+          gtk_bitset_unref (testset);
+        }
+
+       gtk_bitset_unref (iset);
+    }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -364,6 +432,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/bitset/intersect", test_intersect);
   g_test_add_func ("/bitset/difference", test_difference);
   g_test_add_func ("/bitset/subtract", test_subtract);
+  g_test_add_func ("/bitset/shift-left", test_shift_left);
+  g_test_add_func ("/bitset/shift-right", test_shift_right);
 
   return g_test_run ();
 }


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