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
TEST(array_init)33 TEST(array_init)
34 {
35 struct wl_array array;
36
37 /* fill with garbage to emulate uninitialized memory */
38 memset(&array, 0x57, sizeof array);
39
40 wl_array_init(&array);
41 assert(array.size == 0);
42 assert(array.alloc == 0);
43 assert(array.data == 0);
44 }
45
TEST(array_release)46 TEST(array_release)
47 {
48 struct wl_array array;
49 void *ptr;
50
51 wl_array_init(&array);
52 ptr = wl_array_add(&array, 1);
53 assert(ptr != NULL);
54 assert(array.data != NULL);
55
56 wl_array_release(&array);
57 assert(array.data == WL_ARRAY_POISON_PTR);
58 }
59
TEST(array_add)60 TEST(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);
75
76 /* add some data */
77 for (i = 0; i < iterations; i++) {
78 struct mydata* ptr = wl_array_add(&array, 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 const int index = datasize * i;
91 struct mydata* check = (struct mydata*)(array.data + index);
92
93 assert(check->a == i * 3);
94 assert(check->b == 20000 - i);
95 assert(check->c == (double)(i));
96 assert(check->d == (double)(i/2.));
97 }
98
99 wl_array_release(&array);
100 }
101
TEST(array_copy)102 TEST(array_copy)
103 {
104 const int iterations = 1529; /* this is arbitrary */
105 struct wl_array source;
106 struct wl_array copy;
107 int i;
108
109 wl_array_init(&source);
110
111 /* add some data */
112 for (i = 0; i < iterations; i++) {
113 int *p = wl_array_add(&source, sizeof(int));
114 assert(p);
115 *p = i * 2 + i;
116 }
117
118 /* copy the array */
119 wl_array_init(©);
120 wl_array_copy(©, &source);
121
122 /* check the copy */
123 for (i = 0; i < iterations; i++) {
124 const int index = sizeof(int) * i;
125 int *s = (int *)(source.data + index);
126 int *c = (int *)(copy.data + index);
127
128 assert(*s == *c); /* verify the values are the same */
129 assert(s != c); /* ensure the addresses aren't the same */
130 assert(*s == i * 2 + i); /* sanity check */
131 }
132
133 wl_array_release(&source);
134 wl_array_release(©);
135 }
136
TEST(array_for_each)137 TEST(array_for_each)
138 {
139 static const int elements[] = { 77, 12, 45192, 53280, 334455 };
140 struct wl_array array;
141 int *p;
142 int i;
143
144 wl_array_init(&array);
145 for (i = 0; i < 5; i++) {
146 p = wl_array_add(&array, sizeof *p);
147 assert(p);
148 *p = elements[i];
149 }
150
151 i = 0;
152 wl_array_for_each(p, &array) {
153 assert(*p == elements[i]);
154 i++;
155 }
156 assert(i == 5);
157
158 wl_array_release(&array);
159 }
160