feat: All tests run in a child process now.
This commit is contained in:
8
cpp_test.cpp
Normal file
8
cpp_test.cpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "set.h"
|
||||||
|
#include "set_asserts.h"
|
||||||
|
|
||||||
|
TEST(Basic) { ASSERT_EQ(2, 2); }
|
||||||
|
|
||||||
|
SUIT(Basic) { ADD_TEST(Basic); }
|
||||||
|
|
||||||
|
BUNDLE() { ADD_SUIT(Basic); }
|
164
set.c
164
set.c
@@ -3,9 +3,17 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "set_asserts.h"
|
#include "set_asserts.h"
|
||||||
|
|
||||||
|
#ifdef COLORIZED
|
||||||
|
|
||||||
#define ANSI_COLOR_RED "\x1b[31m"
|
#define ANSI_COLOR_RED "\x1b[31m"
|
||||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||||
@@ -14,28 +22,49 @@
|
|||||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define ANSI_COLOR_RED
|
||||||
|
#define ANSI_COLOR_GREEN
|
||||||
|
#define ANSI_COLOR_YELLOW
|
||||||
|
#define ANSI_COLOR_BLUE
|
||||||
|
#define ANSI_COLOR_MAGENTA
|
||||||
|
#define ANSI_COLOR_CYAN
|
||||||
|
#define ANSI_COLOR_RESET
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
#define BANNER \
|
#define BANNER \
|
||||||
"" \
|
"" \
|
||||||
" _____ _ _ _____ _ _____ _ \n"\
|
" _____ _ _ _____ _ " \
|
||||||
" / ___| | | | | ___| | | |_ _| | | \n"\
|
"_____ _ \n" \
|
||||||
" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | | ___ ___| |_ ___ _ __ \n"\
|
" / ___| | | | | ___| | | |_ " \
|
||||||
" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ | |/ _ / __| __/ _ | '__|\n"\
|
"_| | | \n" \
|
||||||
" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | | __\\__ | || __| | \n"\
|
" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | " \
|
||||||
" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| |_| \\_/\\___|___/\\__\\___|_| \n"\
|
"| ___ ___| |_ ___ _ __ \n" \
|
||||||
" __/ | \n"\
|
" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ " \
|
||||||
" |___/ \n"\
|
" | |/ _ / __| __/ _ | '__|\n" \
|
||||||
""
|
" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | " \
|
||||||
|
"| __\\__ | || __| | \n" \
|
||||||
|
" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| " \
|
||||||
|
"|_| \\_/\\___|___/\\__\\___|_| \n" \
|
||||||
|
" __/ | " \
|
||||||
|
" \n" \
|
||||||
|
" |___/ " \
|
||||||
|
" \n" \
|
||||||
|
""
|
||||||
//clang-format on
|
//clang-format on
|
||||||
|
|
||||||
char* format_string(const char* fmt, ...)
|
char *format_string(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
size_t size = vsnprintf(NULL, 0, fmt, args);
|
size_t size = vsnprintf(NULL, 0, fmt, args);
|
||||||
char* out = (char*)malloc(size + 1);
|
char *out = (char *)malloc(size + 1);
|
||||||
|
|
||||||
if (!out) {
|
if (!out)
|
||||||
|
{
|
||||||
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
|
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,79 +77,110 @@ char* format_string(const char* fmt, ...)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void log_test_start(const char* name) { printf("Running test: %s", name); }
|
int create_shared_suit_space(size_t size)
|
||||||
|
|
||||||
static void log_test_summary(struct SETest* test)
|
|
||||||
{
|
{
|
||||||
if (test->passed) {
|
int suit_space_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
||||||
#ifdef COLORIZED
|
|
||||||
printf(ANSI_COLOR_GREEN " - passed.\n" ANSI_COLOR_RESET);
|
if (suit_space_id == -1)
|
||||||
#else
|
{
|
||||||
printf(" - passed.\n");
|
fprintf(stdout, "Couldn't create suite space of size: %lu\n", size);
|
||||||
#endif
|
perror("shmget");
|
||||||
} else {
|
exit(1);
|
||||||
#ifdef COLORIZED
|
}
|
||||||
printf(ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET " Reason: \n %s\n\n",
|
|
||||||
test->error_msg);
|
return suit_space_id;
|
||||||
#else
|
}
|
||||||
printf(" - failed.\n Reason: \n %s\n\n", test->error_msg);
|
|
||||||
#endif
|
|
||||||
|
static void log_test_summary(struct SETest *test)
|
||||||
|
{
|
||||||
|
if (test->passed)
|
||||||
|
{
|
||||||
|
printf("Test: %s" ANSI_COLOR_GREEN " - passed." ANSI_COLOR_RESET"\n",
|
||||||
|
test->name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Test: %s" ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET
|
||||||
|
" Reason: \n %s\n\n",
|
||||||
|
test->name, test->error_msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dispatch_tests(struct SETSuit* suit)
|
static void log_abnormal_termination(int status)
|
||||||
{
|
{
|
||||||
printf("\n\n==== Running suit: %s ====\n\n", suit->name);
|
fprintf(stderr,
|
||||||
for (int i = 0; i < suit->len; i++) {
|
ANSI_COLOR_RED "A test terminated with signal: %d" ANSI_COLOR_RESET,
|
||||||
struct SETest* test = suit->tests[i];
|
WTERMSIG(status));
|
||||||
log_test_start(test->name);
|
}
|
||||||
|
|
||||||
|
static void dispatch_single_test(struct SETest *test)
|
||||||
|
{
|
||||||
|
pid_t testPID = fork();
|
||||||
|
|
||||||
|
if (testPID == 0)
|
||||||
|
{
|
||||||
test->function(test);
|
test->function(test);
|
||||||
log_test_summary(test);
|
log_test_summary(test);
|
||||||
fflush(stdout);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void dispatch_tests(struct SETSuit *suit)
|
||||||
static void free_suit(struct SETSuit* suit)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < suit->len; i++) {
|
printf("\n\n==== Running suit: %s (%d) ====\n\n", suit->name, suit->len);
|
||||||
struct SETest* test = suit->tests[i];
|
struct SETest *test = suit->tests;
|
||||||
free((void*)test->error_msg);
|
for (int i = 0; i < suit->len; i++)
|
||||||
free((void*)test);
|
{
|
||||||
|
dispatch_single_test(&test[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(suit->tests);
|
int status = 0;
|
||||||
free(suit);
|
// Wait for all tests.
|
||||||
|
while (wait(&status) > 0)
|
||||||
|
{
|
||||||
|
if (WTERMSIG(status) != 0)
|
||||||
|
log_abnormal_termination(status);
|
||||||
|
};
|
||||||
|
if (shmdt(suit->tests) < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", "Couldn't detach suit space after dispatch.");
|
||||||
|
perror("shmat");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (shmctl(suit->shm_key, IPC_RMID, 0) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", "Couldn't remove suit space after dispatch.");
|
||||||
|
perror("shctl");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void dispatch_test_suits(struct SETSuit **suits, int counter)
|
||||||
static void dispatch_test_suits(struct SETSuit** suits, int counter)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < counter; i++)
|
for (int i = 0; i < counter; i++)
|
||||||
{
|
{
|
||||||
dispatch_tests(suits[i]);
|
dispatch_tests(suits[i]);
|
||||||
free_suit(suits[i]);
|
munmap((void*)suits[i], sizeof(struct SETSuit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
#ifndef NO_BANNER
|
#ifndef NO_BANNER
|
||||||
|
|
||||||
printf("\n\n%s", BANNER);
|
printf("\n\n%s", BANNER);
|
||||||
#endif /* ifndef NO_BANNER */
|
#endif /* ifndef NO_BANNER */
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
||||||
set_bundle_suits(NULL, &counter, true);
|
set_bundle_suits(NULL, &counter, true);
|
||||||
|
|
||||||
struct SETSuit ** suits = (struct SETSuit**)malloc(counter * sizeof(struct SETSuit*));
|
struct SETSuit *suits[counter];
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
|
|
||||||
set_bundle_suits(suits, &counter, false);
|
set_bundle_suits(suits, &counter, false);
|
||||||
|
|
||||||
dispatch_test_suits(suits, counter);
|
dispatch_test_suits(suits, counter);
|
||||||
|
|
||||||
free(suits);
|
|
||||||
}
|
}
|
||||||
|
44
set.h
44
set.h
@@ -1,24 +1,30 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
#ifndef INCLUDE_SET_H
|
#ifndef INCLUDE_SET_H
|
||||||
#define INCLUDE_SET_H
|
#define INCLUDE_SET_H
|
||||||
|
|
||||||
|
#define SET_MAX_ERROR_MSG_SIZE 256
|
||||||
|
#define SET_MAX_NAME_SIZE 64
|
||||||
|
|
||||||
struct SETest
|
struct SETest
|
||||||
{
|
{
|
||||||
char *name;
|
|
||||||
void (*function)(struct SETest *test);
|
void (*function)(struct SETest *test);
|
||||||
|
const char *error_msg;
|
||||||
|
const char *name;
|
||||||
bool passed;
|
bool passed;
|
||||||
char *error_msg;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SETSuit
|
struct SETSuit
|
||||||
{
|
{
|
||||||
char *name;
|
const char *name;
|
||||||
struct SETest **tests;
|
struct SETest *tests;
|
||||||
bool passed;
|
|
||||||
int len;
|
int len;
|
||||||
|
int shm_key;
|
||||||
|
bool passed;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *format_string(const char *fmt, ...);
|
char *format_string(const char *fmt, ...);
|
||||||
@@ -26,6 +32,8 @@ char *format_string(const char *fmt, ...);
|
|||||||
// To be implemented by user.
|
// To be implemented by user.
|
||||||
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
||||||
|
|
||||||
|
int create_shared_suit_space(size_t size);
|
||||||
|
|
||||||
#define BUNDLE() \
|
#define BUNDLE() \
|
||||||
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count)
|
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count)
|
||||||
|
|
||||||
@@ -36,16 +44,28 @@ void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
|||||||
} \
|
} \
|
||||||
(*counter)++;
|
(*counter)++;
|
||||||
|
|
||||||
|
// 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(suit_name) \
|
||||||
void suit_name##_suit(struct SETSuit *suit, bool count); \
|
void suit_name##_suit(struct SETSuit *suit, bool count); \
|
||||||
struct SETSuit *suit_name##_suit_contructor() \
|
struct SETSuit *suit_name##_suit_contructor() \
|
||||||
{ \
|
{ \
|
||||||
struct SETSuit *suit = \
|
struct SETSuit *suit = \
|
||||||
(struct SETSuit *)malloc(sizeof(struct SETSuit)); \
|
mmap(NULL, sizeof(struct SETSuit), PROT_READ | PROT_WRITE, \
|
||||||
suit->name = #suit_name; \
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
|
||||||
|
madvise(suit, sizeof(struct SETSuit), MADV_DONTFORK); \
|
||||||
|
suit->len = 0; \
|
||||||
suit_name##_suit(suit, true); \
|
suit_name##_suit(suit, true); \
|
||||||
suit->tests = \
|
suit->shm_key = \
|
||||||
(struct SETest **)malloc(suit->len * sizeof(struct SETest *)); \
|
create_shared_suit_space(suit->len * sizeof(struct SETest)); \
|
||||||
|
suit->name = #suit_name; \
|
||||||
|
suit->tests = shmat(suit->shm_key, 0, 0); \
|
||||||
|
if (suit->tests == (struct SETest *)-1) \
|
||||||
|
{ \
|
||||||
|
fprintf(stderr, "Couldn't attach suit space.\n"); \
|
||||||
|
perror("shmat"); \
|
||||||
|
exit(1); \
|
||||||
|
} \
|
||||||
suit->len = 0; \
|
suit->len = 0; \
|
||||||
suit_name##_suit(suit, false); \
|
suit_name##_suit(suit, false); \
|
||||||
return suit; \
|
return suit; \
|
||||||
@@ -55,21 +75,19 @@ void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
|||||||
#define ADD_TEST(test_name) \
|
#define ADD_TEST(test_name) \
|
||||||
if (!count) \
|
if (!count) \
|
||||||
{ \
|
{ \
|
||||||
suit->tests[suit->len] = test_name##_test_constructor(); \
|
test_name##_test_constructor(&suit->tests[suit->len]); \
|
||||||
} \
|
} \
|
||||||
suit->len++;
|
suit->len++;
|
||||||
|
|
||||||
// TEST MACRO
|
// TEST MACRO
|
||||||
#define TEST(test_name) \
|
#define TEST(test_name) \
|
||||||
void test_name##_test(struct SETest *test); \
|
void test_name##_test(struct SETest *test); \
|
||||||
struct SETest *test_name##_test_constructor() \
|
void test_name##_test_constructor(struct SETest *test) \
|
||||||
{ \
|
{ \
|
||||||
struct SETest *test = (struct SETest *)malloc(sizeof(struct SETest)); \
|
|
||||||
test->name = #test_name; \
|
test->name = #test_name; \
|
||||||
test->function = &test_name##_test; \
|
test->function = &test_name##_test; \
|
||||||
test->passed = true; \
|
test->passed = true; \
|
||||||
test->error_msg = NULL; \
|
test->error_msg = NULL; \
|
||||||
return test; \
|
|
||||||
} \
|
} \
|
||||||
void test_name##_test(struct SETest *test)
|
void test_name##_test(struct SETest *test)
|
||||||
|
|
||||||
|
@@ -21,7 +21,7 @@
|
|||||||
{ \
|
{ \
|
||||||
test->passed = false; \
|
test->passed = false; \
|
||||||
test->error_msg = \
|
test->error_msg = \
|
||||||
format_string("Expect %d to be %d\n", act, exp) return; \
|
format_string("Expect %d to be %d.\n", act, exp) return; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ASSERT_EQ_MSG(exp, act, msg) \
|
#define ASSERT_EQ_MSG(exp, act, msg) \
|
||||||
|
@@ -1,13 +1,17 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "set_asserts.h"
|
#include "set_asserts.h"
|
||||||
|
|
||||||
int fac(int n)
|
int fac(int n)
|
||||||
{
|
{
|
||||||
if (n <= 1)
|
if (n == 1)
|
||||||
return 1;
|
return 1;
|
||||||
return n * fac(n - 1);
|
return n * fac(n - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void heavy_load() { sleep(5); }
|
||||||
|
|
||||||
TEST(Faculty_Basic)
|
TEST(Faculty_Basic)
|
||||||
{
|
{
|
||||||
ASSERT_EQ(fac(5), 120);
|
ASSERT_EQ(fac(5), 120);
|
||||||
|
Reference in New Issue
Block a user