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
8 changed files with 186 additions and 76 deletions

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,19 +1,28 @@
#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.
*
@@ -44,8 +53,8 @@ struct SETSuit
};
/**
* 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);
@@ -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,16 +21,25 @@
{ \
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) \
if (exp != act) \
{ \
test->passed = false; \
test->error_msg = \
format_string("Expected %d to be %d\n Failed with message: %s", \
act, exp, msg); \
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; \
}
@@ -42,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) \
{ \
@@ -50,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) \
{ \
@@ -59,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; \
}
@@ -77,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,9 +24,10 @@ 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)
@@ -38,8 +40,11 @@ struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
}
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

@@ -48,8 +48,9 @@ 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)
{

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)
@@ -77,6 +78,7 @@ TEST(Other_With_Malloc)
set_free(array);
set_free(some_array);
void *some_other = set_malloc(15);
ASSERT_TRUE(false);
}

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); }