Compare commits

..

9 Commits

Author SHA1 Message Date
f4a359b871 chore: Adding macros with message for all ASSERTS. 2025-07-09 22:38:25 +02:00
ba65e6c433 amend: Removing debug prints. 2025-06-30 19:47:13 +02:00
f3ca4cdd72 fix: Pointer arithmetic error during block start calculation. 2025-06-30 19:45:06 +02:00
84ba41e180 fix: Adding Header guards. 2025-06-29 15:07:31 +02:00
1aba159f8f fix: More C++ support. 2025-06-29 02:52:53 +02:00
1401e1d70b fix: Adding casts for C++ and optimizing pointer comparison. 2025-06-29 02:33:07 +02:00
3f5cbdd276 fix: Removing debug prints. 2025-06-26 09:36:36 +02:00
5c08d33f22 fix: set_free. 2025-06-26 09:33:25 +02:00
ed41c04209 feature (#1)
This feature adds the following functions:

 - set_malloc()
 - set_free()

Which is a safe malloc wrapper, that frees all memory after a test has finished.

Reviewed-on: #1
2025-06-25 23:17:02 +00:00
11 changed files with 198 additions and 142 deletions

View File

@@ -1,41 +0,0 @@
ifndef verbose
SILENT = @
endif
CC := gcc
AR := ar
CFLAGS := -I include/
ifeq ($(colorized), y)
CFLAGS += -D COLORIZED
endif
ifeq ($(no_banner), y)
CFLAGS += -D NO_BANNER
endif
ifeq ($(testing), y)
CFLAGS += -g2 -O0
else
CFLAGS += -O2
endif
all: create_output_dir libset.a
clean:
$(SILENT) rm -rf int/
$(SILENT) rm -f libset.a
create_output_dir:
$(SILENT) mkdir -p int/
libset.a: $(patsubst src/%.c, int/%.o, $(wildcard src/*.c))
@echo "Archiving $^ to $@"
$(SILENT) $(AR) rcs $@ int/**.o
# Recipe
int/%.o: src/%.c
@echo "Building $< => $@"
$(SILENT) $(CC) $(CFLAGS) -c $< -o $@
.PHONY: all clean test create_output_dir

View File

@@ -2,33 +2,11 @@
Tiny C testing framework.
## Build
To build the library simply call `make` in the repository root.
Alternatively it is possible to directly call the compiler with:
gcc -c src/*.c
ar rcs libset.a *.o
rm *.o # Clean up
### Options
SET comes with a couple of build options that might come in handy in different scenarios.
- no_banner: Do not print banner every time the tests are run.
make no_banner=y
- colorized: Build with colorized output.
make colorized=y
## Building a test.
To build a test, first build the library. Then link it to the `test.c` via:
gcc -static test.c -L . -lset -o test
gcc src/*.c test.c -o test
## LICENSE

View File

@@ -1,3 +1,6 @@
#ifndef INCLUDE_SET_LIST_
#define INCLUDE_SET_LIST_
#include <stddef.h>
struct SETBlockMeta
@@ -31,3 +34,5 @@ void set_ll_free_all(struct SETBlockMeta *head);
*/
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address);
#endif

View File

@@ -1,39 +1,48 @@
#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
#define SET_MAX_ERROR_MSG_SIZE 256
#define SET_MAX_NAME_SIZE 64
/**
/**
* Meta data for one test function.
*
* See also: struct SETSuit
*/
struct SETest
{
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
{
struct SETSuit
{
const char *name;
struct SETest *tests;
int len;
@@ -41,39 +50,39 @@ struct SETSuit
bool (*setup)();
bool (*tear_down)();
bool passed;
};
};
/**
* Internal function header for bundle. Use BUNDLE() macro to define bundle in
* test.
/**
* Internal function header for bundle. Use BUNDLE() macro to define bundle
* in test.
*/
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
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; }
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; }
static bool EMPTY_suit_tear_down() { return true; }
/**
/**
* Internal header for global setup. Use SETUP() macro instead.
*/
bool set_up();
bool set_up();
/**
/**
* Internal header for global tear down. Use TEAR_DOWN() macro instead.
*/
bool tear_down();
bool tear_down();
/**
* Global setup function.
@@ -160,8 +169,8 @@ bool tear_down();
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, \
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; \
@@ -169,8 +178,8 @@ bool tear_down();
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) \
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"); \
@@ -242,3 +251,7 @@ bool tear_down();
void test_name##_test(struct SETest *test)
#endif // !INCLUDE_SET_H
#ifdef __cplusplus
}
#endif

View File

@@ -3,8 +3,8 @@
#include "set.h"
#ifndef ASSERT_H
#define ASSERT_H
#ifndef INCLUDE_SET_ASSERT_H
#define INCLUDE_SET_ASSERT_H
#define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))])
@@ -21,7 +21,7 @@
{ \
test->passed = false; \
test->error_msg = \
format_string("Expect %d to be %d.\n", act, exp) return; \
format_string("Expect %d not to be %d.\n", act, exp) return; \
}
#define ASSERT_EQ_MSG(exp, act, msg) \
@@ -33,6 +33,16 @@
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) \
{ \
@@ -41,6 +51,15 @@
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) \
{ \
@@ -49,6 +68,15 @@
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) \
{ \
@@ -58,12 +86,32 @@
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 to be %f (e: %f)", act, exp, epsilon); \
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; \
}
@@ -76,12 +124,32 @@
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: %d\nBut got: %d", act, exp); \
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; \
}

View File

@@ -1,5 +1,8 @@
#include <stddef.h>
#ifndef INCLUDE_SET_UTILS_H_
#define INCLUDE_SET_UTILS_H_
/*
* Returns pointer to formatted the string.
*
@@ -52,3 +55,5 @@ void *set_calloc(size_t n, size_t size);
* See also: set_malloc()
*/
void *set_realloc(size_t n);
#endif

View File

@@ -1,5 +1,6 @@
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next)
@@ -23,20 +24,27 @@ void set_ll_free_all(struct SETBlockMeta *head)
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
{
struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta));
struct SETBlockMeta *meta =
(struct SETBlockMeta *)(address - (sizeof(struct SETBlockMeta)));
if (meta == head)
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)
if (meta->next != NULL)
meta->next->prev = meta->prev;
else
head->end = meta->prev;
free(meta);

View File

@@ -86,7 +86,7 @@ static void dispatch_single_test(struct SETest *test)
{
test->function(test);
log_test_summary(test);
// set_free_all();
set_free_all();
exit(0);
}
}

View File

@@ -15,7 +15,7 @@ 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);
char *out = (char *)set_malloc(size + 1);
if (!out)
{
@@ -48,12 +48,14 @@ int create_shared_suit_space(size_t size)
void *set_malloc(size_t n)
{
void *blocks = malloc(n + sizeof(struct SETBlockMeta));
struct SETBlockMeta *meta = blocks;
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
{

View File

@@ -32,6 +32,7 @@ TEST(Faculty_Negative)
{
ASSERT_EQ(fac(-2), 1);
ASSERT_EQ(fac(0), 1);
ASSERT_TRUE_MSG(false, "Hello");
}
SUIT_SETUP(Basic_Setup)
@@ -61,6 +62,7 @@ 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++)
{
@@ -73,7 +75,11 @@ TEST(Other_With_Malloc)
}
fprintf(stdout, ".\n");
ASSERT_TRUE(true);
set_free(array);
set_free(some_array);
void *some_other = set_malloc(15);
ASSERT_TRUE(false);
}
SUIT_ST(Other, Basic_Setup, EMPTY)

12
testtest.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include "set.h"
#include "set_asserts.h"
NO_SETUP;
NO_TEAR_DOWN;
TEST(Basic) { ASSERT_EQ(1, 1); }
SUIT(Basic) { ADD_TEST(Basic); }
BUNDLE() { ADD_SUIT(Basic); }