feat: Adding suit wise setup and tear down.

This commit is contained in:
2025-06-25 14:56:38 +02:00
parent 5efea9a9b6
commit 717af6378b
3 changed files with 40 additions and 4 deletions

11
set.c
View File

@@ -129,6 +129,12 @@ static void dispatch_single_test(struct SETest *test)
static void dispatch_tests(struct SETSuit *suit)
{
printf("\n\n==== Running suit: %s (%d) ====\n\n", suit->name, suit->len);
if (suit->setup != NULL && !suit->setup())
{
fprintf(stderr, "Suit not executed: Setup failed.\n");
}
struct SETest *test = suit->tests;
for (int i = 0; i < suit->len; i++)
{
@@ -154,6 +160,11 @@ static void dispatch_tests(struct SETSuit *suit)
perror("shctl");
exit(1);
}
if (suit->tear_down != NULL && !suit->tear_down())
{
fprintf(stderr, "Teardown failed.\n");
}
}
static void dispatch_test_suits(struct SETSuit **suits, int counter)

17
set.h
View File

@@ -27,6 +27,8 @@ struct SETSuit
struct SETest *tests;
int len;
int shm_key;
bool (*setup)();
bool (*tear_down)();
bool passed;
};
@@ -37,8 +39,11 @@ void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
int create_shared_suit_space(size_t size);
bool set_up();
static bool EMPTY_suit_setup() { return true; }
static bool EMPTY_suit_tear_down() { return true; }
bool set_up();
bool tear_down();
#define SETUP() bool set_up()
@@ -56,7 +61,7 @@ bool tear_down();
// We manually allocate suits via mmap be able to mark them DONTFORK.
// Since we don't need the suits in a test fork.
#define SUIT(suit_name) \
#define SUIT_ST(suit_name, setup_func, tear_down_func) \
void suit_name##_suit(struct SETSuit *suit, bool count); \
struct SETSuit *suit_name##_suit_contructor() \
{ \
@@ -77,11 +82,19 @@ bool tear_down();
exit(1); \
} \
suit->len = 0; \
suit->setup = &setup_func##_suit_setup; \
suit->tear_down = &tear_down_func##_suit_tear_down; \
suit_name##_suit(suit, false); \
return suit; \
} \
void suit_name##_suit(struct SETSuit *suit, bool count)
#define SUIT_SETUP(suit_name) bool suit_name##_suit_setup()
#define SUIT_TEAR_DOWN(suit_name) bool suit_name##_suit_tear_down()
#define SUIT(suit_name) SUIT_ST(suit_name, EMPTY, EMPTY)
#define ADD_TEST(test_name) \
if (!count) \
{ \

View File

@@ -38,7 +38,19 @@ TEST(Faculty_Negative)
ASSERT_EQ(fac(0), 1);
}
SUIT(Basic)
SUIT_SETUP(Basic)
{
fprintf(stdout, "Some suit setup.\n");
return true;
}
SUIT_TEAR_DOWN(Basic)
{
fprintf(stdout, "Some suit teardown\n");
return true;
}
SUIT_ST(Basic, Basic, Basic)
{
ADD_TEST(Faculty_Basic);
ADD_TEST(Faculty_Negative);
@@ -50,7 +62,7 @@ TEST(Other_Basic)
ASSERT_FALSE(false);
}
SUIT(Other) { ADD_TEST(Other_Basic); }
SUIT_ST(Other, Basic, EMPTY) { ADD_TEST(Other_Basic); }
BUNDLE()
{