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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_arrays.h>
32 static void test_array_insert_remove(void)
34 DECL_ARRAY(int) 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);
61 static void test_array_foreach(void)
63 DECL_ARRAY(int) array
;
66 for (int i
= 0; i
< 10; ++i
)
67 ARRAY_APPEND(array
, i
);
71 ARRAY_FOREACH(item
, array
)
73 assert(item
== count
);
81 static void test_array_find(void)
83 DECL_ARRAY(int) 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);
97 ARRAY_FIND(array
, 17, index
);
100 ARRAY_FIND(array
, 52, index
);
103 ARRAY_FIND(array
, 26, index
);
106 ARRAY_FIND(array
, 13, index
);
109 ARRAY_FIND(array
, 10, index
);
112 ARRAY_FIND(array
, 5, index
);
115 ARRAY_FIND(array
, 14, index
);
121 static void test_array_bsearch(void)
128 DECL_ARRAY(struct item
) 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 });
141 ARRAY_BSEARCH(array
, .value
, int, 1, index
);
144 ARRAY_BSEARCH(array
, .value
, int, 2, index
);
147 ARRAY_BSEARCH(array
, .value
, int, 3, index
);
150 ARRAY_BSEARCH(array
, .value
, int, 8, index
);
153 ARRAY_BSEARCH(array
, .value
, int, 21, index
);
156 ARRAY_BSEARCH(array
, .value
, int, 4, index
);
164 test_array_insert_remove();
165 test_array_foreach();
167 test_array_bsearch();