summaryrefslogtreecommitdiff
path: root/gtk/tests/bitmask.c
blob: 3aa9d34c46c5770a9435acf1416badc575d95ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* GtkRBTree tests.
 *
 * Copyright (C) 2011, Red Hat, Inc.
 * Authors: Benjamin Otte <otte@gnome.org>
 *
 * 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 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/>.
 */

#include <locale.h>

#include "../gtkbitmaskprivate.h"

#include <string.h>

/* how often we run the random tests */
#define N_RUNS 20

/* how many tries we do in our random tests */
#define N_TRIES 100

/* the maximum index we use for bitmask values */
#define MAX_INDEX 1000

/* UTILITIES */

static GtkBitmask *
gtk_bitmask_new_parse (const char *string)
{
  guint i, length;
  GtkBitmask *mask;

  length = strlen (string);
  mask = _gtk_bitmask_new ();

  for (i = 0; i < length; i++)
    {
      if (string[i] == '0')
        _gtk_bitmask_set (mask, length - i - 1, FALSE);
      else if (string[i] == '1')
        _gtk_bitmask_set (mask, length - i - 1, TRUE);
      else
        g_assert_not_reached ();
    }

  return mask;
}

#define assert_cmpmasks(mask,other) G_STMT_START { \
  if (G_UNLIKELY (!_gtk_bitmask_equals (mask, other))) \
    { \
      char *mask_string = _gtk_bitmask_to_string (mask); \
      char *other_string = _gtk_bitmask_to_string (other); \
      char *msg = g_strdup_printf ("%s (%s) != %s (%s)", \
                                   G_STRINGIFY (mask), mask_string, \
                                   G_STRINGIFY (other), other_string); \
      g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); \
      g_free (msg); \
      g_free (mask_string); \
      g_free (other_string); \
    } \
}G_STMT_END

static const char *tests[] = {
                                                                                                                                   "0",
                                                                                                                                   "1",
                                                                    "1000000000000000000000000000000000000000000000000000000000000000",
                                                                   "10000000000000000000000000000000000000000000000000000000000000000",
  "1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
  "1000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000",
  "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
};

static GtkBitmask *masks[G_N_ELEMENTS (tests)];

/* TEST */

static void
test_to_string (void)
{
  guint i;
  char *to_string;

  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      to_string = _gtk_bitmask_to_string (masks[i]);
      g_assert_cmpstr (to_string, ==, tests[i]);
      g_free (to_string);
    }
}

static void
test_is_empty (void)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      g_assert_cmpint (_gtk_bitmask_is_empty (masks[i]), ==, i == 0);
    }
}

static void
test_equals (void)
{
  guint i, j;

  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      for (j = 0; j < G_N_ELEMENTS (tests); j++)
        {
          g_assert_cmpint (_gtk_bitmask_equals (masks[i], masks[j]), ==, i == j);
        }
    }
}

static void
test_set (void)
{
  guint i, j;
  guint indexes[N_TRIES];
  GtkBitmask *copy;
  const GtkBitmask *mask;

  for (i = 0; i < N_RUNS; i++)
    {
      mask = masks[g_test_rand_int_range (0, G_N_ELEMENTS (tests))];
      copy = _gtk_bitmask_copy (mask);

      for (j = 0; j < N_TRIES; j++)
        {
          indexes[j] = g_test_rand_int_range (0, MAX_INDEX);
          _gtk_bitmask_set (copy, indexes[j], g_test_rand_bit ());
        }

      for (j = 0; j < N_TRIES; j++)
        {
          _gtk_bitmask_set (copy, indexes[j], _gtk_bitmask_get (mask, indexes[j]));
        }

      assert_cmpmasks (copy, mask);
      _gtk_bitmask_free (copy);
    }
}

static void
test_union (void)
{
  GtkBitmask *left, *right, *expected;
  guint run, try, n_tries;

  for (run = 0; run < N_RUNS; run++)
    {
      left = _gtk_bitmask_new ();
      right = _gtk_bitmask_new ();
      expected = _gtk_bitmask_new ();

      n_tries = g_test_perf () ? N_TRIES : g_test_rand_int_range (0, N_TRIES);
      for (try = 0; try < n_tries; try++)
        {
          guint id = g_test_rand_int_range (0, MAX_INDEX);

          if (g_test_rand_bit ())
            _gtk_bitmask_set (left, id, TRUE);
          else
            _gtk_bitmask_set (right, id, TRUE);

          _gtk_bitmask_set (expected, id, TRUE);
        }

      _gtk_bitmask_union (left, right);
      _gtk_bitmask_union (right, left);

      assert_cmpmasks (left, expected);
      assert_cmpmasks (right, expected);
      _gtk_bitmask_free (left);
      _gtk_bitmask_free (right);
      _gtk_bitmask_free (expected);
    }
}

static void
test_intersect (void)
{
  GtkBitmask *left, *right, *expected;
  guint run, try;
  gboolean intersects;

  for (run = 0; run < N_RUNS; run++)
    {
      left = _gtk_bitmask_new ();
      right = _gtk_bitmask_new ();
      expected = _gtk_bitmask_new ();

      for (try = 0; try < N_TRIES; try++)
        {
          guint id = g_test_rand_int_range (0, MAX_INDEX);
          gboolean set = g_test_rand_bit ();

          if (g_test_rand_bit ())
            {
              _gtk_bitmask_set (left, id, set);
              _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (right, id) : 0);
            }
          else
            {
              _gtk_bitmask_set (right, id, set);
              _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (left, id) : 0);
            }
        }

      intersects = _gtk_bitmask_intersects (left, right);
      g_assert_cmpint (intersects, ==, _gtk_bitmask_intersects (right, left));
      g_assert_cmpint (intersects, !=, _gtk_bitmask_is_empty (expected));

      _gtk_bitmask_intersect (left, right);
      _gtk_bitmask_intersect (right, left);

      assert_cmpmasks (left, expected);
      assert_cmpmasks (right, expected);
      _gtk_bitmask_free (left);
      _gtk_bitmask_free (right);
      _gtk_bitmask_free (expected);
    }
}

#define SWAP(_a, _b) G_STMT_START{ \
  guint _tmp = _a; \
  _a = _b; \
  _b = _tmp; \
}G_STMT_END

static void
test_invert_range (void)
{
  GtkBitmask *left, *right, *intersection, *expected;
  guint run;
  guint left_start, left_end, right_start, right_end, start, end;

  for (run = 0; run < N_RUNS; run++)
    {
      left = _gtk_bitmask_new ();
      right = _gtk_bitmask_new ();
      expected = _gtk_bitmask_new ();

      left_start = g_test_rand_int_range (0, MAX_INDEX);
      left_end = g_test_rand_int_range (0, MAX_INDEX);
      if (left_start > left_end)
        SWAP (left_start, left_end);
      right_start = g_test_rand_int_range (0, MAX_INDEX);
      right_end = g_test_rand_int_range (0, MAX_INDEX);
      if (right_start > right_end)
        SWAP (right_start, right_end);
      start = MAX (left_start, right_start);
      end = MIN (left_end, right_end);

      if (left_start != left_end)
        _gtk_bitmask_invert_range (left, left_start, left_end);
      if (right_start != right_end)
        _gtk_bitmask_invert_range (right, right_start, right_end);
      if (start < end)
        _gtk_bitmask_invert_range (expected, start, end);

      intersection = _gtk_bitmask_copy (left);
      _gtk_bitmask_intersect (intersection, right);

      assert_cmpmasks (intersection, expected);

      if (start < end)
        _gtk_bitmask_invert_range (expected, start, end);

      g_assert_cmpint (_gtk_bitmask_is_empty (expected), ==, TRUE);

      _gtk_bitmask_free (left);
      _gtk_bitmask_free (right);
      _gtk_bitmask_free (intersection);
      _gtk_bitmask_free (expected);
    }
}

/* SETUP & RUNNING */

static void
create_masks (void)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    masks[i] = gtk_bitmask_new_parse (tests[i]);
}

static void
free_masks (void)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      _gtk_bitmask_free (masks[i]);
      masks[i] = NULL;
    }
}

int
main (int argc, char *argv[])
{
  int result;

  g_test_init (&argc, &argv, NULL);
  setlocale (LC_ALL, "C");
  g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=%s");

  create_masks ();

  g_test_add_func ("/bitmask/to_string", test_to_string);
  g_test_add_func ("/bitmask/is_empty", test_is_empty);
  g_test_add_func ("/bitmask/equals", test_equals);
  g_test_add_func ("/bitmask/set", test_set);
  g_test_add_func ("/bitmask/union", test_union);
  g_test_add_func ("/bitmask/intersect", test_intersect);
  g_test_add_func ("/bitmask/invert_range", test_invert_range);

  result = g_test_run ();

  free_masks ();

  return result;
}