Skip to content

Commit e4aea8f

Browse files
bjh83shuahkh
authored andcommitted
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>
1 parent e4eb117 commit e4aea8f

3 files changed

Lines changed: 283 additions & 8 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 280 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ struct kunit;
8787
* @name: the name of the test case.
8888
*
8989
* 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
9293
* with a &struct kunit_suite and will be run after the suite's init
9394
* function and followed by the suite's exit function.
9495
*
@@ -1210,4 +1211,281 @@ do { \
12101211
fmt, \
12111212
##__VA_ARGS__)
12121213

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

‎lib/kunit/string-stream-test.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void string_stream_test_get_string(struct kunit *test)
3535
string_stream_add(stream, " %s", "bar");
3636

3737
output = string_stream_get_string(stream);
38-
KUNIT_EXPECT_STREQ(test, output, "Foo bar");
38+
KUNIT_ASSERT_STREQ(test, output, "Foo bar");
3939
}
4040

4141
static struct kunit_case string_stream_test_cases[] = {

‎lib/kunit/test-test.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,13 @@ static int kunit_try_catch_test_init(struct kunit *test)
7878
struct kunit_try_catch_test_context *ctx;
7979

8080
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
81-
if (!ctx)
82-
return -ENOMEM;
83-
81+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
8482
test->priv = ctx;
8583

8684
ctx->try_catch = kunit_kmalloc(test,
8785
sizeof(*ctx->try_catch),
8886
GFP_KERNEL);
89-
if (!ctx->try_catch)
90-
return -ENOMEM;
87+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
9188

9289
return 0;
9390
}

0 commit comments

Comments
 (0)