| File: | testsuite/ctk/./../../ctk/ctkbitmaskprivateimpl.h |
| Warning: | line 132, column 58 The result of left shift is undefined because the right operand '4294967295' is not smaller than 64, the capacity of 'gsize' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* CtkRBTree tests. | ||||
| 2 | * | ||||
| 3 | * Copyright (C) 2011, Red Hat, Inc. | ||||
| 4 | * Authors: Benjamin Otte <otte@gnome.org> | ||||
| 5 | * | ||||
| 6 | * This library is free software; you can redistribute it and/or | ||||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||||
| 8 | * License as published by the Free Software Foundation; either | ||||
| 9 | * version 2 of the License, or (at your option) any later version. | ||||
| 10 | * | ||||
| 11 | * This library is distributed in the hope that it will be useful, | ||||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
| 14 | * Lesser General Public License for more details. | ||||
| 15 | * | ||||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||||
| 17 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
| 18 | */ | ||||
| 19 | |||||
| 20 | #include <locale.h> | ||||
| 21 | |||||
| 22 | #include "../../ctk/ctkbitmaskprivate.h" | ||||
| 23 | |||||
| 24 | #include <string.h> | ||||
| 25 | |||||
| 26 | /* how often we run the random tests */ | ||||
| 27 | #define N_RUNS20 20 | ||||
| 28 | |||||
| 29 | /* how many tries we do in our random tests */ | ||||
| 30 | #define N_TRIES100 100 | ||||
| 31 | |||||
| 32 | /* the maximum index we use for bitmask values */ | ||||
| 33 | #define MAX_INDEX1000 1000 | ||||
| 34 | |||||
| 35 | /* UTILITIES */ | ||||
| 36 | |||||
| 37 | static CtkBitmask * | ||||
| 38 | ctk_bitmask_new_parse (const char *string) | ||||
| 39 | { | ||||
| 40 | guint i, length; | ||||
| 41 | CtkBitmask *mask; | ||||
| 42 | |||||
| 43 | length = strlen (string); | ||||
| 44 | mask = _ctk_bitmask_new (); | ||||
| 45 | |||||
| 46 | for (i = 0; i < length; i++) | ||||
| 47 | { | ||||
| 48 | if (string[i] == '0') | ||||
| 49 | mask = _ctk_bitmask_set (mask, length - i - 1, FALSE(0)); | ||||
| 50 | else if (string[i] == '1') | ||||
| 51 | mask = _ctk_bitmask_set (mask, length - i - 1, TRUE(!(0))); | ||||
| 52 | else | ||||
| 53 | g_assert_not_reached ()do { g_assertion_message_expr (((gchar*) 0), "bitmask.c", 53, ((const char*) (__func__)), ((void*)0)); } while (0); | ||||
| 54 | } | ||||
| 55 | |||||
| 56 | return mask; | ||||
| 57 | } | ||||
| 58 | |||||
| 59 | #define assert_cmpmasks(mask,other)do { if ((!_ctk_bitmask_equals (mask, other))) { char *mask_string = _ctk_bitmask_to_string (mask); char *other_string = _ctk_bitmask_to_string (other); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "mask" , mask_string, "other", other_string); g_assertion_message (( (gchar*) 0), "bitmask.c", 59, ((const char*) (__func__)), msg ); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0) G_STMT_STARTdo { \ | ||||
| 60 | if (G_UNLIKELY (!_ctk_bitmask_equals (mask, other))(!_ctk_bitmask_equals (mask, other))) \ | ||||
| 61 | { \ | ||||
| 62 | char *mask_string = _ctk_bitmask_to_string (mask); \ | ||||
| 63 | char *other_string = _ctk_bitmask_to_string (other); \ | ||||
| 64 | char *msg = g_strdup_printf ("%s (%s) != %s (%s)", \ | ||||
| 65 | G_STRINGIFY (mask)"mask", mask_string, \ | ||||
| 66 | G_STRINGIFY (other)"other", other_string); \ | ||||
| 67 | g_assertion_message (G_LOG_DOMAIN((gchar*) 0), __FILE__"bitmask.c", __LINE__67, G_STRFUNC((const char*) (__func__)), msg); \ | ||||
| 68 | g_free (msg); \ | ||||
| 69 | g_free (mask_string); \ | ||||
| 70 | g_free (other_string); \ | ||||
| 71 | } \ | ||||
| 72 | }G_STMT_ENDwhile (0) | ||||
| 73 | |||||
| 74 | static const char *tests[] = { | ||||
| 75 | "0", | ||||
| 76 | "1", | ||||
| 77 | "1000000000000000000000000000000", | ||||
| 78 | "10000000000000000000000000000000", | ||||
| 79 | "100000000000000000000000000000000000000000000000000000000000000", | ||||
| 80 | "1000000000000000000000000000000000000000000000000000000000000000", | ||||
| 81 | "10000000000000000000000000000000000000000000000000000000000000000", | ||||
| 82 | "1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010", | ||||
| 83 | "1000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000", | ||||
| 84 | "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" | ||||
| 85 | }; | ||||
| 86 | |||||
| 87 | static CtkBitmask *masks[G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0]))]; | ||||
| 88 | |||||
| 89 | /* TEST */ | ||||
| 90 | |||||
| 91 | static void | ||||
| 92 | test_to_string (void) | ||||
| 93 | { | ||||
| 94 | guint i; | ||||
| 95 | char *to_string; | ||||
| 96 | |||||
| 97 | for (i = 0; i < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); i++) | ||||
| 98 | { | ||||
| 99 | to_string = _ctk_bitmask_to_string (masks[i]); | ||||
| 100 | g_assert_cmpstr (to_string, ==, tests[i])do { const char *__s1 = (to_string), *__s2 = (tests[i]); if ( g_strcmp0 (__s1, __s2) == 0) ; else g_assertion_message_cmpstr (((gchar*) 0), "bitmask.c", 100, ((const char*) (__func__)), "to_string" " " "==" " " "tests[i]", __s1, "==", __s2); } while (0); | ||||
| 101 | g_free (to_string); | ||||
| 102 | } | ||||
| 103 | } | ||||
| 104 | |||||
| 105 | static void | ||||
| 106 | test_is_empty (void) | ||||
| 107 | { | ||||
| 108 | guint i; | ||||
| 109 | |||||
| 110 | for (i = 0; i < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); i++) | ||||
| 111 | { | ||||
| 112 | g_assert_cmpint (_ctk_bitmask_is_empty (masks[i]), ==, i == 0)do { gint64 __n1 = (_ctk_bitmask_is_empty (masks[i])), __n2 = (i == 0); if (__n1 == __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 112, ((const char*) (__func__)), "_ctk_bitmask_is_empty (masks[i])" " " "==" " " "i == 0", (guint64 )__n1, "==", (guint64)__n2, 'i'); } while (0); | ||||
| 113 | } | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | static void | ||||
| 117 | test_equals (void) | ||||
| 118 | { | ||||
| 119 | guint i, j; | ||||
| 120 | |||||
| 121 | for (i = 0; i < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); i++) | ||||
| 122 | { | ||||
| 123 | for (j = 0; j < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); j++) | ||||
| 124 | { | ||||
| 125 | g_assert_cmpint (_ctk_bitmask_equals (masks[i], masks[j]), ==, i == j)do { gint64 __n1 = (_ctk_bitmask_equals (masks[i], masks[j])) , __n2 = (i == j); if (__n1 == __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 125, ((const char*) (__func__)), "_ctk_bitmask_equals (masks[i], masks[j])" " " "==" " " "i == j" , (guint64)__n1, "==", (guint64)__n2, 'i'); } while (0); | ||||
| 126 | } | ||||
| 127 | } | ||||
| 128 | } | ||||
| 129 | |||||
| 130 | static void | ||||
| 131 | test_set (void) | ||||
| 132 | { | ||||
| 133 | guint i, j; | ||||
| 134 | guint indexes[N_TRIES100]; | ||||
| 135 | CtkBitmask *copy; | ||||
| 136 | const CtkBitmask *mask; | ||||
| 137 | |||||
| 138 | for (i = 0; i < N_RUNS20; i++) | ||||
| 139 | { | ||||
| 140 | mask = masks[g_test_rand_int_range (0, G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])))]; | ||||
| 141 | copy = _ctk_bitmask_copy (mask); | ||||
| 142 | |||||
| 143 | for (j = 0; j < N_TRIES100; j++) | ||||
| 144 | { | ||||
| 145 | indexes[j] = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 146 | copy = _ctk_bitmask_set (copy, indexes[j], g_test_rand_bit ()(0 != (g_test_rand_int() & (1 << 15)))); | ||||
| 147 | } | ||||
| 148 | |||||
| 149 | for (j = 0; j < N_TRIES100; j++) | ||||
| 150 | { | ||||
| 151 | copy = _ctk_bitmask_set (copy, indexes[j], _ctk_bitmask_get (mask, indexes[j])); | ||||
| 152 | } | ||||
| 153 | |||||
| 154 | assert_cmpmasks (copy, mask)do { if ((!_ctk_bitmask_equals (copy, mask))) { char *mask_string = _ctk_bitmask_to_string (copy); char *other_string = _ctk_bitmask_to_string (mask); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "copy" , mask_string, "mask", other_string); g_assertion_message ((( gchar*) 0), "bitmask.c", 154, ((const char*) (__func__)), msg ); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0); | ||||
| 155 | _ctk_bitmask_free (copy); | ||||
| 156 | } | ||||
| 157 | } | ||||
| 158 | |||||
| 159 | static void | ||||
| 160 | test_union (void) | ||||
| 161 | { | ||||
| 162 | CtkBitmask *left, *right, *expected; | ||||
| 163 | guint run, try, n_tries; | ||||
| 164 | |||||
| 165 | for (run = 0; run < N_RUNS20; run++) | ||||
| 166 | { | ||||
| 167 | left = _ctk_bitmask_new (); | ||||
| 168 | right = _ctk_bitmask_new (); | ||||
| 169 | expected = _ctk_bitmask_new (); | ||||
| 170 | |||||
| 171 | n_tries = g_test_perf ()(g_test_config_vars->test_perf) ? N_TRIES100 : g_test_rand_int_range (0, N_TRIES100); | ||||
| 172 | for (try = 0; try < n_tries; try++) | ||||
| 173 | { | ||||
| 174 | guint id = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 175 | |||||
| 176 | if (g_test_rand_bit ()(0 != (g_test_rand_int() & (1 << 15)))) | ||||
| 177 | left = _ctk_bitmask_set (left, id, TRUE(!(0))); | ||||
| 178 | else | ||||
| 179 | right = _ctk_bitmask_set (right, id, TRUE(!(0))); | ||||
| 180 | |||||
| 181 | expected = _ctk_bitmask_set (expected, id, TRUE(!(0))); | ||||
| 182 | } | ||||
| 183 | |||||
| 184 | left = _ctk_bitmask_union (left, right); | ||||
| 185 | right = _ctk_bitmask_union (right, left); | ||||
| 186 | |||||
| 187 | assert_cmpmasks (left, expected)do { if ((!_ctk_bitmask_equals (left, expected))) { char *mask_string = _ctk_bitmask_to_string (left); char *other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)" , "left", mask_string, "expected", other_string); g_assertion_message (((gchar*) 0), "bitmask.c", 187, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string ); } }while (0); | ||||
| 188 | assert_cmpmasks (right, expected)do { if ((!_ctk_bitmask_equals (right, expected))) { char *mask_string = _ctk_bitmask_to_string (right); char *other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)" , "right", mask_string, "expected", other_string); g_assertion_message (((gchar*) 0), "bitmask.c", 188, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string ); } }while (0); | ||||
| 189 | _ctk_bitmask_free (left); | ||||
| 190 | _ctk_bitmask_free (right); | ||||
| 191 | _ctk_bitmask_free (expected); | ||||
| 192 | } | ||||
| 193 | } | ||||
| 194 | |||||
| 195 | static void | ||||
| 196 | test_intersect (void) | ||||
| 197 | { | ||||
| 198 | CtkBitmask *left, *right, *expected; | ||||
| 199 | guint run, try; | ||||
| 200 | gboolean intersects; | ||||
| 201 | |||||
| 202 | for (run = 0; run < N_RUNS20; run++) | ||||
| 203 | { | ||||
| 204 | left = _ctk_bitmask_new (); | ||||
| 205 | right = _ctk_bitmask_new (); | ||||
| 206 | expected = _ctk_bitmask_new (); | ||||
| 207 | |||||
| 208 | for (try = 0; try < N_TRIES100; try++) | ||||
| 209 | { | ||||
| 210 | guint id = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 211 | gboolean set = g_test_rand_bit ()(0 != (g_test_rand_int() & (1 << 15))); | ||||
| 212 | |||||
| 213 | if (g_test_rand_bit ()(0 != (g_test_rand_int() & (1 << 15)))) | ||||
| 214 | { | ||||
| 215 | left = _ctk_bitmask_set (left, id, set); | ||||
| 216 | expected = _ctk_bitmask_set (expected, id, set ? _ctk_bitmask_get (right, id) : 0); | ||||
| 217 | } | ||||
| 218 | else | ||||
| 219 | { | ||||
| 220 | right = _ctk_bitmask_set (right, id, set); | ||||
| 221 | expected = _ctk_bitmask_set (expected, id, set ? _ctk_bitmask_get (left, id) : 0); | ||||
| 222 | } | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | intersects = _ctk_bitmask_intersects (left, right); | ||||
| 226 | g_assert_cmpint (intersects, ==, _ctk_bitmask_intersects (right, left))do { gint64 __n1 = (intersects), __n2 = (_ctk_bitmask_intersects (right, left)); if (__n1 == __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 226, ((const char*) (__func__)), "intersects" " " "==" " " "_ctk_bitmask_intersects (right, left)" , (guint64)__n1, "==", (guint64)__n2, 'i'); } while (0); | ||||
| 227 | g_assert_cmpint (intersects, !=, _ctk_bitmask_is_empty (expected))do { gint64 __n1 = (intersects), __n2 = (_ctk_bitmask_is_empty (expected)); if (__n1 != __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 227, ((const char*) (__func__)), "intersects" " " "!=" " " "_ctk_bitmask_is_empty (expected)" , (guint64)__n1, "!=", (guint64)__n2, 'i'); } while (0); | ||||
| 228 | |||||
| 229 | left = _ctk_bitmask_intersect (left, right); | ||||
| 230 | right = _ctk_bitmask_intersect (right, left); | ||||
| 231 | |||||
| 232 | assert_cmpmasks (left, expected)do { if ((!_ctk_bitmask_equals (left, expected))) { char *mask_string = _ctk_bitmask_to_string (left); char *other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)" , "left", mask_string, "expected", other_string); g_assertion_message (((gchar*) 0), "bitmask.c", 232, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string ); } }while (0); | ||||
| 233 | assert_cmpmasks (right, expected)do { if ((!_ctk_bitmask_equals (right, expected))) { char *mask_string = _ctk_bitmask_to_string (right); char *other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)" , "right", mask_string, "expected", other_string); g_assertion_message (((gchar*) 0), "bitmask.c", 233, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string ); } }while (0); | ||||
| 234 | _ctk_bitmask_free (left); | ||||
| 235 | _ctk_bitmask_free (right); | ||||
| 236 | _ctk_bitmask_free (expected); | ||||
| 237 | } | ||||
| 238 | } | ||||
| 239 | |||||
| 240 | static void | ||||
| 241 | test_intersect_hardcoded (void) | ||||
| 242 | { | ||||
| 243 | CtkBitmask *left, *right, *intersection, *expected; | ||||
| 244 | const char *left_str, *right_str; | ||||
| 245 | guint left_len, right_len; | ||||
| 246 | guint i, l, r; | ||||
| 247 | |||||
| 248 | for (l = 0; l < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); l++) | ||||
| 249 | { | ||||
| 250 | for (r = 0; r < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); r++) | ||||
| 251 | { | ||||
| 252 | left = masks[l]; | ||||
| 253 | right = masks[r]; | ||||
| 254 | left_str = tests[l]; | ||||
| 255 | right_str = tests[r]; | ||||
| 256 | left_len = strlen (tests[l]); | ||||
| 257 | right_len = strlen (tests[r]); | ||||
| 258 | |||||
| 259 | expected = _ctk_bitmask_new (); | ||||
| 260 | if (left_len > right_len) | ||||
| 261 | left_str += left_len - right_len; | ||||
| 262 | if (right_len > left_len) | ||||
| 263 | right_str += right_len - left_len; | ||||
| 264 | i = MIN (right_len, left_len)(((right_len) < (left_len)) ? (right_len) : (left_len)); | ||||
| 265 | while (i--) | ||||
| 266 | { | ||||
| 267 | expected = _ctk_bitmask_set (expected, i, left_str[0] == '1' && right_str[0] == '1'); | ||||
| 268 | right_str++; | ||||
| 269 | left_str++; | ||||
| 270 | } | ||||
| 271 | |||||
| 272 | intersection = _ctk_bitmask_intersect (_ctk_bitmask_copy (left), right); | ||||
| 273 | |||||
| 274 | assert_cmpmasks (intersection, expected)do { if ((!_ctk_bitmask_equals (intersection, expected))) { char *mask_string = _ctk_bitmask_to_string (intersection); char * other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "intersection", mask_string , "expected", other_string); g_assertion_message (((gchar*) 0 ), "bitmask.c", 274, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0); | ||||
| 275 | g_assert_cmpint (_ctk_bitmask_is_empty (expected), ==, !_ctk_bitmask_intersects (left, right))do { gint64 __n1 = (_ctk_bitmask_is_empty (expected)), __n2 = (!_ctk_bitmask_intersects (left, right)); if (__n1 == __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 275 , ((const char*) (__func__)), "_ctk_bitmask_is_empty (expected)" " " "==" " " "!_ctk_bitmask_intersects (left, right)", (guint64 )__n1, "==", (guint64)__n2, 'i'); } while (0); | ||||
| 276 | |||||
| 277 | _ctk_bitmask_free (intersection); | ||||
| 278 | _ctk_bitmask_free (expected); | ||||
| 279 | } | ||||
| 280 | } | ||||
| 281 | } | ||||
| 282 | |||||
| 283 | static void | ||||
| 284 | test_subtract_hardcoded (void) | ||||
| 285 | { | ||||
| 286 | CtkBitmask *left, *right, *subtracted, *expected; | ||||
| 287 | const char *left_str, *right_str; | ||||
| 288 | guint left_len, right_len; | ||||
| 289 | guint i, l, r; | ||||
| 290 | |||||
| 291 | for (l = 0; l < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); l++) | ||||
| 292 | { | ||||
| 293 | for (r = 0; r < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); r++) | ||||
| 294 | { | ||||
| 295 | left = masks[l]; | ||||
| 296 | right = masks[r]; | ||||
| 297 | left_str = tests[l]; | ||||
| 298 | right_str = tests[r]; | ||||
| 299 | left_len = strlen (tests[l]); | ||||
| 300 | right_len = strlen (tests[r]); | ||||
| 301 | |||||
| 302 | expected = _ctk_bitmask_new (); | ||||
| 303 | for (i = MIN (right_len, left_len)(((right_len) < (left_len)) ? (right_len) : (left_len)); i < left_len; i++) | ||||
| 304 | { | ||||
| 305 | expected = _ctk_bitmask_set (expected, i, left_str[left_len - i - 1] == '1'); | ||||
| 306 | } | ||||
| 307 | if (left_len > right_len) | ||||
| 308 | left_str += left_len - right_len; | ||||
| 309 | if (right_len > left_len) | ||||
| 310 | right_str += right_len - left_len; | ||||
| 311 | i = MIN (right_len, left_len)(((right_len) < (left_len)) ? (right_len) : (left_len)); | ||||
| 312 | while (i--) | ||||
| 313 | { | ||||
| 314 | expected = _ctk_bitmask_set (expected, i, left_str[0] == '1' && right_str[0] == '0'); | ||||
| 315 | right_str++; | ||||
| 316 | left_str++; | ||||
| 317 | } | ||||
| 318 | |||||
| 319 | g_test_message ("%s - %s\n", _ctk_bitmask_to_string (left), _ctk_bitmask_to_string (right)); | ||||
| 320 | subtracted = _ctk_bitmask_subtract (_ctk_bitmask_copy (left), right); | ||||
| 321 | |||||
| 322 | assert_cmpmasks (subtracted, expected)do { if ((!_ctk_bitmask_equals (subtracted, expected))) { char *mask_string = _ctk_bitmask_to_string (subtracted); char *other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "subtracted", mask_string, "expected" , other_string); g_assertion_message (((gchar*) 0), "bitmask.c" , 322, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0); | ||||
| 323 | |||||
| 324 | _ctk_bitmask_free (subtracted); | ||||
| 325 | _ctk_bitmask_free (expected); | ||||
| 326 | } | ||||
| 327 | } | ||||
| 328 | } | ||||
| 329 | |||||
| 330 | #define SWAP(_a, _b)do{ guint _tmp = _a; _a = _b; _b = _tmp; }while (0) G_STMT_STARTdo{ \ | ||||
| 331 | guint _tmp = _a; \ | ||||
| 332 | _a = _b; \ | ||||
| 333 | _b = _tmp; \ | ||||
| 334 | }G_STMT_ENDwhile (0) | ||||
| 335 | |||||
| 336 | static void | ||||
| 337 | test_invert_range_hardcoded (void) | ||||
| 338 | { | ||||
| 339 | guint t, l, r, i; | ||||
| 340 | gsize r_len, l_len, ref_len; | ||||
| 341 | char *ref_str; | ||||
| 342 | CtkBitmask *bitmask, *ref; | ||||
| 343 | |||||
| 344 | for (t = 0; t < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); t++) | ||||
| |||||
| 345 | { | ||||
| 346 | for (l = 0; l < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); l++) | ||||
| 347 | { | ||||
| 348 | l_len = strlen (tests[l]); | ||||
| 349 | |||||
| 350 | for (r = 0; r < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); r++) | ||||
| 351 | { | ||||
| 352 | r_len = strlen (tests[r]); | ||||
| 353 | if (r_len
| ||||
| 354 | continue; | ||||
| 355 | |||||
| 356 | ref_len = MAX (r_len, strlen (tests[t]))(((r_len) > (strlen (tests[t]))) ? (r_len) : (strlen (tests [t]))); | ||||
| 357 | ref_str = g_strdup_printf ("%*s", (int) ref_len, tests[t]); | ||||
| 358 | for (i = 0; i < ref_len && ref_str[i] == ' '; i++) | ||||
| 359 | ref_str[i] = '0'; | ||||
| 360 | for (i = l_len - 1; i < r_len; i++) | ||||
| 361 | { | ||||
| 362 | ref_str[ref_len-i-1] = ref_str[ref_len-i-1] == '0' ? '1' : '0'; | ||||
| 363 | } | ||||
| 364 | ref = ctk_bitmask_new_parse (ref_str); | ||||
| 365 | g_free (ref_str); | ||||
| 366 | |||||
| 367 | bitmask = ctk_bitmask_new_parse (tests[t]); | ||||
| 368 | bitmask = _ctk_bitmask_invert_range (bitmask, l_len - 1, r_len); | ||||
| 369 | |||||
| 370 | assert_cmpmasks (bitmask, ref)do { if ((!_ctk_bitmask_equals (bitmask, ref))) { char *mask_string = _ctk_bitmask_to_string (bitmask); char *other_string = _ctk_bitmask_to_string (ref); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "bitmask" , mask_string, "ref", other_string); g_assertion_message (((gchar *) 0), "bitmask.c", 370, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0); | ||||
| 371 | |||||
| 372 | _ctk_bitmask_free (bitmask); | ||||
| 373 | _ctk_bitmask_free (ref); | ||||
| 374 | } | ||||
| 375 | } | ||||
| 376 | } | ||||
| 377 | } | ||||
| 378 | |||||
| 379 | static void | ||||
| 380 | test_invert_range (void) | ||||
| 381 | { | ||||
| 382 | CtkBitmask *left, *right, *intersection, *expected; | ||||
| 383 | guint run; | ||||
| 384 | guint left_start, left_end, right_start, right_end, start, end; | ||||
| 385 | |||||
| 386 | for (run = 0; run < N_RUNS20; run++) | ||||
| 387 | { | ||||
| 388 | left = _ctk_bitmask_new (); | ||||
| 389 | right = _ctk_bitmask_new (); | ||||
| 390 | expected = _ctk_bitmask_new (); | ||||
| 391 | |||||
| 392 | left_start = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 393 | left_end = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 394 | if (left_start > left_end) | ||||
| 395 | SWAP (left_start, left_end)do{ guint _tmp = left_start; left_start = left_end; left_end = _tmp; }while (0); | ||||
| 396 | right_start = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 397 | right_end = g_test_rand_int_range (0, MAX_INDEX1000); | ||||
| 398 | if (right_start > right_end) | ||||
| 399 | SWAP (right_start, right_end)do{ guint _tmp = right_start; right_start = right_end; right_end = _tmp; }while (0); | ||||
| 400 | start = MAX (left_start, right_start)(((left_start) > (right_start)) ? (left_start) : (right_start )); | ||||
| 401 | end = MIN (left_end, right_end)(((left_end) < (right_end)) ? (left_end) : (right_end)); | ||||
| 402 | |||||
| 403 | if (left_start != left_end) | ||||
| 404 | left = _ctk_bitmask_invert_range (left, left_start, left_end); | ||||
| 405 | if (right_start != right_end) | ||||
| 406 | right = _ctk_bitmask_invert_range (right, right_start, right_end); | ||||
| 407 | if (start < end) | ||||
| 408 | expected = _ctk_bitmask_invert_range (expected, start, end); | ||||
| 409 | |||||
| 410 | intersection = _ctk_bitmask_copy (left); | ||||
| 411 | intersection = _ctk_bitmask_intersect (intersection, right); | ||||
| 412 | |||||
| 413 | assert_cmpmasks (intersection, expected)do { if ((!_ctk_bitmask_equals (intersection, expected))) { char *mask_string = _ctk_bitmask_to_string (intersection); char * other_string = _ctk_bitmask_to_string (expected); char *msg = g_strdup_printf ("%s (%s) != %s (%s)", "intersection", mask_string , "expected", other_string); g_assertion_message (((gchar*) 0 ), "bitmask.c", 413, ((const char*) (__func__)), msg); g_free (msg); g_free (mask_string); g_free (other_string); } }while (0); | ||||
| 414 | |||||
| 415 | if (start < end) | ||||
| 416 | expected = _ctk_bitmask_invert_range (expected, start, end); | ||||
| 417 | |||||
| 418 | g_assert_cmpint (_ctk_bitmask_is_empty (expected), ==, TRUE)do { gint64 __n1 = (_ctk_bitmask_is_empty (expected)), __n2 = ((!(0))); if (__n1 == __n2) ; else g_assertion_message_cmpint (((gchar*) 0), "bitmask.c", 418, ((const char*) (__func__)), "_ctk_bitmask_is_empty (expected)" " " "==" " " "TRUE", (guint64 )__n1, "==", (guint64)__n2, 'i'); } while (0); | ||||
| 419 | |||||
| 420 | _ctk_bitmask_free (left); | ||||
| 421 | _ctk_bitmask_free (right); | ||||
| 422 | _ctk_bitmask_free (intersection); | ||||
| 423 | _ctk_bitmask_free (expected); | ||||
| 424 | } | ||||
| 425 | } | ||||
| 426 | |||||
| 427 | /* SETUP & RUNNING */ | ||||
| 428 | |||||
| 429 | static void | ||||
| 430 | create_masks (void) | ||||
| 431 | { | ||||
| 432 | guint i; | ||||
| 433 | |||||
| 434 | for (i = 0; i < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); i++) | ||||
| 435 | masks[i] = ctk_bitmask_new_parse (tests[i]); | ||||
| 436 | } | ||||
| 437 | |||||
| 438 | static void | ||||
| 439 | free_masks (void) | ||||
| 440 | { | ||||
| 441 | guint i; | ||||
| 442 | |||||
| 443 | for (i = 0; i < G_N_ELEMENTS (tests)(sizeof (tests) / sizeof ((tests)[0])); i++) | ||||
| 444 | { | ||||
| 445 | _ctk_bitmask_free (masks[i]); | ||||
| 446 | masks[i] = NULL((void*)0); | ||||
| 447 | } | ||||
| 448 | } | ||||
| 449 | |||||
| 450 | int | ||||
| 451 | main (int argc, char *argv[]) | ||||
| 452 | { | ||||
| 453 | int result; | ||||
| 454 | |||||
| 455 | g_test_init (&argc, &argv, NULL((void*)0)); | ||||
| 456 | setlocale (LC_ALL6, "C"); | ||||
| 457 | g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=%s"); | ||||
| 458 | |||||
| 459 | create_masks (); | ||||
| 460 | |||||
| 461 | g_test_add_func ("/bitmask/to_string", test_to_string); | ||||
| 462 | g_test_add_func ("/bitmask/is_empty", test_is_empty); | ||||
| 463 | g_test_add_func ("/bitmask/equals", test_equals); | ||||
| 464 | g_test_add_func ("/bitmask/set", test_set); | ||||
| 465 | g_test_add_func ("/bitmask/union", test_union); | ||||
| 466 | g_test_add_func ("/bitmask/intersect", test_intersect); | ||||
| 467 | g_test_add_func ("/bitmask/intersect_hardcoded", test_intersect_hardcoded); | ||||
| 468 | g_test_add_func ("/bitmask/subtract_hardcoded", test_subtract_hardcoded); | ||||
| 469 | g_test_add_func ("/bitmask/invert_range", test_invert_range); | ||||
| 470 | g_test_add_func ("/bitmask/invert_range_hardcoded", test_invert_range_hardcoded); | ||||
| 471 | |||||
| 472 | result = g_test_run (); | ||||
| 473 | |||||
| 474 | free_masks (); | ||||
| 475 | |||||
| 476 | return result; | ||||
| 477 | } |
| 1 | /* | |||
| 2 | * Copyright © 2011 Red Hat Inc. | |||
| 3 | * | |||
| 4 | * This library is free software; you can redistribute it and/or | |||
| 5 | * modify it under the terms of the GNU Lesser General Public | |||
| 6 | * License as published by the Free Software Foundation; either | |||
| 7 | * version 2.1 of the License, or (at your option) any later version. | |||
| 8 | * | |||
| 9 | * This library is distributed in the hope that it will be useful, | |||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
| 12 | * Lesser General Public License for more details. | |||
| 13 | * | |||
| 14 | * You should have received a copy of the GNU Lesser General Public | |||
| 15 | * License along with this library; if not, write to the Free Software | |||
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| 17 | * | |||
| 18 | * Authors: Benjamin Otte <otte@gnome.org> | |||
| 19 | */ | |||
| 20 | ||||
| 21 | ||||
| 22 | static inline CtkBitmask * | |||
| 23 | _ctk_bitmask_new (void) | |||
| 24 | { | |||
| 25 | return _ctk_bitmask_from_bits (0)((gpointer) (guintptr) (gsize) ((((gsize) (0)) << 1) | 1 )); | |||
| 26 | } | |||
| 27 | ||||
| 28 | static inline CtkBitmask * | |||
| 29 | _ctk_bitmask_copy (const CtkBitmask *mask) | |||
| 30 | { | |||
| 31 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1))) | |||
| 32 | return _ctk_allocated_bitmask_copy (mask); | |||
| 33 | else | |||
| 34 | return (CtkBitmask *) mask; | |||
| 35 | } | |||
| 36 | ||||
| 37 | static inline void | |||
| 38 | _ctk_bitmask_free (CtkBitmask *mask) | |||
| 39 | { | |||
| 40 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1))) | |||
| 41 | _ctk_allocated_bitmask_free (mask); | |||
| 42 | } | |||
| 43 | ||||
| 44 | static inline char * | |||
| 45 | _ctk_bitmask_to_string (const CtkBitmask *mask) | |||
| 46 | { | |||
| 47 | GString *string; | |||
| 48 | ||||
| 49 | string = g_string_new (NULL((void*)0)); | |||
| 50 | _ctk_allocated_bitmask_print (mask, string); | |||
| 51 | return g_string_free (string, FALSE)(__builtin_constant_p ((0)) ? (((0)) ? (g_string_free) ((string ), ((0))) : g_string_free_and_steal (string)) : (g_string_free ) ((string), ((0)))); | |||
| 52 | } | |||
| 53 | ||||
| 54 | static inline void | |||
| 55 | _ctk_bitmask_print (const CtkBitmask *mask, | |||
| 56 | GString *string) | |||
| 57 | { | |||
| 58 | _ctk_allocated_bitmask_print (mask, string); | |||
| 59 | } | |||
| 60 | ||||
| 61 | static inline CtkBitmask * | |||
| 62 | _ctk_bitmask_intersect (CtkBitmask *mask, | |||
| 63 | const CtkBitmask *other) | |||
| 64 | { | |||
| 65 | return _ctk_allocated_bitmask_intersect (mask, other); | |||
| 66 | } | |||
| 67 | ||||
| 68 | static inline CtkBitmask * | |||
| 69 | _ctk_bitmask_union (CtkBitmask *mask, | |||
| 70 | const CtkBitmask *other) | |||
| 71 | { | |||
| 72 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1)) || | |||
| 73 | _ctk_bitmask_is_allocated (other)(!(((gsize) (other)) & 1))) | |||
| 74 | return _ctk_allocated_bitmask_union (mask, other); | |||
| 75 | else | |||
| 76 | return _ctk_bitmask_from_bits (_ctk_bitmask_to_bits (mask)((gpointer) (guintptr) (gsize) ((((gsize) ((((gsize) (mask)) >> ((gsize) 1)) | (((gsize) (other)) >> ((gsize) 1)))) << 1) | 1)) | |||
| 77 | | _ctk_bitmask_to_bits (other))((gpointer) (guintptr) (gsize) ((((gsize) ((((gsize) (mask)) >> ((gsize) 1)) | (((gsize) (other)) >> ((gsize) 1)))) << 1) | 1)); | |||
| 78 | } | |||
| 79 | ||||
| 80 | static inline CtkBitmask * | |||
| 81 | _ctk_bitmask_subtract (CtkBitmask *mask, | |||
| 82 | const CtkBitmask *other) | |||
| 83 | { | |||
| 84 | return _ctk_allocated_bitmask_subtract (mask, other); | |||
| 85 | } | |||
| 86 | ||||
| 87 | static inline gboolean | |||
| 88 | _ctk_bitmask_get (const CtkBitmask *mask, | |||
| 89 | guint index_) | |||
| 90 | { | |||
| 91 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1))) | |||
| 92 | return _ctk_allocated_bitmask_get (mask, index_); | |||
| 93 | else | |||
| 94 | return index_ < CTK_BITMASK_N_DIRECT_BITS(sizeof (gsize) * 8 - 1) | |||
| 95 | ? !!(_ctk_bitmask_to_bits (mask)(((gsize) (mask)) >> ((gsize) 1)) & (((gsize) 1) << index_)) | |||
| 96 | : FALSE(0); | |||
| 97 | } | |||
| 98 | ||||
| 99 | static inline CtkBitmask * | |||
| 100 | _ctk_bitmask_set (CtkBitmask *mask, | |||
| 101 | guint index_, | |||
| 102 | gboolean value) | |||
| 103 | { | |||
| 104 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1)) || | |||
| 105 | (index_ >= CTK_BITMASK_N_DIRECT_BITS(sizeof (gsize) * 8 - 1) && value)) | |||
| 106 | return _ctk_allocated_bitmask_set (mask, index_, value); | |||
| 107 | else if (index_ < CTK_BITMASK_N_DIRECT_BITS(sizeof (gsize) * 8 - 1)) | |||
| 108 | { | |||
| 109 | gsize bits = _ctk_bitmask_to_bits (mask)(((gsize) (mask)) >> ((gsize) 1)); | |||
| 110 | ||||
| 111 | if (value) | |||
| 112 | bits |= ((gsize) 1) << index_; | |||
| 113 | else | |||
| 114 | bits &= ~(((gsize) 1) << index_); | |||
| 115 | ||||
| 116 | return _ctk_bitmask_from_bits (bits)((gpointer) (guintptr) (gsize) ((((gsize) (bits)) << 1) | 1)); | |||
| 117 | } | |||
| 118 | else | |||
| 119 | return mask; | |||
| 120 | } | |||
| 121 | ||||
| 122 | static inline CtkBitmask * | |||
| 123 | _ctk_bitmask_invert_range (CtkBitmask *mask, | |||
| 124 | guint start, | |||
| 125 | guint end) | |||
| 126 | { | |||
| 127 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1)) || | |||
| 128 | (end > CTK_BITMASK_N_DIRECT_BITS(sizeof (gsize) * 8 - 1))) | |||
| 129 | return _ctk_allocated_bitmask_invert_range (mask, start, end); | |||
| 130 | else | |||
| 131 | { | |||
| 132 | gsize invert = (((gsize) 1) << end) - (((gsize) 1) << start); | |||
| ||||
| 133 | ||||
| 134 | return _ctk_bitmask_from_bits (_ctk_bitmask_to_bits (mask) ^ invert)((gpointer) (guintptr) (gsize) ((((gsize) ((((gsize) (mask)) >> ((gsize) 1)) ^ invert)) << 1) | 1)); | |||
| 135 | } | |||
| 136 | } | |||
| 137 | ||||
| 138 | static inline gboolean | |||
| 139 | _ctk_bitmask_is_empty (const CtkBitmask *mask) | |||
| 140 | { | |||
| 141 | return mask == _ctk_bitmask_from_bits (0)((gpointer) (guintptr) (gsize) ((((gsize) (0)) << 1) | 1 )); | |||
| 142 | } | |||
| 143 | ||||
| 144 | static inline gboolean | |||
| 145 | _ctk_bitmask_equals (const CtkBitmask *mask, | |||
| 146 | const CtkBitmask *other) | |||
| 147 | { | |||
| 148 | if (mask == other) | |||
| 149 | return TRUE(!(0)); | |||
| 150 | ||||
| 151 | if (!_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1)) || | |||
| 152 | !_ctk_bitmask_is_allocated (other)(!(((gsize) (other)) & 1))) | |||
| 153 | return FALSE(0); | |||
| 154 | ||||
| 155 | return _ctk_allocated_bitmask_equals (mask, other); | |||
| 156 | } | |||
| 157 | ||||
| 158 | static inline gboolean | |||
| 159 | _ctk_bitmask_intersects (const CtkBitmask *mask, | |||
| 160 | const CtkBitmask *other) | |||
| 161 | { | |||
| 162 | if (_ctk_bitmask_is_allocated (mask)(!(((gsize) (mask)) & 1)) || | |||
| 163 | _ctk_bitmask_is_allocated (other)(!(((gsize) (other)) & 1))) | |||
| 164 | return _ctk_allocated_bitmask_intersects (mask, other); | |||
| 165 | else | |||
| 166 | return _ctk_bitmask_to_bits (mask)(((gsize) (mask)) >> ((gsize) 1)) & _ctk_bitmask_to_bits (other)(((gsize) (other)) >> ((gsize) 1)) ? TRUE(!(0)) : FALSE(0); | |||
| 167 | } |