1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file isc/cmocka.h */
13
14 #pragma once
15
16 #include <cmocka.h>
17
18 #include <isc/lang.h>
19
20 ISC_LANG_BEGINDECLS
21
22 /*
23 * Copy the test identified by 'name' from 'tests' to 'selected'.
24 */
25 #define cmocka_add_test_byname(tests, name, selected) \
26 _cmocka_add_test_byname(tests, sizeof(tests) / sizeof(tests[0]), name, \
27 selected, \
28 sizeof(selected) / sizeof(selected[0]))
29
30 static inline bool
_cmocka_add_test_byname(const struct CMUnitTest * tests,size_t ntests,const char * name,struct CMUnitTest * selected,size_t nselected)31 _cmocka_add_test_byname(const struct CMUnitTest *tests, size_t ntests,
32 const char *name, struct CMUnitTest *selected,
33 size_t nselected) {
34 size_t i, j;
35
36 for (i = 0; i < ntests && tests[i].name != NULL; i++) {
37 if (strcmp(tests[i].name, name) != 0) {
38 continue;
39 }
40 for (j = 0; j < nselected && selected[j].name != NULL; j++) {
41 if (strcmp(tests[j].name, name) == 0) {
42 break;
43 }
44 }
45 if (j < nselected && selected[j].name == NULL) {
46 selected[j] = tests[i];
47 }
48 return (true);
49 }
50 return (false);
51 }
52
53 ISC_LANG_ENDDECLS
54