Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: test: add the concept of assertions

Add support for assertions which are like expectations except the test
terminates if the assertion is not satisfied.

The idea with assertions is that you use them to state all the
preconditions for your test. Logically speaking, these are the premises
of the test case, so if a premise isn't true, there is no point in
continuing the test case because there are no conclusions that can be
drawn without the premises. Whereas, the expectation is the thing you
are trying to prove. It is not used universally in x-unit style test
frameworks, but I really like it as a convention. You could still
express the idea of a premise using the above idiom, but I think
KUNIT_ASSERT_* states the intended idea perfectly.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Brendan Higgins and committed by
Shuah Khan
e4aea8f8 e4eb117f

+283 -8
+280 -2
include/kunit/test.h
··· 87 87 * @name: the name of the test case. 88 88 * 89 89 * A test case is a function with the signature, 90 - * ``void (*)(struct kunit *)`` that makes expectations (see 91 - * KUNIT_EXPECT_TRUE()) about code under test. Each test case is associated 90 + * ``void (*)(struct kunit *)`` 91 + * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and 92 + * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated 92 93 * with a &struct kunit_suite and will be run after the suite's init 93 94 * function and followed by the suite's exit function. 94 95 * ··· 1207 1206 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1208 1207 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1209 1208 KUNIT_EXPECTATION, \ 1209 + ptr, \ 1210 + fmt, \ 1211 + ##__VA_ARGS__) 1212 + 1213 + #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ 1214 + KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) 1215 + 1216 + /** 1217 + * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. 1218 + * @test: The test context object. 1219 + * @condition: an arbitrary boolean expression. The test fails and aborts when 1220 + * this does not evaluate to true. 1221 + * 1222 + * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to 1223 + * fail *and immediately abort* when the specified condition is not met. Unlike 1224 + * an expectation failure, it will prevent the test case from continuing to run; 1225 + * this is otherwise known as an *assertion failure*. 1226 + */ 1227 + #define KUNIT_ASSERT_TRUE(test, condition) \ 1228 + KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition) 1229 + 1230 + #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ 1231 + KUNIT_TRUE_MSG_ASSERTION(test, \ 1232 + KUNIT_ASSERTION, \ 1233 + condition, \ 1234 + fmt, \ 1235 + ##__VA_ARGS__) 1236 + 1237 + /** 1238 + * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. 1239 + * @test: The test context object. 1240 + * @condition: an arbitrary boolean expression. 1241 + * 1242 + * Sets an assertion that the value that @condition evaluates to is false. This 1243 + * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure 1244 + * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1245 + */ 1246 + #define KUNIT_ASSERT_FALSE(test, condition) \ 1247 + KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition) 1248 + 1249 + #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ 1250 + KUNIT_FALSE_MSG_ASSERTION(test, \ 1251 + KUNIT_ASSERTION, \ 1252 + condition, \ 1253 + fmt, \ 1254 + ##__VA_ARGS__) 1255 + 1256 + /** 1257 + * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. 1258 + * @test: The test context object. 1259 + * @left: an arbitrary expression that evaluates to a primitive C type. 1260 + * @right: an arbitrary expression that evaluates to a primitive C type. 1261 + * 1262 + * Sets an assertion that the values that @left and @right evaluate to are 1263 + * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1264 + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1265 + */ 1266 + #define KUNIT_ASSERT_EQ(test, left, right) \ 1267 + KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1268 + 1269 + #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ 1270 + KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 1271 + KUNIT_ASSERTION, \ 1272 + left, \ 1273 + right, \ 1274 + fmt, \ 1275 + ##__VA_ARGS__) 1276 + 1277 + /** 1278 + * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1279 + * @test: The test context object. 1280 + * @left: an arbitrary expression that evaluates to a pointer. 1281 + * @right: an arbitrary expression that evaluates to a pointer. 1282 + * 1283 + * Sets an assertion that the values that @left and @right evaluate to are 1284 + * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1285 + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1286 + */ 1287 + #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ 1288 + KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1289 + 1290 + #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1291 + KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 1292 + KUNIT_ASSERTION, \ 1293 + left, \ 1294 + right, \ 1295 + fmt, \ 1296 + ##__VA_ARGS__) 1297 + 1298 + /** 1299 + * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. 1300 + * @test: The test context object. 1301 + * @left: an arbitrary expression that evaluates to a primitive C type. 1302 + * @right: an arbitrary expression that evaluates to a primitive C type. 1303 + * 1304 + * Sets an assertion that the values that @left and @right evaluate to are not 1305 + * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1306 + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1307 + */ 1308 + #define KUNIT_ASSERT_NE(test, left, right) \ 1309 + KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1310 + 1311 + #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ 1312 + KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 1313 + KUNIT_ASSERTION, \ 1314 + left, \ 1315 + right, \ 1316 + fmt, \ 1317 + ##__VA_ARGS__) 1318 + 1319 + /** 1320 + * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. 1321 + * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1322 + * @test: The test context object. 1323 + * @left: an arbitrary expression that evaluates to a pointer. 1324 + * @right: an arbitrary expression that evaluates to a pointer. 1325 + * 1326 + * Sets an assertion that the values that @left and @right evaluate to are not 1327 + * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1328 + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1329 + */ 1330 + #define KUNIT_ASSERT_PTR_NE(test, left, right) \ 1331 + KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1332 + 1333 + #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1334 + KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 1335 + KUNIT_ASSERTION, \ 1336 + left, \ 1337 + right, \ 1338 + fmt, \ 1339 + ##__VA_ARGS__) 1340 + /** 1341 + * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. 1342 + * @test: The test context object. 1343 + * @left: an arbitrary expression that evaluates to a primitive C type. 1344 + * @right: an arbitrary expression that evaluates to a primitive C type. 1345 + * 1346 + * Sets an assertion that the value that @left evaluates to is less than the 1347 + * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except 1348 + * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1349 + * is not met. 1350 + */ 1351 + #define KUNIT_ASSERT_LT(test, left, right) \ 1352 + KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1353 + 1354 + #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ 1355 + KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 1356 + KUNIT_ASSERTION, \ 1357 + left, \ 1358 + right, \ 1359 + fmt, \ 1360 + ##__VA_ARGS__) 1361 + /** 1362 + * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. 1363 + * @test: The test context object. 1364 + * @left: an arbitrary expression that evaluates to a primitive C type. 1365 + * @right: an arbitrary expression that evaluates to a primitive C type. 1366 + * 1367 + * Sets an assertion that the value that @left evaluates to is less than or 1368 + * equal to the value that @right evaluates to. This is the same as 1369 + * KUNIT_EXPECT_LE(), except it causes an assertion failure (see 1370 + * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1371 + */ 1372 + #define KUNIT_ASSERT_LE(test, left, right) \ 1373 + KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1374 + 1375 + #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ 1376 + KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1377 + KUNIT_ASSERTION, \ 1378 + left, \ 1379 + right, \ 1380 + fmt, \ 1381 + ##__VA_ARGS__) 1382 + 1383 + /** 1384 + * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. 1385 + * @test: The test context object. 1386 + * @left: an arbitrary expression that evaluates to a primitive C type. 1387 + * @right: an arbitrary expression that evaluates to a primitive C type. 1388 + * 1389 + * Sets an assertion that the value that @left evaluates to is greater than the 1390 + * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except 1391 + * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1392 + * is not met. 1393 + */ 1394 + #define KUNIT_ASSERT_GT(test, left, right) \ 1395 + KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1396 + 1397 + #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ 1398 + KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1399 + KUNIT_ASSERTION, \ 1400 + left, \ 1401 + right, \ 1402 + fmt, \ 1403 + ##__VA_ARGS__) 1404 + 1405 + /** 1406 + * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. 1407 + * @test: The test context object. 1408 + * @left: an arbitrary expression that evaluates to a primitive C type. 1409 + * @right: an arbitrary expression that evaluates to a primitive C type. 1410 + * 1411 + * Sets an assertion that the value that @left evaluates to is greater than the 1412 + * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except 1413 + * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1414 + * is not met. 1415 + */ 1416 + #define KUNIT_ASSERT_GE(test, left, right) \ 1417 + KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1418 + 1419 + #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ 1420 + KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1421 + KUNIT_ASSERTION, \ 1422 + left, \ 1423 + right, \ 1424 + fmt, \ 1425 + ##__VA_ARGS__) 1426 + 1427 + /** 1428 + * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. 1429 + * @test: The test context object. 1430 + * @left: an arbitrary expression that evaluates to a null terminated string. 1431 + * @right: an arbitrary expression that evaluates to a null terminated string. 1432 + * 1433 + * Sets an assertion that the values that @left and @right evaluate to are 1434 + * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an 1435 + * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1436 + */ 1437 + #define KUNIT_ASSERT_STREQ(test, left, right) \ 1438 + KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1439 + 1440 + #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ 1441 + KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1442 + KUNIT_ASSERTION, \ 1443 + left, \ 1444 + right, \ 1445 + fmt, \ 1446 + ##__VA_ARGS__) 1447 + 1448 + /** 1449 + * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. 1450 + * @test: The test context object. 1451 + * @left: an arbitrary expression that evaluates to a null terminated string. 1452 + * @right: an arbitrary expression that evaluates to a null terminated string. 1453 + * 1454 + * Sets an expectation that the values that @left and @right evaluate to are 1455 + * not equal. This is semantically equivalent to 1456 + * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() 1457 + * for more information. 1458 + */ 1459 + #define KUNIT_ASSERT_STRNEQ(test, left, right) \ 1460 + KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1461 + 1462 + #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1463 + KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1464 + KUNIT_ASSERTION, \ 1465 + left, \ 1466 + right, \ 1467 + fmt, \ 1468 + ##__VA_ARGS__) 1469 + 1470 + /** 1471 + * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. 1472 + * @test: The test context object. 1473 + * @ptr: an arbitrary pointer. 1474 + * 1475 + * Sets an assertion that the value that @ptr evaluates to is not null and not 1476 + * an errno stored in a pointer. This is the same as 1477 + * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see 1478 + * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1479 + */ 1480 + #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ 1481 + KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr) 1482 + 1483 + #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1484 + KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1485 + KUNIT_ASSERTION, \ 1210 1486 ptr, \ 1211 1487 fmt, \ 1212 1488 ##__VA_ARGS__)
+1 -1
lib/kunit/string-stream-test.c
··· 35 35 string_stream_add(stream, " %s", "bar"); 36 36 37 37 output = string_stream_get_string(stream); 38 - KUNIT_EXPECT_STREQ(test, output, "Foo bar"); 38 + KUNIT_ASSERT_STREQ(test, output, "Foo bar"); 39 39 } 40 40 41 41 static struct kunit_case string_stream_test_cases[] = {
+2 -5
lib/kunit/test-test.c
··· 78 78 struct kunit_try_catch_test_context *ctx; 79 79 80 80 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 81 - if (!ctx) 82 - return -ENOMEM; 83 - 81 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 84 82 test->priv = ctx; 85 83 86 84 ctx->try_catch = kunit_kmalloc(test, 87 85 sizeof(*ctx->try_catch), 88 86 GFP_KERNEL); 89 - if (!ctx->try_catch) 90 - return -ENOMEM; 87 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch); 91 88 92 89 return 0; 93 90 }