2011-10-28 14:00:54 +00:00
|
|
|
#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 */
|
|
|
|
|
2012-04-16 16:23:15 +00:00
|
|
|
#define _COGL_IN_TEST_BITMASK
|
2011-10-28 14:00:54 +00:00
|
|
|
#include <cogl/cogl-bitmask.h>
|
|
|
|
#include <cogl/cogl-bitmask.c>
|
2011-10-28 19:09:53 +00:00
|
|
|
#include <cogl/cogl-util.c>
|
2011-10-28 14:00:54 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int n_bits;
|
|
|
|
int *bits;
|
|
|
|
} CheckData;
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
static CoglBool
|
2011-10-28 14:00:54 +00:00
|
|
|
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;
|
2011-10-26 17:43:21 +00:00
|
|
|
return TRUE;
|
2011-10-28 14:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
2011-10-26 17:43:21 +00:00
|
|
|
return TRUE;
|
2011-10-28 14:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-11-04 17:11:50 +00:00
|
|
|
g_assert_cmpint (_cogl_bitmask_popcount (bitmask), ==, data.n_bits);
|
|
|
|
|
2011-10-28 14:00:54 +00:00
|
|
|
for (i = 0; i < 1024; i++)
|
|
|
|
{
|
2011-11-04 17:11:50 +00:00
|
|
|
int upto_popcount = 0;
|
2011-10-28 14:00:54 +00:00
|
|
|
int j;
|
|
|
|
|
|
|
|
G_VA_COPY (ap, ap_copy);
|
|
|
|
|
2011-11-04 17:11:50 +00:00
|
|
|
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);
|
|
|
|
|
2011-10-28 14:00:54 +00:00
|
|
|
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
|
2012-03-16 19:54:13 +00:00
|
|
|
test_bitmask (void)
|
2011-10-28 14:00:54 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2012-02-23 12:30:51 +00:00
|
|
|
if (cogl_test_verbose ())
|
2011-10-28 14:00:54 +00:00
|
|
|
g_print ("OK\n");
|
|
|
|
}
|