[cogl] Add a conformance test for CoglBitmask



commit ec1c3406ada93feffb6f6e23b39924643e493e81
Author: Neil Roberts <neil linux intel com>
Date:   Fri Oct 28 15:00:54 2011 +0100

    Add a conformance test for CoglBitmask
    
    This adds a test which tries manipulating some bits on a CoglBitmask
    and verifies that it gets the expected result. This test is fairly
    unusual in that it is directly testing some internal Cogl code that
    isn't exposed through the public API. To make this work it directly
    includes the source for CoglBitmask.
    
    CoglBitmask does some somewhat dodgy things with converting longs to
    pointers and back so it makes sense to have a test case to verify that
    this is working on all platforms.
    
    Reviewed-by: Robert Bragg <robert linux intel com>

 tests/conform/Makefile.am         |   11 ++-
 tests/conform/test-bitmask.c      |  171 +++++++++++++++++++++++++++++++++++++
 tests/conform/test-conform-main.c |    2 +
 3 files changed, 182 insertions(+), 2 deletions(-)
---
diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am
index cc7bbdc..4f32991 100644
--- a/tests/conform/Makefile.am
+++ b/tests/conform/Makefile.am
@@ -34,6 +34,7 @@ unported_test_sources = \
 	$(NULL)
 
 test_sources = \
+	test-bitmask.c \
 	test-blend-strings.c \
 	test-depth-test.c \
 	test-color-mask.c \
@@ -103,12 +104,18 @@ clean-wrappers:
 # a phony rule that will generate symlink scripts for running individual tests
 BUILT_SOURCES = wrappers
 
-INCLUDES = -I$(top_srcdir)
+# The include of the $(buildir)/cogl directory here is to make it so
+# that tests that directly include Cogl source code for whitebox
+# testing (such as test-bitmask) will still compile
+INCLUDES = \
+	-I$(top_srcdir) \
+	-I$(top_builddir)/cogl
 
 test_conformance_CPPFLAGS = \
 	-DCOGL_ENABLE_EXPERIMENTAL_API \
 	-DCOGL_DISABLE_DEPRECATED \
-	-DTESTS_DATADIR=\""$(top_srcdir)/tests/data"\"
+	-DTESTS_DATADIR=\""$(top_srcdir)/tests/data"\" \
+	-DCLUTTER_COMPILATION
 
 test_conformance_CFLAGS = -g3 -O0 $(COGL_DEP_CFLAGS) $(COGL_EXTRA_CFLAGS)
 test_conformance_LDADD = $(COGL_DEP_LIBS) $(top_builddir)/cogl/libcogl.la
diff --git a/tests/conform/test-bitmask.c b/tests/conform/test-bitmask.c
new file mode 100644
index 0000000..5f67657
--- /dev/null
+++ b/tests/conform/test-bitmask.c
@@ -0,0 +1,171 @@
+#include <cogl/cogl.h>
+
+#include <string.h>
+#include <stdarg.h>
+
+#include "test-utils.h"
+
+/* This is testing CoglBitmask which is an internal data structure
+   within Cogl. Cogl doesn't export the symbols for this data type so
+   we just directly include the source instead */
+
+#include <cogl/cogl-bitmask.h>
+#include <cogl/cogl-bitmask.c>
+
+typedef struct
+{
+  int n_bits;
+  int *bits;
+} CheckData;
+
+static void
+check_bit (int bit_num, void *user_data)
+{
+  CheckData *data = user_data;
+  int i;
+
+  for (i = 0; i < data->n_bits; i++)
+    if (data->bits[i] == bit_num)
+      {
+        data->bits[i] = -1;
+        return;
+      }
+
+  g_assert_not_reached ();
+
+  return;
+}
+
+static void
+verify_bits (const CoglBitmask *bitmask,
+             ...)
+{
+  CheckData data;
+  va_list ap, ap_copy;
+  int i;
+
+  va_start (ap, bitmask);
+  G_VA_COPY (ap_copy, ap);
+
+  for (data.n_bits = 0; va_arg (ap, int) != -1; data.n_bits++);
+
+  data.bits = alloca (data.n_bits * (sizeof (int)));
+
+  G_VA_COPY (ap, ap_copy);
+
+  for (i = 0; i < data.n_bits; i++)
+    data.bits[i] = va_arg (ap, int);
+
+  _cogl_bitmask_foreach (bitmask, check_bit, &data);
+
+  for (i = 0; i < data.n_bits; i++)
+    g_assert_cmpint (data.bits[i], ==, -1);
+
+  for (i = 0; i < 1024; i++)
+    {
+      int j;
+
+      G_VA_COPY (ap, ap_copy);
+
+      for (j = 0; j < data.n_bits; j++)
+        if (va_arg (ap, int) == i)
+          break;
+
+      g_assert_cmpint (_cogl_bitmask_get (bitmask, i), ==, (j < data.n_bits));
+    }
+}
+
+void
+test_cogl_bitmask (TestUtilsGTestFixture *fixture,
+                   void *data)
+{
+  CoglBitmask bitmask;
+  CoglBitmask other_bitmask;
+  /* A dummy bit to make it use arrays sometimes */
+  int dummy_bit;
+  int i;
+
+  for (dummy_bit = -1; dummy_bit < 256; dummy_bit += 40)
+    {
+      _cogl_bitmask_init (&bitmask);
+      _cogl_bitmask_init (&other_bitmask);
+
+      if (dummy_bit != -1)
+        _cogl_bitmask_set (&bitmask, dummy_bit, TRUE);
+
+      verify_bits (&bitmask, dummy_bit, -1);
+
+      _cogl_bitmask_set (&bitmask, 1, TRUE);
+      _cogl_bitmask_set (&bitmask, 4, TRUE);
+      _cogl_bitmask_set (&bitmask, 5, TRUE);
+
+      verify_bits (&bitmask, 1, 4, 5, dummy_bit, -1);
+
+      _cogl_bitmask_set (&bitmask, 4, FALSE);
+
+      verify_bits (&bitmask, 1, 5, dummy_bit, -1);
+
+      _cogl_bitmask_clear_all (&bitmask);
+
+      verify_bits (&bitmask, -1);
+
+      if (dummy_bit != -1)
+        _cogl_bitmask_set (&bitmask, dummy_bit, TRUE);
+
+      verify_bits (&bitmask, dummy_bit, -1);
+
+      _cogl_bitmask_set (&bitmask, 1, TRUE);
+      _cogl_bitmask_set (&bitmask, 4, TRUE);
+      _cogl_bitmask_set (&bitmask, 5, TRUE);
+      _cogl_bitmask_set (&other_bitmask, 5, TRUE);
+      _cogl_bitmask_set (&other_bitmask, 6, TRUE);
+
+      _cogl_bitmask_set_bits (&bitmask, &other_bitmask);
+
+      verify_bits (&bitmask, 1, 4, 5, 6, dummy_bit, -1);
+      verify_bits (&other_bitmask, 5, 6, -1);
+
+      _cogl_bitmask_set (&bitmask, 6, FALSE);
+
+      verify_bits (&bitmask, 1, 4, 5, dummy_bit, -1);
+
+      _cogl_bitmask_xor_bits (&bitmask, &other_bitmask);
+
+      verify_bits (&bitmask, 1, 4, 6, dummy_bit, -1);
+      verify_bits (&other_bitmask, 5, 6, -1);
+
+      _cogl_bitmask_set_range (&bitmask, 5, TRUE);
+
+      verify_bits (&bitmask, 0, 1, 2, 3, 4, 6, dummy_bit, -1);
+
+      _cogl_bitmask_set_range (&bitmask, 4, FALSE);
+
+      verify_bits (&bitmask, 4, 6, dummy_bit, -1);
+
+      _cogl_bitmask_destroy (&other_bitmask);
+      _cogl_bitmask_destroy (&bitmask);
+    }
+
+  /* Extra tests for really long bitmasks */
+  _cogl_bitmask_init (&bitmask);
+  _cogl_bitmask_set_range (&bitmask, 400, TRUE);
+  _cogl_bitmask_init (&other_bitmask);
+  _cogl_bitmask_set (&other_bitmask, 5, TRUE);
+  _cogl_bitmask_xor_bits (&bitmask, &other_bitmask);
+
+  for (i = 0; i < 1024; i++)
+    g_assert_cmpint (_cogl_bitmask_get (&bitmask, i),
+                     ==,
+                     (i == 5 ? FALSE :
+                      i < 400 ? TRUE :
+                      FALSE));
+
+  _cogl_bitmask_set_range (&other_bitmask, 500, TRUE);
+  _cogl_bitmask_set_bits (&bitmask, &other_bitmask);
+
+  for (i = 0; i < 1024; i++)
+    g_assert_cmpint (_cogl_bitmask_get (&bitmask, i), ==, (i < 500));
+
+  if (g_test_verbose ())
+    g_print ("OK\n");
+}
diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index 6e25c2c..ecb4bb2 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -162,6 +162,8 @@ main (int argc, char **argv)
 
   ADD_TEST ("/cogl/shaders", test_cogl_just_vertex_shader);
 
+  ADD_TEST ("/cogl/internal/bitmask", test_cogl_bitmask);
+
   /* left to the end because they aren't currently very orthogonal and tend to
    * break subsequent tests! */
   UNPORTED_TEST ("/cogl", test_cogl_viewport);



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