[Spice-commits] 7 commits - common/region.c tests/Makefile.am tests/meson.build tests/test-region.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jun 27 12:18:19 UTC 2018


 common/region.c     |  378 -----------------------------------------------
 tests/Makefile.am   |   12 +
 tests/meson.build   |    2 
 tests/test-region.c |  418 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 431 insertions(+), 379 deletions(-)

New commits:
commit 5110dbcbb77246488c19571fa5923b14a51da7a3
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 11:47:22 2018 +0200

    test-region: Add g_assert() checks
    
    At the moment, test success/failure is only printed to stdout. This
    commit adds some g_assert() so that test failures can be automatically
    detected.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index 2effc99..2ece5a7 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -103,19 +103,29 @@ static void test(const QRegion *r1, const QRegion *r2, int *expected)
 {
     g_debug("r1 is_empty %s [%s]",
             region_is_empty(r1) ? "TRUE" : "FALSE",
-            (region_is_empty(r1) == *(expected++)) ? "OK" : "ERR");
+            (region_is_empty(r1) == *expected) ? "OK" : "ERR");
+    g_assert_cmpint(region_is_empty(r1), ==, *expected);
+    expected++;
     g_debug("r2 is_empty %s [%s]",
             region_is_empty(r2) ? "TRUE" : "FALSE",
-            (region_is_empty(r2) == *(expected++)) ? "OK" : "ERR");
+            (region_is_empty(r2) == *expected) ? "OK" : "ERR");
+    g_assert_cmpint(region_is_empty(r2), ==, *expected);
+    expected++;
     g_debug("is_equal %s [%s]",
             region_is_equal(r1, r2) ? "TRUE" : "FALSE",
-            (region_is_equal(r1, r2) == *(expected++)) ? "OK" : "ERR");
+            (region_is_equal(r1, r2) == *expected) ? "OK" : "ERR");
+    g_assert_cmpint(region_is_equal(r1, r2), ==, *expected);
+    expected++;
     g_debug("intersects %s [%s]",
             region_intersects(r1, r2) ? "TRUE" : "FALSE",
-            (region_intersects(r1, r2) == *(expected++)) ? "OK" : "ERR");
+            (region_intersects(r1, r2) == *expected) ? "OK" : "ERR");
+    g_assert_cmpint(region_intersects(r1, r2), ==, *expected);
+    expected++;
     g_debug("contains %s [%s]",
             region_contains(r1, r2) ? "TRUE" : "FALSE",
-            (region_contains(r1, r2) == *(expected++)) ? "OK" : "ERR");
+            (region_contains(r1, r2) == *expected) ? "OK" : "ERR");
+    g_assert_cmpint(region_contains(r1, r2), ==, *expected);
+    expected++;
 }
 
 enum {
@@ -388,6 +398,7 @@ static void test_region(void)
                 g_debug("r2:");
                 region_dump(r2, "");
             }
+            g_assert_cmpint(res1, ==, res2);
             j++;
         }
     }
commit cfbae20c48972eda66834c9482d11d41baba427b
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 11:47:21 2018 +0200

    test-region: Replace spice_assert() with g_assert_true()
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index 4e0947e..2effc99 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -73,7 +73,7 @@ static void rect_set(SpiceRect *r, int32_t top, int32_t left, int32_t bottom, in
     r->left = left;
     r->bottom = bottom;
     r->right = right;
-    spice_assert(rect_is_valid(r));
+    g_assert_true(rect_is_valid(r));
 }
 
 static void random_region(QRegion *reg)
commit 968ce158f03548e73b45055deccd716a63ba6183
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 11:47:20 2018 +0200

    test-region: Remove unneeded printf
    
    __FUNCTION__ will always be rect_is_valid, and there is a g_assert to
    check the region is valid, so we will get notified anyway if the
    validity check fails.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index b7f5a22..4e0947e 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -62,7 +62,6 @@ static int slow_region_test(const QRegion *rgn, const QRegion *other_rgn, int qu
 static int rect_is_valid(const SpiceRect *r)
 {
     if (r->top > r->bottom || r->left > r->right) {
-        printf("%s: invalid rect\n", __FUNCTION__);
         return FALSE;
     }
     return TRUE;
commit 30ff9c3807c889608cfae3a920e316a168d03e5c
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 12:48:10 2018 +0200

    test-region: Don't call region_dump() by default
    
    This prints to stdout, which is not desirable in an automated test.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index a5a5351..b7f5a22 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -142,7 +142,7 @@ static void test_region(void)
     region_init(r2);
 
     g_debug("dump r1 empty rgn [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = TRUE;
@@ -153,7 +153,7 @@ static void test_region(void)
 
     region_clone(r3, r1);
     g_debug("dump r3 clone rgn [%s]", region_is_valid(r3) ? "VALID" : "INVALID");
-    region_dump(r3, "");
+    //region_dump(r3, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = TRUE;
@@ -166,7 +166,7 @@ static void test_region(void)
     rect_set(r, 0, 0, 100, 100);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -179,7 +179,7 @@ static void test_region(void)
     rect_set(r, 0, 0, 0, 0);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = TRUE;
@@ -191,7 +191,7 @@ static void test_region(void)
     rect_set(r, -100, -100, 0, 0);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -204,7 +204,7 @@ static void test_region(void)
     rect_set(r, -100, -100, 100, 100);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -220,7 +220,7 @@ static void test_region(void)
     rect_set(r, 100, 100, 200, 200);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -232,7 +232,7 @@ static void test_region(void)
     rect_set(r, 300, 300, 400, 400);
     region_add(r1, r);
     g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
+    //region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -244,7 +244,7 @@ static void test_region(void)
     rect_set(r, 500, 500, 600, 600);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -260,7 +260,7 @@ static void test_region(void)
     rect_set(r, 300, 300, 400, 400);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = TRUE;
@@ -274,7 +274,7 @@ static void test_region(void)
     rect_set(r, 100, 100, 200, 200);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -288,7 +288,7 @@ static void test_region(void)
     rect_set(r, -2000, -2000, -1000, -1000);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -302,7 +302,7 @@ static void test_region(void)
     rect_set(r, -2000, -2000, 1000, 1000);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -316,7 +316,7 @@ static void test_region(void)
     rect_set(r, 150, 150, 175, 175);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -330,7 +330,7 @@ static void test_region(void)
     rect_set(r, 150, 150, 350, 350);
     region_add(r2, r);
     g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -341,7 +341,7 @@ static void test_region(void)
 
     region_and(r2, r1);
     g_debug("dump r2 and r1 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
+    //region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = FALSE;
@@ -353,7 +353,7 @@ static void test_region(void)
 
     region_clone(r3, r1);
     g_debug("dump r3 clone rgn [%s]", region_is_valid(r3) ? "VALID" : "INVALID");
-    region_dump(r3, "");
+    //region_dump(r3, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
     expected[EXPECT_EQUAL] = TRUE;
commit 3a6a84e172bb0531ae49f66f4bae44949155b46b
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 12:48:09 2018 +0200

    test-region: Replace direct printf with g_debug() calls
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index 5a00cce..a5a5351 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -102,21 +102,21 @@ static void random_region(QRegion *reg)
 
 static void test(const QRegion *r1, const QRegion *r2, int *expected)
 {
-    printf("r1 is_empty %s [%s]\n",
-           region_is_empty(r1) ? "TRUE" : "FALSE",
-           (region_is_empty(r1) == *(expected++)) ? "OK" : "ERR");
-    printf("r2 is_empty %s [%s]\n",
-           region_is_empty(r2) ? "TRUE" : "FALSE",
-           (region_is_empty(r2) == *(expected++)) ? "OK" : "ERR");
-    printf("is_equal %s [%s]\n",
-           region_is_equal(r1, r2) ? "TRUE" : "FALSE",
-           (region_is_equal(r1, r2) == *(expected++)) ? "OK" : "ERR");
-    printf("intersects %s [%s]\n",
-           region_intersects(r1, r2) ? "TRUE" : "FALSE",
-           (region_intersects(r1, r2) == *(expected++)) ? "OK" : "ERR");
-    printf("contains %s [%s]\n",
-           region_contains(r1, r2) ? "TRUE" : "FALSE",
-           (region_contains(r1, r2) == *(expected++)) ? "OK" : "ERR");
+    g_debug("r1 is_empty %s [%s]",
+            region_is_empty(r1) ? "TRUE" : "FALSE",
+            (region_is_empty(r1) == *(expected++)) ? "OK" : "ERR");
+    g_debug("r2 is_empty %s [%s]",
+            region_is_empty(r2) ? "TRUE" : "FALSE",
+            (region_is_empty(r2) == *(expected++)) ? "OK" : "ERR");
+    g_debug("is_equal %s [%s]",
+            region_is_equal(r1, r2) ? "TRUE" : "FALSE",
+            (region_is_equal(r1, r2) == *(expected++)) ? "OK" : "ERR");
+    g_debug("intersects %s [%s]",
+            region_intersects(r1, r2) ? "TRUE" : "FALSE",
+            (region_intersects(r1, r2) == *(expected++)) ? "OK" : "ERR");
+    g_debug("contains %s [%s]",
+            region_contains(r1, r2) ? "TRUE" : "FALSE",
+            (region_contains(r1, r2) == *(expected++)) ? "OK" : "ERR");
 }
 
 enum {
@@ -141,7 +141,7 @@ static void test_region(void)
     region_init(r1);
     region_init(r2);
 
-    printf("dump r1 empty rgn [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 empty rgn [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -149,10 +149,10 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clone(r3, r1);
-    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
+    g_debug("dump r3 clone rgn [%s]", region_is_valid(r3) ? "VALID" : "INVALID");
     region_dump(r3, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -161,11 +161,11 @@ static void test_region(void)
     expected[EXPECT_CONT] = TRUE;
     test(r1, r3, expected);
     region_destroy(r3);
-    printf("\n");
+    g_debug("\n");
 
     rect_set(r, 0, 0, 100, 100);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -173,12 +173,12 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r1);
     rect_set(r, 0, 0, 0, 0);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = TRUE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -186,11 +186,11 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     rect_set(r, -100, -100, 0, 0);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -198,12 +198,12 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r1);
     rect_set(r, -100, -100, 100, 100);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -211,7 +211,7 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
 
     region_clear(r1);
@@ -219,7 +219,7 @@ static void test_region(void)
 
     rect_set(r, 100, 100, 200, 200);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -227,11 +227,11 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     rect_set(r, 300, 300, 400, 400);
     region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    g_debug("dump r1 [%s]", region_is_valid(r1) ? "VALID" : "INVALID");
     region_dump(r1, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = TRUE;
@@ -239,11 +239,11 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     rect_set(r, 500, 500, 600, 600);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -251,7 +251,7 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = FALSE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
@@ -259,7 +259,7 @@ static void test_region(void)
     region_add(r2, r);
     rect_set(r, 300, 300, 400, 400);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -267,13 +267,13 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
     rect_set(r, 100, 100, 200, 200);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -281,13 +281,13 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
     rect_set(r, -2000, -2000, -1000, -1000);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -295,13 +295,13 @@ static void test_region(void)
     expected[EXPECT_SECT] = FALSE;
     expected[EXPECT_CONT] = FALSE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
     rect_set(r, -2000, -2000, 1000, 1000);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -309,13 +309,13 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = FALSE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
     rect_set(r, 150, 150, 175, 175);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -323,13 +323,13 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_clear(r2);
 
     rect_set(r, 150, 150, 350, 350);
     region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -337,10 +337,10 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = FALSE;
     test(r1, r2, expected);
-    printf("\n");
+    g_debug("\n");
 
     region_and(r2, r1);
-    printf("dump r2 and r1 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    g_debug("dump r2 and r1 [%s]", region_is_valid(r2) ? "VALID" : "INVALID");
     region_dump(r2, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -348,11 +348,11 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = FALSE;
     test(r2, r1, expected);
-    printf("\n");
+    g_debug("\n");
 
 
     region_clone(r3, r1);
-    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
+    g_debug("dump r3 clone rgn [%s]", region_is_valid(r3) ? "VALID" : "INVALID");
     region_dump(r3, "");
     expected[EXPECT_R1_EMPTY] = FALSE;
     expected[EXPECT_R2_EMPTY] = FALSE;
@@ -360,7 +360,7 @@ static void test_region(void)
     expected[EXPECT_SECT] = TRUE;
     expected[EXPECT_CONT] = TRUE;
     test(r1, r3, expected);
-    printf("\n");
+    g_debug("\n");
 
     j = 0;
     for (i = 0; i < 1000000; i++) {
@@ -382,11 +382,11 @@ static void test_region(void)
             res1 = region_test(r1, r2, tests[test]);
             res2 = slow_region_test(r1, r2, tests[test]);
             if (res1 != res2) {
-                printf ("Error in region_test %d, got %d, expected %d, query=%d\n",
-                        j, res1, res2, tests[test]);
-                printf ("r1:\n");
+                g_warning("Error in region_test %d, got %d, expected %d, query=%d",
+                          j, res1, res2, tests[test]);
+                g_debug("r1:");
                 region_dump(r1, "");
-                printf ("r2:\n");
+                g_debug("r2:");
                 region_dump(r2, "");
             }
             j++;
commit e13c1d66b7a58752be642905b9904d3cad1763d5
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Wed Jun 27 11:47:18 2018 +0200

    test-region: Use GTest API
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/tests/test-region.c b/tests/test-region.c
index 38b3203..5a00cce 100644
--- a/tests/test-region.c
+++ b/tests/test-region.c
@@ -127,7 +127,7 @@ enum {
     EXPECT_CONT,
 };
 
-int main(void)
+static void test_region(void)
 {
     QRegion _r1, _r2, _r3;
     QRegion *r1 = &_r1;
@@ -396,6 +396,13 @@ int main(void)
     region_destroy(r3);
     region_destroy(r1);
     region_destroy(r2);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/spice-common/region", test_region);
 
-    return 0;
+    return g_test_run();
 }
commit 0ac8e55ddfe465437c3c18717c3e68487edb83ea
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Jun 27 11:47:17 2018 +0200

    test-region: Create proper test for region from source code
    
    region.c contained code to test the module.
    Separate test code into a proper test.
    Test is copied verbatim from the original code.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/common/region.c b/common/region.c
index e9abb32..c950a9b 100644
--- a/common/region.c
+++ b/common/region.c
@@ -522,381 +522,3 @@ void region_dump(const QRegion *rgn, const char *prefix)
                rects[i].y2);
     }
 }
-
-#ifdef REGION_TEST
-
-static int slow_region_test(const QRegion *rgn, const QRegion *other_rgn, int query)
-{
-    pixman_region32_t intersection;
-    int res;
-
-    pixman_region32_init(&intersection);
-    pixman_region32_intersect(&intersection,
-                              (pixman_region32_t *)rgn,
-                              (pixman_region32_t *)other_rgn);
-
-    res = 0;
-
-    if (query & REGION_TEST_SHARED &&
-        pixman_region32_not_empty(&intersection)) {
-        res |= REGION_TEST_SHARED;
-    }
-
-    if (query & REGION_TEST_LEFT_EXCLUSIVE &&
-        !pixman_region32_equal(&intersection, (pixman_region32_t *)rgn)) {
-        res |= REGION_TEST_LEFT_EXCLUSIVE;
-    }
-
-    if (query & REGION_TEST_RIGHT_EXCLUSIVE &&
-        !pixman_region32_equal(&intersection, (pixman_region32_t *)other_rgn)) {
-        res |= REGION_TEST_RIGHT_EXCLUSIVE;
-    }
-
-    pixman_region32_fini(&intersection);
-
-    return res;
-}
-
-
-static int rect_is_valid(const SpiceRect *r)
-{
-    if (r->top > r->bottom || r->left > r->right) {
-        printf("%s: invalid rect\n", __FUNCTION__);
-        return FALSE;
-    }
-    return TRUE;
-}
-
-static void rect_set(SpiceRect *r, int32_t top, int32_t left, int32_t bottom, int32_t right)
-{
-    r->top = top;
-    r->left = left;
-    r->bottom = bottom;
-    r->right = right;
-    spice_assert(rect_is_valid(r));
-}
-
-static void random_region(QRegion *reg)
-{
-    int i;
-    int num_rects;
-    int x, y, w, h;
-    SpiceRect _r;
-    SpiceRect *r = &_r;
-
-    region_clear(reg);
-
-    num_rects = rand() % 20;
-    for (i = 0; i < num_rects; i++) {
-        x = rand()%100;
-        y = rand()%100;
-        w = rand()%100;
-        h = rand()%100;
-        rect_set(r,
-                 x, y,
-                 x+w, y+h);
-        region_add(reg, r);
-    }
-}
-
-static void test(const QRegion *r1, const QRegion *r2, int *expected)
-{
-    printf("r1 is_empty %s [%s]\n",
-           region_is_empty(r1) ? "TRUE" : "FALSE",
-           (region_is_empty(r1) == *(expected++)) ? "OK" : "ERR");
-    printf("r2 is_empty %s [%s]\n",
-           region_is_empty(r2) ? "TRUE" : "FALSE",
-           (region_is_empty(r2) == *(expected++)) ? "OK" : "ERR");
-    printf("is_equal %s [%s]\n",
-           region_is_equal(r1, r2) ? "TRUE" : "FALSE",
-           (region_is_equal(r1, r2) == *(expected++)) ? "OK" : "ERR");
-    printf("intersects %s [%s]\n",
-           region_intersects(r1, r2) ? "TRUE" : "FALSE",
-           (region_intersects(r1, r2) == *(expected++)) ? "OK" : "ERR");
-    printf("contains %s [%s]\n",
-           region_contains(r1, r2) ? "TRUE" : "FALSE",
-           (region_contains(r1, r2) == *(expected++)) ? "OK" : "ERR");
-}
-
-enum {
-    EXPECT_R1_EMPTY,
-    EXPECT_R2_EMPTY,
-    EXPECT_EQUAL,
-    EXPECT_SECT,
-    EXPECT_CONT,
-};
-
-int main(void)
-{
-    QRegion _r1, _r2, _r3;
-    QRegion *r1 = &_r1;
-    QRegion *r2 = &_r2;
-    QRegion *r3 = &_r3;
-    SpiceRect _r;
-    SpiceRect *r = &_r;
-    int expected[5];
-    int i, j;
-
-    region_init(r1);
-    region_init(r2);
-
-    printf("dump r1 empty rgn [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = TRUE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = TRUE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clone(r3, r1);
-    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
-    region_dump(r3, "");
-    expected[EXPECT_R1_EMPTY] = TRUE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = TRUE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r3, expected);
-    region_destroy(r3);
-    printf("\n");
-
-    rect_set(r, 0, 0, 100, 100);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r1);
-    rect_set(r, 0, 0, 0, 0);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = TRUE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = TRUE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    rect_set(r, -100, -100, 0, 0);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r1);
-    rect_set(r, -100, -100, 100, 100);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-
-    region_clear(r1);
-    region_clear(r2);
-
-    rect_set(r, 100, 100, 200, 200);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    rect_set(r, 300, 300, 400, 400);
-    region_add(r1, r);
-    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
-    region_dump(r1, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = TRUE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    rect_set(r, 500, 500, 600, 600);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = FALSE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, 100, 100, 200, 200);
-    region_add(r2, r);
-    rect_set(r, 300, 300, 400, 400);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = TRUE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, 100, 100, 200, 200);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, -2000, -2000, -1000, -1000);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = FALSE;
-    expected[EXPECT_CONT] = FALSE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, -2000, -2000, 1000, 1000);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = FALSE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, 150, 150, 175, 175);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_clear(r2);
-
-    rect_set(r, 150, 150, 350, 350);
-    region_add(r2, r);
-    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = FALSE;
-    test(r1, r2, expected);
-    printf("\n");
-
-    region_and(r2, r1);
-    printf("dump r2 and r1 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
-    region_dump(r2, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = FALSE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = FALSE;
-    test(r2, r1, expected);
-    printf("\n");
-
-
-    region_clone(r3, r1);
-    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
-    region_dump(r3, "");
-    expected[EXPECT_R1_EMPTY] = FALSE;
-    expected[EXPECT_R2_EMPTY] = FALSE;
-    expected[EXPECT_EQUAL] = TRUE;
-    expected[EXPECT_SECT] = TRUE;
-    expected[EXPECT_CONT] = TRUE;
-    test(r1, r3, expected);
-    printf("\n");
-
-    j = 0;
-    for (i = 0; i < 1000000; i++) {
-        int res1, res2, test;
-        int tests[] = {
-            REGION_TEST_LEFT_EXCLUSIVE,
-            REGION_TEST_RIGHT_EXCLUSIVE,
-            REGION_TEST_SHARED,
-            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_RIGHT_EXCLUSIVE,
-            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_SHARED,
-            REGION_TEST_RIGHT_EXCLUSIVE | REGION_TEST_SHARED,
-            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_RIGHT_EXCLUSIVE | REGION_TEST_SHARED
-        };
-
-        random_region(r1);
-        random_region(r2);
-
-        for (test = 0; test < 7; test++) {
-            res1 = region_test(r1, r2, tests[test]);
-            res2 = slow_region_test(r1, r2, tests[test]);
-            if (res1 != res2) {
-                printf ("Error in region_test %d, got %d, expected %d, query=%d\n",
-                        j, res1, res2, tests[test]);
-                printf ("r1:\n");
-                region_dump(r1, "");
-                printf ("r2:\n");
-                region_dump(r2, "");
-            }
-            j++;
-        }
-    }
-
-    region_destroy(r3);
-    region_destroy(r1);
-    region_destroy(r2);
-
-    return 0;
-}
-
-#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7022808..457bb23 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -54,6 +54,18 @@ test_quic_LDADD =					\
 	$(NULL)
 endif
 
+TESTS += test_region
+test_region_SOURCES = \
+	test-region.c \
+	$(NULL)
+test_region_CFLAGS =			\
+	-I$(top_srcdir)			\
+	$(SPICE_COMMON_CFLAGS)		\
+	$(PROTOCOL_CFLAGS)		\
+	$(NULL)
+test_region_LDADD =					\
+	$(top_builddir)/common/libspice-common.la	\
+	$(NULL)
 
 # Avoid need for python(pyparsing) by end users
 TEST_MARSHALLERS =				\
diff --git a/tests/meson.build b/tests/meson.build
index 1dc0e5c..94c72c6 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,7 +1,7 @@
 #
 # Build tests
 #
-tests = ['test-logging']
+tests = ['test-logging', 'test-region']
 tests_deps = [spice_common_dep]
 
 if spice_common_generate_code == 'all'
diff --git a/tests/test-region.c b/tests/test-region.c
new file mode 100644
index 0000000..38b3203
--- /dev/null
+++ b/tests/test-region.c
@@ -0,0 +1,401 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2009 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <spice/macros.h>
+
+#include <common/region.h>
+
+static int slow_region_test(const QRegion *rgn, const QRegion *other_rgn, int query)
+{
+    pixman_region32_t intersection;
+    int res;
+
+    pixman_region32_init(&intersection);
+    pixman_region32_intersect(&intersection,
+                              (pixman_region32_t *)rgn,
+                              (pixman_region32_t *)other_rgn);
+
+    res = 0;
+
+    if (query & REGION_TEST_SHARED &&
+        pixman_region32_not_empty(&intersection)) {
+        res |= REGION_TEST_SHARED;
+    }
+
+    if (query & REGION_TEST_LEFT_EXCLUSIVE &&
+        !pixman_region32_equal(&intersection, (pixman_region32_t *)rgn)) {
+        res |= REGION_TEST_LEFT_EXCLUSIVE;
+    }
+
+    if (query & REGION_TEST_RIGHT_EXCLUSIVE &&
+        !pixman_region32_equal(&intersection, (pixman_region32_t *)other_rgn)) {
+        res |= REGION_TEST_RIGHT_EXCLUSIVE;
+    }
+
+    pixman_region32_fini(&intersection);
+
+    return res;
+}
+
+
+static int rect_is_valid(const SpiceRect *r)
+{
+    if (r->top > r->bottom || r->left > r->right) {
+        printf("%s: invalid rect\n", __FUNCTION__);
+        return FALSE;
+    }
+    return TRUE;
+}
+
+static void rect_set(SpiceRect *r, int32_t top, int32_t left, int32_t bottom, int32_t right)
+{
+    r->top = top;
+    r->left = left;
+    r->bottom = bottom;
+    r->right = right;
+    spice_assert(rect_is_valid(r));
+}
+
+static void random_region(QRegion *reg)
+{
+    int i;
+    int num_rects;
+    int x, y, w, h;
+    SpiceRect _r;
+    SpiceRect *r = &_r;
+
+    region_clear(reg);
+
+    num_rects = rand() % 20;
+    for (i = 0; i < num_rects; i++) {
+        x = rand()%100;
+        y = rand()%100;
+        w = rand()%100;
+        h = rand()%100;
+        rect_set(r,
+                 x, y,
+                 x+w, y+h);
+        region_add(reg, r);
+    }
+}
+
+static void test(const QRegion *r1, const QRegion *r2, int *expected)
+{
+    printf("r1 is_empty %s [%s]\n",
+           region_is_empty(r1) ? "TRUE" : "FALSE",
+           (region_is_empty(r1) == *(expected++)) ? "OK" : "ERR");
+    printf("r2 is_empty %s [%s]\n",
+           region_is_empty(r2) ? "TRUE" : "FALSE",
+           (region_is_empty(r2) == *(expected++)) ? "OK" : "ERR");
+    printf("is_equal %s [%s]\n",
+           region_is_equal(r1, r2) ? "TRUE" : "FALSE",
+           (region_is_equal(r1, r2) == *(expected++)) ? "OK" : "ERR");
+    printf("intersects %s [%s]\n",
+           region_intersects(r1, r2) ? "TRUE" : "FALSE",
+           (region_intersects(r1, r2) == *(expected++)) ? "OK" : "ERR");
+    printf("contains %s [%s]\n",
+           region_contains(r1, r2) ? "TRUE" : "FALSE",
+           (region_contains(r1, r2) == *(expected++)) ? "OK" : "ERR");
+}
+
+enum {
+    EXPECT_R1_EMPTY,
+    EXPECT_R2_EMPTY,
+    EXPECT_EQUAL,
+    EXPECT_SECT,
+    EXPECT_CONT,
+};
+
+int main(void)
+{
+    QRegion _r1, _r2, _r3;
+    QRegion *r1 = &_r1;
+    QRegion *r2 = &_r2;
+    QRegion *r3 = &_r3;
+    SpiceRect _r;
+    SpiceRect *r = &_r;
+    int expected[5];
+    int i, j;
+
+    region_init(r1);
+    region_init(r2);
+
+    printf("dump r1 empty rgn [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = TRUE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = TRUE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clone(r3, r1);
+    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
+    region_dump(r3, "");
+    expected[EXPECT_R1_EMPTY] = TRUE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = TRUE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r3, expected);
+    region_destroy(r3);
+    printf("\n");
+
+    rect_set(r, 0, 0, 100, 100);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r1);
+    rect_set(r, 0, 0, 0, 0);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = TRUE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = TRUE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    rect_set(r, -100, -100, 0, 0);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r1);
+    rect_set(r, -100, -100, 100, 100);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+
+    region_clear(r1);
+    region_clear(r2);
+
+    rect_set(r, 100, 100, 200, 200);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    rect_set(r, 300, 300, 400, 400);
+    region_add(r1, r);
+    printf("dump r1 [%s]\n", region_is_valid(r1) ? "VALID" : "INVALID");
+    region_dump(r1, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = TRUE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    rect_set(r, 500, 500, 600, 600);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = FALSE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, 100, 100, 200, 200);
+    region_add(r2, r);
+    rect_set(r, 300, 300, 400, 400);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = TRUE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, 100, 100, 200, 200);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, -2000, -2000, -1000, -1000);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = FALSE;
+    expected[EXPECT_CONT] = FALSE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, -2000, -2000, 1000, 1000);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = FALSE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, 150, 150, 175, 175);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_clear(r2);
+
+    rect_set(r, 150, 150, 350, 350);
+    region_add(r2, r);
+    printf("dump r2 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = FALSE;
+    test(r1, r2, expected);
+    printf("\n");
+
+    region_and(r2, r1);
+    printf("dump r2 and r1 [%s]\n", region_is_valid(r2) ? "VALID" : "INVALID");
+    region_dump(r2, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = FALSE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = FALSE;
+    test(r2, r1, expected);
+    printf("\n");
+
+
+    region_clone(r3, r1);
+    printf("dump r3 clone rgn [%s]\n", region_is_valid(r3) ? "VALID" : "INVALID");
+    region_dump(r3, "");
+    expected[EXPECT_R1_EMPTY] = FALSE;
+    expected[EXPECT_R2_EMPTY] = FALSE;
+    expected[EXPECT_EQUAL] = TRUE;
+    expected[EXPECT_SECT] = TRUE;
+    expected[EXPECT_CONT] = TRUE;
+    test(r1, r3, expected);
+    printf("\n");
+
+    j = 0;
+    for (i = 0; i < 1000000; i++) {
+        int res1, res2, test;
+        int tests[] = {
+            REGION_TEST_LEFT_EXCLUSIVE,
+            REGION_TEST_RIGHT_EXCLUSIVE,
+            REGION_TEST_SHARED,
+            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_RIGHT_EXCLUSIVE,
+            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_SHARED,
+            REGION_TEST_RIGHT_EXCLUSIVE | REGION_TEST_SHARED,
+            REGION_TEST_LEFT_EXCLUSIVE | REGION_TEST_RIGHT_EXCLUSIVE | REGION_TEST_SHARED
+        };
+
+        random_region(r1);
+        random_region(r2);
+
+        for (test = 0; test < 7; test++) {
+            res1 = region_test(r1, r2, tests[test]);
+            res2 = slow_region_test(r1, r2, tests[test]);
+            if (res1 != res2) {
+                printf ("Error in region_test %d, got %d, expected %d, query=%d\n",
+                        j, res1, res2, tests[test]);
+                printf ("r1:\n");
+                region_dump(r1, "");
+                printf ("r2:\n");
+                region_dump(r2, "");
+            }
+            j++;
+        }
+    }
+
+    region_destroy(r3);
+    region_destroy(r1);
+    region_destroy(r2);
+
+    return 0;
+}


More information about the Spice-commits mailing list