1/*
2 * Copyright © 2020 Benjamin Otte
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, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <otte@gnome.org>
18 */
19
20#include <gtk/gtk.h>
21
22#define GDK_ARRAY_NO_UNDEF
23
24#include "../../gdk/gdkarrayimpl.c"
25
26static void
27gdk_array(test_simple) (void)
28{
29 GdkArray v;
30 gsize i;
31
32 gdk_array(init) (self: &v);
33
34 for (i = 0; i < 1000; i++)
35 {
36 g_assert_cmpint (gdk_array(get_size) (&v), ==, i);
37 g_assert_cmpint (gdk_array(get_size) (&v), <=, gdk_array(get_capacity) (&v));
38 gdk_array(append) (self: &v, value: i);
39#ifdef GDK_ARRAY_NULL_TERMINATED
40 g_assert_cmpint (*gdk_array(index) (&v, gdk_array(get_size) (&v)), ==, 0);
41#endif
42 }
43 g_assert_cmpint (gdk_array(get_size) (&v), ==, i);
44 g_assert_cmpint (gdk_array(get_size) (&v), <=, gdk_array(get_capacity) (&v));
45
46 for (i = 0; i < 1000; i++)
47 {
48 g_assert_cmpint (gdk_array(get) (&v, i), ==, i);
49 }
50
51 gdk_array(clear) (self: &v);
52}
53
54static void
55gdk_array(test_splice) (void)
56{
57 GdkArray v;
58 gsize i, j, sum;
59 gsize pos, add, remove;
60 int additions[4] = { 0, 1, 2, 3 };
61
62 gdk_array(init) (self: &v);
63 sum = 0;
64
65 for (i = 0; i < 1000; i++)
66 {
67 gsize old_size = gdk_array(get_size) (self: &v);
68
69 pos = g_random_int_range (begin: 0, end: old_size + 1);
70 g_assert_true (pos <= old_size);
71 remove = g_random_int_range (begin: 0, end: 4);
72 remove = MIN (remove, old_size - pos);
73 add = g_random_int_range (begin: 0, end: 4);
74
75 for (j = 0; j < remove; j++)
76 sum -= gdk_array(get) (self: &v, pos: pos + j);
77 for (j = 0; j < add; j++)
78 sum += ++additions[j];
79
80 gdk_array(splice) (self: &v, pos, removed: remove, FALSE, additions, added: add);
81 {
82 gsize total = 0;
83 for (j = 0; j < gdk_array(get_size) (self: &v); j++)
84 total += gdk_array(get) (self: &v, pos: j);
85 g_assert_cmpint (total, ==, sum);
86 }
87
88 g_assert_cmpint (gdk_array(get_size) (&v), ==, old_size + add - remove);
89 g_assert_cmpint (gdk_array(get_size) (&v), <=, gdk_array(get_capacity) (&v));
90#ifdef GDK_ARRAY_NULL_TERMINATED
91 if (gdk_array(get_size) (&v))
92 g_assert_cmpint (*gdk_array(index) (&v, gdk_array(get_size) (&v)), ==, 0);
93#endif
94 for (j = 0; j < add; j++)
95 g_assert_cmpint (gdk_array(get) (&v, pos + j), ==, additions[j]);
96 }
97
98 for (i = 0; i < gdk_array(get_size) (self: &v); i++)
99 {
100 sum -= gdk_array(get) (self: &v, pos: i);
101 }
102 g_assert_cmpint (sum, ==, 0);
103
104 gdk_array(clear) (self: &v);
105}
106
107#undef _T_
108#undef GdkArray
109#undef gdk_array_paste_more
110#undef gdk_array_paste
111#undef gdk_array
112#undef GDK_ARRAY_REAL_SIZE
113
114#undef GDK_ARRAY_ELEMENT_TYPE
115#undef GDK_ARRAY_NAME
116#undef GDK_ARRAY_TYPE_NAME
117#undef GDK_ARRAY_PREALLOC
118#undef GDK_ARRAY_NULL_TERMINATED
119
120

source code of gtk/testsuite/gdk/arrayimpl.c