Fortunes about QT and "hearing" video codecs
[vlc.git] / src / test / arrays.c
blobf20c8f6edca8aa5b0550194d080156731088a267
1 /*****************************************************************************
2 * arrays.h : Test for ARRAY_* macros
3 *****************************************************************************
4 * Copyright (C) 2018 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #undef NDEBUG
27 #include <assert.h>
29 #include <vlc_common.h>
30 #include <vlc_arrays.h>
32 static void test_array_insert_remove(void)
34 DECL_ARRAY(int) array;
35 ARRAY_INIT(array);
37 ARRAY_APPEND(array, 42);
38 assert(array.i_size == 1);
39 assert(ARRAY_VAL(array, 0) == 42);
41 ARRAY_REMOVE(array, 0);
42 assert(array.i_size == 0);
44 ARRAY_APPEND(array, 43);
45 ARRAY_APPEND(array, 44);
46 ARRAY_APPEND(array, 45);
47 ARRAY_REMOVE(array, 1);
48 assert(array.i_size == 2);
49 assert(ARRAY_VAL(array, 0) == 43);
50 assert(ARRAY_VAL(array, 1) == 45);
52 ARRAY_INSERT(array, 100, 1);
53 assert(array.i_size == 3);
54 assert(ARRAY_VAL(array, 0) == 43);
55 assert(ARRAY_VAL(array, 1) == 100);
56 assert(ARRAY_VAL(array, 2) == 45);
58 ARRAY_RESET(array);
61 static void test_array_foreach(void)
63 DECL_ARRAY(int) array;
64 ARRAY_INIT(array);
66 for (int i = 0; i < 10; ++i)
67 ARRAY_APPEND(array, i);
69 int count = 0;
70 int item;
71 ARRAY_FOREACH(item, array)
73 assert(item == count);
74 count++;
76 assert(count == 10);
78 ARRAY_RESET(array);
81 static void test_array_find(void)
83 DECL_ARRAY(int) array;
84 ARRAY_INIT(array);
86 ARRAY_APPEND(array, 17);
87 ARRAY_APPEND(array, 52);
88 ARRAY_APPEND(array, 26);
89 ARRAY_APPEND(array, 13);
90 ARRAY_APPEND(array, 40);
91 ARRAY_APPEND(array, 20);
92 ARRAY_APPEND(array, 10);
93 ARRAY_APPEND(array, 5);
95 int index;
97 ARRAY_FIND(array, 17, index);
98 assert(index == 0);
100 ARRAY_FIND(array, 52, index);
101 assert(index == 1);
103 ARRAY_FIND(array, 26, index);
104 assert(index == 2);
106 ARRAY_FIND(array, 13, index);
107 assert(index == 3);
109 ARRAY_FIND(array, 10, index);
110 assert(index == 6);
112 ARRAY_FIND(array, 5, index);
113 assert(index == 7);
115 ARRAY_FIND(array, 14, index);
116 assert(index == -1);
118 ARRAY_RESET(array);
121 static void test_array_bsearch(void)
123 struct item {
124 int value;
126 struct item item;
128 DECL_ARRAY(struct item) array;
129 ARRAY_INIT(array);
131 ARRAY_APPEND(array, (struct item) { 1 });
132 ARRAY_APPEND(array, (struct item) { 2 });
133 ARRAY_APPEND(array, (struct item) { 3 });
134 ARRAY_APPEND(array, (struct item) { 5 });
135 ARRAY_APPEND(array, (struct item) { 8 });
136 ARRAY_APPEND(array, (struct item) { 13 });
137 ARRAY_APPEND(array, (struct item) { 21 });
139 int index;
141 ARRAY_BSEARCH(array, .value, int, 1, index);
142 assert(index == 0);
144 ARRAY_BSEARCH(array, .value, int, 2, index);
145 assert(index == 1);
147 ARRAY_BSEARCH(array, .value, int, 3, index);
148 assert(index == 2);
150 ARRAY_BSEARCH(array, .value, int, 8, index);
151 assert(index == 4);
153 ARRAY_BSEARCH(array, .value, int, 21, index);
154 assert(index == 6);
156 ARRAY_BSEARCH(array, .value, int, 4, index);
157 assert(index == -1);
159 ARRAY_RESET(array);
162 int main(void)
164 test_array_insert_remove();
165 test_array_foreach();
166 test_array_find();
167 test_array_bsearch();