1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2015-2020 Cyril Hrubis <chrubis@suse.cz> 4 * Copyright (c) Linux Test Project, 2021-2022 5 */ 6 7 #ifndef TST_TEST_MACROS_H__ 8 #define TST_TEST_MACROS_H__ 9 10 #define TEST(SCALL) \ 11 do { \ 12 errno = 0; \ 13 TST_RET = SCALL; \ 14 TST_ERR = errno; \ 15 } while (0) 16 17 #define TEST_VOID(SCALL) \ 18 do { \ 19 errno = 0; \ 20 SCALL; \ 21 TST_ERR = errno; \ 22 } while (0) 23 24 extern long TST_RET; 25 extern int TST_ERR; 26 extern int TST_PASS; 27 28 extern void *TST_RET_PTR; 29 30 #define TESTPTR(SCALL) \ 31 do { \ 32 errno = 0; \ 33 TST_RET_PTR = (void*)SCALL; \ 34 TST_ERR = errno; \ 35 } while (0) 36 37 38 #define TST_2_(_1, _2, ...) _2 39 40 #define TST_FMT_(FMT, _1, ...) FMT, ##__VA_ARGS__ 41 42 #define TST_MSG_(RES, FMT, SCALL, ...) \ 43 tst_res_(__FILE__, __LINE__, RES, \ 44 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__)) 45 46 #define TST_MSGP_(RES, FMT, PAR, SCALL, ...) \ 47 tst_res_(__FILE__, __LINE__, RES, \ 48 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__), PAR) 49 50 #define TST_MSGP2_(RES, FMT, PAR, PAR2, SCALL, ...) \ 51 tst_res_(__FILE__, __LINE__, RES, \ 52 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__), PAR, PAR2) 53 54 #define TST_EXP_POSITIVE_(SCALL, SSCALL, ...) \ 55 do { \ 56 TEST(SCALL); \ 57 \ 58 TST_PASS = 0; \ 59 \ 60 if (TST_RET == -1) { \ 61 TST_MSG_(TFAIL | TTERRNO, " failed", \ 62 SSCALL, ##__VA_ARGS__); \ 63 break; \ 64 } \ 65 \ 66 if (TST_RET < 0) { \ 67 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 68 TST_RET, SSCALL, ##__VA_ARGS__); \ 69 break; \ 70 } \ 71 \ 72 TST_PASS = 1; \ 73 \ 74 } while (0) 75 76 #define TST_EXP_POSITIVE(SCALL, ...) \ 77 do { \ 78 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 79 \ 80 if (TST_PASS) { \ 81 TST_MSGP_(TPASS, " returned %ld", \ 82 TST_RET, #SCALL, ##__VA_ARGS__); \ 83 } \ 84 } while (0) 85 86 #define TST_EXP_FD_SILENT(SCALL, ...) TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__) 87 88 #define TST_EXP_FD(SCALL, ...) \ 89 do { \ 90 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 91 \ 92 if (TST_PASS) \ 93 TST_MSGP_(TPASS, " returned fd %ld", TST_RET, \ 94 #SCALL, ##__VA_ARGS__); \ 95 } while (0) 96 97 #define TST_EXP_FD_OR_FAIL(SCALL, ERRNO, ...) \ 98 do { \ 99 if (ERRNO) \ 100 TST_EXP_FAIL(SCALL, ERRNO, ##__VA_ARGS__); \ 101 else \ 102 TST_EXP_FD(SCALL, ##__VA_ARGS__); \ 103 \ 104 } while (0) 105 106 #define TST_EXP_PID_SILENT(SCALL, ...) TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__) 107 108 #define TST_EXP_PID(SCALL, ...) \ 109 do { \ 110 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 111 \ 112 if (TST_PASS) \ 113 TST_MSGP_(TPASS, " returned pid %ld", TST_RET, \ 114 #SCALL, ##__VA_ARGS__); \ 115 } while (0) 116 117 #define TST_EXP_VAL_SILENT_(SCALL, VAL, SSCALL, ...) \ 118 do { \ 119 TEST(SCALL); \ 120 \ 121 TST_PASS = 0; \ 122 \ 123 if (TST_RET != VAL) { \ 124 TST_MSGP2_(TFAIL | TTERRNO, " retval %ld != %ld", \ 125 TST_RET, (long)VAL, SSCALL, ##__VA_ARGS__); \ 126 break; \ 127 } \ 128 \ 129 TST_PASS = 1; \ 130 \ 131 } while (0) 132 133 #define TST_EXP_VAL_SILENT(SCALL, VAL, ...) TST_EXP_VAL_SILENT_(SCALL, VAL, #SCALL, ##__VA_ARGS__) 134 135 #define TST_EXP_VAL(SCALL, VAL, ...) \ 136 do { \ 137 TST_EXP_VAL_SILENT_(SCALL, VAL, #SCALL, ##__VA_ARGS__); \ 138 \ 139 if (TST_PASS) \ 140 TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__); \ 141 \ 142 } while(0) 143 144 #define TST_EXP_PASS_SILENT_(SCALL, SSCALL, ...) \ 145 do { \ 146 TEST(SCALL); \ 147 \ 148 TST_PASS = 0; \ 149 \ 150 if (TST_RET == -1) { \ 151 TST_MSG_(TFAIL | TTERRNO, " failed", \ 152 SSCALL, ##__VA_ARGS__); \ 153 break; \ 154 } \ 155 \ 156 if (TST_RET != 0) { \ 157 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 158 TST_RET, SSCALL, ##__VA_ARGS__); \ 159 break; \ 160 } \ 161 \ 162 TST_PASS = 1; \ 163 \ 164 } while (0) 165 166 #define TST_EXP_PASS_SILENT(SCALL, ...) TST_EXP_PASS_SILENT_(SCALL, #SCALL, ##__VA_ARGS__) 167 168 #define TST_EXP_PASS(SCALL, ...) \ 169 do { \ 170 TST_EXP_PASS_SILENT_(SCALL, #SCALL, ##__VA_ARGS__); \ 171 \ 172 if (TST_PASS) \ 173 TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__); \ 174 } while (0) \ 175 176 #define TST_EXP_FAIL_SILENT_(PASS_COND, SCALL, SSCALL, ERRNO, ...) \ 177 do { \ 178 TEST(SCALL); \ 179 \ 180 TST_PASS = 0; \ 181 \ 182 if (PASS_COND) { \ 183 TST_MSG_(TFAIL, " succeeded", SSCALL, ##__VA_ARGS__); \ 184 break; \ 185 } \ 186 \ 187 if (TST_RET != -1) { \ 188 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 189 TST_RET, SSCALL, ##__VA_ARGS__); \ 190 break; \ 191 } \ 192 \ 193 if (TST_ERR == (ERRNO)) { \ 194 TST_PASS = 1; \ 195 } else { \ 196 TST_MSGP_(TFAIL | TTERRNO, " expected %s", \ 197 tst_strerrno(ERRNO), \ 198 SSCALL, ##__VA_ARGS__); \ 199 } \ 200 } while (0) 201 202 #define TST_EXP_FAIL(SCALL, ERRNO, ...) \ 203 do { \ 204 TST_EXP_FAIL_SILENT_(TST_RET == 0, SCALL, #SCALL, \ 205 ERRNO, ##__VA_ARGS__); \ 206 if (TST_PASS) \ 207 TST_MSG_(TPASS | TTERRNO, " ", #SCALL, ##__VA_ARGS__); \ 208 } while (0) 209 210 #define TST_EXP_FAIL2(SCALL, ERRNO, ...) \ 211 do { \ 212 TST_EXP_FAIL_SILENT_(TST_RET >= 0, SCALL, #SCALL, \ 213 ERRNO, ##__VA_ARGS__); \ 214 if (TST_PASS) \ 215 TST_MSG_(TPASS | TTERRNO, " ", #SCALL, ##__VA_ARGS__); \ 216 } while (0) 217 218 #define TST_EXP_FAIL_SILENT(SCALL, ERRNO, ...) \ 219 TST_EXP_FAIL_SILENT_(TST_RET == 0, SCALL, #SCALL, ERRNO, ##__VA_ARGS__) 220 221 #define TST_EXP_FAIL2_SILENT(SCALL, ERRNO, ...) \ 222 TST_EXP_FAIL_SILENT_(TST_RET >= 0, SCALL, #SCALL, ERRNO, ##__VA_ARGS__) 223 224 #define TST_EXP_EXPR(EXPR, FMT, ...) \ 225 tst_res_(__FILE__, __LINE__, (EXPR) ? TPASS : TFAIL, "Expect: " FMT, ##__VA_ARGS__); 226 227 #define TST_EXP_EQ_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS) do {\ 228 TYPE tst_tmp_a__ = VAL_A; \ 229 TYPE tst_tmp_b__ = VAL_B; \ 230 if (tst_tmp_a__ == tst_tmp_b__) { \ 231 tst_res_(__FILE__, __LINE__, TPASS, \ 232 SVAL_A " == " SVAL_B " (" PFS ")", tst_tmp_a__); \ 233 } else { \ 234 tst_res_(__FILE__, __LINE__, TFAIL, \ 235 SVAL_A " (" PFS ") != " SVAL_B " (" PFS ")", \ 236 tst_tmp_a__, tst_tmp_b__); \ 237 } \ 238 } while (0) 239 240 #define TST_EXP_EQ_LI(VAL_A, VAL_B) \ 241 TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, long long, "%lli") 242 243 #define TST_EXP_EQ_LU(VAL_A, VAL_B) \ 244 TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu") 245 246 #define TST_EXP_EQ_SZ(VAL_A, VAL_B) \ 247 TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu") 248 249 #define TST_EXP_EQ_SSZ(VAL_A, VAL_B) \ 250 TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi") 251 252 #endif /* TST_TEST_MACROS_H__ */ 253