xref: /aosp_15_r20/external/igt-gpu-tools/lib/tests/igt_assert.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2015 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  */
24 
25 #include <errno.h>
26 #include <float.h>
27 #include <limits.h>
28 #include <math.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 
36 #include "igt_core.h"
37 #include "drmtest.h"
38 
39 #include "igt_tests_common.h"
40 
41 char test[] = "test";
42 char *argv_run[] = { test };
43 void (*test_to_run)(void) = NULL;
44 
45 /*
46  * A really tedious way of making sure we execute every negative test, and that
47  * they all really fail.
48  */
49 #define CHECK_NEG(x) { \
50 	igt_subtest_f("XFAIL_simple_%d", __LINE__) { \
51 		(*exec_before)++; \
52 		x; \
53 		raise(SIGBUS); \
54 	} \
55 	exec_total++; \
56 }
57 
do_fork(void)58 static int do_fork(void)
59 {
60 	int pid, status;
61 	int argc;
62 
63 	switch (pid = fork()) {
64 	case -1:
65 		internal_assert(0);
66 	case 0:
67 		argc = ARRAY_SIZE(argv_run);
68 		igt_simple_init(argc, argv_run);
69 		test_to_run();
70 		igt_exit();
71 	default:
72 		while (waitpid(pid, &status, 0) == -1 &&
73 		       errno == EINTR)
74 			;
75 
76 		return status;
77 	}
78 }
79 
test_cmpint_negative(void)80 static void test_cmpint_negative(void)
81 {
82 	int *exec_before = calloc(1, sizeof(int));
83 	int exec_total = 0;
84 
85 	CHECK_NEG(igt_assert_eq(INT_MIN, INT_MAX));
86 
87 	CHECK_NEG(igt_assert_eq_u32(0xfffffffeUL, 0xffffffffUL));
88 
89 	CHECK_NEG(igt_assert_eq_u64(0xfffeffffffffffffULL, 0xffffffffffffffffULL));
90 	CHECK_NEG(igt_assert_eq_u64(0xfffffffeffffffffULL, 0xffffffffffffffffULL));
91 	CHECK_NEG(igt_assert_eq_u64(0xfffffffffffeffffULL, 0xffffffffffffffffULL));
92 
93 	CHECK_NEG(igt_assert_eq_double(0.0, DBL_MAX));
94 	CHECK_NEG(igt_assert_eq_double(DBL_MAX, nexttoward(DBL_MAX, 0.0)));
95 
96 	if (*exec_before != exec_total)
97 		raise(SIGSEGV);
98 }
99 
test_cmpint(void)100 static void test_cmpint(void)
101 {
102 	igt_assert_eq(0, 0);
103 	igt_assert_eq(INT_MAX, INT_MAX);
104 	igt_assert_eq(INT_MAX, INT_MAX);
105 	igt_assert_neq(INT_MIN, INT_MAX);
106 
107 	igt_assert_eq_u32(0, 0);
108 	igt_assert_eq_u32(0xffffffffUL, 0xffffffffUL);
109 	igt_assert_neq_u32(0xfffffffeUL, 0xffffffffUL);
110 
111 	igt_assert_eq_u64(0, 0);
112 	igt_assert_eq_u64(0xffffffffffffffffULL, 0xffffffffffffffffULL);
113 	igt_assert_neq_u64(0xfffffffffffffffeULL, 0xffffffffffffffffULL);
114 
115 	igt_assert_eq_double(0.0, 0.0);
116 	igt_assert_eq_double(DBL_MAX, DBL_MAX);
117 	igt_assert_neq_double(0.0, DBL_MAX);
118 }
119 
test_fd_negative(void)120 static void test_fd_negative(void)
121 {
122 	int *exec_before = calloc(1, sizeof(int));
123 	int exec_total = 0;
124 
125 	CHECK_NEG(igt_assert_fd(-1));
126 	CHECK_NEG(igt_assert_fd(INT_MIN));
127 
128 	if (*exec_before != exec_total)
129 		raise(SIGSEGV);
130 }
131 
test_fd(void)132 static void test_fd(void)
133 {
134 	igt_assert_fd(0);
135 	igt_assert_fd(1);
136 	igt_assert_fd(INT_MAX);
137 }
138 
139 igt_main
140 {
141 	int ret;
142 
143 	igt_subtest("igt_cmpint")
144 		test_cmpint();
145 
146 	/*
147 	 * The awkward subtest dance here is because we really want to use
148 	 * subtests in our negative tests, to ensure we actually execute all
149 	 * the subtests. But we can't begin a subtest within a subtest, and
150 	 * we inherit the state from the parent, so ...
151 	 */
152 	test_to_run = test_cmpint_negative;
153 	ret = do_fork();
154 	igt_subtest("igt_cmpint_negative")
155 		internal_assert_wexited(ret, IGT_EXIT_FAILURE);
156 
157 	igt_subtest("igt_assert_fd")
158 		test_fd();
159 
160 	test_to_run = test_fd_negative;
161 	ret = do_fork();
162 	igt_subtest("igt_assert_fd_negative")
163 		internal_assert_wexited(ret, IGT_EXIT_FAILURE);
164 }
165