Compare commits

..

4 Commits

8 changed files with 76 additions and 186 deletions

View File

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

View File

@@ -1,88 +1,79 @@
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
#ifdef __cplusplus #define _GNU_SOURCE
extern "C"
{
#endif
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/shm.h> #include <sys/shm.h>
#ifdef __cplusplus
#include <cstdio>
#endif
#include "utils.h" #include "utils.h"
#ifndef INCLUDE_SET_H #ifndef INCLUDE_SET_H
#define INCLUDE_SET_H #define INCLUDE_SET_H
/** #define SET_MAX_ERROR_MSG_SIZE 256
* Meta data for one test function. #define SET_MAX_NAME_SIZE 64
*
* 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. * Meta data for one test function.
* *
* See also: struct SETest * See also: struct SETSuit
*/ */
struct SETSuit struct SETest
{ {
const char *name; void (*function)(struct SETest *test);
struct SETest *tests; const char *error_msg;
int len; const char *name;
int shm_key; bool passed;
bool (*setup)(); };
bool (*tear_down)();
bool passed;
};
/** /**
* Internal function header for bundle. Use BUNDLE() macro to define bundle * Meta data for one test suit.
* in test. *
*/ * See also: struct SETest
void set_bundle_suits(struct SETSuit **suits, int *counter, bool count); */
struct SETSuit
{
const char *name;
struct SETest *tests;
int len;
int shm_key;
bool (*setup)();
bool (*tear_down)();
bool passed;
};
/** /**
* Empty suit_setup. Resolving EMPTY in a suit constructor gets * Internal function header for bundle. Use BUNDLE() macro to define bundle in
* us here. * test.
* */
* Note: Use EMPTY instead of this function. void set_bundle_suits(struct SETSuit **suits, int *counter, bool count);
*/
static bool EMPTY_suit_setup() { return true; }
/** /**
* Empty suit_tear_down. Resolving EMPTY in a suit constructor gets * Empty suit_setup. Resolving EMPTY in a suit constructor gets
* us here. * us here.
* *
* Note: Use EMPTY instead of this function. * Note: Use EMPTY instead of this function.
*/ */
static bool EMPTY_suit_tear_down() { return true; } static bool EMPTY_suit_setup() { return true; }
/** /**
* Internal header for global setup. Use SETUP() macro instead. * Empty suit_tear_down. Resolving EMPTY in a suit constructor gets
*/ * us here.
bool set_up(); *
* Note: Use EMPTY instead of this function.
*/
static bool EMPTY_suit_tear_down() { return true; }
/** /**
* Internal header for global tear down. Use TEAR_DOWN() macro instead. * Internal header for global setup. Use SETUP() macro instead.
*/ */
bool tear_down(); bool set_up();
/**
* Internal header for global tear down. Use TEAR_DOWN() macro instead.
*/
bool tear_down();
/** /**
* Global setup function. * Global setup function.
@@ -169,17 +160,17 @@ extern "C"
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 *)mmap( \ struct SETSuit *suit = \
NULL, sizeof(struct SETSuit), PROT_READ | PROT_WRITE, \ mmap(NULL, sizeof(struct SETSuit), PROT_READ | PROT_WRITE, \
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
madvise(suit, sizeof(struct SETSuit), MADV_DONTFORK); \ madvise(suit, sizeof(struct SETSuit), MADV_DONTFORK); \
suit->len = 0; \ suit->len = 0; \
suit_name##_suit(suit, true); \ suit_name##_suit(suit, true); \
suit->shm_key = \ suit->shm_key = \
create_shared_suit_space(suit->len * sizeof(struct SETest)); \ create_shared_suit_space(suit->len * sizeof(struct SETest)); \
suit->name = #suit_name; \ suit->name = #suit_name; \
suit->tests = (struct SETest *)shmat(suit->shm_key, 0, 0); \ suit->tests = shmat(suit->shm_key, 0, 0); \
if ((uint64_t)suit->tests == -1) \ if (suit->tests == (struct SETest *)-1) \
{ \ { \
fprintf(stderr, "Couldn't attach suit space.\n"); \ fprintf(stderr, "Couldn't attach suit space.\n"); \
perror("shmat"); \ perror("shmat"); \
@@ -251,7 +242,3 @@ extern "C"
void test_name##_test(struct SETest *test) void test_name##_test(struct SETest *test)
#endif // !INCLUDE_SET_H #endif // !INCLUDE_SET_H
#ifdef __cplusplus
}
#endif

View File

@@ -3,8 +3,8 @@
#include "set.h" #include "set.h"
#ifndef INCLUDE_SET_ASSERT_H #ifndef ASSERT_H
#define INCLUDE_SET_ASSERT_H #define ASSERT_H
#define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))]) #define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))])
@@ -21,25 +21,16 @@
{ \ { \
test->passed = false; \ test->passed = false; \
test->error_msg = \ test->error_msg = \
format_string("Expect %d not 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) \
if (exp != act) \ if (exp != act) \
{ \ { \
test->passed = false; \ test->passed = false; \
test->error_msg = format_string( \ test->error_msg = \
"Expected %d to be %d\n Failed with message: " msg, act, exp); \ format_string("Expected %d to be %d\n Failed with message: %s", \
return; \ act, exp, msg); \
}
#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; \ return; \
} }
@@ -51,15 +42,6 @@
return; \ 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) \ #define ASSERT_FALSE(x) \
if (x) \ if (x) \
{ \ { \
@@ -68,15 +50,6 @@
return; \ 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) \ #define ASSERT_EQ_FLOAT(exp, act, epsilon) \
if (fabs(exp - act) <= epsilon) \ if (fabs(exp - act) <= epsilon) \
{ \ { \
@@ -86,32 +59,12 @@
return; \ 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) \ #define ASSERT_NEQ_FLOAT(exp, act, epsilon) \
if (fabs(exp - act) > epsilon) \ if (fabs(exp - act) > epsilon) \
{ \ { \
test->passed = false; \ test->passed = false; \
test->error_msg = format_string("Expected %f not to be %f (e: %f)", \ test->error_msg = \
act, exp, epsilon); \ format_string("Expected %f 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; \ return; \
} }
@@ -124,32 +77,12 @@
return; \ 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) \ #define ASSERT_NEQ_STR(exp, act) \
if (strcmp(exp, act) == 0) \ if (strcmp(exp, act) == 0) \
{ \ { \
test->passed = false; \ test->passed = false; \
type->error_msg = \ type->error_msg = \
format_string("Expected not: %d\nBut got: %d", act, exp); \ format_string("Expected: %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; \ return; \
} }

View File

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

View File

@@ -1,6 +1,5 @@
#include "list.h" #include "list.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next) void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next)
@@ -24,10 +23,9 @@ void set_ll_free_all(struct SETBlockMeta *head)
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address) struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
{ {
struct SETBlockMeta *meta = struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta));
(struct SETBlockMeta *)(address - (sizeof(struct SETBlockMeta)));
if (meta->prev == NULL) if (meta == head)
{ {
struct SETBlockMeta *ret = meta->next; struct SETBlockMeta *ret = meta->next;
if (ret) if (ret)
@@ -40,11 +38,8 @@ struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
} }
meta->prev->next = meta->next; meta->prev->next = meta->next;
if (meta->next)
if (meta->next != NULL)
meta->next->prev = meta->prev; meta->next->prev = meta->prev;
else
head->end = meta->prev;
free(meta); free(meta);

View File

@@ -48,9 +48,8 @@ int create_shared_suit_space(size_t size)
void *set_malloc(size_t n) void *set_malloc(size_t n)
{ {
void *blocks = malloc(n + sizeof(struct SETBlockMeta)); void *blocks = malloc(n + sizeof(struct SETBlockMeta));
struct SETBlockMeta *meta = (struct SETBlockMeta *)blocks; struct SETBlockMeta *meta = blocks;
meta->next = NULL; meta->next = NULL;
meta->prev = NULL;
if (block_meta_head == NULL) if (block_meta_head == NULL)
{ {

View File

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

View File

@@ -1,12 +0,0 @@
#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); }