Compare commits
9 Commits
3f78db4cb7
...
main
Author | SHA1 | Date | |
---|---|---|---|
f4a359b871
|
|||
ba65e6c433
|
|||
f3ca4cdd72
|
|||
84ba41e180
|
|||
1aba159f8f
|
|||
1401e1d70b
|
|||
3f5cbdd276
|
|||
5c08d33f22
|
|||
ed41c04209 |
10
README.md
10
README.md
@@ -2,15 +2,11 @@
|
|||||||
|
|
||||||
Tiny C testing framework.
|
Tiny C testing framework.
|
||||||
|
|
||||||
## Build
|
## Building a test.
|
||||||
|
|
||||||
Building is quite simple. To build a test `test_fac.c` just pick your favourite compiler and run.
|
To build a test, first build the library. Then link it to the `test.c` via:
|
||||||
|
|
||||||
gcc Small-Enough-Tester/set.c test_fac.c -I Small-Enough-Tester/ -D COLORIZED -lm -o test_fac
|
gcc src/*.c test.c -o test
|
||||||
|
|
||||||
Running a test is equally simple just execute the compiled binary.
|
|
||||||
|
|
||||||
./test_fac
|
|
||||||
|
|
||||||
## LICENSE
|
## LICENSE
|
||||||
|
|
||||||
|
38
include/list.h
Normal file
38
include/list.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef INCLUDE_SET_LIST_
|
||||||
|
#define INCLUDE_SET_LIST_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
struct SETBlockMeta
|
||||||
|
{
|
||||||
|
struct SETBlockMeta *next;
|
||||||
|
struct SETBlockMeta *prev;
|
||||||
|
struct SETBlockMeta *end;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new element to the list. The MetaData should sit at the start
|
||||||
|
* of a block.
|
||||||
|
*
|
||||||
|
* head (SETBlockMeta) List head.
|
||||||
|
* next (SETBlockMeta) Next meta to append.
|
||||||
|
*/
|
||||||
|
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees all blocks in the list.
|
||||||
|
*
|
||||||
|
* head (SETBlockMeta) List head.
|
||||||
|
*/
|
||||||
|
void set_ll_free_all(struct SETBlockMeta *head);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees the block with the given address and returns the new head.
|
||||||
|
*
|
||||||
|
* head (SETBlockMeta) List head.
|
||||||
|
* address (void*) Address to free.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address);
|
||||||
|
|
||||||
|
#endif
|
257
include/set.h
Normal file
257
include/set.h
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
#ifndef __cplusplus
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#ifndef INCLUDE_SET_H
|
||||||
|
#define INCLUDE_SET_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meta data for one test function.
|
||||||
|
*
|
||||||
|
* See also: struct SETSuit
|
||||||
|
*/
|
||||||
|
struct SETest
|
||||||
|
{
|
||||||
|
void (*function)(struct SETest *test);
|
||||||
|
const char *error_msg;
|
||||||
|
const char *name;
|
||||||
|
bool passed;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meta data for one test suit.
|
||||||
|
*
|
||||||
|
* See also: struct SETest
|
||||||
|
*/
|
||||||
|
struct SETSuit
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
struct SETest *tests;
|
||||||
|
int len;
|
||||||
|
int shm_key;
|
||||||
|
bool (*setup)();
|
||||||
|
bool (*tear_down)();
|
||||||
|
bool passed;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal function header for bundle. Use BUNDLE() macro to define bundle
|
||||||
|
* in test.
|
||||||
|
*/
|
||||||
|
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty suit_setup. Resolving EMPTY in a suit constructor gets
|
||||||
|
* us here.
|
||||||
|
*
|
||||||
|
* Note: Use EMPTY instead of this function.
|
||||||
|
*/
|
||||||
|
static bool EMPTY_suit_setup() { return true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty suit_tear_down. Resolving EMPTY in a suit constructor gets
|
||||||
|
* us here.
|
||||||
|
*
|
||||||
|
* Note: Use EMPTY instead of this function.
|
||||||
|
*/
|
||||||
|
static bool EMPTY_suit_tear_down() { return true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal header for global setup. Use SETUP() macro instead.
|
||||||
|
*/
|
||||||
|
bool set_up();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal header for global tear down. Use TEAR_DOWN() macro instead.
|
||||||
|
*/
|
||||||
|
bool tear_down();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global setup function.
|
||||||
|
* Gets called at the start of the whole test program.
|
||||||
|
*
|
||||||
|
* Should return True if setup was successfull, false if not.
|
||||||
|
* Tests are not executed on false.
|
||||||
|
*
|
||||||
|
* Note: This function has to be set in the tests even if it is not used.
|
||||||
|
*
|
||||||
|
* See also: NO_SETUP()
|
||||||
|
*/
|
||||||
|
#define SETUP() bool set_up()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty setup function returning true.
|
||||||
|
*/
|
||||||
|
#define NO_SETUP \
|
||||||
|
bool set_up() { return true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup function that allways fails. May be usefull to set if tests shouldn't
|
||||||
|
* be executed.
|
||||||
|
*/
|
||||||
|
#define DEACTIVATE \
|
||||||
|
bool set_up() \
|
||||||
|
{ \
|
||||||
|
printf("All tests are deactivated.\n"); \
|
||||||
|
return false; \
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global tear down function.
|
||||||
|
* Gets called at the end of the while test program.
|
||||||
|
*
|
||||||
|
* Should return true if tear down was successfull, false if not.
|
||||||
|
*
|
||||||
|
* Note: This function has to be set in the tests even if it is not used.
|
||||||
|
*/
|
||||||
|
#define TEAR_DOWN() bool tear_down()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty tear down function returning true.
|
||||||
|
*/
|
||||||
|
#define NO_TEAR_DOWN \
|
||||||
|
bool tear_down() { return true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Section in the tests to register all suits for execution.
|
||||||
|
*
|
||||||
|
* See also: ADD_SUIT()
|
||||||
|
* See also: SUIT, SUIT_ST
|
||||||
|
*/
|
||||||
|
#define BUNDLE() \
|
||||||
|
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in BUNDLE() to register suit.
|
||||||
|
*
|
||||||
|
* suit_name (Parameter) Name of the test suit to add.
|
||||||
|
*/
|
||||||
|
#define ADD_SUIT(suit_name) \
|
||||||
|
if (!count) \
|
||||||
|
{ \
|
||||||
|
suits[(*counter)] = suit_name##_suit_contructor(); \
|
||||||
|
} \
|
||||||
|
(*counter)++;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new test suit with local setup and tear_down function.
|
||||||
|
*
|
||||||
|
* suit_name (Parameter) Name of the suit. (Must be globally unique for suits.)
|
||||||
|
* setup_func (SUIT_SETUP) Suit setup function name. (Write name EMPTY if no
|
||||||
|
* setup is required.) tear_down_func (SUIT_TEAR_DOWN) Suit tear down function
|
||||||
|
* name. (Write name EMPTY if no tear down is required)
|
||||||
|
*
|
||||||
|
* See also: SUIT_SETUP()
|
||||||
|
* See also: SUIT_TEAR_DOWN()
|
||||||
|
*
|
||||||
|
* Technical Note: We manually allocate suits via mmap to be able to mark them
|
||||||
|
* DONTFORK. Since we don't need the suits in a test fork.
|
||||||
|
*/
|
||||||
|
#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() \
|
||||||
|
{ \
|
||||||
|
struct SETSuit *suit = (struct SETSuit *)mmap( \
|
||||||
|
NULL, sizeof(struct SETSuit), PROT_READ | PROT_WRITE, \
|
||||||
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
|
||||||
|
madvise(suit, sizeof(struct SETSuit), MADV_DONTFORK); \
|
||||||
|
suit->len = 0; \
|
||||||
|
suit_name##_suit(suit, true); \
|
||||||
|
suit->shm_key = \
|
||||||
|
create_shared_suit_space(suit->len * sizeof(struct SETest)); \
|
||||||
|
suit->name = #suit_name; \
|
||||||
|
suit->tests = (struct SETest *)shmat(suit->shm_key, 0, 0); \
|
||||||
|
if ((uint64_t)suit->tests == -1) \
|
||||||
|
{ \
|
||||||
|
fprintf(stderr, "Couldn't attach suit space.\n"); \
|
||||||
|
perror("shmat"); \
|
||||||
|
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)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suit local setup function. If registered in suit it get's executed before the
|
||||||
|
* suit starts the first test.
|
||||||
|
*
|
||||||
|
* suit_name (Parameter) Name of the suit setup function. (Must be globally
|
||||||
|
* unique for suit setup functions!)
|
||||||
|
*/
|
||||||
|
#define SUIT_SETUP(suit_name) bool suit_name##_suit_setup()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suit local tear down function. If registered in suit it get's executed after
|
||||||
|
* the last test was executed.
|
||||||
|
*
|
||||||
|
* suit_name (Parameter) Name of the suit setup function. (Must be globally
|
||||||
|
* unique for suit tear down functions!)
|
||||||
|
*/
|
||||||
|
#define SUIT_TEAR_DOWN(suit_name) bool suit_name##_suit_tear_down()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suit without setup or tear down.
|
||||||
|
*
|
||||||
|
* suit_name (Parameter) Name of the suit. (Must be globally unique for suits!)
|
||||||
|
*/
|
||||||
|
#define SUIT(suit_name) SUIT_ST(suit_name, EMPTY, EMPTY)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a test with a test suit.
|
||||||
|
*
|
||||||
|
* test_name (Parameter) Name of the test.
|
||||||
|
*
|
||||||
|
* See also: TEST()
|
||||||
|
*/
|
||||||
|
#define ADD_TEST(test_name) \
|
||||||
|
if (!count) \
|
||||||
|
{ \
|
||||||
|
test_name##_test_constructor(&suit->tests[suit->len]); \
|
||||||
|
} \
|
||||||
|
suit->len++;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function. If registered in suit it gets executed be that suit.
|
||||||
|
*
|
||||||
|
* test_name (Parameter) Name of the test (Must be globally unique for tests!)
|
||||||
|
*
|
||||||
|
* See also: set_asserts.h
|
||||||
|
*/
|
||||||
|
#define TEST(test_name) \
|
||||||
|
void test_name##_test(struct SETest *test); \
|
||||||
|
void test_name##_test_constructor(struct SETest *test) \
|
||||||
|
{ \
|
||||||
|
test->name = #test_name; \
|
||||||
|
test->function = &test_name##_test; \
|
||||||
|
test->passed = true; \
|
||||||
|
test->error_msg = NULL; \
|
||||||
|
} \
|
||||||
|
void test_name##_test(struct SETest *test)
|
||||||
|
|
||||||
|
#endif // !INCLUDE_SET_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
156
include/set_asserts.h
Normal file
156
include/set_asserts.h
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "set.h"
|
||||||
|
|
||||||
|
#ifndef INCLUDE_SET_ASSERT_H
|
||||||
|
#define INCLUDE_SET_ASSERT_H
|
||||||
|
|
||||||
|
#define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))])
|
||||||
|
|
||||||
|
#define ASSERT_EQ(exp, act) \
|
||||||
|
if (exp != act) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string("Expected %d to be %d\n.", act, exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ(exp, act) \
|
||||||
|
if (exp == act) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = \
|
||||||
|
format_string("Expect %d not to be %d.\n", act, exp) return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_EQ_MSG(exp, act, msg) \
|
||||||
|
if (exp != act) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected %d to be %d\n Failed with message: " msg, act, exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ_MSG(exp, act, msg) \
|
||||||
|
if (exp == act) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected %d not to be %d\n Failed with message: " msg, act, \
|
||||||
|
exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_TRUE(x) \
|
||||||
|
if (!x) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string("Expected true got false."); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_TRUE_MSG(x, msg) \
|
||||||
|
if (!x) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected true got false.\n Failed with message: " msg); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_FALSE(x) \
|
||||||
|
if (x) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string("Expected false got true."); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_FALSE_MSG(x, msg) \
|
||||||
|
if (x) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected false got true.\n Failed with message: " msg); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_EQ_FLOAT(exp, act, epsilon) \
|
||||||
|
if (fabs(exp - act) <= epsilon) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = \
|
||||||
|
format_string("Expected %f to be %f (e: %f)", act, exp, epsilon); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_EQ_FLOAT_MSG(exp, act, epsilon, msg) \
|
||||||
|
if (fabs(exp - act) <= epsilon) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected %f to be %f (e: %f)\n Failed with message: " msg, \
|
||||||
|
act, exp, epsilon); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ_FLOAT(exp, act, epsilon) \
|
||||||
|
if (fabs(exp - act) > epsilon) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string("Expected %f not to be %f (e: %f)", \
|
||||||
|
act, exp, epsilon); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ_FLOAT_MSG(exp, act, epsilon, msg) \
|
||||||
|
if (fabs(exp - act) > epsilon) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
test->error_msg = format_string( \
|
||||||
|
"Expected %f not to be %f (e: %f)\n Failed with message: " msg, \
|
||||||
|
act, exp, epsilon); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_EQ_STR(exp, act) \
|
||||||
|
if (strcmp(exp, act) != 0) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
type->error_msg = \
|
||||||
|
format_string("Expected: %d\nBut got: %d", act, exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_EQ_STR_MSG(exp, act, msg) \
|
||||||
|
if (strcmp(exp, act) != 0) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
type->error_msg = format_string( \
|
||||||
|
"Expected: %d\nBut got: %d\n Failed with message: " msg, act, \
|
||||||
|
exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ_STR(exp, act) \
|
||||||
|
if (strcmp(exp, act) == 0) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
type->error_msg = \
|
||||||
|
format_string("Expected not: %d\nBut got: %d", act, exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ASSERT_NEQ_STR_MSG(exp, act, msg) \
|
||||||
|
if (strcmp(exp, act) == 0) \
|
||||||
|
{ \
|
||||||
|
test->passed = false; \
|
||||||
|
type->error_msg = format_string( \
|
||||||
|
"Expected not: %d\nBut got: %d\n Failed with message: " msg, \
|
||||||
|
act, exp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
59
include/utils.h
Normal file
59
include/utils.h
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#ifndef INCLUDE_SET_UTILS_H_
|
||||||
|
#define INCLUDE_SET_UTILS_H_
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns pointer to formatted the string.
|
||||||
|
*
|
||||||
|
* fmt (const char*) Formatter string.
|
||||||
|
* ... (args) Arguments.
|
||||||
|
*
|
||||||
|
* Note: The string is dynamically allocated and must be freed.
|
||||||
|
*/
|
||||||
|
char *format_string(const char *fmt, ...);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal function to create shared memory for all tests in a suit.
|
||||||
|
*
|
||||||
|
* size (size_t) Size of space in bytes.
|
||||||
|
*/
|
||||||
|
int create_shared_suit_space(size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for malloc function that notes down the allocated blocks. All
|
||||||
|
* blocks allocated with this function get freed after test execution.
|
||||||
|
* This function should only be used if the object might still be in memory
|
||||||
|
* once a test fails. Manual allocation should be preferred.
|
||||||
|
*
|
||||||
|
* n (size_t) Amount of bytes to allocate.
|
||||||
|
*/
|
||||||
|
void *set_malloc(size_t n);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for fee(void*).
|
||||||
|
*
|
||||||
|
* See also: set_malloc()
|
||||||
|
*/
|
||||||
|
void set_free(void *address);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal function to free all allocated and registered memory.
|
||||||
|
*/
|
||||||
|
void set_free_all();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for calloc.
|
||||||
|
*
|
||||||
|
* See also: set_malloc()
|
||||||
|
*/
|
||||||
|
void *set_calloc(size_t n, size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for realloc().
|
||||||
|
*
|
||||||
|
* See also: set_malloc()
|
||||||
|
*/
|
||||||
|
void *set_realloc(size_t n);
|
||||||
|
|
||||||
|
#endif
|
117
set.h
117
set.h
@@ -1,117 +0,0 @@
|
|||||||
|
|
||||||
#define _GNU_SOURCE
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <sys/shm.h>
|
|
||||||
|
|
||||||
#ifndef INCLUDE_SET_H
|
|
||||||
#define INCLUDE_SET_H
|
|
||||||
|
|
||||||
#define SET_MAX_ERROR_MSG_SIZE 256
|
|
||||||
#define SET_MAX_NAME_SIZE 64
|
|
||||||
|
|
||||||
struct SETest
|
|
||||||
{
|
|
||||||
void (*function)(struct SETest *test);
|
|
||||||
const char *error_msg;
|
|
||||||
const char *name;
|
|
||||||
bool passed;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SETSuit
|
|
||||||
{
|
|
||||||
const char *name;
|
|
||||||
struct SETest *tests;
|
|
||||||
int len;
|
|
||||||
int shm_key;
|
|
||||||
bool (*setup)();
|
|
||||||
bool (*tear_down)();
|
|
||||||
bool passed;
|
|
||||||
};
|
|
||||||
|
|
||||||
char *format_string(const char *fmt, ...);
|
|
||||||
|
|
||||||
// To be implemented by user.
|
|
||||||
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
|
|
||||||
|
|
||||||
int create_shared_suit_space(size_t size);
|
|
||||||
|
|
||||||
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()
|
|
||||||
#define TEAR_DOWN() bool tear_down()
|
|
||||||
|
|
||||||
#define BUNDLE() \
|
|
||||||
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count)
|
|
||||||
|
|
||||||
#define ADD_SUIT(suit_name) \
|
|
||||||
if (!count) \
|
|
||||||
{ \
|
|
||||||
suits[(*counter)] = suit_name##_suit_contructor(); \
|
|
||||||
} \
|
|
||||||
(*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_ST(suit_name, setup_func, tear_down_func) \
|
|
||||||
void suit_name##_suit(struct SETSuit *suit, bool count); \
|
|
||||||
struct SETSuit *suit_name##_suit_contructor() \
|
|
||||||
{ \
|
|
||||||
struct SETSuit *suit = \
|
|
||||||
mmap(NULL, sizeof(struct SETSuit), PROT_READ | PROT_WRITE, \
|
|
||||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
|
|
||||||
madvise(suit, sizeof(struct SETSuit), MADV_DONTFORK); \
|
|
||||||
suit->len = 0; \
|
|
||||||
suit_name##_suit(suit, true); \
|
|
||||||
suit->shm_key = \
|
|
||||||
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->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) \
|
|
||||||
{ \
|
|
||||||
test_name##_test_constructor(&suit->tests[suit->len]); \
|
|
||||||
} \
|
|
||||||
suit->len++;
|
|
||||||
|
|
||||||
// TEST MACRO
|
|
||||||
#define TEST(test_name) \
|
|
||||||
void test_name##_test(struct SETest *test); \
|
|
||||||
void test_name##_test_constructor(struct SETest *test) \
|
|
||||||
{ \
|
|
||||||
test->name = #test_name; \
|
|
||||||
test->function = &test_name##_test; \
|
|
||||||
test->passed = true; \
|
|
||||||
test->error_msg = NULL; \
|
|
||||||
} \
|
|
||||||
void test_name##_test(struct SETest *test)
|
|
||||||
|
|
||||||
#endif // !INCLUDE_SET_H
|
|
@@ -1,88 +0,0 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "set.h"
|
|
||||||
|
|
||||||
#ifndef ASSERT_H
|
|
||||||
#define ASSERT_H
|
|
||||||
|
|
||||||
#define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))])
|
|
||||||
|
|
||||||
#define ASSERT_EQ(exp, act) \
|
|
||||||
if (exp != act) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = format_string("Expected %d to be %d\n.", act, exp); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_NEQ(exp, act) \
|
|
||||||
if (exp == act) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = \
|
|
||||||
format_string("Expect %d to be %d.\n", act, exp) return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_EQ_MSG(exp, act, msg) \
|
|
||||||
if (exp != act) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = format_string( \
|
|
||||||
"Expected %d to be %d\n Failed with message: " msg, act, exp); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_TRUE(x) \
|
|
||||||
if (!x) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = format_string("Expected true got false."); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_FALSE(x) \
|
|
||||||
if (x) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = format_string("Expected false got true."); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_EQ_FLOAT(exp, act, epsilon) \
|
|
||||||
if (fabs(exp - act) <= epsilon) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = \
|
|
||||||
format_string("Expected %f to be %f (e: %f)", act, exp, epsilon); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_NEQ_FLOAT(exp, act, epsilon) \
|
|
||||||
if (fabs(exp - act) > epsilon) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
test->error_msg = \
|
|
||||||
format_string("Expected %f to be %f (e: %f)", act, exp, epsilon); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_EQ_STR(exp, act) \
|
|
||||||
if (strcmp(exp, act) != 0) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
type->error_msg = \
|
|
||||||
format_string("Expected: %d\nBut got: %d", act, exp); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT_NEQ_STR(exp, act) \
|
|
||||||
if (strcmp(exp, act) == 0) \
|
|
||||||
{ \
|
|
||||||
test->passed = false; \
|
|
||||||
type->error_msg = \
|
|
||||||
format_string("Expected: %d\nBut got: %d", act, exp); \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
52
src/list.c
Normal file
52
src/list.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next)
|
||||||
|
{
|
||||||
|
while (head->next)
|
||||||
|
head = head->next;
|
||||||
|
|
||||||
|
head->next = next;
|
||||||
|
next->prev = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_ll_free_all(struct SETBlockMeta *head)
|
||||||
|
{
|
||||||
|
while (head)
|
||||||
|
{
|
||||||
|
void *block_start = head;
|
||||||
|
head = head->next;
|
||||||
|
free(block_start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
|
||||||
|
{
|
||||||
|
struct SETBlockMeta *meta =
|
||||||
|
(struct SETBlockMeta *)(address - (sizeof(struct SETBlockMeta)));
|
||||||
|
|
||||||
|
if (meta->prev == NULL)
|
||||||
|
{
|
||||||
|
struct SETBlockMeta *ret = meta->next;
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
ret->prev = NULL;
|
||||||
|
ret->end = meta->end;
|
||||||
|
}
|
||||||
|
free(meta);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
meta->prev->next = meta->next;
|
||||||
|
|
||||||
|
if (meta->next != NULL)
|
||||||
|
meta->next->prev = meta->prev;
|
||||||
|
else
|
||||||
|
head->end = meta->prev;
|
||||||
|
|
||||||
|
free(meta);
|
||||||
|
|
||||||
|
return head;
|
||||||
|
}
|
@@ -2,7 +2,6 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -56,41 +55,6 @@
|
|||||||
""
|
""
|
||||||
//clang-format on
|
//clang-format on
|
||||||
|
|
||||||
char *format_string(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
size_t size = vsnprintf(NULL, 0, fmt, args);
|
|
||||||
char *out = (char *)malloc(size + 1);
|
|
||||||
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
va_start(args, fmt);
|
|
||||||
vsnprintf(out, size + 1, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
int create_shared_suit_space(size_t size)
|
|
||||||
{
|
|
||||||
int suit_space_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
|
||||||
|
|
||||||
if (suit_space_id == -1)
|
|
||||||
{
|
|
||||||
fprintf(stdout, "Couldn't create suite space of size: %lu\n", size);
|
|
||||||
perror("shmget");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return suit_space_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void log_test_summary(struct SETest *test)
|
static void log_test_summary(struct SETest *test)
|
||||||
{
|
{
|
||||||
@@ -122,6 +86,7 @@ static void dispatch_single_test(struct SETest *test)
|
|||||||
{
|
{
|
||||||
test->function(test);
|
test->function(test);
|
||||||
log_test_summary(test);
|
log_test_summary(test);
|
||||||
|
set_free_all();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
79
src/utils.c
Normal file
79
src/utils.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
struct SETBlockMeta *block_meta_head = NULL;
|
||||||
|
|
||||||
|
char *format_string(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
size_t size = vsnprintf(NULL, 0, fmt, args);
|
||||||
|
char *out = (char *)set_malloc(size + 1);
|
||||||
|
|
||||||
|
if (!out)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
vsnprintf(out, size + 1, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
int create_shared_suit_space(size_t size)
|
||||||
|
{
|
||||||
|
int suit_space_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
||||||
|
|
||||||
|
if (suit_space_id == -1)
|
||||||
|
{
|
||||||
|
fprintf(stdout, "Couldn't create suite space of size: %lu\n", size);
|
||||||
|
perror("shmget");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return suit_space_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *set_malloc(size_t n)
|
||||||
|
{
|
||||||
|
void *blocks = malloc(n + sizeof(struct SETBlockMeta));
|
||||||
|
struct SETBlockMeta *meta = (struct SETBlockMeta *)blocks;
|
||||||
|
meta->next = NULL;
|
||||||
|
meta->prev = NULL;
|
||||||
|
|
||||||
|
if (block_meta_head == NULL)
|
||||||
|
{
|
||||||
|
meta->end = meta;
|
||||||
|
block_meta_head = meta;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
meta->end = NULL;
|
||||||
|
set_ll_append(block_meta_head->end, meta);
|
||||||
|
block_meta_head->end = meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
return blocks + sizeof(struct SETBlockMeta);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_free(void *address)
|
||||||
|
{
|
||||||
|
block_meta_head = set_ll_free_one(block_meta_head, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_free_all()
|
||||||
|
{
|
||||||
|
set_ll_free_all(block_meta_head);
|
||||||
|
block_meta_head = NULL;
|
||||||
|
}
|
44
testtest.c
44
testtest.c
@@ -1,5 +1,6 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "set_asserts.h"
|
#include "set_asserts.h"
|
||||||
@@ -18,12 +19,7 @@ SETUP()
|
|||||||
printf("Setting up.\n");
|
printf("Setting up.\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
NO_TEAR_DOWN
|
||||||
TEAR_DOWN()
|
|
||||||
{
|
|
||||||
printf("Ending execution.");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(Faculty_Basic)
|
TEST(Faculty_Basic)
|
||||||
{
|
{
|
||||||
@@ -36,21 +32,22 @@ TEST(Faculty_Negative)
|
|||||||
{
|
{
|
||||||
ASSERT_EQ(fac(-2), 1);
|
ASSERT_EQ(fac(-2), 1);
|
||||||
ASSERT_EQ(fac(0), 1);
|
ASSERT_EQ(fac(0), 1);
|
||||||
|
ASSERT_TRUE_MSG(false, "Hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
SUIT_SETUP(Basic)
|
SUIT_SETUP(Basic_Setup)
|
||||||
{
|
{
|
||||||
fprintf(stdout, "Some suit setup.\n");
|
fprintf(stdout, "Some suit setup.\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUIT_TEAR_DOWN(Basic)
|
SUIT_TEAR_DOWN(Basic_Tear_Down)
|
||||||
{
|
{
|
||||||
fprintf(stdout, "Some suit teardown\n");
|
fprintf(stdout, "Some suit teardown\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUIT_ST(Basic, Basic, Basic)
|
SUIT_ST(Basic, Basic_Setup, Basic_Tear_Down)
|
||||||
{
|
{
|
||||||
ADD_TEST(Faculty_Basic);
|
ADD_TEST(Faculty_Basic);
|
||||||
ADD_TEST(Faculty_Negative);
|
ADD_TEST(Faculty_Negative);
|
||||||
@@ -62,7 +59,34 @@ TEST(Other_Basic)
|
|||||||
ASSERT_FALSE(false);
|
ASSERT_FALSE(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SUIT_ST(Other, Basic, EMPTY) { ADD_TEST(Other_Basic); }
|
TEST(Other_With_Malloc)
|
||||||
|
{
|
||||||
|
int *some_array = set_malloc(20 * sizeof(int));
|
||||||
|
int *array = set_malloc(20);
|
||||||
|
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
some_array[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
fprintf(stdout, "%d ", some_array[i]);
|
||||||
|
}
|
||||||
|
fprintf(stdout, ".\n");
|
||||||
|
|
||||||
|
set_free(array);
|
||||||
|
set_free(some_array);
|
||||||
|
|
||||||
|
void *some_other = set_malloc(15);
|
||||||
|
ASSERT_TRUE(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
SUIT_ST(Other, Basic_Setup, EMPTY)
|
||||||
|
{
|
||||||
|
ADD_TEST(Other_Basic);
|
||||||
|
ADD_TEST(Other_With_Malloc);
|
||||||
|
}
|
||||||
|
|
||||||
BUNDLE()
|
BUNDLE()
|
||||||
{
|
{
|
||||||
|
@@ -1,7 +1,11 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
#include "set_asserts.h"
|
#include "set_asserts.h"
|
||||||
|
|
||||||
TEST(Basic) { ASSERT_EQ(2, 2); }
|
NO_SETUP;
|
||||||
|
|
||||||
|
NO_TEAR_DOWN;
|
||||||
|
|
||||||
|
TEST(Basic) { ASSERT_EQ(1, 1); }
|
||||||
|
|
||||||
SUIT(Basic) { ADD_TEST(Basic); }
|
SUIT(Basic) { ADD_TEST(Basic); }
|
||||||
|
|
Reference in New Issue
Block a user