mirror of
https://github.com/systemd/systemd.git
synced 2025-01-24 06:04:05 +03:00
Add a set of assertion macros to tests.h
(ASSERT_OK(), ASSERT_EQ(), ASSERT_GE(), ASSERT_LE()) that log the failed condition before crashing and convert test-gpt.c test file to use them
This commit is contained in:
parent
6644445586
commit
e19186359a
@ -200,3 +200,42 @@ static inline int run_test_table(void) {
|
||||
DEFINE_TEST_MAIN_FULL(log_level, intro, NULL)
|
||||
#define DEFINE_TEST_MAIN(log_level) \
|
||||
DEFINE_TEST_MAIN_FULL(log_level, NULL, NULL)
|
||||
|
||||
#define ASSERT_OK(expr) \
|
||||
({ \
|
||||
int _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno("Assertion failed: %s (result: %d, error: %m)", #expr, _result); \
|
||||
abort(); \
|
||||
} \
|
||||
})
|
||||
|
||||
#define ASSERT_EQ(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 != _expr2) { \
|
||||
log_error("Assertion failed: expected %s == %s, but %d != %d", #expr1, #expr2, _expr1, _expr2); \
|
||||
abort(); \
|
||||
} \
|
||||
})
|
||||
|
||||
#define ASSERT_GE(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 < _expr2) { \
|
||||
log_error("Assertion failed: expected %s >= %s, but %d < %d", #expr1, #expr2, _expr1, _expr2); \
|
||||
abort(); \
|
||||
} \
|
||||
})
|
||||
|
||||
#define ASSERT_LE(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 > _expr2) { \
|
||||
log_error("Assertion failed: expected %s <= %s, but %d > %d", #expr1, #expr2, _expr1, _expr2); \
|
||||
abort(); \
|
||||
} \
|
||||
})
|
||||
|
@ -34,15 +34,15 @@ TEST(gpt_types_against_architectures) {
|
||||
printf("%s %s\n", GREEN_CHECK_MARK(), joined);
|
||||
|
||||
if (streq(prefix, "root-") && streq(suffix, ""))
|
||||
assert_se(type.designator == PARTITION_ROOT);
|
||||
ASSERT_EQ(type.designator, PARTITION_ROOT);
|
||||
if (streq(prefix, "root-") && streq(suffix, "-verity"))
|
||||
assert_se(type.designator == PARTITION_ROOT_VERITY);
|
||||
ASSERT_EQ(type.designator, PARTITION_ROOT_VERITY);
|
||||
if (streq(prefix, "usr-") && streq(suffix, ""))
|
||||
assert_se(type.designator == PARTITION_USR);
|
||||
ASSERT_EQ(type.designator, PARTITION_USR);
|
||||
if (streq(prefix, "usr-") && streq(suffix, "-verity"))
|
||||
assert_se(type.designator == PARTITION_USR_VERITY);
|
||||
ASSERT_EQ(type.designator, PARTITION_USR_VERITY);
|
||||
|
||||
assert_se(type.arch == a);
|
||||
ASSERT_EQ(type.arch, a);
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,38 +72,38 @@ TEST(type_alias_same) {
|
||||
GptPartitionType x, y;
|
||||
|
||||
x = gpt_partition_type_from_uuid(t->uuid); /* search first by uuid */
|
||||
assert_se(gpt_partition_type_from_string(t->name, &y) >= 0); /* search first by name */
|
||||
ASSERT_GE(gpt_partition_type_from_string(t->name, &y), 0); /* search first by name */
|
||||
|
||||
assert_se(t->arch == x.arch);
|
||||
assert_se(t->arch == y.arch);
|
||||
assert_se(t->designator == x.designator);
|
||||
assert_se(t->designator == y.designator);
|
||||
ASSERT_EQ(t->arch, x.arch);
|
||||
ASSERT_EQ(t->arch, y.arch);
|
||||
ASSERT_EQ(t->designator, x.designator);
|
||||
ASSERT_EQ(t->designator, y.designator);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(override_architecture) {
|
||||
GptPartitionType x, y;
|
||||
|
||||
assert_se(gpt_partition_type_from_string("root-x86-64", &x) >= 0);
|
||||
assert_se(x.arch == ARCHITECTURE_X86_64);
|
||||
ASSERT_GE(gpt_partition_type_from_string("root-x86-64", &x), 0);
|
||||
ASSERT_EQ(x.arch, ARCHITECTURE_X86_64);
|
||||
|
||||
assert_se(gpt_partition_type_from_string("root-arm64", &y) >= 0);
|
||||
assert(y.arch == ARCHITECTURE_ARM64);
|
||||
ASSERT_GE(gpt_partition_type_from_string("root-arm64", &y), 0);
|
||||
ASSERT_EQ(y.arch, ARCHITECTURE_ARM64);
|
||||
|
||||
x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64);
|
||||
assert_se(x.arch == y.arch);
|
||||
assert_se(x.designator == y.designator);
|
||||
ASSERT_EQ(x.arch, y.arch);
|
||||
ASSERT_EQ(x.designator, y.designator);
|
||||
assert_se(sd_id128_equal(x.uuid, y.uuid));
|
||||
assert_se(streq(x.name, y.name));
|
||||
|
||||
/* If the partition type does not have an architecture, nothing should change. */
|
||||
|
||||
assert_se(gpt_partition_type_from_string("esp", &x) >= 0);
|
||||
ASSERT_GE(gpt_partition_type_from_string("esp", &x), 0);
|
||||
y = x;
|
||||
|
||||
x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64);
|
||||
assert_se(x.arch == y.arch);
|
||||
assert_se(x.designator == y.designator);
|
||||
ASSERT_EQ(x.arch, y.arch);
|
||||
ASSERT_EQ(x.designator, y.designator);
|
||||
assert_se(sd_id128_equal(x.uuid, y.uuid));
|
||||
assert_se(streq(x.name, y.name));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user