Remove old hashtable and replace tests
[platform/upstream/libtsm.git] / test / test_common.h
1 /*
2  * TSM - Test Helper
3  *
4  * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Test Helper
28  * This header includes all kinds of helpers for testing. It tries to include
29  * everything required and provides simple macros to avoid duplicating code in
30  * each test. We try to keep tests as small as possible and move everything that
31  * might be common here.
32  *
33  * We avoid sticking to our usual coding conventions (including headers in
34  * source files, etc. ..) and instead make this the most convenient we can.
35  */
36
37 #ifndef TEST_COMMON_H
38 #define TEST_COMMON_H
39
40 #include <check.h>
41 #include <inttypes.h>
42 #include <stdarg.h>
43 #include <stdbool.h>
44 #include <stdlib.h>
45 #include "libtsm.h"
46 #include "libtsm_int.h"
47 #include "shl_htable.h"
48
49 /* lower address-space is protected from user-allocation, so this is invalid */
50 #define TEST_INVALID_PTR ((void*)0x10)
51
52 #define TEST_DEFINE_CASE(_name)                                 \
53         static TCase *test_create_case_##_name(void)            \
54         {                                                       \
55                 TCase *tc;                                      \
56                                                                 \
57                 tc = tcase_create(#_name);                      \
58
59 #define TEST(_name) tcase_add_test(tc, _name);
60
61 #define TEST_END_CASE                                           \
62                 return tc;                                      \
63         }                                                       \
64
65 #define TEST_END NULL
66
67 #define TEST_CASE(_name) test_create_case_##_name
68
69 static inline Suite *test_create_suite(const char *name, ...)
70 {
71         Suite *s;
72         va_list list;
73         TCase *(*fn)(void);
74
75         s = suite_create(name);
76
77         va_start(list, name);
78         while ((fn = va_arg(list, TCase *(*)(void))))
79                 suite_add_tcase(s, fn());
80         va_end(list);
81
82         return s;
83 }
84
85 #define TEST_SUITE(_name, ...) test_create_suite((#_name), ##__VA_ARGS__)
86
87 static inline int test_run_suite(Suite *s)
88 {
89         int ret;
90         SRunner *sr;
91
92         sr = srunner_create(s);
93         srunner_run_all(sr, CK_NORMAL);
94         ret = srunner_ntests_failed(sr);
95         srunner_free(sr);
96
97         return ret;
98 }
99
100 #define TEST_DEFINE(_suite) \
101         int main(int argc, char **argv) \
102         { \
103                 return test_run_suite(_suite); \
104         }
105
106 #endif /* TEST_COMMON_H */