1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2015-2020 Cyril Hrubis <chrubis@suse.cz> 4 */ 5 6 #ifndef TST_TEST_MACROS_H__ 7 #define TST_TEST_MACROS_H__ 8 9 #define TEST(SCALL) \ 10 do { \ 11 errno = 0; \ 12 TST_RET = SCALL; \ 13 TST_ERR = errno; \ 14 } while (0) 15 16 #define TEST_VOID(SCALL) \ 17 do { \ 18 errno = 0; \ 19 SCALL; \ 20 TST_ERR = errno; \ 21 } while (0) 22 23 extern long TST_RET; 24 extern int TST_ERR; 25 extern int TST_PASS; 26 27 extern void *TST_RET_PTR; 28 29 #define TESTPTR(SCALL) \ 30 do { \ 31 errno = 0; \ 32 TST_RET_PTR = (void*)SCALL; \ 33 TST_ERR = errno; \ 34 } while (0) 35 36 37 #define TST_2_(_1, _2, ...) _2 38 39 #define TST_FMT_(FMT, _1, ...) FMT, ##__VA_ARGS__ 40 41 #define TST_MSG_(RES, FMT, SCALL, ...) \ 42 tst_res_(__FILE__, __LINE__, RES, \ 43 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__)) 44 45 #define TST_MSGP_(RES, FMT, PAR, SCALL, ...) \ 46 tst_res_(__FILE__, __LINE__, RES, \ 47 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__), PAR) 48 49 #define TST_MSGP2_(RES, FMT, PAR, PAR2, SCALL, ...) \ 50 tst_res_(__FILE__, __LINE__, RES, \ 51 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__), PAR, PAR2) 52 53 #define TST_EXP_POSITIVE_(SCALL, SSCALL, ...) \ 54 do { \ 55 TEST(SCALL); \ 56 \ 57 TST_PASS = 0; \ 58 \ 59 if (TST_RET == -1) { \ 60 TST_MSG_(TFAIL | TTERRNO, " failed", \ 61 SSCALL, ##__VA_ARGS__); \ 62 break; \ 63 } \ 64 \ 65 if (TST_RET < 0) { \ 66 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 67 TST_RET, SSCALL, ##__VA_ARGS__); \ 68 break; \ 69 } \ 70 \ 71 TST_PASS = 1; \ 72 \ 73 } while (0) 74 75 #define TST_EXP_POSITIVE(SCALL, ...) \ 76 do { \ 77 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 78 \ 79 if (TST_PASS) { \ 80 TST_MSGP_(TPASS, " returned %ld", \ 81 TST_RET, #SCALL, ##__VA_ARGS__); \ 82 } \ 83 } while (0) 84 85 #define TST_EXP_FD_SILENT(SCALL, ...) TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__) 86 87 #define TST_EXP_FD(SCALL, ...) \ 88 do { \ 89 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 90 \ 91 if (TST_PASS) \ 92 TST_MSGP_(TPASS, " returned fd %ld", TST_RET, \ 93 #SCALL, ##__VA_ARGS__); \ 94 } while (0) 95 96 #define TST_EXP_PID_SILENT(SCALL, ...) TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__) 97 98 #define TST_EXP_PID(SCALL, ...) \ 99 do { \ 100 TST_EXP_POSITIVE_(SCALL, #SCALL, ##__VA_ARGS__); \ 101 \ 102 if (TST_PASS) \ 103 TST_MSGP_(TPASS, " returned pid %ld", TST_RET, \ 104 #SCALL, ##__VA_ARGS__); \ 105 } while (0) 106 107 #define TST_EXP_VAL_SILENT_(SCALL, VAL, SSCALL, ...) \ 108 do { \ 109 TEST(SCALL); \ 110 \ 111 TST_PASS = 0; \ 112 \ 113 if (TST_RET != VAL) { \ 114 TST_MSGP2_(TFAIL | TTERRNO, " retval %ld != %ld", \ 115 TST_RET, (long)VAL, SSCALL, ##__VA_ARGS__); \ 116 break; \ 117 } \ 118 \ 119 TST_PASS = 1; \ 120 \ 121 } while (0) 122 123 #define TST_EXP_VAL_SILENT(SCALL, VAL, ...) TST_EXP_VAL_SILENT_(SCALL, VAL, #SCALL, ##__VA_ARGS__) 124 125 #define TST_EXP_VAL(SCALL, VAL, ...) \ 126 do { \ 127 TST_EXP_VAL_SILENT_(SCALL, VAL, #SCALL, ##__VA_ARGS__); \ 128 \ 129 if (TST_PASS) \ 130 TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__); \ 131 \ 132 } while(0) 133 134 #define TST_EXP_PASS_SILENT_(SCALL, SSCALL, ...) \ 135 do { \ 136 TEST(SCALL); \ 137 \ 138 TST_PASS = 0; \ 139 \ 140 if (TST_RET == -1) { \ 141 TST_MSG_(TFAIL | TTERRNO, " failed", \ 142 SSCALL, ##__VA_ARGS__); \ 143 break; \ 144 } \ 145 \ 146 if (TST_RET != 0) { \ 147 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 148 TST_RET, SSCALL, ##__VA_ARGS__); \ 149 break; \ 150 } \ 151 \ 152 TST_PASS = 1; \ 153 \ 154 } while (0) 155 156 #define TST_EXP_PASS_SILENT(SCALL, ...) TST_EXP_PASS_SILENT_(SCALL, #SCALL, ##__VA_ARGS__) 157 158 #define TST_EXP_PASS(SCALL, ...) \ 159 do { \ 160 TST_EXP_PASS_SILENT_(SCALL, #SCALL, ##__VA_ARGS__); \ 161 \ 162 if (TST_PASS) \ 163 TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__); \ 164 } while (0) \ 165 166 #define TST_EXP_FAIL_(PASS_COND, SCALL, SSCALL, ERRNO, ...) \ 167 do { \ 168 TEST(SCALL); \ 169 \ 170 TST_PASS = 0; \ 171 \ 172 if (PASS_COND) { \ 173 TST_MSG_(TFAIL, " succeeded", SSCALL, ##__VA_ARGS__); \ 174 break; \ 175 } \ 176 \ 177 if (TST_RET != -1) { \ 178 TST_MSGP_(TFAIL | TTERRNO, " invalid retval %ld", \ 179 TST_RET, SSCALL, ##__VA_ARGS__); \ 180 break; \ 181 } \ 182 \ 183 if (TST_ERR == (ERRNO)) { \ 184 TST_MSG_(TPASS | TTERRNO, " ", \ 185 SSCALL, ##__VA_ARGS__); \ 186 TST_PASS = 1; \ 187 } else { \ 188 TST_MSGP_(TFAIL | TTERRNO, " expected %s", \ 189 tst_strerrno(ERRNO), \ 190 SSCALL, ##__VA_ARGS__); \ 191 } \ 192 } while (0) 193 194 #define TST_EXP_FAIL(SCALL, ERRNO, ...) TST_EXP_FAIL_(TST_RET == 0, SCALL, #SCALL, ERRNO, ##__VA_ARGS__) 195 196 #define TST_EXP_FAIL2(SCALL, ERRNO, ...) TST_EXP_FAIL_(TST_RET >= 0, SCALL, #SCALL, ERRNO, ##__VA_ARGS__) 197 198 #define TST_EXP_EXPR(EXPR, FMT, ...) \ 199 tst_res_(__FILE__, __LINE__, (EXPR) ? TPASS : TFAIL, "Expect: " FMT, ##__VA_ARGS__); 200 201 #endif /* TST_TEST_MACROS_H__ */ 202