moving more to decl
This commit is contained in:
parent
a1c08f6fd8
commit
e8d3645ed4
12 changed files with 3071 additions and 3018 deletions
|
@ -99,9 +99,9 @@ IndentPPDirectives: None
|
|||
Language: Cpp
|
||||
NamespaceIndentation: None
|
||||
PointerAlignment: Right
|
||||
ContinuationIndentWidth: 4
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
ContinuationIndentWidth: 2
|
||||
IndentWidth: 2
|
||||
TabWidth: 2
|
||||
ColumnLimit: 80
|
||||
UseTab: Never
|
||||
SpaceAfterCStyleCast: false
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# Generated from CLion Inspection settings
|
||||
#bugprone-reserved-identifier,
|
||||
#misc-no-recursion,
|
||||
---
|
||||
Checks: '-*,
|
||||
-deprecated-non-prototype
|
||||
|
@ -74,7 +75,6 @@ hicpp-exception-baseclass,
|
|||
hicpp-multiway-paths-covered,
|
||||
misc-misplaced-const,
|
||||
misc-new-delete-overloads,
|
||||
misc-no-recursion,
|
||||
misc-non-copyable-objects,
|
||||
misc-throw-by-value-catch-by-reference,
|
||||
misc-unconventional-assign-operator,
|
||||
|
|
6
Makefile
6
Makefile
|
@ -44,7 +44,7 @@ clean:
|
|||
rm -f $(EXAMPLES)
|
||||
|
||||
format:
|
||||
clang-format -i include/*.h src/*.c tests/*.c tests/*.h
|
||||
clang-format -i include/*.h src/*.c tests/*.c tests/*.h examples/*.c
|
||||
|
||||
%.o: src/%.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $^
|
||||
|
@ -60,3 +60,7 @@ examples/mls.c: examples/slm.c
|
|||
|
||||
examples/mls: examples/mls.o $(STATIC_LIB)
|
||||
$(CC) $^ -o $@ $(CFLAGS) $(TEST_FLAGS) -pthread
|
||||
|
||||
#dot:
|
||||
# ./examples/mls
|
||||
# dot -Tpdf /tmp/slm.dot -o /tmp/slm.pdf >/dev/null 2>&1
|
||||
|
|
238
examples/skip.c
238
examples/skip.c
|
@ -8,30 +8,30 @@
|
|||
|
||||
// Define a node that contains key and value pair.
|
||||
struct my_node {
|
||||
// Metadata for skiplist node.
|
||||
sl_node snode;
|
||||
// My data here: {int, int} pair.
|
||||
int key;
|
||||
int value;
|
||||
// Metadata for skiplist node.
|
||||
sl_node snode;
|
||||
// My data here: {int, int} pair.
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
|
||||
// Define a comparison function for `my_node`.
|
||||
static int
|
||||
my_cmp(sl_node *a, sl_node *b, void *aux)
|
||||
{
|
||||
// Get `my_node` from skiplist node `a` and `b`.
|
||||
struct my_node *aa, *bb;
|
||||
aa = sl_get_entry(a, struct my_node, snode);
|
||||
bb = sl_get_entry(b, struct my_node, snode);
|
||||
// Get `my_node` from skiplist node `a` and `b`.
|
||||
struct my_node *aa, *bb;
|
||||
aa = sl_get_entry(a, struct my_node, snode);
|
||||
bb = sl_get_entry(b, struct my_node, snode);
|
||||
|
||||
// aa < bb: return neg
|
||||
// aa == bb: return 0
|
||||
// aa > bb: return pos
|
||||
if (aa->key < bb->key)
|
||||
return -1;
|
||||
if (aa->key > bb->key)
|
||||
return 1;
|
||||
return 0;
|
||||
// aa < bb: return neg
|
||||
// aa == bb: return 0
|
||||
// aa > bb: return pos
|
||||
if (aa->key < bb->key)
|
||||
return -1;
|
||||
if (aa->key > bb->key)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define NUM_NODES 10000
|
||||
|
@ -39,118 +39,116 @@ my_cmp(sl_node *a, sl_node *b, void *aux)
|
|||
int
|
||||
main()
|
||||
{
|
||||
// seed the PRNG
|
||||
srandom((unsigned)(time(NULL) | getpid()));
|
||||
// seed the PRNG
|
||||
srandom((unsigned)(time(NULL) | getpid()));
|
||||
|
||||
sl_raw slist;
|
||||
sl_raw slist;
|
||||
|
||||
// Initialize skiplist.
|
||||
sl_init(&slist, my_cmp);
|
||||
// Initialize skiplist.
|
||||
sl_init(&slist, my_cmp);
|
||||
|
||||
// << Insertion >>
|
||||
// Allocate & insert NUM_NODES KV pairs: {0, 0}, {1, 10}, {2, 20}.
|
||||
struct my_node *nodes[NUM_NODES];
|
||||
for (int i = 0; i < NUM_NODES; ++i) {
|
||||
// Allocate memory.
|
||||
nodes[i] = (struct my_node *)malloc(sizeof(struct my_node));
|
||||
// Initialize node.
|
||||
sl_init_node(&nodes[i]->snode);
|
||||
// Assign key and value.
|
||||
nodes[i]->key = i;
|
||||
nodes[i]->value = i * 10;
|
||||
// Insert into skiplist.
|
||||
sl_insert(&slist, &nodes[i]->snode);
|
||||
// << Insertion >>
|
||||
// Allocate & insert NUM_NODES KV pairs: {0, 0}, {1, 10}, {2, 20}.
|
||||
struct my_node *nodes[NUM_NODES];
|
||||
for (int i = 0; i < NUM_NODES; ++i) {
|
||||
// Allocate memory.
|
||||
nodes[i] = (struct my_node *)malloc(sizeof(struct my_node));
|
||||
// Initialize node.
|
||||
sl_init_node(&nodes[i]->snode);
|
||||
// Assign key and value.
|
||||
nodes[i]->key = i;
|
||||
nodes[i]->value = i * 10;
|
||||
// Insert into skiplist.
|
||||
sl_insert(&slist, &nodes[i]->snode);
|
||||
}
|
||||
|
||||
// << Point lookup >>
|
||||
for (int i = 0; i < NUM_NODES; ++i) {
|
||||
// Define a query.
|
||||
struct my_node query;
|
||||
int min = 1, max = NUM_NODES - 1;
|
||||
int k = min + (int)random() / (RAND_MAX / (max - min + 1) + 1);
|
||||
query.key = k;
|
||||
// Find a skiplist node `cursor`.
|
||||
sl_node *cursor = sl_find(&slist, &query.snode);
|
||||
// If `cursor` is NULL, key doesn't exist.
|
||||
if (!cursor)
|
||||
continue;
|
||||
// Get `my_node` from `cursor`.
|
||||
// Note: found->snode == *cursor
|
||||
struct my_node *found = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[point lookup] key: %d, value: %d\n", found->key, found->value);
|
||||
if (found->key != found->value / 10) {
|
||||
printf("FAILURE: key: %d * 10 != value: %d\n", found->key, found->value);
|
||||
exit(-1);
|
||||
}
|
||||
// Release `cursor` (== &found->snode).
|
||||
// Other thread cannot free `cursor` until `cursor` is released.
|
||||
sl_release_node(cursor);
|
||||
}
|
||||
|
||||
// << Point lookup >>
|
||||
for (int i = 0; i < NUM_NODES; ++i) {
|
||||
// Define a query.
|
||||
struct my_node query;
|
||||
int min = 1, max = NUM_NODES - 1;
|
||||
int k = min + (int)random() / (RAND_MAX / (max - min + 1) + 1);
|
||||
query.key = k;
|
||||
// Find a skiplist node `cursor`.
|
||||
sl_node *cursor = sl_find(&slist, &query.snode);
|
||||
// If `cursor` is NULL, key doesn't exist.
|
||||
if (!cursor)
|
||||
continue;
|
||||
// Get `my_node` from `cursor`.
|
||||
// Note: found->snode == *cursor
|
||||
struct my_node *found = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[point lookup] key: %d, value: %d\n", found->key, found->value);
|
||||
if (found->key != found->value / 10) {
|
||||
printf("FAILURE: key: %d * 10 != value: %d\n", found->key,
|
||||
found->value);
|
||||
exit(-1);
|
||||
}
|
||||
// Release `cursor` (== &found->snode).
|
||||
// Other thread cannot free `cursor` until `cursor` is released.
|
||||
sl_release_node(cursor);
|
||||
// << Erase >>
|
||||
// Erase the KV pair for key 1: {1, 10}.
|
||||
{
|
||||
// Define a query.
|
||||
struct my_node query;
|
||||
query.key = 1;
|
||||
// Find a skiplist node `cursor`.
|
||||
sl_node *cursor = sl_find(&slist, &query.snode);
|
||||
// Get `my_node` from `cursor`.
|
||||
// Note: found->snode == *cursor
|
||||
struct my_node *found = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[erase] key: %d, value: %d\n", found->key, found->value);
|
||||
|
||||
// Detach `found` from skiplist.
|
||||
sl_erase_node(&slist, &found->snode);
|
||||
// Release `found`, to free its memory.
|
||||
sl_release_node(&found->snode);
|
||||
// Free `found` after it becomes safe.
|
||||
sl_wait_for_free(&found->snode);
|
||||
sl_free_node(&found->snode);
|
||||
free(found);
|
||||
}
|
||||
|
||||
// << Iteration >>
|
||||
{
|
||||
// Get the first cursor.
|
||||
sl_node *cursor = sl_begin(&slist);
|
||||
while (cursor) {
|
||||
// Get `entry` from `cursor`.
|
||||
// Note: entry->snode == *cursor
|
||||
struct my_node *entry = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[iteration] key: %d, value: %d\n", entry->key, entry->value);
|
||||
// Get next `cursor`.
|
||||
cursor = sl_next(&slist, cursor);
|
||||
// Release `entry`.
|
||||
sl_release_node(&entry->snode);
|
||||
}
|
||||
}
|
||||
|
||||
// << Erase >>
|
||||
// Erase the KV pair for key 1: {1, 10}.
|
||||
{
|
||||
// Define a query.
|
||||
struct my_node query;
|
||||
query.key = 1;
|
||||
// Find a skiplist node `cursor`.
|
||||
sl_node *cursor = sl_find(&slist, &query.snode);
|
||||
// Get `my_node` from `cursor`.
|
||||
// Note: found->snode == *cursor
|
||||
struct my_node *found = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[erase] key: %d, value: %d\n", found->key, found->value);
|
||||
// << Destroy >>
|
||||
{
|
||||
// Iterate and free all nodes.
|
||||
sl_node *cursor = sl_begin(&slist);
|
||||
while (cursor) {
|
||||
struct my_node *entry = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[destroy] key: %d, value: %d\n", entry->key, entry->value);
|
||||
// Get next `cursor`.
|
||||
cursor = sl_next(&slist, cursor);
|
||||
|
||||
// Detach `found` from skiplist.
|
||||
sl_erase_node(&slist, &found->snode);
|
||||
// Release `found`, to free its memory.
|
||||
sl_release_node(&found->snode);
|
||||
// Free `found` after it becomes safe.
|
||||
sl_wait_for_free(&found->snode);
|
||||
sl_free_node(&found->snode);
|
||||
free(found);
|
||||
// Detach `entry` from skiplist.
|
||||
sl_erase_node(&slist, &entry->snode);
|
||||
// Release `entry`, to free its memory.
|
||||
sl_release_node(&entry->snode);
|
||||
// Free `entry` after it becomes safe.
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// << Iteration >>
|
||||
{
|
||||
// Get the first cursor.
|
||||
sl_node *cursor = sl_begin(&slist);
|
||||
while (cursor) {
|
||||
// Get `entry` from `cursor`.
|
||||
// Note: entry->snode == *cursor
|
||||
struct my_node *entry = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[iteration] key: %d, value: %d\n", entry->key,
|
||||
entry->value);
|
||||
// Get next `cursor`.
|
||||
cursor = sl_next(&slist, cursor);
|
||||
// Release `entry`.
|
||||
sl_release_node(&entry->snode);
|
||||
}
|
||||
}
|
||||
// Free skiplist.
|
||||
sl_free(&slist);
|
||||
|
||||
// << Destroy >>
|
||||
{
|
||||
// Iterate and free all nodes.
|
||||
sl_node *cursor = sl_begin(&slist);
|
||||
while (cursor) {
|
||||
struct my_node *entry = sl_get_entry(cursor, struct my_node, snode);
|
||||
printf("[destroy] key: %d, value: %d\n", entry->key, entry->value);
|
||||
// Get next `cursor`.
|
||||
cursor = sl_next(&slist, cursor);
|
||||
|
||||
// Detach `entry` from skiplist.
|
||||
sl_erase_node(&slist, &entry->snode);
|
||||
// Release `entry`, to free its memory.
|
||||
sl_release_node(&entry->snode);
|
||||
// Free `entry` after it becomes safe.
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// Free skiplist.
|
||||
sl_free(&slist);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../include/sl.h"
|
||||
|
||||
/*
|
||||
|
@ -49,54 +49,61 @@ struct slex_node {
|
|||
* list or when `a == b`. In those cases the comparison function
|
||||
* returns before using the code in your block, don't panic. :)
|
||||
*/
|
||||
SKIPLIST_DECL(slex, api_, entries, {
|
||||
(void)aux;
|
||||
if (a->key < b->key)
|
||||
return -1;
|
||||
if (a->key > b->key)
|
||||
return 1;
|
||||
return 0;
|
||||
})
|
||||
|
||||
void sprintf_slex_node(slex_node_t *node, char *buf) {
|
||||
sprintf(buf, "%d", node->key);
|
||||
SKIPLIST_DECL(slex, api_, entries)
|
||||
int
|
||||
__slm_key_compare(slex_t *list, slex_node_t *a, slex_node_t *b, void *aux)
|
||||
{
|
||||
(void)list;
|
||||
(void)aux;
|
||||
if (a->key < b->key)
|
||||
return -1;
|
||||
if (a->key > b->key)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
/* Allocate and initialize a Skiplist. */
|
||||
slex_t _list = SKIP_HEAD_DEFAULT_INITIALIZER(__skip_key_compare_slex);
|
||||
_list.slh_tail = (struct slex_node *)&_list.slh_head; // TODO...
|
||||
/* Dynamic allocation, init. */
|
||||
slex_t *list = (slex_t *)malloc(sizeof(slex_t));
|
||||
SKIP_DEFAULT_INIT(list, __skip_key_compare_slex, slex_node, entries);
|
||||
#ifdef STATIC_INIT
|
||||
free(list);
|
||||
slex_t *list = &_list;
|
||||
#else
|
||||
#define DOT
|
||||
#ifdef DOT
|
||||
/* Also declare the functions used to visualize a Sliplist (DOT/Graphviz) */
|
||||
SKIPLIST_DECL_DOT(slex, api_, entries)
|
||||
|
||||
void
|
||||
sprintf_slex_node(slex_node_t *node, char *buf)
|
||||
{
|
||||
sprintf(buf, "%d:%d", node->key, node->value);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct slex_node *n;
|
||||
SKIP_ALLOC_NODE(list, n, slex_node, entries);
|
||||
n->key = -1;
|
||||
n->value = -1;
|
||||
api_skip_insert_slex(list, n);
|
||||
int
|
||||
main()
|
||||
{
|
||||
int rc = 0;
|
||||
/* Allocate and initialize a Skiplist. */
|
||||
slex_t *list = (slex_t *)malloc(sizeof(slex_t));
|
||||
if (list == NULL) {
|
||||
rc = ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
rc = api_skip_init_slex(list, 12, 4, __slm_key_compare);
|
||||
|
||||
FILE* of = fopen("/tmp/slm.dot", "w");
|
||||
struct slex_node *n;
|
||||
|
||||
/* Insert 7 key/value pairs into the list. */
|
||||
for (int i = -2; i <= 2; i++) {
|
||||
SKIP_ALLOC_NODE(list, n, slex_node, entries);
|
||||
n->key = i;
|
||||
n->value = i;
|
||||
api_skip_insert_slex(list, n);
|
||||
}
|
||||
|
||||
FILE *of = fopen("/tmp/slm.dot", "w");
|
||||
if (!of) {
|
||||
perror("Failed to open file /tmp/slm.dot");
|
||||
return EXIT_FAILURE;
|
||||
perror("Failed to open file /tmp/slm.dot");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
api_skip_dot_slex(of, list, sprintf_slex_node);
|
||||
fclose(of);
|
||||
|
||||
/* Insert 10 key/value pairs into the list. */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
SKIP_ALLOC_NODE(list, n, slex_node, entries);
|
||||
n->key = i;
|
||||
n->value = i;
|
||||
api_skip_insert_slex(list, n);
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* Delete a specific element in the list. */
|
||||
struct slex_node query;
|
||||
|
@ -124,5 +131,6 @@ int main() {
|
|||
}
|
||||
SKIP_INIT(&head);
|
||||
#endif
|
||||
return 0;
|
||||
fail:;
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -50,13 +50,13 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
typedef struct _sl_node {
|
||||
atm_node_ptr *next;
|
||||
atm_bool is_fully_linked;
|
||||
atm_bool being_modified;
|
||||
atm_bool removed;
|
||||
uint8_t top_layer; /* 0: bottom */
|
||||
atm_uint16_t ref_count;
|
||||
atm_uint32_t accessing_next;
|
||||
atm_node_ptr *next;
|
||||
atm_bool is_fully_linked;
|
||||
atm_bool being_modified;
|
||||
atm_bool removed;
|
||||
uint8_t top_layer; /* 0: bottom */
|
||||
atm_uint16_t ref_count;
|
||||
atm_uint32_t accessing_next;
|
||||
} sl_node;
|
||||
|
||||
/*
|
||||
|
@ -67,26 +67,26 @@ typedef struct _sl_node {
|
|||
typedef int sl_cmp_t(sl_node *a, sl_node *b, void *aux);
|
||||
|
||||
typedef struct {
|
||||
size_t fanout;
|
||||
size_t maxLayer;
|
||||
void *aux;
|
||||
size_t fanout;
|
||||
size_t maxLayer;
|
||||
void *aux;
|
||||
} sl_raw_config;
|
||||
|
||||
typedef struct {
|
||||
sl_node head;
|
||||
sl_node tail;
|
||||
sl_cmp_t *cmp_func;
|
||||
void *aux;
|
||||
atm_uint32_t num_entries;
|
||||
atm_uint32_t *layer_entries;
|
||||
atm_uint8_t top_layer;
|
||||
uint8_t fanout;
|
||||
uint8_t max_layer;
|
||||
sl_node head;
|
||||
sl_node tail;
|
||||
sl_cmp_t *cmp_func;
|
||||
void *aux;
|
||||
atm_uint32_t num_entries;
|
||||
atm_uint32_t *layer_entries;
|
||||
atm_uint8_t top_layer;
|
||||
uint8_t fanout;
|
||||
uint8_t max_layer;
|
||||
} sl_raw;
|
||||
|
||||
#ifndef sl_get_entry
|
||||
#define sl_get_entry(ELEM, STRUCT, MEMBER) \
|
||||
((STRUCT *)((uint8_t *)(ELEM)-offsetof(STRUCT, MEMBER)))
|
||||
((STRUCT *)((uint8_t *)(ELEM)-offsetof(STRUCT, MEMBER)))
|
||||
#endif
|
||||
|
||||
void sl_init(sl_raw *slist, sl_cmp_t *cmp_func);
|
||||
|
|
907
include/sl.h
907
include/sl.h
|
@ -30,8 +30,8 @@
|
|||
* - async_nif.h
|
||||
*/
|
||||
|
||||
#ifndef _SYS_SKIPLIST_H_
|
||||
#define _SYS_SKIPLIST_H_
|
||||
#ifndef _SYS_SKIPLIST_H_
|
||||
#define _SYS_SKIPLIST_H_
|
||||
|
||||
/*
|
||||
* This file defines a skiplist data structure with a similar API to those
|
||||
|
@ -68,40 +68,44 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#define SKIPLIST_MACRO_DEBUG 0
|
||||
#define SKIPLIST_MACRO_DEBUG 0
|
||||
#if SKIPLIST_MACRO_DEBUG
|
||||
/* Store the last 2 places the element or head was altered */
|
||||
struct sl_trace {
|
||||
char * lastfile;
|
||||
int lastline;
|
||||
char * prevfile;
|
||||
int prevline;
|
||||
char *lastfile;
|
||||
int lastline;
|
||||
char *prevfile;
|
||||
int prevline;
|
||||
};
|
||||
|
||||
#define TRACEBUF struct sl_trace trace;
|
||||
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
||||
#define TRACEBUF struct sl_trace trace;
|
||||
#define TRASHIT(x) \
|
||||
do { \
|
||||
(x) = (void *)-1; \
|
||||
} while (0)
|
||||
|
||||
#define SLD_TRACE_HEAD(head) do { \
|
||||
(head)->trace.prevline = (head)->trace.lastline; \
|
||||
(head)->trace.prevfile = (head)->trace.lastfile; \
|
||||
(head)->trace.lastline = __LINE__; \
|
||||
(head)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
#define SLD_TRACE_HEAD(head) \
|
||||
do { \
|
||||
(head)->trace.prevline = (head)->trace.lastline; \
|
||||
(head)->trace.prevfile = (head)->trace.lastfile; \
|
||||
(head)->trace.lastline = __LINE__; \
|
||||
(head)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
|
||||
#define SLD_TRACE_ELEM(elem) do { \
|
||||
(elem)->trace.prevline = (elem)->trace.lastline; \
|
||||
(elem)->trace.prevfile = (elem)->trace.lastfile; \
|
||||
(elem)->trace.lastline = __LINE__; \
|
||||
(elem)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
#define SLD_TRACE_ELEM(elem) \
|
||||
do { \
|
||||
(elem)->trace.prevline = (elem)->trace.lastline; \
|
||||
(elem)->trace.prevfile = (elem)->trace.lastfile; \
|
||||
(elem)->trace.lastline = __LINE__; \
|
||||
(elem)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
#define SLD_TRACE_ELEM(elem)
|
||||
#define SLD_TRACE_HEAD(head)
|
||||
#define TRACEBUF
|
||||
#define TRASHIT(x)
|
||||
#endif /* QUEUE_MACRO_DEBUG */
|
||||
|
||||
#define SLD_TRACE_ELEM(elem)
|
||||
#define SLD_TRACE_HEAD(head)
|
||||
#define TRACEBUF
|
||||
#define TRASHIT(x)
|
||||
#endif /* QUEUE_MACRO_DEBUG */
|
||||
|
||||
/*
|
||||
* Private, internal API.
|
||||
|
@ -113,14 +117,15 @@ struct sl_trace {
|
|||
* can grow to (`array[-2]`) and the length, or number of elements used in the
|
||||
* array so far (`array[-1]`).
|
||||
*/
|
||||
#define ARRAY_ALLOC(var, type, size) do { \
|
||||
(size) = (size == 0 ? 254 : size); \
|
||||
(var) = (type**)calloc(sizeof(type*), size + 2); \
|
||||
if ((var) != NULL) { \
|
||||
*(var)++ = (type *)size; \
|
||||
*(var)++ = 0; \
|
||||
} \
|
||||
} while(0)
|
||||
#define ARRAY_ALLOC(var, type, size) \
|
||||
do { \
|
||||
(size) = (size == 0 ? 254 : size); \
|
||||
(var) = (type **)calloc(sizeof(type *), size + 2); \
|
||||
if ((var) != NULL) { \
|
||||
*(var)++ = (type *)size; \
|
||||
*(var)++ = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
#define ARRAY_FREE(var) free((var)-2)
|
||||
#define ARRAY_SIZE(list) (unsigned int)(uintptr_t)(list)[-2]
|
||||
#define ARRAY_SET_SIZE(list, size) (list)[-2] = (void *)(uintptr_t)(size)
|
||||
|
@ -130,414 +135,492 @@ struct sl_trace {
|
|||
/*
|
||||
* Skiplist declarations.
|
||||
*/
|
||||
#define SKIP_HEAD(name, type) \
|
||||
struct name { \
|
||||
size_t level, length, max, fanout; \
|
||||
int (*cmp)(struct name *, struct type *, struct type *, void *); \
|
||||
void *aux; \
|
||||
struct type *slh_head; \
|
||||
struct type *slh_tail; \
|
||||
TRACEBUF \
|
||||
#define SKIP_HEAD(name, type) \
|
||||
struct name { \
|
||||
size_t level, length, max, fanout; \
|
||||
int (*cmp)(struct name *, struct type *, struct type *, void *); \
|
||||
void *aux; \
|
||||
struct type *slh_head; \
|
||||
struct type *slh_tail; \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
#define SKIP_HEAD_DEFAULT_INITIALIZER(cmp) \
|
||||
{ 0, 0, 12, 4, cmp, NULL, NULL, NULL }
|
||||
#define SKIP_HEAD_DEFAULT_INITIALIZER(cmp) \
|
||||
{ \
|
||||
0, 0, 12, 4, cmp, NULL, NULL, NULL \
|
||||
}
|
||||
|
||||
#define SKIP_HEAD_INITIALIZER(cmp, max, fanout) \
|
||||
{ 0, 0, max, fanout, cmp, NULL, NULL, NULL }
|
||||
#define SKIP_HEAD_INITIALIZER(cmp, max, fanout) \
|
||||
{ \
|
||||
0, 0, max, fanout, cmp, NULL, NULL, NULL \
|
||||
}
|
||||
|
||||
#define SKIP_ENTRY(type) \
|
||||
struct { \
|
||||
struct type **sle_next; \
|
||||
struct type *sle_prev; \
|
||||
TRACEBUF \
|
||||
#define SKIP_ENTRY(type) \
|
||||
struct { \
|
||||
struct type **sle_next; \
|
||||
struct type *sle_prev; \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip List access methods.
|
||||
*/
|
||||
#define SKIP_FIRST(head) ((head)->slh_head)
|
||||
#define SKIP_LAST(head) ((head)->slh_tail)
|
||||
#define SKIP_NEXT(elm, field) ((elm)->field.sle_next[0])
|
||||
#define SKIP_PREV(elm, field) ((elm)->field.sle_prev)
|
||||
#define SKIP_EMPTY(head) ((head)->length == 0)
|
||||
#define SKIP_FIRST(head) ((head)->slh_head)
|
||||
#define SKIP_LAST(head) ((head)->slh_tail)
|
||||
#define SKIP_NEXT(elm, field) ((elm)->field.sle_next[0])
|
||||
#define SKIP_PREV(elm, field) ((elm)->field.sle_prev)
|
||||
#define SKIP_EMPTY(head) ((head)->length == 0)
|
||||
|
||||
#if 0
|
||||
#define SKIP_FOREACH(var, head, field) \
|
||||
for((var) = SKIP_FIRST(head); \
|
||||
(var)!= SKIP_END(head); \
|
||||
(var) = SKIP_NEXT(var, field))
|
||||
#define SKIP_FOREACH(var, head, field) \
|
||||
for ((var) = SKIP_FIRST(head); (var) != SKIP_END(head); \
|
||||
(var) = SKIP_NEXT(var, field))
|
||||
|
||||
#define SKIP_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = SKIP_FIRST(head); \
|
||||
(var) && ((tvar) = SKIP_NEXT(var, field), 1); \
|
||||
(var) = (tvar))
|
||||
#define SKIP_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = SKIP_FIRST(head); (var) && ((tvar) = SKIP_NEXT(var, field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||
for((var) = TAILQ_LAST(head, headname); \
|
||||
(var) != TAILQ_END(head); \
|
||||
(var) = TAILQ_PREV(var, headname, field))
|
||||
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||
for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head); \
|
||||
(var) = TAILQ_PREV(var, headname, field))
|
||||
|
||||
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
||||
for ((var) = TAILQ_LAST(head, headname); \
|
||||
(var) != TAILQ_END(head) && \
|
||||
((tvar) = TAILQ_PREV(var, headname, field), 1); \
|
||||
(var) = (tvar))
|
||||
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
||||
for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head) && \
|
||||
((tvar) = TAILQ_PREV(var, headname, field), 1); \
|
||||
(var) = (tvar))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Skip List functions.
|
||||
*/
|
||||
|
||||
#define SKIP_COMPARATOR(list, type, fn) \
|
||||
int __skip_cmp_##type(struct list *head, struct type *a, struct type *b, void *aux) { \
|
||||
if (a == b) \
|
||||
return 0; \
|
||||
if (a == (head)->slh_head || b == (head)->slh_tail) \
|
||||
return -1; \
|
||||
if (a == (head)->slh_tail || b == (head)->slh_head) \
|
||||
return 1; \
|
||||
fn }
|
||||
#define SKIP_COMPARATOR(list, type, fn) \
|
||||
int __skip_cmp_##type(struct list *head, struct type *a, struct type *b, \
|
||||
void *aux) \
|
||||
{ \
|
||||
if (a == b) \
|
||||
return 0; \
|
||||
if (a == (head)->slh_head || b == (head)->slh_tail) \
|
||||
return -1; \
|
||||
if (a == (head)->slh_tail || b == (head)->slh_head) \
|
||||
return 1; \
|
||||
fn \
|
||||
}
|
||||
|
||||
#define SKIP_INIT(head, max, fanout, type, field, fn) do { \
|
||||
(head)->level = 0; \
|
||||
(head)->length = 0; \
|
||||
(head)->max = max; \
|
||||
(head)->fanout = fanout; \
|
||||
(head)->cmp = fn; \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_head, type, field); \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_tail, type, field); \
|
||||
ARRAY_SET_LENGTH((head)->slh_head->field.sle_next, max); \
|
||||
for(size_t __i = 0; __i < ARRAY_SIZE((head)->slh_head->field.sle_next); __i++) { \
|
||||
(head)->slh_head->field.sle_next[__i] = (head)->slh_tail; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = NULL; \
|
||||
ARRAY_SET_LENGTH((head)->slh_tail->field.sle_next, max); \
|
||||
for(size_t __i = 0; __i < ARRAY_SIZE((head)->slh_tail->field.sle_next); __i++) { \
|
||||
(head)->slh_tail->field.sle_next[__i] = NULL; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = (head)->slh_tail; \
|
||||
SLD_TRACE_HEAD(head); \
|
||||
#define SKIP_INIT(head, max, fanout, type, field, fn) \
|
||||
do { \
|
||||
(head)->level = 0; \
|
||||
(head)->length = 0; \
|
||||
(head)->max = max; \
|
||||
(head)->fanout = fanout; \
|
||||
(head)->cmp = fn; \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_head, type, field); \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_tail, type, field); \
|
||||
ARRAY_SET_LENGTH((head)->slh_head->field.sle_next, max); \
|
||||
for (size_t __i = 0; __i < ARRAY_SIZE((head)->slh_head->field.sle_next); \
|
||||
__i++) { \
|
||||
(head)->slh_head->field.sle_next[__i] = (head)->slh_tail; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = NULL; \
|
||||
ARRAY_SET_LENGTH((head)->slh_tail->field.sle_next, max); \
|
||||
for (size_t __i = 0; __i < ARRAY_SIZE((head)->slh_tail->field.sle_next); \
|
||||
__i++) { \
|
||||
(head)->slh_tail->field.sle_next[__i] = NULL; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = (head)->slh_tail; \
|
||||
SLD_TRACE_HEAD(head); \
|
||||
} while (0)
|
||||
|
||||
#define SKIP_DEFAULT_INIT(head, fn, type, field) do { \
|
||||
(head)->level = 0; \
|
||||
(head)->length = 0; \
|
||||
(head)->max = 12; \
|
||||
(head)->fanout = 4; \
|
||||
(head)->cmp = fn; \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_head, type, field); \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_tail, type, field); \
|
||||
ARRAY_SET_LENGTH((head)->slh_head->field.sle_next, (head)->max); \
|
||||
for(size_t __i = 0; __i < ARRAY_SIZE((head)->slh_head->field.sle_next); __i++) { \
|
||||
(head)->slh_head->field.sle_next[__i] = (head)->slh_tail; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = NULL; \
|
||||
ARRAY_SET_LENGTH((head)->slh_tail->field.sle_next, (head)->max); \
|
||||
for(size_t __i = 0; __i < ARRAY_SIZE((head)->slh_tail->field.sle_next); __i++) { \
|
||||
(head)->slh_tail->field.sle_next[__i] = NULL; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = (head)->slh_tail; \
|
||||
SLD_TRACE_HEAD(head); \
|
||||
#define SKIP_DEFAULT_INIT(head, fn, type, field) \
|
||||
do { \
|
||||
(head)->level = 0; \
|
||||
(head)->length = 0; \
|
||||
(head)->max = 12; \
|
||||
(head)->fanout = 4; \
|
||||
(head)->cmp = fn; \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_head, type, field); \
|
||||
SKIP_ALLOC_NODE(head, (head)->slh_tail, type, field); \
|
||||
ARRAY_SET_LENGTH((head)->slh_head->field.sle_next, (head)->max); \
|
||||
for (size_t __i = 0; __i < ARRAY_SIZE((head)->slh_head->field.sle_next); \
|
||||
__i++) { \
|
||||
(head)->slh_head->field.sle_next[__i] = (head)->slh_tail; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = NULL; \
|
||||
ARRAY_SET_LENGTH((head)->slh_tail->field.sle_next, (head)->max); \
|
||||
for (size_t __i = 0; __i < ARRAY_SIZE((head)->slh_tail->field.sle_next); \
|
||||
__i++) { \
|
||||
(head)->slh_tail->field.sle_next[__i] = NULL; \
|
||||
} \
|
||||
(head)->slh_head->field.sle_prev = (head)->slh_tail; \
|
||||
SLD_TRACE_HEAD(head); \
|
||||
} while (0)
|
||||
|
||||
|
||||
#define SKIP_ALLOC_NODE(head, var, type, field) do { \
|
||||
(var) = (struct type *)calloc(1, sizeof(struct type)); \
|
||||
ARRAY_ALLOC((var)->field.sle_next, struct type, (head)->max); \
|
||||
if ((var) && (var)->field.sle_next) { \
|
||||
ARRAY_SET_SIZE((var)->field.sle_next, (head)->max); \
|
||||
ARRAY_SET_LENGTH((var)->field.sle_next, 0); \
|
||||
} \
|
||||
#define SKIP_ALLOC_NODE(head, var, type, field) \
|
||||
do { \
|
||||
(var) = (struct type *)calloc(1, sizeof(struct type)); \
|
||||
ARRAY_ALLOC((var)->field.sle_next, struct type, (head)->max); \
|
||||
if ((var) && (var)->field.sle_next) { \
|
||||
ARRAY_SET_SIZE((var)->field.sle_next, (head)->max); \
|
||||
ARRAY_SET_LENGTH((var)->field.sle_next, 0); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define SKIP_FREE_NODE(node, field) do { \
|
||||
free((node)->field.sle_next); \
|
||||
free((node)); \
|
||||
} while (0)
|
||||
#define SKIPLIST_DECL(decl, prefix, field) \
|
||||
\
|
||||
/* Skiplist node type */ \
|
||||
typedef struct decl##_node decl##_node_t; \
|
||||
\
|
||||
/* Skiplist type */ \
|
||||
typedef struct decl { \
|
||||
size_t level, length, max, fanout; \
|
||||
int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \
|
||||
void *aux; \
|
||||
decl##_node_t *slh_head; \
|
||||
decl##_node_t *slh_tail; \
|
||||
TRACEBUF \
|
||||
} decl##_t; \
|
||||
\
|
||||
/* -- __skip_key_compare_ \
|
||||
* \
|
||||
* This function takes four arguments: \
|
||||
* - a reference to the Skiplist \
|
||||
* - the two nodes to compare, `a` and `b` \
|
||||
* - `aux` an additional auxiliary argument \
|
||||
* and returns: \
|
||||
* a < b : return -1 \
|
||||
* a == b : return 0 \
|
||||
* a > b : return 1 \
|
||||
*/ \
|
||||
static int __skip_key_compare_##decl(decl##_t *slist, decl##_node_t *a, \
|
||||
decl##_node_t *b, void *aux) \
|
||||
{ \
|
||||
if (a == b) \
|
||||
return 0; \
|
||||
if (a == slist->slh_head || b == slist->slh_tail) \
|
||||
return -1; \
|
||||
if (a == slist->slh_tail || b == slist->slh_head) \
|
||||
return 1; \
|
||||
return slist->cmp(slist, a, b, aux); \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_toss_ */ \
|
||||
static int __skip_toss_##decl(size_t max, size_t fanout) \
|
||||
{ \
|
||||
size_t level = 0; \
|
||||
while (level + 1 < max) { \
|
||||
if (rand() % fanout == 0) /* NOLINT(*-msc50-cpp) */ \
|
||||
level++; \
|
||||
else \
|
||||
break; \
|
||||
} \
|
||||
return level; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_alloc_node_ */ \
|
||||
int prefix##skip_alloc_node_##decl(decl##_t *slist, decl##_node_t **node) \
|
||||
{ \
|
||||
decl##_node_t *n; \
|
||||
n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t)); \
|
||||
ARRAY_ALLOC(n->field.sle_next, struct decl##_node, slist->max); \
|
||||
if (n && n->field.sle_next) { \
|
||||
ARRAY_SET_SIZE(n->field.sle_next, slist->max); \
|
||||
ARRAY_SET_LENGTH(n->field.sle_next, 0); \
|
||||
*node = n; \
|
||||
return 0; \
|
||||
} \
|
||||
return ENOMEM; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_init_ \
|
||||
* max: 12, fanout: 4 are good defaults. \
|
||||
*/ \
|
||||
int prefix##skip_init_##decl(decl##_t *slist, size_t max, size_t fanout, \
|
||||
int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *)) \
|
||||
{ \
|
||||
int rc = 0; \
|
||||
slist->level = 0; \
|
||||
slist->length = 0; \
|
||||
slist->max = max; \
|
||||
slist->fanout = fanout; \
|
||||
slist->cmp = cmp; \
|
||||
rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \
|
||||
if (rc) \
|
||||
goto fail; \
|
||||
rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_tail); \
|
||||
if (rc) \
|
||||
goto fail; \
|
||||
ARRAY_SET_LENGTH(slist->slh_head->field.sle_next, max); \
|
||||
for (size_t __i = 0; __i < max; __i++) \
|
||||
slist->slh_head->field.sle_next[__i] = slist->slh_tail; \
|
||||
slist->slh_head->field.sle_prev = NULL; \
|
||||
ARRAY_SET_LENGTH(slist->slh_tail->field.sle_next, max); \
|
||||
for (size_t __i = 0; __i < max; __i++) \
|
||||
slist->slh_tail->field.sle_next[__i] = NULL; \
|
||||
slist->slh_head->field.sle_prev = slist->slh_tail; \
|
||||
fail:; \
|
||||
return rc; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_free_node_ */ \
|
||||
void prefix##skip_free_node_##decl(decl##_node_t *node) \
|
||||
{ \
|
||||
free(node->field.sle_next); \
|
||||
free(node); \
|
||||
} \
|
||||
\
|
||||
/* -- skip_insert_ */ \
|
||||
int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) \
|
||||
{ \
|
||||
if (n == NULL) \
|
||||
return ENOMEM; \
|
||||
decl##_node_t *prev, *elm = slist->slh_head; \
|
||||
unsigned int i; \
|
||||
decl##_node_t **path; \
|
||||
i = slist->level; \
|
||||
ARRAY_ALLOC(path, decl##_node_t, slist->max); \
|
||||
if (path == NULL) \
|
||||
return ENOMEM; \
|
||||
/* Find the position in the list where this element belongs. */ \
|
||||
do { \
|
||||
while (elm && \
|
||||
__skip_key_compare_##decl(slist, elm->field.sle_next[i], n, \
|
||||
slist->aux) < 0) \
|
||||
elm = elm->field.sle_next[i]; \
|
||||
path[i] = elm; \
|
||||
ARRAY_SET_LENGTH(path, ARRAY_LENGTH(path) + 1); \
|
||||
} while (i--); \
|
||||
i = 0; \
|
||||
prev = elm; \
|
||||
elm = elm->field.sle_next[0]; \
|
||||
if (__skip_key_compare_##decl(slist, elm, n, slist->aux) == 0) { \
|
||||
/* Don't overwrite, to do that use _REPLACE not _INSERT */ \
|
||||
ARRAY_FREE(path); \
|
||||
return -1; \
|
||||
} \
|
||||
size_t level = __skip_toss_##decl(slist->max, slist->fanout); \
|
||||
ARRAY_SET_LENGTH(n->field.sle_next, level); \
|
||||
if (level > slist->level) { \
|
||||
for (i = slist->level + 1; i <= level; i++) { \
|
||||
path[i] = slist->slh_tail; \
|
||||
} \
|
||||
slist->level = level; \
|
||||
} \
|
||||
for (i = 0; i <= level; i++) { \
|
||||
n->field.sle_next[i] = path[i]->field.sle_next[i]; \
|
||||
path[i]->field.sle_next[i] = n; \
|
||||
} \
|
||||
n->field.sle_prev = prev; \
|
||||
if (n->field.sle_next[0] == slist->slh_tail) { \
|
||||
slist->slh_tail->field.sle_prev = n; \
|
||||
} \
|
||||
slist->length++; \
|
||||
ARRAY_FREE(path); \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_integrity_check_ */ \
|
||||
static int __skip_integrity_check_##decl(decl##_t *slist) \
|
||||
{ \
|
||||
((void)slist); /* TODO */ \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define SKIPLIST_DECL(decl, prefix, field, key_cmp_logic) \
|
||||
\
|
||||
/* Skiplist node type */ \
|
||||
typedef struct decl##_node decl##_node_t; \
|
||||
\
|
||||
/* Skiplist type */ \
|
||||
typedef struct decl { \
|
||||
size_t level, length, max, fanout; \
|
||||
int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \
|
||||
void *aux; \
|
||||
decl##_node_t *slh_head; \
|
||||
decl##_node_t *slh_tail; \
|
||||
TRACEBUF \
|
||||
}decl##_t; \
|
||||
\
|
||||
/* -- __skip_key_compare_ \
|
||||
* \
|
||||
* This function takes four arguments: \
|
||||
* - a reference to the Skiplist \
|
||||
* - the two nodes to compare, `a` and `b` \
|
||||
* - `aux` an additional auxiliary argument \
|
||||
* and returns: \
|
||||
* a < b : return -1 \
|
||||
* a == b : return 0 \
|
||||
* a > b : return 1 \
|
||||
*/ \
|
||||
static int __skip_key_compare_##decl(decl##_t *slist, decl##_node_t *a, decl##_node_t *b, void *aux) { \
|
||||
if (a == b) \
|
||||
return 0; \
|
||||
if (a == slist->slh_head || b == slist->slh_tail) \
|
||||
return -1; \
|
||||
if (a == slist->slh_tail || b == slist->slh_head) \
|
||||
return 1; \
|
||||
do { key_cmp_logic } while(0); \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_toss_ */ \
|
||||
static int __skip_toss_##decl(size_t max, size_t fanout) { \
|
||||
size_t level = 0; \
|
||||
while (level + 1 < max) { \
|
||||
if (rand() % fanout == 0) /* NOLINT(*-msc50-cpp) */ \
|
||||
level++; \
|
||||
else \
|
||||
break; \
|
||||
} \
|
||||
return level; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_insert_ */ \
|
||||
int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) { \
|
||||
if (n == NULL) \
|
||||
return ENOMEM; \
|
||||
decl##_node_t *prev, *elm = slist->slh_head; \
|
||||
unsigned int i; \
|
||||
decl##_node_t **path; \
|
||||
i = slist->level; \
|
||||
ARRAY_ALLOC(path, decl##_node_t, slist->max); \
|
||||
if (path == NULL) \
|
||||
return ENOMEM; \
|
||||
/* Find the position in the list where this element belongs. */ \
|
||||
do { \
|
||||
while(elm && slist->cmp(slist, elm->field.sle_next[i], n, slist->aux) < 0) \
|
||||
elm = elm->field.sle_next[i]; \
|
||||
path[i] = elm; \
|
||||
ARRAY_SET_LENGTH(path, ARRAY_LENGTH(path)+1); \
|
||||
} while(i--); \
|
||||
i = 0; \
|
||||
prev = elm; \
|
||||
elm = elm->field.sle_next[0]; \
|
||||
if (slist->cmp(slist, elm, n, slist->aux) == 0) { \
|
||||
ARRAY_FREE(path); \
|
||||
/* Don't overwrite, to do that use _REPLACE not _INSERT */ \
|
||||
return -1; \
|
||||
} \
|
||||
size_t level = __skip_toss_##decl(slist->max, slist->fanout); \
|
||||
ARRAY_SET_LENGTH(n->field.sle_next, level); \
|
||||
if (level > slist->level) { \
|
||||
for (i = slist->level + 1; i <= level; i++) { \
|
||||
path[i] = slist->slh_tail; \
|
||||
} \
|
||||
slist->level = level; \
|
||||
} \
|
||||
for (i = 0; i <= level; i++) { \
|
||||
n->field.sle_next[i] = path[i]->field.sle_next[i]; \
|
||||
path[i]->field.sle_next[i] = n; \
|
||||
} \
|
||||
n->field.sle_prev = prev; \
|
||||
if (n->field.sle_next[0] == slist->slh_tail) { \
|
||||
slist->slh_tail->field.sle_prev = n; \
|
||||
} \
|
||||
slist->length++; \
|
||||
ARRAY_FREE(path); \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_integrity_check_ */ \
|
||||
static int __skip_integrity_check_##decl() { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
/* A type for a function that writes into a char[2048] buffer \
|
||||
* a description of the value within the node. */ \
|
||||
typedef void (*skip_sprintf_node_##decl##_t)(decl##_node_t *, char *); \
|
||||
\
|
||||
/* -- __skip_dot_node_ \
|
||||
* Writes out a fragment of a DOT file representing a node. \
|
||||
*/ \
|
||||
static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) { \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void*)node); \
|
||||
fprintf(os, " [label = \""); \
|
||||
size_t level = ARRAY_LENGTH(node->field.sle_next); \
|
||||
do { \
|
||||
fprintf(os, " { <w%zu> | <f%zu> %p }", level, level, (void*)node->field.sle_next[level]); \
|
||||
if (level != 0) fprintf(os, " | "); \
|
||||
} while(level--); \
|
||||
if (fn) { \
|
||||
char buf[2048]; \
|
||||
fn(node, buf); \
|
||||
fprintf(os, " <f0> %s\"\n", buf); \
|
||||
} else { \
|
||||
fprintf(os, " <f0> ?\"\n"); \
|
||||
} \
|
||||
fprintf(os, "shape = \"record\"\n"); \
|
||||
fprintf(os, "];\n"); \
|
||||
\
|
||||
/* Now edges */ \
|
||||
level = 0; \
|
||||
size_t size = ARRAY_LENGTH(node->field.sle_next); \
|
||||
for (size_t level = 0; level <= size; level++) { \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void*)node); \
|
||||
fprintf(os, ":f%zu -> ", level); \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void*)node->field.sle_next[level]); \
|
||||
fprintf(os, ":w%zu [];\n", level); \
|
||||
} \
|
||||
\
|
||||
if (node->field.sle_next[0]) \
|
||||
__skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_dot_finish_ \
|
||||
* Finalise the DOT file of the internal representation. \
|
||||
*/ \
|
||||
static void __skip_dot_finish_##decl(FILE *os, size_t nsg) { \
|
||||
if (nsg > 0) { \
|
||||
/* Link the nodes together with an invisible node. \
|
||||
* node0 [shape=record, label = "<f0> | <f1> | <f2> | <f3> | <f4> | <f5> | <f6> | <f7> | <f8> | ", \
|
||||
* style=invis, \
|
||||
* width=0.01]; \
|
||||
*/ \
|
||||
fprintf(os, "node0 [shape=record, label = \""); \
|
||||
for (size_t i = 0; i < nsg; ++i) { \
|
||||
fprintf(os, "<f%zu> | ", i); \
|
||||
} \
|
||||
fprintf(os, "\", style=invis, width=0.01];\n"); \
|
||||
\
|
||||
/* Now connect nodes with invisible edges \
|
||||
* \
|
||||
* node0:f0 -> HeadNode [style=invis]; \
|
||||
* node0:f1 -> HeadNode1 [style=invis]; \
|
||||
*/ \
|
||||
for (size_t i = 0; i < nsg; ++i) { \
|
||||
fprintf(os, "node0:f%zu -> HeadNode%zu [style=invis];\n", i, i);\
|
||||
} \
|
||||
nsg = 0; \
|
||||
} \
|
||||
fprintf(os, "}\n"); \
|
||||
} \
|
||||
\
|
||||
/* -- skip_dot_start_ */ \
|
||||
static int __skip_dot_start_##decl(FILE *os, decl##_t *slist, size_t nsg, skip_sprintf_node_##decl##_t fn) { \
|
||||
if (nsg == 0) { \
|
||||
fprintf(os, "digraph Skiplist {\n"); \
|
||||
fprintf(os, "label = \"Skiplist.\"\n"); \
|
||||
fprintf(os, "graph [rankdir = \"LR\"];\n"); \
|
||||
fprintf(os, "node [fontsize = \"12\" shape = \"ellipse\"];\n"); \
|
||||
fprintf(os, "edge [];\n\n"); \
|
||||
} \
|
||||
fprintf(os, "subgraph cluster%zu {\n", nsg); \
|
||||
fprintf(os, "style=dashed\n"); \
|
||||
fprintf(os, "label=\"Skip list iteration %zu\"\n\n", nsg); \
|
||||
fprintf(os, "\"HeadNode%zu\" [\n", nsg); \
|
||||
fprintf(os, "label = \""); \
|
||||
\
|
||||
/* Write out the head node fields */ \
|
||||
decl##_node_t *head = slist->slh_head; \
|
||||
size_t level; \
|
||||
if (SKIP_EMPTY(slist)) fprintf(os, "Empty HeadNode"); else { \
|
||||
level = ARRAY_LENGTH(head->field.sle_next) - 1; \
|
||||
do { \
|
||||
decl##_node_t *node = head->field.sle_next[level]; \
|
||||
fprintf(os, "{ <f%zu> %p }", level, (void *)node); \
|
||||
if (level != 0) fprintf(os, " | "); \
|
||||
} while(level--); \
|
||||
} \
|
||||
fprintf(os, "\"\n"); \
|
||||
fprintf(os, "shape = \"record\"\n"); \
|
||||
fprintf(os, "];\n"); \
|
||||
\
|
||||
/* Edges for head node */ \
|
||||
decl##_node_t *node = slist->slh_head; \
|
||||
level = 0; \
|
||||
do { \
|
||||
fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level); \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void*)node->field.sle_next[level]); \
|
||||
fprintf(os, ":w%zu [];\n", level); \
|
||||
} while(level++ < slist->level); \
|
||||
fprintf(os, "}\n\n"); \
|
||||
\
|
||||
/* Now all nodes via level 0, if non-empty */ \
|
||||
node = slist->slh_head; \
|
||||
if (ARRAY_LENGTH(node->field.sle_next)) \
|
||||
__skip_dot_node_##decl(os, slist, node, nsg, fn); \
|
||||
fprintf(os, "\n"); \
|
||||
\
|
||||
/* The tail, sentinal node */ \
|
||||
if (!SKIP_EMPTY(slist)) { \
|
||||
fprintf(os,"\"node%zu0x0\" [label = \"", nsg); \
|
||||
size_t level = slist->level; \
|
||||
do { \
|
||||
fprintf(os, "<w%zu> NULL", level); \
|
||||
if (level != 0) \
|
||||
fprintf(os, " | "); \
|
||||
} while(level-- > 0); \
|
||||
fprintf(os, "\" shape = \"record\"];\n"); \
|
||||
} \
|
||||
\
|
||||
/* End: "subgraph cluster1 {" */ \
|
||||
fprintf(os, "}\n\n"); \
|
||||
nsg += 1; \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_dot_ \
|
||||
* Create a DOT file of the internal representation of the \
|
||||
* Skiplist on the provided file descriptor (default: STDOUT). \
|
||||
* \
|
||||
* To view the output: \
|
||||
* $ dot -Tps filename.dot -o outfile.ps \
|
||||
* You can change the output format by varying the value after -T and \
|
||||
* choosing an appropriate filename extension after -o. \
|
||||
* See: https://graphviz.org/docs/outputs/ for the format options. \
|
||||
* \
|
||||
* https://en.wikipedia.org/wiki/DOT_(graph_description_language) \
|
||||
*/ \
|
||||
int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, skip_sprintf_node_##decl##_t fn) { \
|
||||
size_t nsg = 0; \
|
||||
if (__skip_integrity_check_##decl(slist) != 0) { \
|
||||
perror("Skiplist failed integrity checks, impossible to diagram.");\
|
||||
return -1; \
|
||||
} \
|
||||
if (os == NULL) \
|
||||
os = stdout; \
|
||||
if (!os) { \
|
||||
perror("Failed to open output file, unable to write DOT file.");\
|
||||
return -1; \
|
||||
} \
|
||||
__skip_dot_start_##decl(os, slist, nsg, fn); \
|
||||
__skip_dot_finish_##decl(os, nsg); \
|
||||
return 0; \
|
||||
} \
|
||||
/* END */
|
||||
#define SKIPLIST_DECL_DOT(decl, prefix, field) \
|
||||
\
|
||||
/* A type for a function that writes into a char[2048] buffer \
|
||||
* a description of the value within the node. */ \
|
||||
typedef void (*skip_sprintf_node_##decl##_t)(decl##_node_t *, char *); \
|
||||
\
|
||||
/* -- __skip_dot_node_ \
|
||||
* Writes out a fragment of a DOT file representing a node. \
|
||||
*/ \
|
||||
static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, \
|
||||
decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) \
|
||||
{ \
|
||||
char buf[2048]; \
|
||||
size_t level, height = ARRAY_LENGTH(node->field.sle_next); \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void *)node); \
|
||||
fprintf(os, " [label = \""); \
|
||||
level = height; \
|
||||
do { \
|
||||
fprintf(os, " { <w%zu> | <f%zu> %p }", level + 1, level + 1, \
|
||||
(void *)node->field.sle_next[level]); \
|
||||
if (level != 0) \
|
||||
fprintf(os, " | "); \
|
||||
} while (level--); \
|
||||
if (fn) { \
|
||||
fn(node, buf); \
|
||||
fprintf(os, " <f0> %s\"\n", buf); \
|
||||
} else { \
|
||||
fprintf(os, " <f0> ?\"\n"); \
|
||||
} \
|
||||
fprintf(os, "shape = \"record\"\n"); \
|
||||
fprintf(os, "];\n"); \
|
||||
\
|
||||
/* Now edges */ \
|
||||
level = 0; \
|
||||
for (level = 0; level < height; level++) { \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void *)node); \
|
||||
fprintf(os, ":f%zu -> ", level + 1); \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void *)node->field.sle_next[level]); \
|
||||
fprintf(os, ":w%zu [];\n", level + 1); \
|
||||
} \
|
||||
\
|
||||
if (node->field.sle_next[0] != SKIP_LAST(slist)) \
|
||||
__skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \
|
||||
} \
|
||||
\
|
||||
/* -- __skip_dot_finish_ \
|
||||
* Finalise the DOT file of the internal representation. \
|
||||
*/ \
|
||||
static void __skip_dot_finish_##decl(FILE *os, size_t nsg) \
|
||||
{ \
|
||||
size_t i; \
|
||||
if (nsg > 0) { \
|
||||
/* Link the nodes together with an invisible node. \
|
||||
* node0 [shape=record, label = "<f0> | <f1> | <f2> | <f3> | \
|
||||
* <f4> | <f5> | <f6> | <f7> | <f8> | ", style=invis, width=0.01]; \
|
||||
*/ \
|
||||
fprintf(os, "node0 [shape=record, label = \""); \
|
||||
for (i = 0; i < nsg; ++i) { \
|
||||
fprintf(os, "<f%zu> | ", i); \
|
||||
} \
|
||||
fprintf(os, "\", style=invis, width=0.01];\n"); \
|
||||
\
|
||||
/* Now connect nodes with invisible edges \
|
||||
* \
|
||||
* node0:f0 -> HeadNode [style=invis]; \
|
||||
* node0:f1 -> HeadNode1 [style=invis]; \
|
||||
*/ \
|
||||
for (i = 0; i < nsg; ++i) { \
|
||||
fprintf(os, "node0:f%zu -> HeadNode%zu [style=invis];\n", i, i); \
|
||||
} \
|
||||
nsg = 0; \
|
||||
} \
|
||||
fprintf(os, "}\n"); \
|
||||
} \
|
||||
\
|
||||
/* -- skip_dot_start_ */ \
|
||||
static int __skip_dot_start_##decl(FILE *os, decl##_t *slist, size_t nsg, \
|
||||
skip_sprintf_node_##decl##_t fn) \
|
||||
{ \
|
||||
size_t level; \
|
||||
decl##_node_t *head; \
|
||||
if (nsg == 0) { \
|
||||
fprintf(os, "digraph Skiplist {\n"); \
|
||||
fprintf(os, "label = \"Skiplist.\"\n"); \
|
||||
fprintf(os, "graph [rankdir = \"LR\"];\n"); \
|
||||
fprintf(os, "node [fontsize = \"12\" shape = \"ellipse\"];\n"); \
|
||||
fprintf(os, "edge [];\n\n"); \
|
||||
} \
|
||||
fprintf(os, "subgraph cluster%zu {\n", nsg); \
|
||||
fprintf(os, "style=dashed\n"); \
|
||||
fprintf(os, "label=\"Skip list iteration %zu\"\n\n", nsg); \
|
||||
fprintf(os, "\"HeadNode%zu\" [\n", nsg); \
|
||||
fprintf(os, "label = \""); \
|
||||
\
|
||||
/* Write out the fields */ \
|
||||
head = slist->slh_head; \
|
||||
if (SKIP_EMPTY(slist)) \
|
||||
fprintf(os, "Empty HeadNode"); \
|
||||
else { \
|
||||
level = ARRAY_LENGTH(head->field.sle_next); \
|
||||
while (level--) { \
|
||||
decl##_node_t *node = head->field.sle_next[level]; \
|
||||
fprintf(os, "{ <f%zu> %p }", level + 1, (void *)node); \
|
||||
if (level + 1 != 0) \
|
||||
fprintf(os, " | "); \
|
||||
} \
|
||||
} \
|
||||
fprintf(os, "\"\n"); \
|
||||
fprintf(os, "shape = \"record\"\n"); \
|
||||
fprintf(os, "];\n"); \
|
||||
\
|
||||
/* Edges for head node */ \
|
||||
decl##_node_t *node = slist->slh_head; \
|
||||
level = 0; \
|
||||
do { \
|
||||
fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level + 1); \
|
||||
fprintf(os, "\"node%zu%p\"", nsg, (void *)node->field.sle_next[level]); \
|
||||
fprintf(os, ":w%zu [];\n", level + 1); \
|
||||
} while (level++ < slist->level); \
|
||||
fprintf(os, "}\n\n"); \
|
||||
\
|
||||
/* Now all nodes via level 0, if non-empty */ \
|
||||
node = slist->slh_head; \
|
||||
if (ARRAY_LENGTH(node->field.sle_next)) \
|
||||
__skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \
|
||||
fprintf(os, "\n"); \
|
||||
\
|
||||
/* The tail, sentinal node */ \
|
||||
if (!SKIP_EMPTY(slist)) { \
|
||||
fprintf(os, "\"node%zu0x0\" [label = \"", nsg); \
|
||||
level = slist->level; \
|
||||
do { \
|
||||
fprintf(os, "<w%zu> NULL", level + 1); \
|
||||
if (level != 0) \
|
||||
fprintf(os, " | "); \
|
||||
} while (level-- > 0); \
|
||||
fprintf(os, "\" shape = \"record\"];\n"); \
|
||||
} \
|
||||
\
|
||||
/* End: "subgraph cluster1 {" */ \
|
||||
fprintf(os, "}\n\n"); \
|
||||
nsg += 1; \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
/* -- skip_dot_ \
|
||||
* Create a DOT file of the internal representation of the \
|
||||
* Skiplist on the provided file descriptor (default: STDOUT). \
|
||||
* \
|
||||
* To view the output: \
|
||||
* $ dot -Tps filename.dot -o outfile.ps \
|
||||
* You can change the output format by varying the value after -T and \
|
||||
* choosing an appropriate filename extension after -o. \
|
||||
* See: https://graphviz.org/docs/outputs/ for the format options. \
|
||||
* \
|
||||
* https://en.wikipedia.org/wiki/DOT_(graph_description_language) \
|
||||
*/ \
|
||||
int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, \
|
||||
skip_sprintf_node_##decl##_t fn) \
|
||||
{ \
|
||||
size_t nsg = 0; \
|
||||
if (__skip_integrity_check_##decl(slist) != 0) { \
|
||||
perror("Skiplist failed integrity checks, impossible to diagram."); \
|
||||
return -1; \
|
||||
} \
|
||||
if (os == NULL) \
|
||||
os = stdout; \
|
||||
if (!os) { \
|
||||
perror("Failed to open output file, unable to write DOT file."); \
|
||||
return -1; \
|
||||
} \
|
||||
__skip_dot_start_##decl(os, slist, nsg, fn); \
|
||||
__skip_dot_finish_##decl(os, nsg); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#if 0
|
||||
#define SKIP_REMOVE(head, elm, field) do { \
|
||||
if ((elm)->field.le_next != NULL) \
|
||||
(elm)->field.le_next->field.le_prev = \
|
||||
(elm)->field.le_prev; \
|
||||
*(elm)->field.le_prev = (elm)->field.le_next; \
|
||||
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||
_Q_INVALIDATE((elm)->field.le_next); \
|
||||
} while (0)
|
||||
#define SKIP_REMOVE(head, elm, field) \
|
||||
do { \
|
||||
if ((elm)->field.le_next != NULL) \
|
||||
(elm)->field.le_next->field.le_prev = (elm)->field.le_prev; \
|
||||
*(elm)->field.le_prev = (elm)->field.le_next; \
|
||||
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||
_Q_INVALIDATE((elm)->field.le_next); \
|
||||
} while (0)
|
||||
|
||||
#define SKIP_REPLACE(elm, elm2, field) do { \
|
||||
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
|
||||
(elm2)->field.le_next->field.le_prev = \
|
||||
&(elm2)->field.le_next; \
|
||||
(elm2)->field.le_prev = (elm)->field.le_prev; \
|
||||
*(elm2)->field.le_prev = (elm2); \
|
||||
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||
_Q_INVALIDATE((elm)->field.le_next); \
|
||||
} while (0)
|
||||
#define SKIP_REPLACE(elm, elm2, field) \
|
||||
do { \
|
||||
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
|
||||
(elm2)->field.le_next->field.le_prev = &(elm2)->field.le_next; \
|
||||
(elm2)->field.le_prev = (elm)->field.le_prev; \
|
||||
*(elm2)->field.le_prev = (elm2); \
|
||||
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||
_Q_INVALIDATE((elm)->field.le_next); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#endif /* _SYS_SKIPLIST_H_ */
|
||||
|
|
1336
src/skiplist.c
1336
src/skiplist.c
File diff suppressed because it is too large
Load diff
164
tests/api.c
164
tests/api.c
|
@ -1,196 +1,196 @@
|
|||
static void *
|
||||
test_api_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
struct test_info *info = (struct test_info *)user_data;
|
||||
(void)info;
|
||||
(void)params;
|
||||
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;
|
||||
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);
|
||||
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 void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_insert(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
assert_ptr_not_null(data);
|
||||
int n = munit_rand_int_range(128, 4096);
|
||||
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
|
||||
while (n--) {
|
||||
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
assert_ptr_not_null(data);
|
||||
int n = munit_rand_int_range(128, 4096);
|
||||
int key = munit_rand_int_range(0, (((uint32_t)0) - 1) / 10);
|
||||
while (n--) {
|
||||
ex_node_t *node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_remove_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_remove_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_remove(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_find_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
|
2508
tests/munit.c
2508
tests/munit.c
File diff suppressed because it is too large
Load diff
349
tests/munit.h
349
tests/munit.h
|
@ -29,7 +29,7 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#define MUNIT_VERSION(major, minor, revision) \
|
||||
(((major) << 16) | ((minor) << 8) | (revision))
|
||||
(((major) << 16) | ((minor) << 8) | (revision))
|
||||
|
||||
#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
|
||||
|
||||
|
@ -132,7 +132,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
|
||||
!defined(__PGI)
|
||||
!defined(__PGI)
|
||||
#define MUNIT_ARRAY_PARAM(name) name
|
||||
#else
|
||||
#define MUNIT_ARRAY_PARAM(name)
|
||||
|
@ -164,7 +164,7 @@ extern "C" {
|
|||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
__pragma(warning(push)) __pragma(warning(disable : 4127))
|
||||
__pragma(warning(push)) __pragma(warning(disable : 4127))
|
||||
#define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))
|
||||
#else
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_
|
||||
|
@ -172,25 +172,25 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
typedef enum {
|
||||
MUNIT_LOG_DEBUG,
|
||||
MUNIT_LOG_INFO,
|
||||
MUNIT_LOG_WARNING,
|
||||
MUNIT_LOG_ERROR
|
||||
MUNIT_LOG_DEBUG,
|
||||
MUNIT_LOG_INFO,
|
||||
MUNIT_LOG_WARNING,
|
||||
MUNIT_LOG_ERROR
|
||||
} MunitLogLevel;
|
||||
|
||||
#if defined(__GNUC__) && !defined(__MINGW32__)
|
||||
#define MUNIT_PRINTF(string_index, first_to_check) \
|
||||
__attribute__((format(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, ...);
|
||||
const char *format, ...);
|
||||
|
||||
#define munit_logf(level, format, ...) \
|
||||
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
|
||||
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
|
||||
|
||||
#define munit_log(level, msg) munit_logf(level, "%s", msg)
|
||||
|
||||
|
@ -199,170 +199,167 @@ MUNIT_PRINTF(3, 4)
|
|||
void munit_errorf_ex(const char *filename, int line, const char *format, ...);
|
||||
|
||||
#define munit_errorf(format, ...) \
|
||||
munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
|
||||
munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
|
||||
|
||||
#define munit_error(msg) munit_errorf("%s", msg)
|
||||
|
||||
#define munit_assert(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#define munit_assert(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_true(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr " is not true"); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#define munit_assert_true(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(expr)) { \
|
||||
munit_error("assertion failed: " #expr " is not true"); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#define munit_assert_false(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(!(expr))) { \
|
||||
munit_error("assertion failed: " #expr " is not false"); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#define munit_assert_false(expr) \
|
||||
do { \
|
||||
if (!MUNIT_LIKELY(!(expr))) { \
|
||||
munit_error("assertion failed: " #expr " is not false"); \
|
||||
} \
|
||||
MUNIT_PUSH_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 ")", \
|
||||
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_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 ")", \
|
||||
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_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)
|
||||
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)
|
||||
#define munit_assert_uchar(a, op, b) \
|
||||
munit_assert_type_full("'\\x", "'", unsigned char, \
|
||||
"02" MUNIT_CHAR_MODIFIER "x", a, op, b)
|
||||
#define munit_assert_char(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)
|
||||
#define munit_assert_short(a, op, b) \
|
||||
munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", 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)
|
||||
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_uint(a, op, b) \
|
||||
munit_assert_type(unsigned int, "u", 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_ulong(a, op, b) \
|
||||
munit_assert_type(unsigned long int, "lu", a, op, b)
|
||||
munit_assert_type(unsigned long int, "lu", a, op, b)
|
||||
#define munit_assert_llong(a, op, b) \
|
||||
munit_assert_type(long long int, "lld", a, op, b)
|
||||
munit_assert_type(long long int, "lld", a, op, b)
|
||||
#define munit_assert_ullong(a, op, b) \
|
||||
munit_assert_type(unsigned long long int, "llu", a, op, b)
|
||||
munit_assert_type(unsigned long long int, "llu", a, op, b)
|
||||
|
||||
#define munit_assert_size(a, op, b) \
|
||||
munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", 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_ptr(a, op, b) \
|
||||
munit_assert_type(const void *, "p", a, op, b)
|
||||
munit_assert_type(const void *, "p", a, op, b)
|
||||
|
||||
#define munit_assert_int8(a, op, b) \
|
||||
munit_assert_type(munit_int8_t, PRIi8, a, op, b)
|
||||
munit_assert_type(munit_int8_t, PRIi8, a, op, b)
|
||||
#define munit_assert_uint8(a, op, b) \
|
||||
munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
|
||||
munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
|
||||
#define munit_assert_int16(a, op, b) \
|
||||
munit_assert_type(munit_int16_t, PRIi16, a, op, b)
|
||||
munit_assert_type(munit_int16_t, PRIi16, a, op, b)
|
||||
#define munit_assert_uint16(a, op, b) \
|
||||
munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
|
||||
munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
|
||||
#define munit_assert_int32(a, op, b) \
|
||||
munit_assert_type(munit_int32_t, PRIi32, a, op, b)
|
||||
munit_assert_type(munit_int32_t, PRIi32, a, op, b)
|
||||
#define munit_assert_uint32(a, op, b) \
|
||||
munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
|
||||
munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
|
||||
#define munit_assert_int64(a, op, b) \
|
||||
munit_assert_type(munit_int64_t, PRIi64, a, op, b)
|
||||
munit_assert_type(munit_int64_t, PRIi64, a, op, b)
|
||||
#define munit_assert_uint64(a, op, b) \
|
||||
munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
|
||||
munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
|
||||
|
||||
#define munit_assert_double_equal(a, b, precision) \
|
||||
do { \
|
||||
const double munit_tmp_a_ = (a); \
|
||||
const double munit_tmp_b_ = (b); \
|
||||
const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
|
||||
-(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)", \
|
||||
#a, #b, munit_tmp_a_, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#define munit_assert_double_equal(a, b, precision) \
|
||||
do { \
|
||||
const double munit_tmp_a_ = (a); \
|
||||
const double munit_tmp_b_ = (b); \
|
||||
const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
|
||||
-(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)", \
|
||||
#a, #b, munit_tmp_a_, munit_tmp_b_); \
|
||||
} \
|
||||
MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
|
||||
#include <string.h>
|
||||
#define munit_assert_string_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_PUSH_DISABLE_MSVC_C4127_ \
|
||||
} while (0) MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#define munit_assert_string_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_PUSH_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_PUSH_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_PUSH_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) { \
|
||||
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", \
|
||||
#a, #b, munit_tmp_pos_); \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
MUNIT_PUSH_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) { \
|
||||
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", \
|
||||
#a, #b, munit_tmp_pos_); \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
MUNIT_PUSH_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_); \
|
||||
} \
|
||||
MUNIT_PUSH_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_); \
|
||||
} \
|
||||
MUNIT_PUSH_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)
|
||||
|
@ -390,54 +387,54 @@ 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)]);
|
||||
munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
|
||||
|
||||
/*** Tests and Suites ***/
|
||||
|
||||
typedef enum {
|
||||
/* Test successful */
|
||||
MUNIT_OK,
|
||||
/* Test failed */
|
||||
MUNIT_FAIL,
|
||||
/* Test was skipped */
|
||||
MUNIT_SKIP,
|
||||
/* Test failed due to circumstances not intended to be tested
|
||||
* (things like network errors, invalid parameter value, failure to
|
||||
* allocate memory in the test harness, etc.). */
|
||||
MUNIT_ERROR
|
||||
/* Test successful */
|
||||
MUNIT_OK,
|
||||
/* Test failed */
|
||||
MUNIT_FAIL,
|
||||
/* Test was skipped */
|
||||
MUNIT_SKIP,
|
||||
/* Test failed due to circumstances not intended to be tested
|
||||
* (things like network errors, invalid parameter value, failure to
|
||||
* allocate memory in the test harness, etc.). */
|
||||
MUNIT_ERROR
|
||||
} MunitResult;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char **values;
|
||||
char *name;
|
||||
char **values;
|
||||
} MunitParameterEnum;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *value;
|
||||
char *name;
|
||||
char *value;
|
||||
} MunitParameter;
|
||||
|
||||
const char *munit_parameters_get(const MunitParameter params[],
|
||||
const char *key);
|
||||
const char *key);
|
||||
|
||||
typedef enum {
|
||||
MUNIT_TEST_OPTION_NONE = 0,
|
||||
MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,
|
||||
MUNIT_TEST_OPTION_TODO = 1 << 1
|
||||
MUNIT_TEST_OPTION_NONE = 0,
|
||||
MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,
|
||||
MUNIT_TEST_OPTION_TODO = 1 << 1
|
||||
} MunitTestOptions;
|
||||
|
||||
typedef MunitResult (
|
||||
*MunitTestFunc)(const MunitParameter params[], void *user_data_or_fixture);
|
||||
*MunitTestFunc)(const MunitParameter params[], void *user_data_or_fixture);
|
||||
typedef void *(*MunitTestSetup)(const MunitParameter params[], void *user_data);
|
||||
typedef void (*MunitTestTearDown)(void *fixture);
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
MunitTestFunc test;
|
||||
MunitTestSetup setup;
|
||||
MunitTestTearDown tear_down;
|
||||
MunitTestOptions options;
|
||||
MunitParameterEnum *parameters;
|
||||
char *name;
|
||||
MunitTestFunc test;
|
||||
MunitTestSetup setup;
|
||||
MunitTestTearDown tear_down;
|
||||
MunitTestOptions options;
|
||||
MunitParameterEnum *parameters;
|
||||
} MunitTest;
|
||||
|
||||
typedef enum { MUNIT_SUITE_OPTION_NONE = 0 } MunitSuiteOptions;
|
||||
|
@ -445,15 +442,15 @@ typedef enum { MUNIT_SUITE_OPTION_NONE = 0 } MunitSuiteOptions;
|
|||
typedef struct MunitSuite_ MunitSuite;
|
||||
|
||||
struct MunitSuite_ {
|
||||
char *prefix;
|
||||
MunitTest *tests;
|
||||
MunitSuite *suites;
|
||||
unsigned int iterations;
|
||||
MunitSuiteOptions options;
|
||||
char *prefix;
|
||||
MunitTest *tests;
|
||||
MunitSuite *suites;
|
||||
unsigned int iterations;
|
||||
MunitSuiteOptions options;
|
||||
};
|
||||
|
||||
int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
|
||||
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. */
|
||||
|
@ -461,15 +458,15 @@ int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
|
|||
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)]);
|
||||
void (*write_help)(const MunitArgument *argument, void *user_data);
|
||||
char *name;
|
||||
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)],
|
||||
const MunitArgument arguments[]);
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)],
|
||||
const MunitArgument arguments[]);
|
||||
|
||||
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
|
||||
|
||||
|
@ -500,12 +497,12 @@ int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc,
|
|||
#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)
|
||||
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)
|
||||
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)
|
||||
|
|
439
tests/test.c
439
tests/test.c
|
@ -29,55 +29,55 @@
|
|||
#define __SLD_P(...) printf(__VA_ARGS__)
|
||||
#elif __SL_DEBUG >= 3
|
||||
typedef struct dbg_node {
|
||||
sl_node snode;
|
||||
int value;
|
||||
sl_node snode;
|
||||
int value;
|
||||
} dbg_node_t;
|
||||
|
||||
inline void
|
||||
__sld_rt_ins(int error_code, sl_node *node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[INS] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[INS] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_nc_ins(sl_node *node, sl_node *next_node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
|
||||
printf("[INS] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
printf("[INS] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_rt_rmv(int error_code, sl_node *node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] retry (code %d) "
|
||||
"%p (top %d, cur %d) %d\n",
|
||||
error_code, node, top_layer, cur_layer, ddd->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_nc_rmv(sl_node *node, sl_node *next_node, int top_layer, int cur_layer)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
dbg_node_t *ddd_next = sl_get_entry(next_node, dbg_node_t, snode);
|
||||
|
||||
printf("[RMV] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
printf("[RMV] next node changed, "
|
||||
"%p %p (top %d, cur %d) %d %d\n",
|
||||
node, next_node, top_layer, cur_layer, ddd->value, ddd_next->value);
|
||||
}
|
||||
|
||||
inline void
|
||||
__sld_bm(sl_node *node)
|
||||
{
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] node is being modified %d\n", ddd->value);
|
||||
dbg_node_t *ddd = sl_get_entry(node, dbg_node_t, snode);
|
||||
printf("[RMV] node is being modified %d\n", ddd->value);
|
||||
}
|
||||
|
||||
#define __SLD_RT_INS(e, n, t, c) __sld_rt_ins(e, n, t, c)
|
||||
|
@ -95,13 +95,13 @@ __sld_bm(sl_node *node)
|
|||
#endif
|
||||
|
||||
struct user_data {
|
||||
size_t n_ele;
|
||||
size_t n_ele;
|
||||
};
|
||||
|
||||
typedef struct ex_node {
|
||||
sl_node snode;
|
||||
uint32_t key;
|
||||
uint32_t value;
|
||||
sl_node snode;
|
||||
uint32_t key;
|
||||
uint32_t value;
|
||||
} ex_node_t;
|
||||
|
||||
typedef sl_raw ex_sl_t;
|
||||
|
@ -109,340 +109,341 @@ typedef sl_raw ex_sl_t;
|
|||
static int
|
||||
uint32_key_cmp(sl_node *a, sl_node *b, void *aux)
|
||||
{
|
||||
ex_node_t *aa, *bb;
|
||||
(void)aux;
|
||||
aa = sl_get_entry(a, ex_node_t, snode);
|
||||
bb = sl_get_entry(b, ex_node_t, snode);
|
||||
ex_node_t *aa, *bb;
|
||||
(void)aux;
|
||||
aa = sl_get_entry(a, ex_node_t, snode);
|
||||
bb = sl_get_entry(b, ex_node_t, snode);
|
||||
|
||||
if (aa->key < bb->key)
|
||||
return -1;
|
||||
if (aa->key > bb->key)
|
||||
return 1;
|
||||
return 0;
|
||||
if (aa->key < bb->key)
|
||||
return -1;
|
||||
if (aa->key > bb->key)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
__populate_slist(ex_sl_t *slist){
|
||||
size_t inserted = 0;
|
||||
uint32_t n, key;
|
||||
ex_node_t *node;
|
||||
__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;
|
||||
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_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
struct test_info *info = (struct test_info *)user_data;
|
||||
(void)info;
|
||||
(void)params;
|
||||
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;
|
||||
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);
|
||||
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 void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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_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;
|
||||
}
|
||||
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;
|
||||
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);
|
||||
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;
|
||||
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;
|
||||
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)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
|
||||
ex_node_t *node;
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
node = calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = i;
|
||||
node->value = i;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return (void *)slist;
|
||||
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
|
||||
ex_node_t *node;
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
node = calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
sl_init_node(&node->snode);
|
||||
node->key = i;
|
||||
node->value = i;
|
||||
sl_insert(slist, &node->snode);
|
||||
}
|
||||
return (void *)slist;
|
||||
}
|
||||
static void
|
||||
test_api_find_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_find(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
|
||||
/* find equal every value */
|
||||
assert_ptr_not_null(data);
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
ex_node_t query;
|
||||
query.key = i;
|
||||
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);
|
||||
assert_uint32(entry->key, ==, i);
|
||||
}
|
||||
/* find equal every value */
|
||||
assert_ptr_not_null(data);
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
ex_node_t query;
|
||||
query.key = i;
|
||||
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);
|
||||
assert_uint32(entry->key, ==, i);
|
||||
}
|
||||
|
||||
/* */
|
||||
return MUNIT_OK;
|
||||
/* */
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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);
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(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;
|
||||
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 },
|
||||
{ (char *)"/api/remove", test_api_remove, test_api_remove_setup,
|
||||
test_api_remove_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/find", test_api_find, test_api_find_setup,
|
||||
test_api_find_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/update", test_api_update, test_api_update_setup,
|
||||
test_api_update_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/delete", test_api_delete, test_api_delete_setup,
|
||||
test_api_delete_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/duplicates", test_api_duplicates, test_api_duplicates_setup,
|
||||
test_api_duplicates_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/size", test_api_size, test_api_size_setup,
|
||||
test_api_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/iterators", test_api_iterators, test_api_iterators_setup,
|
||||
test_api_iterators_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
{ (char *)"/api/insert", test_api_insert, test_api_insert_setup,
|
||||
test_api_insert_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/remove", test_api_remove, test_api_remove_setup,
|
||||
test_api_remove_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/find", test_api_find, test_api_find_setup,
|
||||
test_api_find_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/update", test_api_update, test_api_update_setup,
|
||||
test_api_update_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/delete", test_api_delete, test_api_delete_setup,
|
||||
test_api_delete_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/duplicates", test_api_duplicates, test_api_duplicates_setup,
|
||||
test_api_duplicates_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/size", test_api_size, test_api_size_setup,
|
||||
test_api_size_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/api/iterators", test_api_iterators, test_api_iterators_setup,
|
||||
test_api_iterators_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
};
|
||||
|
||||
static MunitTest mt_tests[] = { { NULL, NULL, NULL, NULL,
|
||||
MUNIT_TEST_OPTION_NONE, NULL } };
|
||||
MUNIT_TEST_OPTION_NONE, NULL } };
|
||||
|
||||
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL,
|
||||
MUNIT_TEST_OPTION_NONE, 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 } };
|
||||
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 };
|
||||
other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
|
||||
|
||||
int
|
||||
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
||||
{
|
||||
struct user_data info;
|
||||
return munit_suite_main(&main_test_suite, (void *)&info, argc, argv);
|
||||
struct user_data info;
|
||||
return munit_suite_main(&main_test_suite, (void *)&info, argc, argv);
|
||||
}
|
||||
|
||||
/* ARGS: --no-fork --seed 8675309 */
|
||||
|
|
Loading…
Reference in a new issue