Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 1 | /* |
| 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 Vetter | 924115b | 2014-03-22 20:18:51 +0100 | [diff] [blame] | 33 | #include <setjmp.h> |
Daniel Vetter | 6cfcd71 | 2014-03-22 20:07:35 +0100 | [diff] [blame] | 34 | #include <stdbool.h> |
Chris Wilson | dcb39b5 | 2016-03-18 09:04:07 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
Daniel Vetter | 254f19b | 2014-03-22 21:29:01 +0100 | [diff] [blame] | 36 | #include <stdlib.h> |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 37 | #include <stdio.h> |
Daniel Vetter | 924115b | 2014-03-22 20:18:51 +0100 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <sys/types.h> |
Daniel Vetter | 254f19b | 2014-03-22 21:29:01 +0100 | [diff] [blame] | 40 | #include <stdarg.h> |
Thomas Wood | 80d2c9b | 2014-07-21 15:57:16 +0100 | [diff] [blame] | 41 | #include <getopt.h> |
Petri Latvala | 474fae6 | 2016-11-15 13:52:52 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
Daniel Vetter | 6cfcd71 | 2014-03-22 20:07:35 +0100 | [diff] [blame] | 43 | |
Thomas Wood | 0167619 | 2014-12-17 11:37:31 +0000 | [diff] [blame] | 44 | #ifndef IGT_LOG_DOMAIN |
| 45 | #define IGT_LOG_DOMAIN (NULL) |
| 46 | #endif |
| 47 | |
| 48 | |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 49 | extern const char* __igt_test_description __attribute__((weak)); |
Thomas Wood | 790f1f8 | 2015-11-09 17:17:24 +0000 | [diff] [blame] | 50 | extern bool __igt_plain_output; |
Daniel Vetter | a3a3e0f | 2017-09-05 14:36:08 +0200 | [diff] [blame] | 51 | extern char *igt_frame_dump_path; |
Thomas Wood | f8b3c70 | 2014-10-10 16:56:20 +0100 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * IGT_TEST_DESCRIPTION: |
| 55 | * @str: description string |
| 56 | * |
| 57 | * Defines a description for a test. This is used as the output for the |
| 58 | * "--help-description" option and is also included in the generated |
| 59 | * documentation. |
| 60 | */ |
| 61 | #define IGT_TEST_DESCRIPTION(str) const char* __igt_test_description = str |
| 62 | |
Thomas Wood | 17eb062 | 2014-05-13 15:22:52 +0100 | [diff] [blame] | 63 | /** |
| 64 | * IGT_EXIT_TIMEOUT: |
| 65 | * |
| 66 | * Exit status indicating a timeout occurred. |
| 67 | */ |
| 68 | #define IGT_EXIT_TIMEOUT 78 |
| 69 | |
| 70 | /** |
| 71 | * IGT_EXIT_SKIP: |
| 72 | * |
| 73 | * Exit status indicating the test was skipped. |
| 74 | */ |
| 75 | #define IGT_EXIT_SKIP 77 |
| 76 | |
| 77 | /** |
| 78 | * IGT_EXIT_SUCCESS |
| 79 | * |
| 80 | * Exit status indicating the test executed successfully. |
| 81 | */ |
| 82 | #define IGT_EXIT_SUCCESS 0 |
| 83 | |
Thomas Wood | 784344e | 2014-07-23 16:29:39 +0100 | [diff] [blame] | 84 | /** |
| 85 | * IGT_EXIT_INVALID |
| 86 | * |
| 87 | * Exit status indicating an invalid option or subtest was specified |
| 88 | */ |
| 89 | #define IGT_EXIT_INVALID 79 |
| 90 | |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 91 | /** |
| 92 | * IGT_EXIT_FAILURE |
| 93 | * |
| 94 | * Exit status indicating a test failure |
| 95 | */ |
| 96 | #define IGT_EXIT_FAILURE 99 |
Thomas Wood | 17eb062 | 2014-05-13 15:22:52 +0100 | [diff] [blame] | 97 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 98 | bool __igt_fixture(void); |
| 99 | void __igt_fixture_complete(void); |
| 100 | void __igt_fixture_end(void) __attribute__((noreturn)); |
| 101 | /** |
| 102 | * igt_fixture: |
| 103 | * |
| 104 | * Annotate global test fixture code |
| 105 | * |
| 106 | * Testcase with subtests often need to set up a bunch of global state as the |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 107 | * common test fixture. To avoid such code interfering with the subtest |
| 108 | * enumeration (e.g. when enumerating on systems without an intel gpu) such |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 109 | * blocks should be annotated with igt_fixture. |
| 110 | */ |
Tomeu Vizoso | 3450cba | 2016-04-15 14:00:04 +0200 | [diff] [blame] | 111 | #define igt_fixture for (volatile int igt_tokencat(__tmpint,__LINE__) = 0; \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 112 | igt_tokencat(__tmpint,__LINE__) < 1 && \ |
| 113 | __igt_fixture() && \ |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 114 | (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 115 | igt_tokencat(__tmpint,__LINE__) ++, \ |
| 116 | __igt_fixture_complete()) |
| 117 | |
| 118 | /* subtest infrastructure */ |
| 119 | jmp_buf igt_subtest_jmpbuf; |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 120 | typedef int (*igt_opt_handler_t)(int opt, int opt_index, void *data); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 121 | #ifndef __GTK_DOC_IGNORE__ /* gtkdoc wants to document this forward decl */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 122 | struct option; |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 123 | #endif |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 124 | int igt_subtest_init_parse_opts(int *argc, char **argv, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 125 | const char *extra_short_opts, |
Ville Syrjälä | 793aff1 | 2015-12-18 19:25:45 +0200 | [diff] [blame] | 126 | const struct option *extra_long_opts, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 127 | const char *help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 128 | igt_opt_handler_t extra_opt_handler, |
| 129 | void *handler_data); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 130 | |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * igt_subtest_init: |
| 134 | * @argc: argc from the test's main() |
| 135 | * @argv: argv from the test's main() |
| 136 | * |
| 137 | * This initializes the for tests with subtests without the need for additional |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 138 | * command line options. It is just a simplified version of |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 139 | * igt_subtest_init_parse_opts(). |
| 140 | * |
| 141 | * If there's not a reason to the contrary it's less error prone to just use an |
| 142 | * #igt_main block instead of stitching the test's main() function together |
| 143 | * manually. |
| 144 | */ |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 145 | #define igt_subtest_init(argc, argv) \ |
| 146 | igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL); |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 147 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 148 | bool __igt_run_subtest(const char *subtest_name); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 149 | #define __igt_tokencat2(x, y) x ## y |
| 150 | |
| 151 | /** |
| 152 | * igt_tokencat: |
| 153 | * @x: first variable |
| 154 | * @y: second variable |
| 155 | * |
| 156 | * C preprocessor helper to concatenate two variables while properly expanding |
| 157 | * them. |
| 158 | */ |
| 159 | #define igt_tokencat(x, y) __igt_tokencat2(x, y) |
| 160 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 161 | /** |
| 162 | * igt_subtest: |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 163 | * @name: name of the subtest |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 164 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 165 | * This is a magic control flow block which denotes a subtest code block. Within |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 166 | * that code block igt_skip|success will only bail out of the subtest. The _f |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 167 | * variant accepts a printf format string, which is useful for constructing |
| 168 | * combinatorial tests. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 169 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 170 | * This is a simpler version of igt_subtest_f() |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 171 | */ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 172 | #define igt_subtest(name) for (; __igt_run_subtest((name)) && \ |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 173 | (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 174 | igt_success()) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 175 | #define __igt_subtest_f(tmp, format...) \ |
| 176 | for (char tmp [256]; \ |
| 177 | snprintf( tmp , sizeof( tmp ), \ |
| 178 | format), \ |
| 179 | __igt_run_subtest( tmp ) && \ |
Chris Wilson | 578795f | 2015-05-08 14:35:37 +0100 | [diff] [blame] | 180 | (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 181 | igt_success()) |
| 182 | |
| 183 | /** |
| 184 | * igt_subtest_f: |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 185 | * @...: format string and optional arguments |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 186 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 187 | * This is a magic control flow block which denotes a subtest code block. Within |
Thomas Wood | 519f377 | 2014-09-26 14:24:52 +0100 | [diff] [blame] | 188 | * that code block igt_skip|success will only bail out of the subtest. The _f |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 189 | * variant accepts a printf format string, which is useful for constructing |
| 190 | * combinatorial tests. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 191 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 192 | * Like igt_subtest(), but also accepts a printf format string instead of a |
| 193 | * static string. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 194 | */ |
| 195 | #define igt_subtest_f(f...) \ |
| 196 | __igt_subtest_f(igt_tokencat(__tmpchar, __LINE__), f) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 197 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 198 | const char *igt_subtest_name(void); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 199 | bool igt_only_list_subtests(void); |
| 200 | |
Daniel Vetter | 72d04b8 | 2016-03-18 21:46:54 +0100 | [diff] [blame] | 201 | void __igt_subtest_group_save(int *); |
| 202 | void __igt_subtest_group_restore(int); |
| 203 | /** |
| 204 | * igt_subtest_group: |
| 205 | * |
| 206 | * Group a set of subtests together with their common setup code |
| 207 | * |
| 208 | * Testcase with subtests often need to set up a bunch of shared state as the |
| 209 | * common test fixture. But if there are multiple with different requirements |
| 210 | * the commont setup code can't be extracted, since a test condition failure in |
| 211 | * e.g. igt_require() would result in all subsequent tests skipping. Even those |
| 212 | * from a different group. |
| 213 | * |
| 214 | * This macro allows to group together a set of #igt_fixture and #igt_subtest |
| 215 | * clauses. If any common setup in a fixture fails, only the subtests in this |
| 216 | * group will fail or skip. Subtest groups can be arbitrarily nested. |
| 217 | */ |
| 218 | #define igt_subtest_group for (int igt_tokencat(__tmpint,__LINE__) = 0, \ |
| 219 | igt_tokencat(__save,__LINE__) = 0; \ |
| 220 | igt_tokencat(__tmpint,__LINE__) < 1 && \ |
| 221 | (__igt_subtest_group_save(& igt_tokencat(__save,__LINE__) ), true); \ |
| 222 | igt_tokencat(__tmpint,__LINE__) ++, \ |
| 223 | __igt_subtest_group_restore(igt_tokencat(__save,__LINE__) )) |
| 224 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 225 | /** |
| 226 | * igt_main: |
| 227 | * |
| 228 | * This is a magic control flow block used instead of a main() function for |
| 229 | * tests with subtests. Open-coding the main() function is only recommended if |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 230 | * the test needs to parse additional command line arguments of its own. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 231 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 232 | #define igt_main \ |
| 233 | static void igt_tokencat(__real_main, __LINE__)(void); \ |
| 234 | int main(int argc, char **argv) { \ |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 235 | igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, \ |
| 236 | NULL, NULL); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 237 | igt_tokencat(__real_main, __LINE__)(); \ |
| 238 | igt_exit(); \ |
| 239 | } \ |
| 240 | static void igt_tokencat(__real_main, __LINE__)(void) \ |
| 241 | |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 242 | |
Chris Wilson | 7763349 | 2015-03-26 08:11:43 +0000 | [diff] [blame] | 243 | const char *igt_test_name(void); |
Thomas Wood | 8fb1978 | 2015-02-18 16:19:59 +0000 | [diff] [blame] | 244 | void igt_simple_init_parse_opts(int *argc, char **argv, |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 245 | const char *extra_short_opts, |
Ville Syrjälä | 793aff1 | 2015-12-18 19:25:45 +0200 | [diff] [blame] | 246 | const struct option *extra_long_opts, |
Thomas Wood | e2cef00 | 2014-07-18 16:49:07 +0100 | [diff] [blame] | 247 | const char *help_str, |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 248 | igt_opt_handler_t extra_opt_handler, |
| 249 | void *handler_data); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 250 | |
| 251 | /** |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 252 | * igt_simple_init: |
| 253 | * @argc: argc from the test's main() |
| 254 | * @argv: argv from the test's main() |
| 255 | * |
| 256 | * This initializes a simple test without any support for subtests. |
| 257 | * |
| 258 | * If there's not a reason to the contrary it's less error prone to just use an |
| 259 | * #igt_simple_main block instead of stitching the test's main() function together |
| 260 | * manually. |
| 261 | */ |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 262 | #define igt_simple_init(argc, argv) \ |
| 263 | igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL); |
Thomas Wood | 55cc132 | 2015-02-17 15:10:13 +0000 | [diff] [blame] | 264 | |
| 265 | /** |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 266 | * igt_simple_main: |
| 267 | * |
| 268 | * This is a magic control flow block used instead of a main() function for |
| 269 | * simple tests. Open-coding the main() function is only recommended if |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 270 | * the test needs to parse additional command line arguments of its own. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 271 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 272 | #define igt_simple_main \ |
| 273 | static void igt_tokencat(__real_main, __LINE__)(void); \ |
| 274 | int main(int argc, char **argv) { \ |
Damien Lespiau | fd6846c | 2015-05-14 14:19:01 +0100 | [diff] [blame] | 275 | igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, \ |
| 276 | NULL, NULL); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 277 | igt_tokencat(__real_main, __LINE__)(); \ |
Tim Gore | f33fa71 | 2014-09-29 13:34:30 +0100 | [diff] [blame] | 278 | igt_exit(); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 279 | } \ |
| 280 | static void igt_tokencat(__real_main, __LINE__)(void) \ |
| 281 | |
Lyude | 31811f4 | 2016-12-21 14:00:54 -0500 | [diff] [blame] | 282 | /** |
| 283 | * igt_constructor: |
| 284 | * |
| 285 | * Convenience macro to run the provided code block when igt first starts, |
| 286 | * before any tests have been run. This should be used for parts of the igt |
| 287 | * library that require initialization of objects with global context. |
| 288 | * |
| 289 | * This code block will be executed exactly once. |
| 290 | */ |
| 291 | #define igt_constructor \ |
| 292 | __attribute__((constructor)) \ |
| 293 | static void igt_tokencat(__igt_constructor_l, __LINE__)(void) |
| 294 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 295 | __attribute__((format(printf, 1, 2))) |
| 296 | void igt_skip(const char *f, ...) __attribute__((noreturn)); |
| 297 | __attribute__((format(printf, 5, 6))) |
| 298 | void __igt_skip_check(const char *file, const int line, |
| 299 | const char *func, const char *check, |
| 300 | const char *format, ...) __attribute__((noreturn)); |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 301 | #define igt_skip_check(E, F...) \ |
Thomas Wood | e622850 | 2014-10-29 12:26:09 +0000 | [diff] [blame] | 302 | __igt_skip_check(__FILE__, __LINE__, __func__, E, F) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 303 | void igt_success(void); |
| 304 | |
Daniel Vetter | 2459b80 | 2017-08-14 11:32:04 +0200 | [diff] [blame] | 305 | bool igt_can_fail(void); |
| 306 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 307 | void igt_fail(int exitcode) __attribute__((noreturn)); |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 308 | __attribute__((format(printf, 6, 7))) |
| 309 | void __igt_fail_assert(const char *domain, const char *file, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 310 | const int line, const char *func, const char *assertion, |
| 311 | const char *format, ...) |
| 312 | __attribute__((noreturn)); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 313 | void igt_exit(void) __attribute__((noreturn)); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 314 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 315 | /** |
Chris Wilson | d86d6eb | 2016-06-23 21:07:36 +0100 | [diff] [blame] | 316 | * igt_ignore_warn: |
| 317 | * @expr: condition to ignore |
| 318 | * |
| 319 | * |
| 320 | * Stops the compiler warning about an unused return value. |
| 321 | */ |
| 322 | static inline void igt_ignore_warn(bool value) |
| 323 | { |
| 324 | } |
| 325 | |
| 326 | /** |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 327 | * igt_assert: |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 328 | * @expr: condition to test |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 329 | * |
Daniel Vetter | 2347e6d | 2014-06-13 18:01:33 +0200 | [diff] [blame] | 330 | * Fails (sub-)test if the condition is not met. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 331 | * |
| 332 | * Should be used everywhere where a test checks results. |
| 333 | */ |
| 334 | #define igt_assert(expr) \ |
| 335 | do { if (!(expr)) \ |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 336 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , NULL); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 337 | } while (0) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * igt_assert_f: |
| 341 | * @expr: condition to test |
| 342 | * @...: format string and optional arguments |
| 343 | * |
Daniel Vetter | 2347e6d | 2014-06-13 18:01:33 +0200 | [diff] [blame] | 344 | * Fails (sub-)test if the condition is not met. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 345 | * |
| 346 | * Should be used everywhere where a test checks results. |
| 347 | * |
| 348 | * In addition to the plain igt_assert() helper this allows to print additional |
| 349 | * information to help debugging test failures. |
| 350 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 351 | #define igt_assert_f(expr, f...) \ |
| 352 | do { if (!(expr)) \ |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 353 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , f); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 354 | } while (0) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 355 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 356 | /** |
Daniel Vetter | 2347e6d | 2014-06-13 18:01:33 +0200 | [diff] [blame] | 357 | * igt_fail_on: |
| 358 | * @expr: condition to test |
| 359 | * |
| 360 | * Fails (sub-)test if the condition is met. |
| 361 | * |
| 362 | * Should be used everywhere where a test checks results. |
| 363 | */ |
| 364 | #define igt_fail_on(expr) igt_assert(!(expr)) |
| 365 | |
| 366 | /** |
Thomas Wood | ae3a946 | 2014-11-25 11:59:37 +0000 | [diff] [blame] | 367 | * igt_fail_on_f: |
Daniel Vetter | 2347e6d | 2014-06-13 18:01:33 +0200 | [diff] [blame] | 368 | * @expr: condition to test |
| 369 | * @...: format string and optional arguments |
| 370 | * |
| 371 | * Fails (sub-)test if the condition is met. |
| 372 | * |
| 373 | * Should be used everywhere where a test checks results. |
| 374 | * |
| 375 | * In addition to the plain igt_assert() helper this allows to print additional |
| 376 | * information to help debugging test failures. |
| 377 | */ |
| 378 | #define igt_fail_on_f(expr, f...) igt_assert_f(!(expr), f) |
| 379 | |
| 380 | /** |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 381 | * igt_assert_cmpint: |
| 382 | * @n1: first value |
| 383 | * @cmp: compare operator |
Thomas Wood | d4e3b6a | 2014-10-08 17:06:58 +0100 | [diff] [blame] | 384 | * @ncmp: negated version of @cmp |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 385 | * @n2: second value |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 386 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 387 | * Fails (sub-)test if the condition is not met |
| 388 | * |
| 389 | * Should be used everywhere where a test compares two integer values. |
| 390 | * |
| 391 | * Like igt_assert(), but displays the values being compared on failure instead |
| 392 | * of simply printing the stringified expression. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 393 | */ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 394 | #define igt_assert_cmpint(n1, cmp, ncmp, n2) \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 395 | do { \ |
| 396 | int __n1 = (n1), __n2 = (n2); \ |
| 397 | if (__n1 cmp __n2) ; else \ |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 398 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 399 | #n1 " " #cmp " " #n2, \ |
Chris Wilson | d28b9d2 | 2014-08-29 17:30:40 +0100 | [diff] [blame] | 400 | "error: %d " #ncmp " %d\n", __n1, __n2); \ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 401 | } while (0) |
| 402 | |
Damien Lespiau | 2388852 | 2015-06-27 15:26:13 +0100 | [diff] [blame] | 403 | /** |
| 404 | * igt_assert_cmpuint: |
| 405 | * @n1: first value |
| 406 | * @cmp: compare operator |
| 407 | * @ncmp: negated version of @cmp |
| 408 | * @n2: second value |
| 409 | * |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 410 | * Like igt_assert_cmpint(), but for unsigned ints. |
Damien Lespiau | 2388852 | 2015-06-27 15:26:13 +0100 | [diff] [blame] | 411 | */ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 412 | #define igt_assert_cmpuint(n1, cmp, ncmp, n2) \ |
| 413 | do { \ |
| 414 | uint32_t __n1 = (n1), __n2 = (n2); \ |
| 415 | if (__n1 cmp __n2) ; else \ |
Thomas Wood | b47032e | 2015-04-09 09:24:12 +0100 | [diff] [blame] | 416 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 417 | #n1 " " #cmp " " #n2, \ |
Chris Wilson | d28b9d2 | 2014-08-29 17:30:40 +0100 | [diff] [blame] | 418 | "error: %#x " #ncmp " %#x\n", __n1, __n2); \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 419 | } while (0) |
| 420 | |
| 421 | /** |
Chris Wilson | 9c2be69 | 2016-10-12 14:41:46 +0100 | [diff] [blame] | 422 | * igt_assert_cmps64: |
| 423 | * @n1: first value |
| 424 | * @cmp: compare operator |
| 425 | * @ncmp: negated version of @cmp |
| 426 | * @n2: second value |
| 427 | * |
| 428 | * Like igt_assert_cmpuint(), but for larger signed ints. |
| 429 | */ |
| 430 | #define igt_assert_cmps64(n1, cmp, ncmp, n2) \ |
| 431 | do { \ |
| 432 | int64_t __n1 = (n1), __n2 = (n2); \ |
| 433 | if (__n1 cmp __n2) ; else \ |
| 434 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \ |
| 435 | #n1 " " #cmp " " #n2, \ |
| 436 | "error: %lld " #ncmp " %lld\n", (long long)__n1, (long long)__n2); \ |
| 437 | } while (0) |
| 438 | |
| 439 | /** |
Chris Wilson | 40ebf95 | 2015-07-28 18:59:47 +0100 | [diff] [blame] | 440 | * igt_assert_cmpu64: |
| 441 | * @n1: first value |
| 442 | * @cmp: compare operator |
| 443 | * @ncmp: negated version of @cmp |
| 444 | * @n2: second value |
| 445 | * |
| 446 | * Like igt_assert_cmpuint(), but for larger ints. |
| 447 | */ |
| 448 | #define igt_assert_cmpu64(n1, cmp, ncmp, n2) \ |
| 449 | do { \ |
| 450 | uint64_t __n1 = (n1), __n2 = (n2); \ |
| 451 | if (__n1 cmp __n2) ; else \ |
| 452 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \ |
| 453 | #n1 " " #cmp " " #n2, \ |
| 454 | "error: %#llx " #ncmp " %#llx\n", (long long)__n1, (long long)__n2); \ |
| 455 | } while (0) |
| 456 | |
| 457 | /** |
Damien Lespiau | 0a38e97 | 2015-06-27 15:26:50 +0100 | [diff] [blame] | 458 | * igt_assert_cmpdouble: |
| 459 | * @n1: first value |
| 460 | * @cmp: compare operator |
| 461 | * @ncmp: negated version of @cmp |
| 462 | * @n2: second value |
| 463 | * |
Thomas Wood | d01ebbd | 2015-06-29 16:47:14 +0100 | [diff] [blame] | 464 | * Like igt_assert_cmpint(), but for doubles. |
Damien Lespiau | 0a38e97 | 2015-06-27 15:26:50 +0100 | [diff] [blame] | 465 | */ |
| 466 | #define igt_assert_cmpdouble(n1, cmp, ncmp, n2) \ |
| 467 | do { \ |
| 468 | double __n1 = (n1), __n2 = (n2); \ |
| 469 | if (__n1 cmp __n2) ; else \ |
| 470 | __igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \ |
| 471 | #n1 " " #cmp " " #n2, \ |
| 472 | "error: %#lf " #ncmp " %#lf\n", __n1, __n2); \ |
| 473 | } while (0) |
| 474 | |
| 475 | /** |
Daniel Vetter | 3448b5a | 2014-06-13 10:46:55 +0200 | [diff] [blame] | 476 | * igt_assert_eq: |
| 477 | * @n1: first integer |
| 478 | * @n2: second integer |
| 479 | * |
| 480 | * Fails (sub-)test if the two integers are not equal. Beware that for now this |
| 481 | * only works on integers. |
| 482 | * |
| 483 | * Like igt_assert(), but displays the values being compared on failure instead |
| 484 | * of simply printing the stringified expression. |
| 485 | */ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 486 | #define igt_assert_eq(n1, n2) igt_assert_cmpint(n1, ==, !=, n2) |
Damien Lespiau | f660d0a | 2015-06-27 15:16:22 +0100 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * igt_assert_eq_u32: |
| 490 | * @n1: first integer |
| 491 | * @n2: second integer |
| 492 | * |
| 493 | * Like igt_assert_eq(), but for uint32_t. |
| 494 | */ |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 495 | #define igt_assert_eq_u32(n1, n2) igt_assert_cmpuint(n1, ==, !=, n2) |
| 496 | |
| 497 | /** |
Chris Wilson | 9c2be69 | 2016-10-12 14:41:46 +0100 | [diff] [blame] | 498 | * igt_assert_eq_s64: |
| 499 | * @n1: first integer |
| 500 | * @n2: second integer |
| 501 | * |
| 502 | * Like igt_assert_eq_u32(), but for int64_t. |
| 503 | */ |
| 504 | #define igt_assert_eq_s64(n1, n2) igt_assert_cmps64(n1, ==, !=, n2) |
| 505 | |
| 506 | /** |
Chris Wilson | 40ebf95 | 2015-07-28 18:59:47 +0100 | [diff] [blame] | 507 | * igt_assert_eq_u64: |
| 508 | * @n1: first integer |
| 509 | * @n2: second integer |
| 510 | * |
| 511 | * Like igt_assert_eq_u32(), but for uint64_t. |
| 512 | */ |
| 513 | #define igt_assert_eq_u64(n1, n2) igt_assert_cmpu64(n1, ==, !=, n2) |
| 514 | |
| 515 | /** |
Damien Lespiau | 0a38e97 | 2015-06-27 15:26:50 +0100 | [diff] [blame] | 516 | * igt_assert_eq_double: |
| 517 | * @n1: first double |
| 518 | * @n2: second double |
| 519 | * |
| 520 | * Like igt_assert_eq(), but for doubles. |
| 521 | */ |
| 522 | #define igt_assert_eq_double(n1, n2) igt_assert_cmpdouble(n1, ==, !=, n2) |
| 523 | |
| 524 | /** |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 525 | * igt_assert_neq: |
| 526 | * @n1: first integer |
| 527 | * @n2: second integer |
| 528 | * |
| 529 | * Fails (sub-)test if the two integers are equal. Beware that for now this |
| 530 | * only works on integers. |
| 531 | * |
| 532 | * Like igt_assert(), but displays the values being compared on failure instead |
| 533 | * of simply printing the stringified expression. |
| 534 | */ |
| 535 | #define igt_assert_neq(n1, n2) igt_assert_cmpint(n1, !=, ==, n2) |
| 536 | |
| 537 | /** |
Daniel Stone | 715a17b | 2015-10-01 13:00:12 +0100 | [diff] [blame] | 538 | * igt_assert_neq_u32: |
| 539 | * @n1: first integer |
| 540 | * @n2: second integer |
| 541 | * |
| 542 | * Like igt_assert_neq(), but for uint32_t. |
| 543 | */ |
| 544 | #define igt_assert_neq_u32(n1, n2) igt_assert_cmpuint(n1, !=, ==, n2) |
| 545 | |
| 546 | /** |
| 547 | * igt_assert_neq_u64: |
| 548 | * @n1: first integer |
| 549 | * @n2: second integer |
| 550 | * |
| 551 | * Like igt_assert_neq_u32(), but for uint64_t. |
| 552 | */ |
| 553 | #define igt_assert_neq_u64(n1, n2) igt_assert_cmpu64(n1, !=, ==, n2) |
| 554 | |
| 555 | /** |
| 556 | * igt_assert_neq_double: |
| 557 | * @n1: first double |
| 558 | * @n2: second double |
| 559 | * |
| 560 | * Like igt_assert_neq(), but for doubles. |
| 561 | */ |
| 562 | #define igt_assert_neq_double(n1, n2) igt_assert_cmpdouble(n1, !=, ==, n2) |
| 563 | |
| 564 | /** |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 565 | * igt_assert_lte: |
| 566 | * @n1: first integer |
| 567 | * @n2: second integer |
| 568 | * |
Daniel Vetter | c9112dc | 2015-10-02 12:56:16 +0200 | [diff] [blame] | 569 | * Fails (sub-)test if the second integer is strictly smaller than the first. |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 570 | * Beware that for now this only works on integers. |
| 571 | * |
| 572 | * Like igt_assert(), but displays the values being compared on failure instead |
| 573 | * of simply printing the stringified expression. |
| 574 | */ |
| 575 | #define igt_assert_lte(n1, n2) igt_assert_cmpint(n1, <=, >, n2) |
Daniel Vetter | 3448b5a | 2014-06-13 10:46:55 +0200 | [diff] [blame] | 576 | |
| 577 | /** |
Daniel Vetter | 305fb1d | 2015-02-27 16:24:25 +0100 | [diff] [blame] | 578 | * igt_assert_lt: |
| 579 | * @n1: first integer |
| 580 | * @n2: second integer |
| 581 | * |
Daniel Vetter | c9112dc | 2015-10-02 12:56:16 +0200 | [diff] [blame] | 582 | * Fails (sub-)test if the second integer is smaller than or equal to the first. |
Daniel Vetter | 305fb1d | 2015-02-27 16:24:25 +0100 | [diff] [blame] | 583 | * Beware that for now this only works on integers. |
| 584 | * |
| 585 | * Like igt_assert(), but displays the values being compared on failure instead |
| 586 | * of simply printing the stringified expression. |
| 587 | */ |
| 588 | #define igt_assert_lt(n1, n2) igt_assert_cmpint(n1, <, >=, n2) |
| 589 | |
| 590 | /** |
Daniel Stone | 6ead44d | 2015-10-01 13:26:36 +0100 | [diff] [blame] | 591 | * igt_assert_fd: |
| 592 | * @fd: file descriptor |
| 593 | * |
| 594 | * Fails (sub-) test if the given file descriptor is invalid. |
| 595 | * |
| 596 | * Like igt_assert(), but displays the values being compared on failure instead |
| 597 | * of simply printing the stringified expression. |
| 598 | */ |
| 599 | #define igt_assert_fd(fd) \ |
| 600 | igt_assert_f(fd >= 0, "file descriptor " #fd " failed\n"); |
| 601 | |
| 602 | /** |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 603 | * igt_require: |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 604 | * @expr: condition to test |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 605 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 606 | * Skip a (sub-)test if a condition is not met. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 607 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 608 | * Should be used everywhere where a test checks results to decide about |
| 609 | * skipping. This is useful to streamline the skip logic since it allows for a more flat |
| 610 | * code control flow, similar to igt_assert() |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 611 | */ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 612 | #define igt_require(expr) do { \ |
| 613 | if (!(expr)) igt_skip_check(#expr , NULL); \ |
Paulo Zanoni | a1fce74 | 2015-08-13 13:49:42 -0300 | [diff] [blame] | 614 | else igt_debug("Test requirement passed: %s\n", #expr); \ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 615 | } while (0) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 616 | |
| 617 | /** |
| 618 | * igt_skip_on: |
| 619 | * @expr: condition to test |
| 620 | * |
| 621 | * Skip a (sub-)test if a condition is met. |
| 622 | * |
| 623 | * Should be used everywhere where a test checks results to decide about |
| 624 | * skipping. This is useful to streamline the skip logic since it allows for a more flat |
| 625 | * code control flow, similar to igt_assert() |
| 626 | */ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 627 | #define igt_skip_on(expr) do { \ |
| 628 | if ((expr)) igt_skip_check("!(" #expr ")" , NULL); \ |
Paulo Zanoni | a1fce74 | 2015-08-13 13:49:42 -0300 | [diff] [blame] | 629 | else igt_debug("Test requirement passed: !(%s)\n", #expr); \ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 630 | } while (0) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * igt_require_f: |
| 634 | * @expr: condition to test |
| 635 | * @...: format string and optional arguments |
| 636 | * |
| 637 | * Skip a (sub-)test if a condition is not met. |
| 638 | * |
| 639 | * Should be used everywhere where a test checks results to decide about |
| 640 | * skipping. This is useful to streamline the skip logic since it allows for a more flat |
| 641 | * code control flow, similar to igt_assert() |
| 642 | * |
| 643 | * In addition to the plain igt_require() helper this allows to print additional |
| 644 | * information to help debugging test failures. |
| 645 | */ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 646 | #define igt_require_f(expr, f...) do { \ |
| 647 | if (!(expr)) igt_skip_check(#expr , f); \ |
Paulo Zanoni | a1fce74 | 2015-08-13 13:49:42 -0300 | [diff] [blame] | 648 | else igt_debug("Test requirement passed: %s\n", #expr); \ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 649 | } while (0) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 650 | |
| 651 | /** |
| 652 | * igt_skip_on_f: |
| 653 | * @expr: condition to test |
| 654 | * @...: format string and optional arguments |
| 655 | * |
| 656 | * Skip a (sub-)test if a condition is met. |
| 657 | * |
| 658 | * Should be used everywhere where a test checks results to decide about |
| 659 | * skipping. This is useful to streamline the skip logic since it allows for a more flat |
| 660 | * code control flow, similar to igt_assert() |
| 661 | * |
| 662 | * In addition to the plain igt_skip_on() helper this allows to print additional |
| 663 | * information to help debugging test failures. |
| 664 | */ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 665 | #define igt_skip_on_f(expr, f...) do { \ |
Thomas Wood | d950f37 | 2014-10-29 12:03:22 +0000 | [diff] [blame] | 666 | if ((expr)) igt_skip_check("!("#expr")", f); \ |
Paulo Zanoni | a1fce74 | 2015-08-13 13:49:42 -0300 | [diff] [blame] | 667 | else igt_debug("Test requirement passed: !(%s)\n", #expr); \ |
Chris Wilson | fb9c9e3 | 2014-09-06 12:21:25 +0100 | [diff] [blame] | 668 | } while (0) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 669 | |
| 670 | /* fork support code */ |
| 671 | bool __igt_fork(void); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 672 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 673 | /** |
| 674 | * igt_fork: |
| 675 | * @child: name of the int variable with the child number |
| 676 | * @num_children: number of children to fork |
| 677 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 678 | * This is a magic control flow block which spawns parallel test threads with |
| 679 | * fork(). |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 680 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 681 | * The test children execute in parallel to the main test thread. Joining all |
| 682 | * test threads should be done with igt_waitchildren to ensure that the exit |
| 683 | * codes of all children are properly reflected in the test status. |
| 684 | * |
| 685 | * Note that igt_skip() will not be forwarded, feature tests need to be done |
| 686 | * before spawning threads with igt_fork(). |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 687 | */ |
| 688 | #define igt_fork(child, num_children) \ |
| 689 | for (int child = 0; child < (num_children); child++) \ |
| 690 | for (; __igt_fork(); exit(0)) |
Tvrtko Ursulin | 054eb1a | 2017-03-30 14:32:29 +0100 | [diff] [blame] | 691 | void igt_child_done(pid_t pid); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 692 | void igt_waitchildren(void); |
Chris Wilson | 7548763 | 2016-02-26 22:11:10 +0000 | [diff] [blame] | 693 | void igt_waitchildren_timeout(int seconds, const char *reason); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 694 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 695 | /** |
Thomas Wood | 26f4081 | 2015-02-20 11:31:01 +0000 | [diff] [blame] | 696 | * igt_helper_process: |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 697 | * @running: indicates whether the process is currently running |
Chris Wilson | 4f7d4dc | 2014-07-21 07:54:29 +0100 | [diff] [blame] | 698 | * @use_SIGKILL: whether the helper should be terminated with SIGKILL or SIGTERM |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 699 | * @pid: pid of the helper if @running is true |
| 700 | * @id: internal id |
| 701 | * |
| 702 | * Tracking structure for helper processes. Users of the i-g-t library should |
| 703 | * only set @use_SIGKILL directly. |
| 704 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 705 | struct igt_helper_process { |
| 706 | bool running; |
| 707 | bool use_SIGKILL; |
| 708 | pid_t pid; |
| 709 | int id; |
| 710 | }; |
| 711 | bool __igt_fork_helper(struct igt_helper_process *proc); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * igt_fork_helper: |
| 715 | * @proc: #igt_helper_process structure |
| 716 | * |
| 717 | * This is a magic control flow block which denotes an asynchronous helper |
| 718 | * process block. The difference compared to igt_fork() is that failures from |
| 719 | * the child process will not be forwarded, making this construct more suitable |
| 720 | * for background processes. Common use cases are regular interference of the |
| 721 | * main test thread through e.g. sending signals or evicting objects through |
| 722 | * debugfs. Through the explicit #igt_helper_process they can also be controlled |
| 723 | * in a more fine-grained way than test children spawned through igt_fork(). |
| 724 | * |
| 725 | * For tests with subtest helper process can be started outside of a |
| 726 | * #igt_subtest block. |
| 727 | * |
| 728 | * Calling igt_wait_helper() joins a helper process and igt_stop_helper() |
| 729 | * forcefully terminates it. |
| 730 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 731 | #define igt_fork_helper(proc) \ |
| 732 | for (; __igt_fork_helper(proc); exit(0)) |
Chris Wilson | 7459455 | 2014-07-24 11:44:45 +0100 | [diff] [blame] | 733 | int igt_wait_helper(struct igt_helper_process *proc); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 734 | void igt_stop_helper(struct igt_helper_process *proc); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 735 | |
| 736 | /* exit handler code */ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 737 | |
| 738 | /** |
| 739 | * igt_exit_handler_t: |
| 740 | * @sig: Signal number which caused the exit or 0. |
| 741 | * |
| 742 | * Exit handler type used by igt_install_exit_handler(). Note that exit handlers |
| 743 | * can potentially be run from signal handling contexts, the @sig parameter can |
| 744 | * be used to figure this out and act accordingly. |
| 745 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 746 | typedef void (*igt_exit_handler_t)(int sig); |
| 747 | |
| 748 | /* reliable atexit helpers, also work when killed by a signal (if possible) */ |
| 749 | void igt_install_exit_handler(igt_exit_handler_t fn); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 750 | |
| 751 | /* helpers to automatically reduce test runtime in simulation */ |
| 752 | bool igt_run_in_simulation(void); |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 753 | /** |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 754 | * SLOW_QUICK: |
| 755 | * @slow: value in simulation mode |
| 756 | * @quick: value in normal mode |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 757 | * |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 758 | * Simple macro to select between two values (e.g. number of test rounds or test |
| 759 | * buffer size) depending upon whether i-g-t is run in simulation mode or not. |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 760 | */ |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 761 | #define SLOW_QUICK(slow,quick) (igt_run_in_simulation() ? (quick) : (slow)) |
| 762 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 763 | void igt_skip_on_simulation(void); |
| 764 | |
Thomas Wood | a6c40c7 | 2015-03-12 14:59:20 +0000 | [diff] [blame] | 765 | extern const char *igt_interactive_debug; |
Rodrigo Vivi | 3d65ff7 | 2015-01-12 10:21:58 -0800 | [diff] [blame] | 766 | |
Daniel Vetter | 6d63c88 | 2016-07-27 14:16:07 +0200 | [diff] [blame] | 767 | /** |
| 768 | * igt_log_level: |
| 769 | * @IGT_LOG_DEBUG: debug information, not printed by default |
| 770 | * @IGT_LOG_INFO: informational message, printed by default |
| 771 | * @IGT_LOG_WARN: non-fatal warnings which should be treated as test failures |
| 772 | * @IGT_LOG_CRITICAL: critical errors which lead to immediate termination of tests |
| 773 | * @IGT_LOG_NONE: unused |
| 774 | * |
| 775 | * Log levels used by functions like igt_log(). |
| 776 | */ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 777 | enum igt_log_level { |
| 778 | IGT_LOG_DEBUG, |
| 779 | IGT_LOG_INFO, |
| 780 | IGT_LOG_WARN, |
Thomas Wood | df11a0f | 2014-12-16 15:18:20 +0000 | [diff] [blame] | 781 | IGT_LOG_CRITICAL, |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 782 | IGT_LOG_NONE, |
| 783 | }; |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 784 | __attribute__((format(printf, 3, 4))) |
| 785 | void igt_log(const char *domain, enum igt_log_level level, const char *format, ...); |
| 786 | __attribute__((format(printf, 3, 0))) |
| 787 | void igt_vlog(const char *domain, enum igt_log_level level, const char *format, va_list args); |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 788 | |
| 789 | /** |
| 790 | * igt_debug: |
| 791 | * @...: format string and optional arguments |
| 792 | * |
Daniel Vetter | 3f50598 | 2014-07-14 23:02:18 +0200 | [diff] [blame] | 793 | * Wrapper for igt_log() for message at the IGT_LOG_DEBUG level. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 794 | */ |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 795 | #define igt_debug(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_DEBUG, f) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 796 | |
| 797 | /** |
| 798 | * igt_info: |
| 799 | * @...: format string and optional arguments |
| 800 | * |
Daniel Vetter | 3f50598 | 2014-07-14 23:02:18 +0200 | [diff] [blame] | 801 | * Wrapper for igt_log() for message at the IGT_LOG_INFO level. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 802 | */ |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 803 | #define igt_info(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_INFO, f) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 804 | |
| 805 | /** |
| 806 | * igt_warn: |
| 807 | * @...: format string and optional arguments |
| 808 | * |
Daniel Vetter | 3f50598 | 2014-07-14 23:02:18 +0200 | [diff] [blame] | 809 | * Wrapper for igt_log() for message at the IGT_LOG_WARN level. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 810 | */ |
Thomas Wood | 8161a21 | 2014-12-02 10:54:54 +0000 | [diff] [blame] | 811 | #define igt_warn(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_WARN, f) |
Thomas Wood | df11a0f | 2014-12-16 15:18:20 +0000 | [diff] [blame] | 812 | |
| 813 | /** |
| 814 | * igt_critical: |
| 815 | * @...: format string and optional arguments |
| 816 | * |
| 817 | * Wrapper for igt_log() for message at the IGT_LOG_CRITICAL level. |
| 818 | */ |
| 819 | #define igt_critical(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_CRITICAL, f) |
| 820 | |
Abdiel Janulgue | ef48fc8 | 2017-04-13 16:18:37 +0300 | [diff] [blame] | 821 | typedef bool (*igt_buffer_log_handler_t)(const char *line, void *data); |
| 822 | void igt_log_buffer_inspect(igt_buffer_log_handler_t check, void *data); |
| 823 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 824 | extern enum igt_log_level igt_log_level; |
| 825 | |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 826 | /** |
| 827 | * igt_warn_on: |
| 828 | * @condition: condition to test |
| 829 | * |
| 830 | * Print a IGT_LOG_WARN level message if a condition is not met. |
| 831 | * |
| 832 | * Should be used everywhere where a test checks results to decide about |
| 833 | * printing warnings. This is useful to streamline the test logic since it |
| 834 | * allows for a more flat code control flow, similar to igt_assert() |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 835 | * |
| 836 | * This macro also returns the value of @condition. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 837 | */ |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 838 | #define igt_warn_on(condition) ({ \ |
| 839 | typeof(condition) ret__ = (condition); \ |
| 840 | if (ret__) \ |
| 841 | igt_warn("Warning on condition %s in function %s, file %s:%i\n", \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 842 | #condition, __func__, __FILE__, __LINE__); \ |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 843 | ret__; \ |
| 844 | }) |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 845 | |
| 846 | /** |
| 847 | * igt_warn_on_f: |
| 848 | * @condition: condition to test |
| 849 | * @...: format string and optional arguments |
| 850 | * |
| 851 | * Skip a (sub-)test if a condition is not met. |
| 852 | * |
| 853 | * Print a IGT_LOG_WARN level message if a condition is not met. |
| 854 | * |
| 855 | * Should be used everywhere where a test checks results to decide about |
| 856 | * printing warnings. This is useful to streamline the test logic since it |
| 857 | * allows for a more flat code control flow, similar to igt_assert() |
| 858 | * |
| 859 | * In addition to the plain igt_warn_on_f() helper this allows to print |
| 860 | * additional information (again as warnings) to help debugging test failures. |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 861 | * |
| 862 | * It also returns the value of @condition. |
Daniel Vetter | 55e6498 | 2014-03-12 02:34:40 +0100 | [diff] [blame] | 863 | */ |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 864 | #define igt_warn_on_f(condition, f...) ({ \ |
| 865 | typeof(condition) ret__ = (condition); \ |
| 866 | if (ret__) {\ |
| 867 | igt_warn("Warning on condition %s in function %s, file %s:%i\n", \ |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 868 | #condition, __func__, __FILE__, __LINE__); \ |
| 869 | igt_warn(f); \ |
| 870 | } \ |
Lyude | bcd7ed6 | 2017-02-07 12:50:47 -0500 | [diff] [blame] | 871 | ret__; \ |
| 872 | }) |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 873 | |
Daniel Vetter | 370c989 | 2015-08-07 19:01:23 +0200 | [diff] [blame] | 874 | void igt_set_timeout(unsigned int seconds, |
| 875 | const char *op); |
Thomas Wood | d8e5313 | 2014-05-12 10:19:52 +0100 | [diff] [blame] | 876 | |
Chris Wilson | dcb39b5 | 2016-03-18 09:04:07 +0000 | [diff] [blame] | 877 | /** |
| 878 | * igt_nsec_elapsed: |
| 879 | * @start: measure from this point in time |
| 880 | * |
| 881 | * Reports the difference in the monotonic clock from the start time |
| 882 | * in nanoseconds. On the first invocation, start should be zeroed and will |
| 883 | * be set by the call. |
| 884 | * |
| 885 | * Typical use would be: |
| 886 | * |
| 887 | * igt_subtest("test") { |
| 888 | * struct timespec start = {}; |
| 889 | * while (igt_nsec_elapsed(&start) < test_timeout_ns) |
| 890 | * do_test(); |
| 891 | * } |
| 892 | * |
| 893 | * A handy approximation is to use nsec >> 30 to convert to seconds, |
| 894 | * nsec >> 20 to convert to milliseconds - the error is about 8%, acceptable |
| 895 | * for test run times. |
| 896 | */ |
| 897 | uint64_t igt_nsec_elapsed(struct timespec *start); |
| 898 | |
| 899 | /** |
| 900 | * igt_seconds_elapsed: |
| 901 | * @start: measure from this point in time |
| 902 | * |
| 903 | * A wrapper around igt_nsec_elapsed that reports the approximate (8% error) |
| 904 | * number of seconds since the start point. |
| 905 | */ |
| 906 | static inline uint32_t igt_seconds_elapsed(struct timespec *start) |
| 907 | { |
| 908 | return igt_nsec_elapsed(start) >> 30; |
| 909 | } |
| 910 | |
Daniel Vetter | 5b0a843 | 2015-08-07 19:12:07 +0200 | [diff] [blame] | 911 | void igt_reset_timeout(void); |
| 912 | |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 913 | FILE *__igt_fopen_data(const char* igt_srcdir, const char* igt_datadir, |
| 914 | const char* filename); |
| 915 | /** |
| 916 | * igt_fopen_data: |
| 917 | * @filename: filename to open. |
| 918 | * |
Matt Roper | 303b380 | 2016-05-26 16:02:05 -0700 | [diff] [blame] | 919 | * Open a datafile for test, first try from installation directory, |
| 920 | * then from build directory, and finally from current directory. |
Joonas Lahtinen | a95033f | 2015-03-31 15:53:17 +0300 | [diff] [blame] | 921 | */ |
| 922 | #define igt_fopen_data(filename) \ |
| 923 | __igt_fopen_data(IGT_SRCDIR, IGT_DATADIR, filename) |
| 924 | |
Abdiel Janulgue | 51f4f9f | 2017-04-13 13:43:20 +0300 | [diff] [blame] | 925 | int igt_system(const char *command); |
| 926 | int igt_system_quiet(const char *command); |
| 927 | #define igt_system_cmd(status, format...) \ |
| 928 | do { \ |
| 929 | char *buf = 0; \ |
| 930 | igt_assert(asprintf(&buf, format) != -1); \ |
| 931 | status = igt_system(buf); \ |
| 932 | free(buf); \ |
| 933 | } while (0) |
| 934 | |
Daniel Vetter | d63fe15 | 2014-03-12 01:29:52 +0100 | [diff] [blame] | 935 | #endif /* IGT_CORE_H */ |