This commit is contained in:
Gregory Burd 2024-03-15 16:24:08 -04:00
parent b01cd70160
commit 3b1a03c86f
6 changed files with 638 additions and 373 deletions

View file

@ -1,6 +1,7 @@
CFLAGS = -Wall -Wextra -Wpedantic -Og -g -std=c99 -Iinclude/ -fPIC
TEST_FLAGS = -Itests/ -fsanitize=address,undefined
TEST_FLAGS = -Itests/
#-fsanitize=address,undefined
OBJS = skiplist.o
STATIC_LIB = libskiplist.a
@ -31,6 +32,7 @@ tests/%.o: tests/%.c
test: $(TESTS)
./tests/test
# env LSAN_OPTIONS=verbosity=1:log_threads=1 ./tests/test
tests/test: tests/test.o tests/munit.o $(STATIC_LIB)
$(CC) $^ -o $@ $(CFLAGS) $(TEST_FLAGS) -pthread
@ -47,4 +49,4 @@ clean:
rm -f $(EXAMPLES)
format:
clang-format -i include/*.h src/*.c tests/*.c
clang-format -i include/*.h src/*.c tests/*.c tests/*.h

View file

@ -35,11 +35,13 @@ test_api_tear_down(void *fixture)
}
static void *
test_api_insert_setup(const MunitParameter params[], void *user_data) {
test_api_insert_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_insert_tear_down(void *fixture) {
test_api_insert_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -61,11 +63,13 @@ test_api_insert(const MunitParameter params[], void *data)
}
static void *
test_api_remove_setup(const MunitParameter params[], void *user_data) {
test_api_remove_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_remove_tear_down(void *fixture) {
test_api_remove_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -78,11 +82,13 @@ test_api_remove(const MunitParameter params[], void *data)
}
static void *
test_api_find_setup(const MunitParameter params[], void *user_data) {
test_api_find_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_find_tear_down(void *fixture) {
test_api_find_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -95,11 +101,13 @@ test_api_find(const MunitParameter params[], void *data)
}
static void *
test_api_update_setup(const MunitParameter params[], void *user_data) {
test_api_update_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_update_tear_down(void *fixture) {
test_api_update_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -112,11 +120,13 @@ test_api_update(const MunitParameter params[], void *data)
}
static void *
test_api_delete_setup(const MunitParameter params[], void *user_data) {
test_api_delete_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_delete_tear_down(void *fixture) {
test_api_delete_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -129,11 +139,13 @@ test_api_delete(const MunitParameter params[], void *data)
}
static void *
test_api_duplicates_setup(const MunitParameter params[], void *user_data) {
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_duplicates_tear_down(void *fixture) {
test_api_duplicates_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -146,11 +158,13 @@ test_api_duplicates(const MunitParameter params[], void *data)
}
static void *
test_api_size_setup(const MunitParameter params[], void *user_data) {
test_api_size_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_size_tear_down(void *fixture) {
test_api_size_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
@ -163,11 +177,13 @@ test_api_size(const MunitParameter params[], void *data)
}
static void *
test_api_iterators_setup(const MunitParameter params[], void *user_data) {
test_api_iterators_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_iterators_tear_down(void *fixture) {
test_api_iterators_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult

View file

@ -1,5 +0,0 @@
static MunitTest mt_tests[] = {
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

View file

@ -131,7 +131,8 @@ extern "C" {
#define MUNIT_UNUSED
#endif
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__PGI)
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
!defined(__PGI)
#define MUNIT_ARRAY_PARAM(name) name
#else
#define MUNIT_ARRAY_PARAM(name)
@ -162,7 +163,8 @@ extern "C" {
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
# define MUNIT_PUSH_DISABLE_MSVC_C4127_ __pragma(warning(push)) __pragma(warning(disable:4127))
#define MUNIT_PUSH_DISABLE_MSVC_C4127_ \
__pragma(warning(push)) __pragma(warning(disable : 4127))
#define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))
#else
#define MUNIT_PUSH_DISABLE_MSVC_C4127_
@ -177,19 +179,20 @@ typedef enum {
} MunitLogLevel;
#if defined(__GNUC__) && !defined(__MINGW32__)
# define MUNIT_PRINTF(string_index, first_to_check) __attribute__((format (printf, string_index, first_to_check)))
#define MUNIT_PRINTF(string_index, first_to_check) \
__attribute__((format(printf, string_index, first_to_check)))
#else
#define MUNIT_PRINTF(string_index, first_to_check)
#endif
MUNIT_PRINTF(4, 5)
void munit_logf_ex(MunitLogLevel level, const char* filename, int line, const char* format, ...);
void munit_logf_ex(MunitLogLevel level, const char *filename, int line,
const char *format, ...);
#define munit_logf(level, format, ...) \
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
#define munit_log(level, msg) \
munit_logf(level, "%s", msg)
#define munit_log(level, msg) munit_logf(level, "%s", msg)
MUNIT_NO_RETURN
MUNIT_PRINTF(3, 4)
@ -198,8 +201,7 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
#define munit_errorf(format, ...) \
munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
#define munit_error(msg) \
munit_errorf("%s", msg)
#define munit_error(msg) munit_errorf("%s", msg)
#define munit_assert(expr) \
do { \
@ -207,8 +209,7 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
munit_error("assertion failed: " #expr); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_true(expr) \
do { \
@ -216,8 +217,7 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
munit_error("assertion failed: " #expr " is not true"); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_false(expr) \
do { \
@ -225,38 +225,37 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
munit_error("assertion failed: " #expr " is not false"); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \
do { \
T munit_tmp_a_ = (a); \
T munit_tmp_b_ = (b); \
if (!(munit_tmp_a_ op munit_tmp_b_)) { \
munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix " %s " prefix "%" fmt suffix ")", \
munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix \
" %s " prefix "%" fmt suffix ")", \
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_type(T, fmt, a, op, b) \
munit_assert_type_full("", "", T, fmt, a, op, b)
#define munit_assert_char(a, op, b) \
munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, \
op, b)
#define munit_assert_uchar(a, op, b) \
munit_assert_type_full("'\\x", "'", unsigned char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
munit_assert_type_full("'\\x", "'", unsigned char, \
"02" MUNIT_CHAR_MODIFIER "x", a, op, b)
#define munit_assert_short(a, op, b) \
munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)
#define munit_assert_ushort(a, op, b) \
munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
#define munit_assert_int(a, op, b) \
munit_assert_type(int, "d", a, op, b)
#define munit_assert_int(a, op, b) munit_assert_type(int, "d", a, op, b)
#define munit_assert_uint(a, op, b) \
munit_assert_type(unsigned int, "u", a, op, b)
#define munit_assert_long(a, op, b) \
munit_assert_type(long int, "ld", a, op, b)
#define munit_assert_long(a, op, b) munit_assert_type(long int, "ld", a, op, b)
#define munit_assert_ulong(a, op, b) \
munit_assert_type(unsigned long int, "lu", a, op, b)
#define munit_assert_llong(a, op, b) \
@ -267,10 +266,8 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
#define munit_assert_size(a, op, b) \
munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)
#define munit_assert_float(a, op, b) \
munit_assert_type(float, "f", a, op, b)
#define munit_assert_double(a, op, b) \
munit_assert_type(double, "g", a, op, b)
#define munit_assert_float(a, op, b) munit_assert_type(float, "f", a, op, b)
#define munit_assert_double(a, op, b) munit_assert_type(double, "g", a, op, b)
#define munit_assert_ptr(a, op, b) \
munit_assert_type(const void *, "p", a, op, b)
@ -299,12 +296,12 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
-(munit_tmp_a_ - munit_tmp_b_) : \
(munit_tmp_a_ - munit_tmp_b_); \
if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \
munit_errorf("assertion failed: %s == %s (%0." #precision "g == %0." #precision "g)", \
munit_errorf("assertion failed: %s == %s (%0." #precision \
"g == %0." #precision "g)", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#include <string.h>
#define munit_assert_string_equal(a, b) \
@ -312,85 +309,79 @@ void munit_errorf_ex(const char* filename, int line, const char* format, ...);
const char *munit_tmp_a_ = a; \
const char *munit_tmp_b_ = b; \
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \
munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
munit_errorf( \
"assertion failed: string %s == %s (\"%s\" == \"%s\")", #a, \
#b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_string_not_equal(a, b) \
do { \
const char *munit_tmp_a_ = a; \
const char *munit_tmp_b_ = b; \
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \
munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
munit_errorf( \
"assertion failed: string %s != %s (\"%s\" == \"%s\")", #a, \
#b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_memory_equal(size, a, b) \
do { \
const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \
const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \
const size_t munit_tmp_size_ = (size); \
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != 0) { \
if (MUNIT_UNLIKELY( \
memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != 0) { \
size_t munit_tmp_pos_; \
for (munit_tmp_pos_ = 0 ; munit_tmp_pos_ < munit_tmp_size_ ; munit_tmp_pos_++) { \
if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \
munit_errorf("assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER "u", \
for (munit_tmp_pos_ = 0; munit_tmp_pos_ < munit_tmp_size_; \
munit_tmp_pos_++) { \
if (munit_tmp_a_[munit_tmp_pos_] != \
munit_tmp_b_[munit_tmp_pos_]) { \
munit_errorf( \
"assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER \
"u", \
#a, #b, munit_tmp_pos_); \
break; \
} \
} \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_memory_not_equal(size, a, b) \
do { \
const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \
const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \
const size_t munit_tmp_size_ = (size); \
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == 0) { \
munit_errorf("assertion failed: memory %s != %s (%zu bytes)", \
#a, #b, munit_tmp_size_); \
if (MUNIT_UNLIKELY( \
memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == 0) { \
munit_errorf("assertion failed: memory %s != %s (%zu bytes)", #a, \
#b, munit_tmp_size_); \
} \
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
} while (0) \
MUNIT_POP_DISABLE_MSVC_C4127_
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
#define munit_assert_ptr_equal(a, b) \
munit_assert_ptr(a, ==, b)
#define munit_assert_ptr_not_equal(a, b) \
munit_assert_ptr(a, !=, b)
#define munit_assert_null(ptr) \
munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_not_null(ptr) \
munit_assert_ptr(ptr, !=, NULL)
#define munit_assert_ptr_null(ptr) \
munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_ptr_not_null(ptr) \
munit_assert_ptr(ptr, !=, NULL)
#define munit_assert_ptr_equal(a, b) munit_assert_ptr(a, ==, b)
#define munit_assert_ptr_not_equal(a, b) munit_assert_ptr(a, !=, b)
#define munit_assert_null(ptr) munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
#define munit_assert_ptr_null(ptr) munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_ptr_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
/*** Memory allocation ***/
void *munit_malloc_ex(const char *filename, int line, size_t size);
#define munit_malloc(size) \
munit_malloc_ex(__FILE__, __LINE__, (size))
#define munit_malloc(size) munit_malloc_ex(__FILE__, __LINE__, (size))
#define munit_new(type) \
((type*) munit_malloc(sizeof(type)))
#define munit_new(type) ((type *)munit_malloc(sizeof(type)))
#define munit_calloc(nmemb, size) \
munit_malloc((nmemb) * (size))
#define munit_calloc(nmemb, size) munit_malloc((nmemb) * (size))
#define munit_newa(type, nmemb) \
((type*) munit_calloc((nmemb), sizeof(type)))
#define munit_newa(type, nmemb) ((type *)munit_calloc((nmemb), sizeof(type)))
/*** Random number generation ***/
@ -398,7 +389,8 @@ void munit_rand_seed(munit_uint32_t seed);
munit_uint32_t munit_rand_uint32(void);
int munit_rand_int_range(int min, int max);
double munit_rand_double(void);
void munit_rand_memory(size_t size, munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
void munit_rand_memory(size_t size,
munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
/*** Tests and Suites ***/
@ -425,7 +417,8 @@ typedef struct {
char *value;
} MunitParameter;
const char* munit_parameters_get(const MunitParameter params[], const char* key);
const char *munit_parameters_get(const MunitParameter params[],
const char *key);
typedef enum {
MUNIT_TEST_OPTION_NONE = 0,
@ -433,7 +426,8 @@ typedef enum {
MUNIT_TEST_OPTION_TODO = 1 << 1
} MunitTestOptions;
typedef MunitResult (* MunitTestFunc)(const MunitParameter params[], void* user_data_or_fixture);
typedef MunitResult (
*MunitTestFunc)(const MunitParameter params[], void *user_data_or_fixture);
typedef void *(*MunitTestSetup)(const MunitParameter params[], void *user_data);
typedef void (*MunitTestTearDown)(void *fixture);
@ -446,9 +440,7 @@ typedef struct {
MunitParameterEnum *parameters;
} MunitTest;
typedef enum {
MUNIT_SUITE_OPTION_NONE = 0
} MunitSuiteOptions;
typedef enum { MUNIT_SUITE_OPTION_NONE = 0 } MunitSuiteOptions;
typedef struct MunitSuite_ MunitSuite;
@ -460,7 +452,8 @@ struct MunitSuite_ {
MunitSuiteOptions options;
};
int munit_suite_main(const MunitSuite* suite, void* user_data, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
/* Note: I'm not very happy with this API; it's likely to change if I
* figure out something better. Suggestions welcome. */
@ -469,13 +462,13 @@ typedef struct MunitArgument_ MunitArgument;
struct MunitArgument_ {
char *name;
munit_bool (* parse_argument)(const MunitSuite* suite, void* user_data, int* arg, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
munit_bool (*parse_argument)(const MunitSuite *suite, void *user_data,
int *arg, int argc, char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
void (*write_help)(const MunitArgument *argument, void *user_data);
};
int munit_suite_main_custom(const MunitSuite* suite,
void* user_data,
int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)],
int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc,
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)],
const MunitArgument arguments[]);
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
@ -506,11 +499,13 @@ int munit_suite_main_custom(const MunitSuite* suite,
#define assert_int64(a, op, b) munit_assert_int64(a, op, b)
#define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
#define assert_double_equal(a, b, precision) munit_assert_double_equal(a, b, precision)
#define assert_double_equal(a, b, precision) \
munit_assert_double_equal(a, b, precision)
#define assert_string_equal(a, b) munit_assert_string_equal(a, b)
#define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
#define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
#define assert_memory_not_equal(size, a, b) munit_assert_memory_not_equal(size, a, b)
#define assert_memory_not_equal(size, a, b) \
munit_assert_memory_not_equal(size, a, b)
#define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)
#define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)
#define assert_ptr_null(ptr) munit_assert_null_equal(ptr)

View file

@ -1,5 +0,0 @@
static MunitTest scale_tests[] = {
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

View file

@ -7,8 +7,10 @@
* details.
*/
#define MUNIT_NO_FORK (1)
#define MUNIT_ENABLE_ASSERT_ALIASES (1)
#include <stdio.h>
#include <stdlib.h>
#define __SL_DEBUG 0
@ -119,7 +121,265 @@ uint32_key_cmp(sl_node *a, sl_node *b, void *aux)
return 0;
}
#include "api.c"
static void *
test_api_setup(const MunitParameter params[], void *user_data)
{
struct test_info *info = (struct test_info *)user_data;
(void)info;
(void)params;
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
if (slist == NULL)
return NULL;
sl_init(slist, uint32_key_cmp);
return (void *)(uintptr_t)slist;
}
static void
test_api_tear_down(void *fixture)
{
ex_sl_t *slist = (ex_sl_t *)fixture;
assert_ptr_not_null(slist);
sl_node *cursor = sl_begin(slist);
while (cursor) {
assert_ptr_not_null(cursor);
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
assert_ptr_not_null(entry);
assert_uint32(entry->key, ==, entry->value);
cursor = sl_next(slist, cursor);
sl_erase_node(slist, &entry->snode);
sl_release_node(&entry->snode);
sl_wait_for_free(&entry->snode);
sl_free_node(&entry->snode);
free(entry);
}
sl_free(slist);
free(fixture);
}
static size_t
__populate_slist(ex_sl_t *slist){
size_t inserted = 0;
uint32_t n, key;
ex_node_t *node;
n = munit_rand_int_range(1024, 4196);
while (n--) {
key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
if (node == NULL)
return MUNIT_ERROR;
sl_init_node(&node->snode);
node->key = key;
node->value = key;
if (sl_insert_nodup(slist, &node->snode) == -1)
continue; /* a random duplicate appeared */
else
inserted++;
}
return inserted;
}
static void *
test_api_insert_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_insert_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_insert(const MunitParameter params[], void *data)
{
int ret;
size_t inserted = 0;
uint32_t n, key;
sl_raw *slist = (sl_raw *)data;
ex_node_t *node;
(void)params;
assert_ptr_not_null(slist);
n = munit_rand_int_range(4096, 8192);
while (n--) {
key = munit_rand_int_range(0, ((uint32_t)0-1) / 10);
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
if (node == NULL)
return MUNIT_ERROR;
sl_init_node(&node->snode);
node->key = key;
node->value = key;
if ((ret = sl_insert_nodup(slist, &node->snode)) == -1)
continue; /* a random duplicate appeared */
else {
assert_int(ret, ==, 0);
inserted++;
}
}
assert_size(inserted, ==, sl_get_size(slist));
return MUNIT_OK;
}
static void *
test_api_remove_setup(const MunitParameter params[], void *user_data)
{
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
__populate_slist(slist);
return (void *)slist;
}
static void
test_api_remove_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_remove(const MunitParameter params[], void *data)
{
uint32_t key;
sl_raw *slist = (sl_raw *)data;
ex_node_t *node;
(void)params;
assert_ptr_not_null(slist);
key = munit_rand_int_range((((uint32_t)0-1) / 10) + 1, ((uint32_t)0-1));
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
if (node == NULL)
return MUNIT_ERROR;
sl_init_node(&node->snode);
node->key = key;
node->value = key;
if (sl_insert_nodup(slist, &node->snode) == -1)
return MUNIT_ERROR;
else {
ex_node_t query;
query.key = key;
sl_node *cursor = sl_find(slist, &query.snode);
assert_ptr_not_null(cursor);
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
sl_erase_node(slist, &entry->snode);
sl_release_node(&entry->snode);
sl_wait_for_free(&entry->snode);
sl_free_node(&entry->snode);
free(entry);
}
return MUNIT_OK;
}
static void *
test_api_find_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_find_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_find(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_update_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_update_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_update(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_delete_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_delete_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_delete(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_duplicates_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_duplicates(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_size_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_size_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_size(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static void *
test_api_iterators_setup(const MunitParameter params[], void *user_data)
{
return test_api_setup(params, user_data);
}
static void
test_api_iterators_tear_down(void *fixture)
{
test_api_tear_down(fixture);
}
static MunitResult
test_api_iterators(const MunitParameter params[], void *data)
{
sl_raw *slist = (sl_raw *)data;
(void)params;
(void)slist;
return MUNIT_OK;
}
static MunitTest api_test_suite[] = {
{ (char *)"/api/insert", test_api_insert, test_api_insert_setup,
test_api_insert_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
@ -140,17 +400,19 @@ static MunitTest api_test_suite[] = {
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
#include "mt.c"
#include "scale.c"
static MunitSuite other_test_suite[] = {
{ "/mt", mt_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
{ "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE}
};
static MunitTest mt_tests[] = { { NULL, NULL, NULL, NULL,
MUNIT_TEST_OPTION_NONE, NULL } };
static const MunitSuite main_test_suite = {
(char *)"/api", api_test_suite,other_test_suite, 1, MUNIT_SUITE_OPTION_NONE
};
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL,
MUNIT_TEST_OPTION_NONE, NULL } };
static MunitSuite other_test_suite[] = { { "/mt", mt_tests, NULL, 1,
MUNIT_SUITE_OPTION_NONE },
{ "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } };
static const MunitSuite main_test_suite = { (char *)"/api", api_test_suite,
other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])