1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include <stdlib.h>
27#include <assert.h>
28#include <string.h>
29#include "wayland-util.h"
30#include "wayland-private.h"
31#include "test-runner.h"
32
33TEST(array_init)
34{
35 struct wl_array array;
36
37 /* fill with garbage to emulate uninitialized memory */
38 memset(s: &array, c: 0x57, n: sizeof array);
39
40 wl_array_init(array: &array);
41 assert(array.size == 0);
42 assert(array.alloc == 0);
43 assert(array.data == 0);
44}
45
46TEST(array_release)
47{
48 struct wl_array array;
49 void *ptr;
50
51 wl_array_init(array: &array);
52 ptr = wl_array_add(array: &array, size: 1);
53 assert(ptr != NULL);
54 assert(array.data != NULL);
55
56 wl_array_release(array: &array);
57 assert(array.data == WL_ARRAY_POISON_PTR);
58}
59
60TEST(array_add)
61{
62 struct mydata {
63 unsigned int a;
64 unsigned int b;
65 double c;
66 double d;
67 };
68
69 const unsigned int iterations = 1321; /* this is arbitrary */
70 const int datasize = sizeof(struct mydata);
71 struct wl_array array;
72 size_t i;
73
74 wl_array_init(array: &array);
75
76 /* add some data */
77 for (i = 0; i < iterations; i++) {
78 struct mydata* ptr = wl_array_add(array: &array, size: datasize);
79 assert(ptr);
80 assert((i + 1) * datasize == array.size);
81
82 ptr->a = i * 3;
83 ptr->b = 20000 - i;
84 ptr->c = (double)(i);
85 ptr->d = (double)(i / 2.);
86 }
87
88 /* verify the data */
89 for (i = 0; i < iterations; ++i) {
90 struct mydata* check = (struct mydata*)array.data + i;
91
92 assert(check->a == i * 3);
93 assert(check->b == 20000 - i);
94 assert(check->c == (double)(i));
95 assert(check->d == (double)(i/2.));
96 }
97
98 wl_array_release(array: &array);
99}
100
101TEST(array_copy)
102{
103 const int iterations = 1529; /* this is arbitrary */
104 struct wl_array source;
105 struct wl_array copy;
106 int i;
107
108 wl_array_init(array: &source);
109
110 /* add some data */
111 for (i = 0; i < iterations; i++) {
112 int *p = wl_array_add(array: &source, size: sizeof(int));
113 assert(p);
114 *p = i * 2 + i;
115 }
116
117 /* copy the array */
118 wl_array_init(array: &copy);
119 wl_array_copy(array: &copy, source: &source);
120
121 /* check the copy */
122 for (i = 0; i < iterations; i++) {
123 int *s = (int *)source.data + i;
124 int *c = (int *)copy.data + i;
125
126 assert(*s == *c); /* verify the values are the same */
127 assert(s != c); /* ensure the addresses aren't the same */
128 assert(*s == i * 2 + i); /* sanity check */
129 }
130
131 wl_array_release(array: &source);
132 wl_array_release(array: &copy);
133}
134
135TEST(array_for_each)
136{
137 static const int elements[] = { 77, 12, 45192, 53280, 334455 };
138 struct wl_array array;
139 int *p;
140 int i;
141
142 wl_array_init(array: &array);
143 for (i = 0; i < 5; i++) {
144 p = wl_array_add(array: &array, size: sizeof *p);
145 assert(p);
146 *p = elements[i];
147 }
148
149 i = 0;
150 wl_array_for_each(p, &array) {
151 assert(*p == elements[i]);
152 i++;
153 }
154 assert(i == 5);
155
156 wl_array_release(array: &array);
157}
158

source code of gtk/subprojects/wayland/tests/array-test.c