blob: 64d823f6bf48fbea1f46bf857eca7013aebbc71d [file] [log] [blame]
Daniel Vetterd63fe152014-03-12 01:29:52 +01001/*
2 * Copyright © 2007,2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 *
27 */
28
29
30#ifndef IGT_CORE_H
31#define IGT_CORE_H
32
Daniel Vetter924115b2014-03-22 20:18:51 +010033#include <setjmp.h>
Daniel Vetter6cfcd712014-03-22 20:07:35 +010034#include <stdbool.h>
Chris Wilsondcb39b52016-03-18 09:04:07 +000035#include <stdint.h>
Daniel Vetter254f19b2014-03-22 21:29:01 +010036#include <stdlib.h>
Joonas Lahtinena95033f2015-03-31 15:53:17 +030037#include <stdio.h>
Daniel Vetter924115b2014-03-22 20:18:51 +010038#include <string.h>
39#include <sys/types.h>
Daniel Vetter254f19b2014-03-22 21:29:01 +010040#include <stdarg.h>
Thomas Wood80d2c9b2014-07-21 15:57:16 +010041#include <getopt.h>
Daniel Vetter6cfcd712014-03-22 20:07:35 +010042
Thomas Wood01676192014-12-17 11:37:31 +000043#ifndef IGT_LOG_DOMAIN
44#define IGT_LOG_DOMAIN (NULL)
45#endif
46
47
Thomas Woodf8b3c702014-10-10 16:56:20 +010048extern const char* __igt_test_description __attribute__((weak));
Thomas Wood790f1f82015-11-09 17:17:24 +000049extern bool __igt_plain_output;
50
Thomas Woodf8b3c702014-10-10 16:56:20 +010051
52/**
53 * IGT_TEST_DESCRIPTION:
54 * @str: description string
55 *
56 * Defines a description for a test. This is used as the output for the
57 * "--help-description" option and is also included in the generated
58 * documentation.
59 */
60#define IGT_TEST_DESCRIPTION(str) const char* __igt_test_description = str
61
Thomas Wood17eb0622014-05-13 15:22:52 +010062/**
63 * IGT_EXIT_TIMEOUT:
64 *
65 * Exit status indicating a timeout occurred.
66 */
67#define IGT_EXIT_TIMEOUT 78
68
69/**
70 * IGT_EXIT_SKIP:
71 *
72 * Exit status indicating the test was skipped.
73 */
74#define IGT_EXIT_SKIP 77
75
76/**
77 * IGT_EXIT_SUCCESS
78 *
79 * Exit status indicating the test executed successfully.
80 */
81#define IGT_EXIT_SUCCESS 0
82
Thomas Wood784344e2014-07-23 16:29:39 +010083/**
84 * IGT_EXIT_INVALID
85 *
86 * Exit status indicating an invalid option or subtest was specified
87 */
88#define IGT_EXIT_INVALID 79
89
Thomas Woodb47032e2015-04-09 09:24:12 +010090/**
91 * IGT_EXIT_FAILURE
92 *
93 * Exit status indicating a test failure
94 */
95#define IGT_EXIT_FAILURE 99
Thomas Wood17eb0622014-05-13 15:22:52 +010096
Daniel Vetterd63fe152014-03-12 01:29:52 +010097bool __igt_fixture(void);
98void __igt_fixture_complete(void);
99void __igt_fixture_end(void) __attribute__((noreturn));
100/**
101 * igt_fixture:
102 *
103 * Annotate global test fixture code
104 *
105 * Testcase with subtests often need to set up a bunch of global state as the
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100106 * common test fixture. To avoid such code interfering with the subtest
107 * enumeration (e.g. when enumerating on systems without an intel gpu) such
Daniel Vetterd63fe152014-03-12 01:29:52 +0100108 * blocks should be annotated with igt_fixture.
109 */
Tomeu Vizoso3450cba2016-04-15 14:00:04 +0200110#define igt_fixture for (volatile int igt_tokencat(__tmpint,__LINE__) = 0; \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100111 igt_tokencat(__tmpint,__LINE__) < 1 && \
112 __igt_fixture() && \
Chris Wilson578795f2015-05-08 14:35:37 +0100113 (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100114 igt_tokencat(__tmpint,__LINE__) ++, \
115 __igt_fixture_complete())
116
117/* subtest infrastructure */
118jmp_buf igt_subtest_jmpbuf;
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100119typedef int (*igt_opt_handler_t)(int opt, int opt_index, void *data);
Daniel Vetter55e64982014-03-12 02:34:40 +0100120#ifndef __GTK_DOC_IGNORE__ /* gtkdoc wants to document this forward decl */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100121struct option;
Daniel Vetter55e64982014-03-12 02:34:40 +0100122#endif
Thomas Wood8fb19782015-02-18 16:19:59 +0000123int igt_subtest_init_parse_opts(int *argc, char **argv,
Daniel Vetterd63fe152014-03-12 01:29:52 +0100124 const char *extra_short_opts,
Ville Syrjälä793aff12015-12-18 19:25:45 +0200125 const struct option *extra_long_opts,
Daniel Vetterd63fe152014-03-12 01:29:52 +0100126 const char *help_str,
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100127 igt_opt_handler_t extra_opt_handler,
128 void *handler_data);
Daniel Vetterd63fe152014-03-12 01:29:52 +0100129
Thomas Wood55cc1322015-02-17 15:10:13 +0000130
131/**
132 * igt_subtest_init:
133 * @argc: argc from the test's main()
134 * @argv: argv from the test's main()
135 *
136 * This initializes the for tests with subtests without the need for additional
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100137 * command line options. It is just a simplified version of
Thomas Wood55cc1322015-02-17 15:10:13 +0000138 * igt_subtest_init_parse_opts().
139 *
140 * If there's not a reason to the contrary it's less error prone to just use an
141 * #igt_main block instead of stitching the test's main() function together
142 * manually.
143 */
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100144#define igt_subtest_init(argc, argv) \
145 igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL);
Thomas Wood55cc1322015-02-17 15:10:13 +0000146
Daniel Vetterd63fe152014-03-12 01:29:52 +0100147bool __igt_run_subtest(const char *subtest_name);
Daniel Vetter55e64982014-03-12 02:34:40 +0100148#define __igt_tokencat2(x, y) x ## y
149
150/**
151 * igt_tokencat:
152 * @x: first variable
153 * @y: second variable
154 *
155 * C preprocessor helper to concatenate two variables while properly expanding
156 * them.
157 */
158#define igt_tokencat(x, y) __igt_tokencat2(x, y)
159
Daniel Vetterd63fe152014-03-12 01:29:52 +0100160/**
161 * igt_subtest:
Daniel Vetter55e64982014-03-12 02:34:40 +0100162 * @name: name of the subtest
Daniel Vetterd63fe152014-03-12 01:29:52 +0100163 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100164 * This is a magic control flow block which denotes a subtest code block. Within
Thomas Wood519f3772014-09-26 14:24:52 +0100165 * that code block igt_skip|success will only bail out of the subtest. The _f
Daniel Vetter55e64982014-03-12 02:34:40 +0100166 * variant accepts a printf format string, which is useful for constructing
167 * combinatorial tests.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100168 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100169 * This is a simpler version of igt_subtest_f()
Daniel Vetterd63fe152014-03-12 01:29:52 +0100170 */
Daniel Vetter55e64982014-03-12 02:34:40 +0100171#define igt_subtest(name) for (; __igt_run_subtest((name)) && \
Chris Wilson578795f2015-05-08 14:35:37 +0100172 (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \
Daniel Vetter55e64982014-03-12 02:34:40 +0100173 igt_success())
Daniel Vetterd63fe152014-03-12 01:29:52 +0100174#define __igt_subtest_f(tmp, format...) \
175 for (char tmp [256]; \
176 snprintf( tmp , sizeof( tmp ), \
177 format), \
178 __igt_run_subtest( tmp ) && \
Chris Wilson578795f2015-05-08 14:35:37 +0100179 (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100180 igt_success())
181
182/**
183 * igt_subtest_f:
Daniel Vetter55e64982014-03-12 02:34:40 +0100184 * @...: format string and optional arguments
Daniel Vetterd63fe152014-03-12 01:29:52 +0100185 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100186 * This is a magic control flow block which denotes a subtest code block. Within
Thomas Wood519f3772014-09-26 14:24:52 +0100187 * that code block igt_skip|success will only bail out of the subtest. The _f
Daniel Vetter55e64982014-03-12 02:34:40 +0100188 * variant accepts a printf format string, which is useful for constructing
189 * combinatorial tests.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100190 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100191 * Like igt_subtest(), but also accepts a printf format string instead of a
192 * static string.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100193 */
194#define igt_subtest_f(f...) \
195 __igt_subtest_f(igt_tokencat(__tmpchar, __LINE__), f)
Daniel Vetter55e64982014-03-12 02:34:40 +0100196
Daniel Vetterd63fe152014-03-12 01:29:52 +0100197const char *igt_subtest_name(void);
Daniel Vetter55e64982014-03-12 02:34:40 +0100198bool igt_only_list_subtests(void);
199
Daniel Vetter72d04b82016-03-18 21:46:54 +0100200void __igt_subtest_group_save(int *);
201void __igt_subtest_group_restore(int);
202/**
203 * igt_subtest_group:
204 *
205 * Group a set of subtests together with their common setup code
206 *
207 * Testcase with subtests often need to set up a bunch of shared state as the
208 * common test fixture. But if there are multiple with different requirements
209 * the commont setup code can't be extracted, since a test condition failure in
210 * e.g. igt_require() would result in all subsequent tests skipping. Even those
211 * from a different group.
212 *
213 * This macro allows to group together a set of #igt_fixture and #igt_subtest
214 * clauses. If any common setup in a fixture fails, only the subtests in this
215 * group will fail or skip. Subtest groups can be arbitrarily nested.
216 */
217#define igt_subtest_group for (int igt_tokencat(__tmpint,__LINE__) = 0, \
218 igt_tokencat(__save,__LINE__) = 0; \
219 igt_tokencat(__tmpint,__LINE__) < 1 && \
220 (__igt_subtest_group_save(& igt_tokencat(__save,__LINE__) ), true); \
221 igt_tokencat(__tmpint,__LINE__) ++, \
222 __igt_subtest_group_restore(igt_tokencat(__save,__LINE__) ))
223
Daniel Vetter55e64982014-03-12 02:34:40 +0100224/**
225 * igt_main:
226 *
227 * This is a magic control flow block used instead of a main() function for
228 * tests with subtests. Open-coding the main() function is only recommended if
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100229 * the test needs to parse additional command line arguments of its own.
Daniel Vetter55e64982014-03-12 02:34:40 +0100230 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100231#define igt_main \
232 static void igt_tokencat(__real_main, __LINE__)(void); \
233 int main(int argc, char **argv) { \
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100234 igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
235 NULL, NULL); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100236 igt_tokencat(__real_main, __LINE__)(); \
237 igt_exit(); \
238 } \
239 static void igt_tokencat(__real_main, __LINE__)(void) \
240
Thomas Wood55cc1322015-02-17 15:10:13 +0000241
Chris Wilson77633492015-03-26 08:11:43 +0000242const char *igt_test_name(void);
Thomas Wood8fb19782015-02-18 16:19:59 +0000243void igt_simple_init_parse_opts(int *argc, char **argv,
Thomas Woode2cef002014-07-18 16:49:07 +0100244 const char *extra_short_opts,
Ville Syrjälä793aff12015-12-18 19:25:45 +0200245 const struct option *extra_long_opts,
Thomas Woode2cef002014-07-18 16:49:07 +0100246 const char *help_str,
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100247 igt_opt_handler_t extra_opt_handler,
248 void *handler_data);
Daniel Vetter55e64982014-03-12 02:34:40 +0100249
250/**
Thomas Wood55cc1322015-02-17 15:10:13 +0000251 * igt_simple_init:
252 * @argc: argc from the test's main()
253 * @argv: argv from the test's main()
254 *
255 * This initializes a simple test without any support for subtests.
256 *
257 * If there's not a reason to the contrary it's less error prone to just use an
258 * #igt_simple_main block instead of stitching the test's main() function together
259 * manually.
260 */
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100261#define igt_simple_init(argc, argv) \
262 igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL);
Thomas Wood55cc1322015-02-17 15:10:13 +0000263
264/**
Daniel Vetter55e64982014-03-12 02:34:40 +0100265 * igt_simple_main:
266 *
267 * This is a magic control flow block used instead of a main() function for
268 * simple tests. Open-coding the main() function is only recommended if
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100269 * the test needs to parse additional command line arguments of its own.
Daniel Vetter55e64982014-03-12 02:34:40 +0100270 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100271#define igt_simple_main \
272 static void igt_tokencat(__real_main, __LINE__)(void); \
273 int main(int argc, char **argv) { \
Damien Lespiaufd6846c2015-05-14 14:19:01 +0100274 igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
275 NULL, NULL); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100276 igt_tokencat(__real_main, __LINE__)(); \
Tim Goref33fa712014-09-29 13:34:30 +0100277 igt_exit(); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100278 } \
279 static void igt_tokencat(__real_main, __LINE__)(void) \
280
Daniel Vetterd63fe152014-03-12 01:29:52 +0100281__attribute__((format(printf, 1, 2)))
282void igt_skip(const char *f, ...) __attribute__((noreturn));
283__attribute__((format(printf, 5, 6)))
284void __igt_skip_check(const char *file, const int line,
285 const char *func, const char *check,
286 const char *format, ...) __attribute__((noreturn));
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100287#define igt_skip_check(E, F...) \
Thomas Woode6228502014-10-29 12:26:09 +0000288 __igt_skip_check(__FILE__, __LINE__, __func__, E, F)
Daniel Vetterd63fe152014-03-12 01:29:52 +0100289void igt_success(void);
290
Daniel Vetterd63fe152014-03-12 01:29:52 +0100291void igt_fail(int exitcode) __attribute__((noreturn));
Thomas Woodb47032e2015-04-09 09:24:12 +0100292__attribute__((format(printf, 6, 7)))
293void __igt_fail_assert(const char *domain, const char *file,
Daniel Vetterd63fe152014-03-12 01:29:52 +0100294 const int line, const char *func, const char *assertion,
295 const char *format, ...)
296 __attribute__((noreturn));
Daniel Vetterd63fe152014-03-12 01:29:52 +0100297void igt_exit(void) __attribute__((noreturn));
Daniel Vetter55e64982014-03-12 02:34:40 +0100298
Daniel Vetterd63fe152014-03-12 01:29:52 +0100299/**
Chris Wilsond86d6eb2016-06-23 21:07:36 +0100300 * igt_ignore_warn:
301 * @expr: condition to ignore
302 *
303 *
304 * Stops the compiler warning about an unused return value.
305 */
306static inline void igt_ignore_warn(bool value)
307{
308}
309
310/**
Daniel Vetterd63fe152014-03-12 01:29:52 +0100311 * igt_assert:
Daniel Vetter55e64982014-03-12 02:34:40 +0100312 * @expr: condition to test
Daniel Vetterd63fe152014-03-12 01:29:52 +0100313 *
Daniel Vetter2347e6d2014-06-13 18:01:33 +0200314 * Fails (sub-)test if the condition is not met.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100315 *
316 * Should be used everywhere where a test checks results.
317 */
318#define igt_assert(expr) \
319 do { if (!(expr)) \
Thomas Woodb47032e2015-04-09 09:24:12 +0100320 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , NULL); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100321 } while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100322
323/**
324 * igt_assert_f:
325 * @expr: condition to test
326 * @...: format string and optional arguments
327 *
Daniel Vetter2347e6d2014-06-13 18:01:33 +0200328 * Fails (sub-)test if the condition is not met.
Daniel Vetter55e64982014-03-12 02:34:40 +0100329 *
330 * Should be used everywhere where a test checks results.
331 *
332 * In addition to the plain igt_assert() helper this allows to print additional
333 * information to help debugging test failures.
334 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100335#define igt_assert_f(expr, f...) \
336 do { if (!(expr)) \
Thomas Woodb47032e2015-04-09 09:24:12 +0100337 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , f); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100338 } while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100339
Daniel Vetterd63fe152014-03-12 01:29:52 +0100340/**
Daniel Vetter2347e6d2014-06-13 18:01:33 +0200341 * igt_fail_on:
342 * @expr: condition to test
343 *
344 * Fails (sub-)test if the condition is met.
345 *
346 * Should be used everywhere where a test checks results.
347 */
348#define igt_fail_on(expr) igt_assert(!(expr))
349
350/**
Thomas Woodae3a9462014-11-25 11:59:37 +0000351 * igt_fail_on_f:
Daniel Vetter2347e6d2014-06-13 18:01:33 +0200352 * @expr: condition to test
353 * @...: format string and optional arguments
354 *
355 * Fails (sub-)test if the condition is met.
356 *
357 * Should be used everywhere where a test checks results.
358 *
359 * In addition to the plain igt_assert() helper this allows to print additional
360 * information to help debugging test failures.
361 */
362#define igt_fail_on_f(expr, f...) igt_assert_f(!(expr), f)
363
364/**
Daniel Vetter55e64982014-03-12 02:34:40 +0100365 * igt_assert_cmpint:
366 * @n1: first value
367 * @cmp: compare operator
Thomas Woodd4e3b6a2014-10-08 17:06:58 +0100368 * @ncmp: negated version of @cmp
Daniel Vetter55e64982014-03-12 02:34:40 +0100369 * @n2: second value
Daniel Vetterd63fe152014-03-12 01:29:52 +0100370 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100371 * Fails (sub-)test if the condition is not met
372 *
373 * Should be used everywhere where a test compares two integer values.
374 *
375 * Like igt_assert(), but displays the values being compared on failure instead
376 * of simply printing the stringified expression.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100377 */
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100378#define igt_assert_cmpint(n1, cmp, ncmp, n2) \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100379 do { \
380 int __n1 = (n1), __n2 = (n2); \
381 if (__n1 cmp __n2) ; else \
Thomas Woodb47032e2015-04-09 09:24:12 +0100382 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100383 #n1 " " #cmp " " #n2, \
Chris Wilsond28b9d22014-08-29 17:30:40 +0100384 "error: %d " #ncmp " %d\n", __n1, __n2); \
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100385 } while (0)
386
Damien Lespiau23888522015-06-27 15:26:13 +0100387/**
388 * igt_assert_cmpuint:
389 * @n1: first value
390 * @cmp: compare operator
391 * @ncmp: negated version of @cmp
392 * @n2: second value
393 *
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100394 * Like igt_assert_cmpint(), but for unsigned ints.
Damien Lespiau23888522015-06-27 15:26:13 +0100395 */
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100396#define igt_assert_cmpuint(n1, cmp, ncmp, n2) \
397 do { \
398 uint32_t __n1 = (n1), __n2 = (n2); \
399 if (__n1 cmp __n2) ; else \
Thomas Woodb47032e2015-04-09 09:24:12 +0100400 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100401 #n1 " " #cmp " " #n2, \
Chris Wilsond28b9d22014-08-29 17:30:40 +0100402 "error: %#x " #ncmp " %#x\n", __n1, __n2); \
Daniel Vetterd63fe152014-03-12 01:29:52 +0100403 } while (0)
404
405/**
Chris Wilson40ebf952015-07-28 18:59:47 +0100406 * igt_assert_cmpu64:
407 * @n1: first value
408 * @cmp: compare operator
409 * @ncmp: negated version of @cmp
410 * @n2: second value
411 *
412 * Like igt_assert_cmpuint(), but for larger ints.
413 */
414#define igt_assert_cmpu64(n1, cmp, ncmp, n2) \
415 do { \
416 uint64_t __n1 = (n1), __n2 = (n2); \
417 if (__n1 cmp __n2) ; else \
418 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
419 #n1 " " #cmp " " #n2, \
420 "error: %#llx " #ncmp " %#llx\n", (long long)__n1, (long long)__n2); \
421 } while (0)
422
423/**
Damien Lespiau0a38e972015-06-27 15:26:50 +0100424 * igt_assert_cmpdouble:
425 * @n1: first value
426 * @cmp: compare operator
427 * @ncmp: negated version of @cmp
428 * @n2: second value
429 *
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100430 * Like igt_assert_cmpint(), but for doubles.
Damien Lespiau0a38e972015-06-27 15:26:50 +0100431 */
432#define igt_assert_cmpdouble(n1, cmp, ncmp, n2) \
433 do { \
434 double __n1 = (n1), __n2 = (n2); \
435 if (__n1 cmp __n2) ; else \
436 __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
437 #n1 " " #cmp " " #n2, \
438 "error: %#lf " #ncmp " %#lf\n", __n1, __n2); \
439 } while (0)
440
441/**
Daniel Vetter3448b5a2014-06-13 10:46:55 +0200442 * igt_assert_eq:
443 * @n1: first integer
444 * @n2: second integer
445 *
446 * Fails (sub-)test if the two integers are not equal. Beware that for now this
447 * only works on integers.
448 *
449 * Like igt_assert(), but displays the values being compared on failure instead
450 * of simply printing the stringified expression.
451 */
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100452#define igt_assert_eq(n1, n2) igt_assert_cmpint(n1, ==, !=, n2)
Damien Lespiauf660d0a2015-06-27 15:16:22 +0100453
454/**
455 * igt_assert_eq_u32:
456 * @n1: first integer
457 * @n2: second integer
458 *
459 * Like igt_assert_eq(), but for uint32_t.
460 */
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100461#define igt_assert_eq_u32(n1, n2) igt_assert_cmpuint(n1, ==, !=, n2)
462
463/**
Chris Wilson40ebf952015-07-28 18:59:47 +0100464 * igt_assert_eq_u64:
465 * @n1: first integer
466 * @n2: second integer
467 *
468 * Like igt_assert_eq_u32(), but for uint64_t.
469 */
470#define igt_assert_eq_u64(n1, n2) igt_assert_cmpu64(n1, ==, !=, n2)
471
472/**
Damien Lespiau0a38e972015-06-27 15:26:50 +0100473 * igt_assert_eq_double:
474 * @n1: first double
475 * @n2: second double
476 *
477 * Like igt_assert_eq(), but for doubles.
478 */
479#define igt_assert_eq_double(n1, n2) igt_assert_cmpdouble(n1, ==, !=, n2)
480
481/**
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100482 * igt_assert_neq:
483 * @n1: first integer
484 * @n2: second integer
485 *
486 * Fails (sub-)test if the two integers are equal. Beware that for now this
487 * only works on integers.
488 *
489 * Like igt_assert(), but displays the values being compared on failure instead
490 * of simply printing the stringified expression.
491 */
492#define igt_assert_neq(n1, n2) igt_assert_cmpint(n1, !=, ==, n2)
493
494/**
Daniel Stone715a17b2015-10-01 13:00:12 +0100495 * igt_assert_neq_u32:
496 * @n1: first integer
497 * @n2: second integer
498 *
499 * Like igt_assert_neq(), but for uint32_t.
500 */
501#define igt_assert_neq_u32(n1, n2) igt_assert_cmpuint(n1, !=, ==, n2)
502
503/**
504 * igt_assert_neq_u64:
505 * @n1: first integer
506 * @n2: second integer
507 *
508 * Like igt_assert_neq_u32(), but for uint64_t.
509 */
510#define igt_assert_neq_u64(n1, n2) igt_assert_cmpu64(n1, !=, ==, n2)
511
512/**
513 * igt_assert_neq_double:
514 * @n1: first double
515 * @n2: second double
516 *
517 * Like igt_assert_neq(), but for doubles.
518 */
519#define igt_assert_neq_double(n1, n2) igt_assert_cmpdouble(n1, !=, ==, n2)
520
521/**
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100522 * igt_assert_lte:
523 * @n1: first integer
524 * @n2: second integer
525 *
Daniel Vetterc9112dc2015-10-02 12:56:16 +0200526 * Fails (sub-)test if the second integer is strictly smaller than the first.
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100527 * Beware that for now this only works on integers.
528 *
529 * Like igt_assert(), but displays the values being compared on failure instead
530 * of simply printing the stringified expression.
531 */
532#define igt_assert_lte(n1, n2) igt_assert_cmpint(n1, <=, >, n2)
Daniel Vetter3448b5a2014-06-13 10:46:55 +0200533
534/**
Daniel Vetter305fb1d2015-02-27 16:24:25 +0100535 * igt_assert_lt:
536 * @n1: first integer
537 * @n2: second integer
538 *
Daniel Vetterc9112dc2015-10-02 12:56:16 +0200539 * Fails (sub-)test if the second integer is smaller than or equal to the first.
Daniel Vetter305fb1d2015-02-27 16:24:25 +0100540 * Beware that for now this only works on integers.
541 *
542 * Like igt_assert(), but displays the values being compared on failure instead
543 * of simply printing the stringified expression.
544 */
545#define igt_assert_lt(n1, n2) igt_assert_cmpint(n1, <, >=, n2)
546
547/**
Daniel Stone6ead44d2015-10-01 13:26:36 +0100548 * igt_assert_fd:
549 * @fd: file descriptor
550 *
551 * Fails (sub-) test if the given file descriptor is invalid.
552 *
553 * Like igt_assert(), but displays the values being compared on failure instead
554 * of simply printing the stringified expression.
555 */
556#define igt_assert_fd(fd) \
557 igt_assert_f(fd >= 0, "file descriptor " #fd " failed\n");
558
559/**
Daniel Vetterd63fe152014-03-12 01:29:52 +0100560 * igt_require:
Daniel Vetter55e64982014-03-12 02:34:40 +0100561 * @expr: condition to test
Daniel Vetterd63fe152014-03-12 01:29:52 +0100562 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100563 * Skip a (sub-)test if a condition is not met.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100564 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100565 * Should be used everywhere where a test checks results to decide about
566 * skipping. This is useful to streamline the skip logic since it allows for a more flat
567 * code control flow, similar to igt_assert()
Daniel Vetterd63fe152014-03-12 01:29:52 +0100568 */
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100569#define igt_require(expr) do { \
570 if (!(expr)) igt_skip_check(#expr , NULL); \
Paulo Zanonia1fce742015-08-13 13:49:42 -0300571 else igt_debug("Test requirement passed: %s\n", #expr); \
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100572} while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100573
574/**
575 * igt_skip_on:
576 * @expr: condition to test
577 *
578 * Skip a (sub-)test if a condition is met.
579 *
580 * Should be used everywhere where a test checks results to decide about
581 * skipping. This is useful to streamline the skip logic since it allows for a more flat
582 * code control flow, similar to igt_assert()
583 */
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100584#define igt_skip_on(expr) do { \
585 if ((expr)) igt_skip_check("!(" #expr ")" , NULL); \
Paulo Zanonia1fce742015-08-13 13:49:42 -0300586 else igt_debug("Test requirement passed: !(%s)\n", #expr); \
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100587} while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100588
589/**
590 * igt_require_f:
591 * @expr: condition to test
592 * @...: format string and optional arguments
593 *
594 * Skip a (sub-)test if a condition is not met.
595 *
596 * Should be used everywhere where a test checks results to decide about
597 * skipping. This is useful to streamline the skip logic since it allows for a more flat
598 * code control flow, similar to igt_assert()
599 *
600 * In addition to the plain igt_require() helper this allows to print additional
601 * information to help debugging test failures.
602 */
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100603#define igt_require_f(expr, f...) do { \
604 if (!(expr)) igt_skip_check(#expr , f); \
Paulo Zanonia1fce742015-08-13 13:49:42 -0300605 else igt_debug("Test requirement passed: %s\n", #expr); \
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100606} while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100607
608/**
609 * igt_skip_on_f:
610 * @expr: condition to test
611 * @...: format string and optional arguments
612 *
613 * Skip a (sub-)test if a condition is met.
614 *
615 * Should be used everywhere where a test checks results to decide about
616 * skipping. This is useful to streamline the skip logic since it allows for a more flat
617 * code control flow, similar to igt_assert()
618 *
619 * In addition to the plain igt_skip_on() helper this allows to print additional
620 * information to help debugging test failures.
621 */
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100622#define igt_skip_on_f(expr, f...) do { \
Thomas Woodd950f372014-10-29 12:03:22 +0000623 if ((expr)) igt_skip_check("!("#expr")", f); \
Paulo Zanonia1fce742015-08-13 13:49:42 -0300624 else igt_debug("Test requirement passed: !(%s)\n", #expr); \
Chris Wilsonfb9c9e32014-09-06 12:21:25 +0100625} while (0)
Daniel Vetterd63fe152014-03-12 01:29:52 +0100626
627/* fork support code */
628bool __igt_fork(void);
Daniel Vetter55e64982014-03-12 02:34:40 +0100629
Daniel Vetterd63fe152014-03-12 01:29:52 +0100630/**
631 * igt_fork:
632 * @child: name of the int variable with the child number
633 * @num_children: number of children to fork
634 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100635 * This is a magic control flow block which spawns parallel test threads with
636 * fork().
Daniel Vetterd63fe152014-03-12 01:29:52 +0100637 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100638 * The test children execute in parallel to the main test thread. Joining all
639 * test threads should be done with igt_waitchildren to ensure that the exit
640 * codes of all children are properly reflected in the test status.
641 *
642 * Note that igt_skip() will not be forwarded, feature tests need to be done
643 * before spawning threads with igt_fork().
Daniel Vetterd63fe152014-03-12 01:29:52 +0100644 */
645#define igt_fork(child, num_children) \
646 for (int child = 0; child < (num_children); child++) \
647 for (; __igt_fork(); exit(0))
648void igt_waitchildren(void);
Chris Wilson75487632016-02-26 22:11:10 +0000649void igt_waitchildren_timeout(int seconds, const char *reason);
Daniel Vetterd63fe152014-03-12 01:29:52 +0100650
Daniel Vetter55e64982014-03-12 02:34:40 +0100651/**
Thomas Wood26f40812015-02-20 11:31:01 +0000652 * igt_helper_process:
Daniel Vetter55e64982014-03-12 02:34:40 +0100653 * @running: indicates whether the process is currently running
Chris Wilson4f7d4dc2014-07-21 07:54:29 +0100654 * @use_SIGKILL: whether the helper should be terminated with SIGKILL or SIGTERM
Daniel Vetter55e64982014-03-12 02:34:40 +0100655 * @pid: pid of the helper if @running is true
656 * @id: internal id
657 *
658 * Tracking structure for helper processes. Users of the i-g-t library should
659 * only set @use_SIGKILL directly.
660 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100661struct igt_helper_process {
662 bool running;
663 bool use_SIGKILL;
664 pid_t pid;
665 int id;
666};
667bool __igt_fork_helper(struct igt_helper_process *proc);
Daniel Vetter55e64982014-03-12 02:34:40 +0100668
669/**
670 * igt_fork_helper:
671 * @proc: #igt_helper_process structure
672 *
673 * This is a magic control flow block which denotes an asynchronous helper
674 * process block. The difference compared to igt_fork() is that failures from
675 * the child process will not be forwarded, making this construct more suitable
676 * for background processes. Common use cases are regular interference of the
677 * main test thread through e.g. sending signals or evicting objects through
678 * debugfs. Through the explicit #igt_helper_process they can also be controlled
679 * in a more fine-grained way than test children spawned through igt_fork().
680 *
681 * For tests with subtest helper process can be started outside of a
682 * #igt_subtest block.
683 *
684 * Calling igt_wait_helper() joins a helper process and igt_stop_helper()
685 * forcefully terminates it.
686 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100687#define igt_fork_helper(proc) \
688 for (; __igt_fork_helper(proc); exit(0))
Chris Wilson74594552014-07-24 11:44:45 +0100689int igt_wait_helper(struct igt_helper_process *proc);
Daniel Vetter55e64982014-03-12 02:34:40 +0100690void igt_stop_helper(struct igt_helper_process *proc);
Daniel Vetterd63fe152014-03-12 01:29:52 +0100691
692/* exit handler code */
Daniel Vetter55e64982014-03-12 02:34:40 +0100693
694/**
695 * igt_exit_handler_t:
696 * @sig: Signal number which caused the exit or 0.
697 *
698 * Exit handler type used by igt_install_exit_handler(). Note that exit handlers
699 * can potentially be run from signal handling contexts, the @sig parameter can
700 * be used to figure this out and act accordingly.
701 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100702typedef void (*igt_exit_handler_t)(int sig);
703
704/* reliable atexit helpers, also work when killed by a signal (if possible) */
705void igt_install_exit_handler(igt_exit_handler_t fn);
Daniel Vetterd63fe152014-03-12 01:29:52 +0100706
707/* helpers to automatically reduce test runtime in simulation */
708bool igt_run_in_simulation(void);
Daniel Vetterd63fe152014-03-12 01:29:52 +0100709/**
Daniel Vetter55e64982014-03-12 02:34:40 +0100710 * SLOW_QUICK:
711 * @slow: value in simulation mode
712 * @quick: value in normal mode
Daniel Vetterd63fe152014-03-12 01:29:52 +0100713 *
Daniel Vetter55e64982014-03-12 02:34:40 +0100714 * Simple macro to select between two values (e.g. number of test rounds or test
715 * buffer size) depending upon whether i-g-t is run in simulation mode or not.
Daniel Vetterd63fe152014-03-12 01:29:52 +0100716 */
Daniel Vetter55e64982014-03-12 02:34:40 +0100717#define SLOW_QUICK(slow,quick) (igt_run_in_simulation() ? (quick) : (slow))
718
Daniel Vetterd63fe152014-03-12 01:29:52 +0100719void igt_skip_on_simulation(void);
720
Thomas Wooda6c40c72015-03-12 14:59:20 +0000721extern const char *igt_interactive_debug;
Rodrigo Vivi3d65ff72015-01-12 10:21:58 -0800722
Daniel Vetterd63fe152014-03-12 01:29:52 +0100723/* structured logging */
724enum igt_log_level {
725 IGT_LOG_DEBUG,
726 IGT_LOG_INFO,
727 IGT_LOG_WARN,
Thomas Wooddf11a0f2014-12-16 15:18:20 +0000728 IGT_LOG_CRITICAL,
Daniel Vetterd63fe152014-03-12 01:29:52 +0100729 IGT_LOG_NONE,
730};
Thomas Wood8161a212014-12-02 10:54:54 +0000731__attribute__((format(printf, 3, 4)))
732void igt_log(const char *domain, enum igt_log_level level, const char *format, ...);
733__attribute__((format(printf, 3, 0)))
734void igt_vlog(const char *domain, enum igt_log_level level, const char *format, va_list args);
Daniel Vetter55e64982014-03-12 02:34:40 +0100735
736/**
737 * igt_debug:
738 * @...: format string and optional arguments
739 *
Daniel Vetter3f505982014-07-14 23:02:18 +0200740 * Wrapper for igt_log() for message at the IGT_LOG_DEBUG level.
Daniel Vetter55e64982014-03-12 02:34:40 +0100741 */
Thomas Wood8161a212014-12-02 10:54:54 +0000742#define igt_debug(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_DEBUG, f)
Daniel Vetter55e64982014-03-12 02:34:40 +0100743
744/**
745 * igt_info:
746 * @...: format string and optional arguments
747 *
Daniel Vetter3f505982014-07-14 23:02:18 +0200748 * Wrapper for igt_log() for message at the IGT_LOG_INFO level.
Daniel Vetter55e64982014-03-12 02:34:40 +0100749 */
Thomas Wood8161a212014-12-02 10:54:54 +0000750#define igt_info(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_INFO, f)
Daniel Vetter55e64982014-03-12 02:34:40 +0100751
752/**
753 * igt_warn:
754 * @...: format string and optional arguments
755 *
Daniel Vetter3f505982014-07-14 23:02:18 +0200756 * Wrapper for igt_log() for message at the IGT_LOG_WARN level.
Daniel Vetter55e64982014-03-12 02:34:40 +0100757 */
Thomas Wood8161a212014-12-02 10:54:54 +0000758#define igt_warn(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_WARN, f)
Thomas Wooddf11a0f2014-12-16 15:18:20 +0000759
760/**
761 * igt_critical:
762 * @...: format string and optional arguments
763 *
764 * Wrapper for igt_log() for message at the IGT_LOG_CRITICAL level.
765 */
766#define igt_critical(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_CRITICAL, f)
767
Daniel Vetterd63fe152014-03-12 01:29:52 +0100768extern enum igt_log_level igt_log_level;
769
Daniel Vetter55e64982014-03-12 02:34:40 +0100770/**
771 * igt_warn_on:
772 * @condition: condition to test
773 *
774 * Print a IGT_LOG_WARN level message if a condition is not met.
775 *
776 * Should be used everywhere where a test checks results to decide about
777 * printing warnings. This is useful to streamline the test logic since it
778 * allows for a more flat code control flow, similar to igt_assert()
779 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100780#define igt_warn_on(condition) do {\
781 if (condition) \
782 igt_warn("Warning on condition %s in fucntion %s, file %s:%i\n", \
783 #condition, __func__, __FILE__, __LINE__); \
784 } while (0)
Daniel Vetter55e64982014-03-12 02:34:40 +0100785
786/**
787 * igt_warn_on_f:
788 * @condition: condition to test
789 * @...: format string and optional arguments
790 *
791 * Skip a (sub-)test if a condition is not met.
792 *
793 * Print a IGT_LOG_WARN level message if a condition is not met.
794 *
795 * Should be used everywhere where a test checks results to decide about
796 * printing warnings. This is useful to streamline the test logic since it
797 * allows for a more flat code control flow, similar to igt_assert()
798 *
799 * In addition to the plain igt_warn_on_f() helper this allows to print
800 * additional information (again as warnings) to help debugging test failures.
801 */
Daniel Vetterd63fe152014-03-12 01:29:52 +0100802#define igt_warn_on_f(condition, f...) do {\
803 if (condition) {\
804 igt_warn("Warning on condition %s in fucntion %s, file %s:%i\n", \
805 #condition, __func__, __FILE__, __LINE__); \
806 igt_warn(f); \
807 } \
808 } while (0)
809
810
Daniel Vetter370c9892015-08-07 19:01:23 +0200811void igt_set_timeout(unsigned int seconds,
812 const char *op);
Thomas Woodd8e53132014-05-12 10:19:52 +0100813
Chris Wilsondcb39b52016-03-18 09:04:07 +0000814/**
815 * igt_nsec_elapsed:
816 * @start: measure from this point in time
817 *
818 * Reports the difference in the monotonic clock from the start time
819 * in nanoseconds. On the first invocation, start should be zeroed and will
820 * be set by the call.
821 *
822 * Typical use would be:
823 *
824 * igt_subtest("test") {
825 * struct timespec start = {};
826 * while (igt_nsec_elapsed(&start) < test_timeout_ns)
827 * do_test();
828 * }
829 *
830 * A handy approximation is to use nsec >> 30 to convert to seconds,
831 * nsec >> 20 to convert to milliseconds - the error is about 8%, acceptable
832 * for test run times.
833 */
834uint64_t igt_nsec_elapsed(struct timespec *start);
835
836/**
837 * igt_seconds_elapsed:
838 * @start: measure from this point in time
839 *
840 * A wrapper around igt_nsec_elapsed that reports the approximate (8% error)
841 * number of seconds since the start point.
842 */
843static inline uint32_t igt_seconds_elapsed(struct timespec *start)
844{
845 return igt_nsec_elapsed(start) >> 30;
846}
847
Daniel Vetter5b0a8432015-08-07 19:12:07 +0200848void igt_reset_timeout(void);
849
Joonas Lahtinena95033f2015-03-31 15:53:17 +0300850FILE *__igt_fopen_data(const char* igt_srcdir, const char* igt_datadir,
851 const char* filename);
852/**
853 * igt_fopen_data:
854 * @filename: filename to open.
855 *
Matt Roper303b3802016-05-26 16:02:05 -0700856 * Open a datafile for test, first try from installation directory,
857 * then from build directory, and finally from current directory.
Joonas Lahtinena95033f2015-03-31 15:53:17 +0300858 */
859#define igt_fopen_data(filename) \
860 __igt_fopen_data(IGT_SRCDIR, IGT_DATADIR, filename)
861
Daniel Vetterd63fe152014-03-12 01:29:52 +0100862#endif /* IGT_CORE_H */