Compare commits
9 Commits
763781b92d
...
main
Author | SHA1 | Date | |
---|---|---|---|
f4a359b871
|
|||
ba65e6c433
|
|||
f3ca4cdd72
|
|||
84ba41e180
|
|||
1aba159f8f
|
|||
1401e1d70b
|
|||
3f5cbdd276
|
|||
5c08d33f22
|
|||
ed41c04209 |
@@ -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
|
||||
|
139
include/set.h
139
include/set.h
@@ -1,79 +1,88 @@
|
||||
|
||||
#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
|
||||
{
|
||||
void (*function)(struct SETest *test);
|
||||
const char *error_msg;
|
||||
const char *name;
|
||||
bool passed;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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_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; }
|
||||
|
||||
/**
|
||||
* 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 setup. Use SETUP() macro instead.
|
||||
*/
|
||||
bool set_up();
|
||||
|
||||
/**
|
||||
* Internal header for global tear down. Use TEAR_DOWN() macro instead.
|
||||
*/
|
||||
bool tear_down();
|
||||
/**
|
||||
* Internal header for global tear down. Use TEAR_DOWN() macro instead.
|
||||
*/
|
||||
bool tear_down();
|
||||
|
||||
/**
|
||||
* Global setup function.
|
||||
@@ -160,17 +169,17 @@ 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, \
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
|
||||
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 = 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
|
||||
|
@@ -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; \
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
11
src/list.c
11
src/list.c
@@ -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);
|
||||
|
||||
|
@@ -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)
|
||||
{
|
||||
|
@@ -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
12
testtest.cpp
Normal 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); }
|
Reference in New Issue
Block a user