From d264b3009015d052619cf3ac561d4222d996c5c7 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Thu, 30 May 2013 15:06:24 +0100 Subject: [PATCH] units: remove test-bitmask and add UNIT_TEST instead This moves the code in test-bitmask into a UNIT_TEST() directly in cogl-bitmask.c which will now be run as a tests/unit/ test. Reviewed-by: Neil Roberts (cherry picked from commit 693c85e0cde8a1ffbffc03a5f8fcc1f92e8d0ac7) Includes fix to build conform tests with -I$(top_builddir)/cogl to be able to find cogl-gl-header.h --- cogl/cogl-bitmask.c | 172 ++++++++++++++++++++++++++- tests/conform/Makefile.am | 11 +- tests/conform/test-bitmask.c | 185 ------------------------------ tests/conform/test-conform-main.c | 2 - 4 files changed, 172 insertions(+), 198 deletions(-) delete mode 100644 tests/conform/test-bitmask.c diff --git a/cogl/cogl-bitmask.c b/cogl/cogl-bitmask.c index 43970b69d..4fbc16d6f 100644 --- a/cogl/cogl-bitmask.c +++ b/cogl/cogl-bitmask.c @@ -24,13 +24,13 @@ * Neil Roberts */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include #include +#include + #include "cogl-bitmask.h" #include "cogl-util.h" #include "cogl-flags.h" @@ -312,3 +312,171 @@ _cogl_bitmask_popcount_upto_in_array (const CoglBitmask *bitmask, return pop + _cogl_util_popcountl (top_mask & ((1UL << bit_index) - 1)); } } + +typedef struct +{ + int n_bits; + int *bits; +} CheckData; + +static CoglBool +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 TRUE; + } + + g_assert_not_reached (); + + return TRUE; +} + +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); + + g_assert_cmpint (_cogl_bitmask_popcount (bitmask), ==, data.n_bits); + + for (i = 0; i < 1024; i++) + { + int upto_popcount = 0; + int j; + + G_VA_COPY (ap, ap_copy); + + for (j = 0; j < data.n_bits; j++) + if (va_arg (ap, int) < i) + upto_popcount++; + + g_assert_cmpint (_cogl_bitmask_popcount_upto (bitmask, i), + ==, + upto_popcount); + + 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)); + } +} + +UNIT_TEST (check_bitmask_api, + 0 /* no requirements */, + 0 /* no failure cases */) +{ + 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)); +} diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am index ee10ac0e8..2f0291ad4 100644 --- a/tests/conform/Makefile.am +++ b/tests/conform/Makefile.am @@ -67,10 +67,6 @@ test_sources = \ $(NULL) if !USING_EMSCRIPTEN -# XXX: the emscripten toolchain gets upset about multiple definitions -# of symbols due to the tricks we play in test-bitmask.c with -# including cogl-util.c -test_sources += test-bitmask.c # test-fence depends on the glib mainloop so it won't compile if using # emscripten which builds in standalone mode. test_sources += test-fence.c @@ -122,13 +118,10 @@ clean-wrappers: # a phony rule that will generate symlink scripts for running individual tests BUILT_SOURCES = wrappers -# 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 AM_CPPFLAGS = \ -I$(top_srcdir) \ - -I$(top_srcdir)/test-fixtures \ - -I$(top_builddir)/cogl + -I$(top_builddir)/cogl \ + -I$(top_srcdir)/test-fixtures if !USE_GLIB AM_CPPFLAGS += -I$(top_builddir)/deps/glib diff --git a/tests/conform/test-bitmask.c b/tests/conform/test-bitmask.c deleted file mode 100644 index 58decd73b..000000000 --- a/tests/conform/test-bitmask.c +++ /dev/null @@ -1,185 +0,0 @@ -#include - -#include -#include - -#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 */ - -#define _COGL_IN_TEST_BITMASK -#include -#include -#include - -typedef struct -{ - int n_bits; - int *bits; -} CheckData; - -static CoglBool -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 TRUE; - } - - g_assert_not_reached (); - - return TRUE; -} - -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); - - g_assert_cmpint (_cogl_bitmask_popcount (bitmask), ==, data.n_bits); - - for (i = 0; i < 1024; i++) - { - int upto_popcount = 0; - int j; - - G_VA_COPY (ap, ap_copy); - - for (j = 0; j < data.n_bits; j++) - if (va_arg (ap, int) < i) - upto_popcount++; - - g_assert_cmpint (_cogl_bitmask_popcount_upto (bitmask, i), - ==, - upto_popcount); - - 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_bitmask (void) -{ - 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 (cogl_test_verbose ()) - g_print ("OK\n"); -} diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c index ef909002b..2c776601d 100644 --- a/tests/conform/test-conform-main.c +++ b/tests/conform/test-conform-main.c @@ -96,8 +96,6 @@ main (int argc, char **argv) ADD_TEST (test_snippets, TEST_REQUIREMENT_GLSL, 0); ADD_TEST (test_custom_attributes, TEST_REQUIREMENT_GLSL, 0); - ADD_TEST (test_bitmask, 0, 0); - ADD_TEST (test_offscreen, 0, 0); ADD_TEST (test_framebuffer_get_bits, TEST_REQUIREMENT_OFFSCREEN | TEST_REQUIREMENT_GL,