2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
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:
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.
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
27 #ifndef _WESTON_TEST_RUNNER_H_
28 #define _WESTON_TEST_RUNNER_H_
34 #include <wayland-util.h>
35 #include "shared/helpers.h"
36 #include "weston-test-fixture-compositor.h"
37 #include "weston-testsuite-data.h"
40 #error "Tests must not be built with NDEBUG defined, they rely on assert()."
43 /** Test program entry
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
50 * \ingroup testharness_private
52 struct weston_test_entry {
55 const void *table_data;
58 } __attribute__ ((aligned (32)));
60 #define TEST_BEGIN(name, arg) \
63 #define TEST_COMMON(func, name, data, size, n_elem) \
64 static void func(void *); \
66 const struct weston_test_entry test##name \
67 __attribute__ ((used, section ("test_section"))) = \
69 #name, func, data, size, n_elem \
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) \
81 TEST_BEGIN(name, void)
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) \
89 /** Add a test with no parameters
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.
94 * \param name Name for the test, must be a valid function name.
96 * \ingroup testharness
98 #define TEST(name) NO_ARG_TEST(name)
100 /** Add an array driven test with a parameter
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.
107 * This macro is not usable if fixture setup is using
108 * weston_test_harness_execute_as_plugin().
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.
114 * \ingroup testharness
116 #define TEST_P(name, data_array) ARG_TEST(name, data_array)
118 /** Add a test with weston_compositor argument
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>.
124 * This macro is only usable if fixture setup is using
125 * weston_test_harness_execute_as_plugin().
127 * \param name Name for the test, must be a valid function name.
129 * \ingroup testharness
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) \
138 TEST_BEGIN(name, struct weston_compositor *compositor)
141 testlog(const char *fmt, ...) WL_PRINTF(1, 2);
147 get_test_fixture_index(void);
149 /** Fixture setup array record
151 * Helper to store the attributes of the data array passed in to
152 * DECLARE_FIXTURE_SETUP_WITH_ARG().
154 * \ingroup testharness_private
156 struct fixture_setup_array {
162 const struct fixture_setup_array *
163 fixture_setup_array_get_(void);
165 /** Test harness context
167 * \ingroup testharness
169 struct weston_test_harness;
171 enum test_result_code
172 fixture_setup_run_(struct weston_test_harness *harness, const void *arg_);
174 /** Register a fixture setup function
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:
180 * enum test_result_code func_(struct weston_test_harness *harness)
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.
190 * The function will be called once to run all tests.
192 * \param func_ The function to be used as fixture setup.
194 * \ingroup testharness
196 #define DECLARE_FIXTURE_SETUP(func_) \
197 enum test_result_code \
198 fixture_setup_run_(struct weston_test_harness *harness, \
201 return func_(harness); \
204 /** Register a fixture setup function with a data array
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:
210 * enum test_result_code func_(struct weston_test_harness *harness, typeof(array_[0]) *arg)
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.
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.
223 * \param func_ The function to be used as fixture setup.
224 * \param array_ A static const array of arbitrary type.
226 * \ingroup testharness
228 #define DECLARE_FIXTURE_SETUP_WITH_ARG(func_, array_) \
229 const struct fixture_setup_array * \
230 fixture_setup_array_get_(void) \
232 static const struct fixture_setup_array arr = { \
234 .element_size = sizeof(array_[0]), \
235 .n_elements = ARRAY_LENGTH(array_) \
240 enum test_result_code \
241 fixture_setup_run_(struct weston_test_harness *harness, \
244 typeof(array_[0]) *arg = arg_; \
245 return func_(harness, arg); \
248 enum test_result_code
249 weston_test_harness_execute_as_client(struct weston_test_harness *harness,
250 const struct compositor_setup *setup);
252 enum test_result_code
253 weston_test_harness_execute_as_plugin(struct weston_test_harness *harness,
254 const struct compositor_setup *setup);
256 enum test_result_code
257 weston_test_harness_execute_standalone(struct weston_test_harness *harness);