• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #ifndef _WESTON_TEST_RUNNER_H_
28 #define _WESTON_TEST_RUNNER_H_
29 
30 #include "config.h"
31 
32 #include <stdlib.h>
33 
34 #include <wayland-util.h>
35 #include "shared/helpers.h"
36 #include "weston-test-fixture-compositor.h"
37 #include "weston-testsuite-data.h"
38 
39 #ifdef NDEBUG
40 #error "Tests must not be built with NDEBUG defined, they rely on assert()."
41 #endif
42 
43 /** Test program entry
44  *
45  * Each invocation of TEST(), TEST_P(), or PLUGIN_TEST() will create one
46  * more weston_test_entry in a custom named section in the final binary.
47  * Iterating through the section then allows to iterate through all
48  * the defined tests.
49  *
50  * \ingroup testharness_private
51  */
52 struct weston_test_entry {
53 	const char *name;
54 	void (*run)(void *);
55 	const void *table_data;
56 	size_t element_size;
57 	int n_elements;
58 } __attribute__ ((aligned (32)));
59 
60 #define TEST_BEGIN(name, arg)						\
61 	static void name(arg)
62 
63 #define TEST_COMMON(func, name, data, size, n_elem)			\
64 	static void func(void *);					\
65 									\
66 	const struct weston_test_entry test##name			\
67 		__attribute__ ((used, section ("test_section"))) =	\
68 	{								\
69 		#name, func, data, size, n_elem				\
70 	};
71 
72 #define NO_ARG_TEST(name)						\
73 	TEST_COMMON(wrap##name, name, NULL, 0, 1)			\
74 	static void name(void);						\
75 	static void wrap##name(void *data)				\
76 	{								\
77 		(void) data;						\
78 		name();							\
79 	}								\
80 									\
81 	TEST_BEGIN(name, void)
82 
83 #define ARG_TEST(name, test_data)					\
84 	TEST_COMMON(name, name, test_data,				\
85 		    sizeof(test_data[0]),				\
86 		    ARRAY_LENGTH(test_data))				\
87 	TEST_BEGIN(name, void *data)					\
88 
89 /** Add a test with no parameters
90  *
91  * This defines one test as a new function. Use this macro in place of the
92  * function signature and put the function body after this.
93  *
94  * \param name Name for the test, must be a valid function name.
95  *
96  * \ingroup testharness
97  */
98 #define TEST(name) NO_ARG_TEST(name)
99 
100 /** Add an array driven test with a parameter
101  *
102  * This defines an array of tests as a new function. Use this macro in place
103  * of the function signature and put the function body after this. The function
104  * will be executed once for each element in \c data_array, passing the
105  * element as the argument <tt>void *data</tt> to the function.
106  *
107  * This macro is not usable if fixture setup is using
108  * weston_test_harness_execute_as_plugin().
109  *
110  * \param name Name for the test, must be a valid function name.
111  * \param data_array A static const array of any type. The length will be
112  * recorded automatically.
113  *
114  * \ingroup testharness
115  */
116 #define TEST_P(name, data_array) ARG_TEST(name, data_array)
117 
118 /** Add a test with weston_compositor argument
119  *
120  * This defines one test as a new function. Use this macro in place of the
121  * function signature and put the function body after this. The function
122  * will have one argument <tt>struct weston_compositor *compositor</tt>.
123  *
124  * This macro is only usable if fixture setup is using
125  * weston_test_harness_execute_as_plugin().
126  *
127  * \param name Name for the test, must be a valid function name.
128  *
129  * \ingroup testharness
130  */
131 #define PLUGIN_TEST(name) 						\
132 	TEST_COMMON(wrap##name, name, NULL, 0, 1)			\
133 	static void name(struct weston_compositor *);			\
134 	static void wrap##name(void *compositor)			\
135 	{								\
136 		name(compositor);					\
137 	}								\
138 	TEST_BEGIN(name, struct weston_compositor *compositor)
139 
140 void
141 testlog(const char *fmt, ...) WL_PRINTF(1, 2);
142 
143 const char *
144 get_test_name(void);
145 
146 int
147 get_test_fixture_index(void);
148 
149 /** Fixture setup array record
150  *
151  * Helper to store the attributes of the data array passed in to
152  * DECLARE_FIXTURE_SETUP_WITH_ARG().
153  *
154  * \ingroup testharness_private
155  */
156 struct fixture_setup_array {
157 	const void *array;
158 	size_t element_size;
159 	int n_elements;
160 };
161 
162 const struct fixture_setup_array *
163 fixture_setup_array_get_(void);
164 
165 /** Test harness context
166  *
167  * \ingroup testharness
168  */
169 struct weston_test_harness;
170 
171 enum test_result_code
172 fixture_setup_run_(struct weston_test_harness *harness, const void *arg_);
173 
174 /** Register a fixture setup function
175  *
176  * This registers the given (preferably static) function to be used for setting
177  * up any fixtures you might need. The function must have the signature:
178  *
179  * \code
180  * enum test_result_code func_(struct weston_test_harness *harness)
181  * \endcode
182  *
183  * The function must call one of weston_test_harness_execute_standalone(),
184  * weston_test_harness_execute_as_plugin() or
185  * weston_test_harness_execute_as_client() passing in the \c harness argument,
186  * and return the return value from that call. The function can also return a
187  * test_result_code on its own if it does not want to run the tests,
188  * e.g. RESULT_SKIP or RESULT_HARD_ERROR.
189  *
190  * The function will be called once to run all tests.
191  *
192  * \param func_ The function to be used as fixture setup.
193  *
194  * \ingroup testharness
195  */
196 #define DECLARE_FIXTURE_SETUP(func_)					\
197 	enum test_result_code						\
198 	fixture_setup_run_(struct weston_test_harness *harness,		\
199 			   const void *arg_)				\
200 	{								\
201 		return func_(harness);					\
202 	}
203 
204 /** Register a fixture setup function with a data array
205  *
206  * This registers the given (preferably static) function to be used for setting
207  * up any fixtures you might need. The function must have the signature:
208  *
209  * \code
210  * enum test_result_code func_(struct weston_test_harness *harness, typeof(array_[0]) *arg)
211  * \endcode
212  *
213  * The function must call one of weston_test_harness_execute_standalone(),
214  * weston_test_harness_execute_as_plugin() or
215  * weston_test_harness_execute_as_client() passing in the \c harness argument,
216  * and return the return value from that call. The function can also return a
217  * test_result_code on its own if it does not want to run the tests,
218  * e.g. RESULT_SKIP or RESULT_HARD_ERROR.
219  *
220  * The function will be called once with each element of the array pointed to
221  * by \c arg, so that all tests would be repeated for each element in turn.
222  *
223  * \param func_ The function to be used as fixture setup.
224  * \param array_ A static const array of arbitrary type.
225  *
226  * \ingroup testharness
227  */
228 #define DECLARE_FIXTURE_SETUP_WITH_ARG(func_, array_)			\
229 	const struct fixture_setup_array *				\
230 	fixture_setup_array_get_(void)					\
231 	{								\
232 		static const struct fixture_setup_array arr = {		\
233 			.array = array_,				\
234 			.element_size = sizeof(array_[0]),		\
235 			.n_elements = ARRAY_LENGTH(array_)		\
236 		};							\
237 		return &arr;						\
238 	}								\
239 									\
240 	enum test_result_code						\
241 	fixture_setup_run_(struct weston_test_harness *harness,		\
242 			   const void *arg_)				\
243 	{								\
244 		typeof(array_[0]) *arg = arg_;				\
245 		return func_(harness, arg);				\
246 	}
247 
248 enum test_result_code
249 weston_test_harness_execute_as_client(struct weston_test_harness *harness,
250 				      const struct compositor_setup *setup);
251 
252 enum test_result_code
253 weston_test_harness_execute_as_plugin(struct weston_test_harness *harness,
254 				      const struct compositor_setup *setup);
255 
256 enum test_result_code
257 weston_test_harness_execute_standalone(struct weston_test_harness *harness);
258 
259 #endif
260