transitioned from C++ to C99
This commit is contained in:
parent
b6ccffe809
commit
3c341a1429
|
@ -99,10 +99,10 @@ IndentPPDirectives: None
|
|||
Language: Cpp
|
||||
NamespaceIndentation: None
|
||||
PointerAlignment: Right
|
||||
ContinuationIndentWidth: 4
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
ColumnLimit: 160
|
||||
ContinuationIndentWidth: 2
|
||||
IndentWidth: 2
|
||||
TabWidth: 2
|
||||
ColumnLimit: 80
|
||||
UseTab: Never
|
||||
SpaceAfterCStyleCast: false
|
||||
IncludeBlocks: Regroup
|
||||
|
|
|
@ -20,6 +20,7 @@ Checks: >
|
|||
-misc-unused-parameters,
|
||||
-misc-no-recursion,
|
||||
-performance-no-int-to-ptr,
|
||||
-readability-braces-around-statements,
|
||||
-bugprone-assignment-in-if-condition,
|
||||
|
||||
...
|
||||
|
|
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,8 +1,8 @@
|
|||
libsparesemap.a
|
||||
libsparesemap.so
|
||||
*.a
|
||||
*.so
|
||||
**/*.o
|
||||
tests/test
|
||||
examples/main
|
||||
examples/ex_??
|
||||
.cache
|
||||
hints.txt
|
||||
tmp/
|
||||
|
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
7
.idea/codeStyles/Project.xml
Normal file
7
.idea/codeStyles/Project.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<clangFormatSettings>
|
||||
<option name="ENABLED" value="true" />
|
||||
</clangFormatSettings>
|
||||
</code_scheme>
|
||||
</component>
|
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
||||
</state>
|
||||
</component>
|
7
.idea/inspectionProfiles/Project_Default.xml
Normal file
7
.idea/inspectionProfiles/Project_Default.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="TsLint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
26
.idea/misc.xml
Normal file
26
.idea/misc.xml
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CidrRootsConfiguration">
|
||||
<sourceRoots>
|
||||
<file path="$PROJECT_DIR$/examples" />
|
||||
<file path="$PROJECT_DIR$/include" />
|
||||
<file path="$PROJECT_DIR$/src" />
|
||||
<file path="$PROJECT_DIR$/tests" />
|
||||
</sourceRoots>
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -1,3 +1,13 @@
|
|||
#if defined(__GNUC__) && (__GNUC__ >= 4)
|
||||
|
||||
// Default to using the GCC builtin popcount. On architectures with
|
||||
// -march popcnt, this compiles to a single popcnt instruction.
|
||||
#ifndef popcountll
|
||||
#define popcountll __builtin_popcountll
|
||||
#else
|
||||
#define popcountll sux_popcountll
|
||||
#endif
|
||||
#else
|
||||
/*
|
||||
*
|
||||
* from https://github.com/efficient/rankselect/popcount.h
|
||||
|
@ -49,7 +59,7 @@
|
|||
// http://sux.dsi.unimi.it/paper.pdf p4
|
||||
// This variant uses multiplication for the last summation instead of
|
||||
// continuing the shift/mask/addition chain.
|
||||
inline int suxpopcount(uint64_t x) {
|
||||
inline int sux_popcountll(uint64_t x) {
|
||||
// Step 1: 00 - 00 = 0; 01 - 00 = 01; 10 - 01 = 01; 11 - 01 = 10;
|
||||
x = x - ((x & G2) >> 1);
|
||||
// step 2: add 2 groups of 2.
|
||||
|
@ -61,12 +71,5 @@ inline int suxpopcount(uint64_t x) {
|
|||
return x;
|
||||
}
|
||||
|
||||
// Default to using the GCC builtin popcount. On architectures
|
||||
// with -march popcnt, this compiles to a single popcnt instruction.
|
||||
#ifndef popcount
|
||||
# define popcount __builtin_popcountll
|
||||
#else
|
||||
# define popcount suxpopcount
|
||||
#endif
|
||||
|
||||
#endif /* _FASTRANK_POPCOUNT_H_ */
|
||||
#endif
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Sparsemap
|
||||
*
|
||||
* This is an implementation for a sparse, compressed bitmap. It is resizable
|
||||
* and mutable, with reasonable performance for random access modifications
|
||||
* and lookups.
|
||||
*
|
||||
* The implementation is separated into tiers.
|
||||
*
|
||||
* Tier 0 (lowest): bits are stored in a sm_bitvec_t (uint64_t).
|
||||
*
|
||||
* Tier 1 (middle): multiple sm_bitvec_t are managed in a chunk map. The chunk
|
||||
* map only stores those sm_bitvec_t that have a mixed payload of bits (i.e.
|
||||
* some bits are 1, some are 0). As soon as ALL bits in a sm_bitvec_t are
|
||||
* identical, this sm_bitvec_t is no longer stored, it is compressed.
|
||||
*
|
||||
* The chunk maps store additional flags (2 bit) for each sm_bitvec_t in an
|
||||
* additional word (same size as the sm_bitvec_t itself).
|
||||
*
|
||||
* 00 11 22 33
|
||||
* ^-- descriptor for sm_bitvec_t 1
|
||||
* ^-- descriptor for sm_bitvec_t 2
|
||||
* ^-- descriptor for sm_bitvec_t 3
|
||||
* ^-- descriptor for sm_bitvec_t 4
|
||||
*
|
||||
* Those flags (*) can have one of the following values:
|
||||
*
|
||||
* 00 The sm_bitvec_t is all zero -> sm_bitvec_t is not stored
|
||||
* 11 The sm_bitvec_t is all one -> sm_bitvec_t is not stored
|
||||
* 10 The sm_bitvec_t contains a bitmap -> sm_bitvec_t is stored
|
||||
* 01 The sm_bitvec_t is not used (**)
|
||||
*
|
||||
* The serialized size of a chunk map in memory therefore is at least
|
||||
* one sm_bitvec_t for the flags, and (optionally) additional sm_bitvec_ts
|
||||
* if they are required.
|
||||
*
|
||||
* (*) The code comments often use the Erlang format for binary
|
||||
* representation, i.e. 2#10 for (binary) 01.
|
||||
*
|
||||
* (**) This flag is set to reduce the capacity of a chunk map.
|
||||
*
|
||||
* Tier 2 (highest): the Sparsemap manages multiple chunk maps. Each chunk
|
||||
* has its own offset (relative to the offset of the Sparsemap). In
|
||||
* addition, the Sparsemap manages the memory of the chunk maps, and
|
||||
* is able to grow or shrink that memory as required.
|
||||
*/
|
||||
#ifndef SPARSEMAP_H
|
||||
#define SPARSEMAP_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* The public interface for a sparse bit-mapped index, a "sparse map".
|
||||
*
|
||||
* |sm_idx_t| is the user's numerical data type which is mapped to a single bit
|
||||
* in the bitmap. Usually this is uint32_t or uint64_t. |sm_bitvec_t| is the
|
||||
* storage type for a bit vector used by the __sm_chunk_t internal maps.
|
||||
* Usually this is an uint64_t.
|
||||
*/
|
||||
|
||||
typedef uint32_t sm_idx_t;
|
||||
typedef uint64_t sm_bitvec_t;
|
||||
|
||||
typedef struct sparsemap {
|
||||
uint8_t *m_data; /* The serialized bitmap data */
|
||||
size_t m_data_size; /* The total size of m_data */
|
||||
size_t m_data_used; /* The used size of m_data */
|
||||
} sparsemap_t;
|
||||
|
||||
/* Allocate on a sparsemap_t on the heap and initialize it. */
|
||||
sparsemap_t *sparsemap(uint8_t *data, size_t size, size_t used);
|
||||
|
||||
/* Initialize sparsemap_t with data. */
|
||||
void sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size, size_t used);
|
||||
|
||||
/* Opens an existing sparsemap at the specified buffer. */
|
||||
sparsemap_t *sparsemap_open(uint8_t *data, size_t data_size);
|
||||
|
||||
/* Resizes the data range. */
|
||||
void sparsemap_set_data_size(sparsemap_t *map, size_t data_size);
|
||||
|
||||
/* Returns the size of the underlying byte array. */
|
||||
size_t sparsemap_get_range_size(sparsemap_t *map);
|
||||
|
||||
/* Returns the value of a bit at index |idx|. */
|
||||
bool sparsemap_is_set(sparsemap_t *map, size_t idx);
|
||||
|
||||
/* Sets the bit at index |idx| to true or false, depending on |value|. */
|
||||
void sparsemap_set(sparsemap_t *map, size_t idx, bool value);
|
||||
|
||||
/* Returns the offset of the very first bit. */
|
||||
sm_idx_t sparsemap_get_start_offset(sparsemap_t *map);
|
||||
|
||||
/* Returns the used size in the data buffer. */
|
||||
size_t sparsemap_get_size(sparsemap_t *map);
|
||||
|
||||
/* Decompresses the whole bitmap; calls scanner for all bits. */
|
||||
void sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t),
|
||||
size_t skip);
|
||||
|
||||
/* Appends all chunk maps from |map| starting at |sstart| to |other|, then
|
||||
reduces the chunk map-count appropriately. */
|
||||
void sparsemap_split(sparsemap_t *map, size_t sstart, sparsemap_t *other);
|
||||
|
||||
/* Returns the index of the n'th set bit; uses a 0-based index. */
|
||||
size_t sparsemap_select(sparsemap_t *map, size_t n);
|
||||
|
||||
/* Counts the set bits in the range [offset, idx]. */
|
||||
size_t sparsemap_rank(sparsemap_t *map, size_t offset, size_t idx);
|
||||
|
||||
#endif
|
2098
src/sparsemap.c
2098
src/sparsemap.c
File diff suppressed because it is too large
Load diff
196
tests/api.c
Normal file
196
tests/api.c
Normal file
|
@ -0,0 +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;
|
||||
|
||||
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
|
||||
if (slist == NULL)
|
||||
return NULL;
|
||||
sl_init(slist, uint32_key_cmp);
|
||||
return (void *)(uintptr_t)slist;
|
||||
}
|
||||
|
||||
static void
|
||||
test_api_tear_down(void *fixture)
|
||||
{
|
||||
ex_sl_t *slist = (ex_sl_t *)fixture;
|
||||
assert_ptr_not_null(slist);
|
||||
sl_node *cursor = sl_begin(slist);
|
||||
while (cursor) {
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
assert_ptr_not_null(entry);
|
||||
assert_uint32(entry->key, ==, entry->value);
|
||||
cursor = sl_next(slist, cursor);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
sl_free(slist);
|
||||
free(fixture);
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_insert(const MunitParameter params[], void *data)
|
||||
{
|
||||
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);
|
||||
}
|
||||
static void
|
||||
test_api_remove_tear_down(void *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;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_find_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_find(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_update(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_delete_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_delete(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_duplicates(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_size_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_size(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_iterators_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_iterators(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
527
tests/munit.h
Normal file
527
tests/munit.h
Normal file
|
@ -0,0 +1,527 @@
|
|||
/* µnit Testing Framework
|
||||
* Copyright (c) 2013-2017 Evan Nemerson <evan@nemerson.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(MUNIT_H)
|
||||
#define MUNIT_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MUNIT_VERSION(major, minor, revision) \
|
||||
(((major) << 16) | ((minor) << 8) | (revision))
|
||||
|
||||
#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1600)
|
||||
#define munit_int8_t __int8
|
||||
#define munit_uint8_t unsigned __int8
|
||||
#define munit_int16_t __int16
|
||||
#define munit_uint16_t unsigned __int16
|
||||
#define munit_int32_t __int32
|
||||
#define munit_uint32_t unsigned __int32
|
||||
#define munit_int64_t __int64
|
||||
#define munit_uint64_t unsigned __int64
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#define munit_int8_t int8_t
|
||||
#define munit_uint8_t uint8_t
|
||||
#define munit_int16_t int16_t
|
||||
#define munit_uint16_t uint16_t
|
||||
#define munit_int32_t int32_t
|
||||
#define munit_uint32_t uint32_t
|
||||
#define munit_int64_t int64_t
|
||||
#define munit_uint64_t uint64_t
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1800)
|
||||
#if !defined(PRIi8)
|
||||
#define PRIi8 "i"
|
||||
#endif
|
||||
#if !defined(PRIi16)
|
||||
#define PRIi16 "i"
|
||||
#endif
|
||||
#if !defined(PRIi32)
|
||||
#define PRIi32 "i"
|
||||
#endif
|
||||
#if !defined(PRIi64)
|
||||
#define PRIi64 "I64i"
|
||||
#endif
|
||||
#if !defined(PRId8)
|
||||
#define PRId8 "d"
|
||||
#endif
|
||||
#if !defined(PRId16)
|
||||
#define PRId16 "d"
|
||||
#endif
|
||||
#if !defined(PRId32)
|
||||
#define PRId32 "d"
|
||||
#endif
|
||||
#if !defined(PRId64)
|
||||
#define PRId64 "I64d"
|
||||
#endif
|
||||
#if !defined(PRIx8)
|
||||
#define PRIx8 "x"
|
||||
#endif
|
||||
#if !defined(PRIx16)
|
||||
#define PRIx16 "x"
|
||||
#endif
|
||||
#if !defined(PRIx32)
|
||||
#define PRIx32 "x"
|
||||
#endif
|
||||
#if !defined(PRIx64)
|
||||
#define PRIx64 "I64x"
|
||||
#endif
|
||||
#if !defined(PRIu8)
|
||||
#define PRIu8 "u"
|
||||
#endif
|
||||
#if !defined(PRIu16)
|
||||
#define PRIu16 "u"
|
||||
#endif
|
||||
#if !defined(PRIu32)
|
||||
#define PRIu32 "u"
|
||||
#endif
|
||||
#if !defined(PRIu64)
|
||||
#define PRIu64 "I64u"
|
||||
#endif
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#if !defined(munit_bool)
|
||||
#if defined(bool)
|
||||
#define munit_bool bool
|
||||
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
||||
#define munit_bool _Bool
|
||||
#else
|
||||
#define munit_bool int
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define MUNIT_LIKELY(expr) (__builtin_expect((expr), 1))
|
||||
#define MUNIT_UNLIKELY(expr) (__builtin_expect((expr), 0))
|
||||
#define MUNIT_UNUSED __attribute__((__unused__))
|
||||
#else
|
||||
#define MUNIT_LIKELY(expr) (expr)
|
||||
#define MUNIT_UNLIKELY(expr) (expr)
|
||||
#define MUNIT_UNUSED
|
||||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
|
||||
!defined(__PGI)
|
||||
#define MUNIT_ARRAY_PARAM(name) name
|
||||
#else
|
||||
#define MUNIT_ARRAY_PARAM(name)
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#define MUNIT_SIZE_MODIFIER "z"
|
||||
#define MUNIT_CHAR_MODIFIER "hh"
|
||||
#define MUNIT_SHORT_MODIFIER "h"
|
||||
#else
|
||||
#if defined(_M_X64) || defined(__amd64__)
|
||||
#define MUNIT_SIZE_MODIFIER "I64"
|
||||
#else
|
||||
#define MUNIT_SIZE_MODIFIER ""
|
||||
#endif
|
||||
#define MUNIT_CHAR_MODIFIER ""
|
||||
#define MUNIT_SHORT_MODIFIER ""
|
||||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
#define MUNIT_NO_RETURN _Noreturn
|
||||
#elif defined(__GNUC__)
|
||||
#define MUNIT_NO_RETURN __attribute__((__noreturn__))
|
||||
#elif defined(_MSC_VER)
|
||||
#define MUNIT_NO_RETURN __declspec(noreturn)
|
||||
#else
|
||||
#define MUNIT_NO_RETURN
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_ \
|
||||
__pragma(warning(push)) __pragma(warning(disable : 4127))
|
||||
#define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))
|
||||
#else
|
||||
#define MUNIT_PUSH_DISABLE_MSVC_C4127_
|
||||
#define MUNIT_POP_DISABLE_MSVC_C4127_
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
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)))
|
||||
#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, ...);
|
||||
|
||||
#define munit_logf(level, format, ...) \
|
||||
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
|
||||
|
||||
#define munit_log(level, msg) munit_logf(level, "%s", msg)
|
||||
|
||||
MUNIT_NO_RETURN
|
||||
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__)
|
||||
|
||||
#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_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_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)
|
||||
|
||||
#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)
|
||||
#define munit_assert_ushort(a, op, b) \
|
||||
munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
|
||||
#define munit_assert_int(a, op, b) munit_assert_type(int, "d", a, op, b)
|
||||
#define munit_assert_uint(a, op, b) \
|
||||
munit_assert_type(unsigned int, "u", a, op, b)
|
||||
#define munit_assert_long(a, op, b) munit_assert_type(long int, "ld", a, op, b)
|
||||
#define munit_assert_ulong(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)
|
||||
#define munit_assert_ullong(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)
|
||||
|
||||
#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)
|
||||
|
||||
#define munit_assert_int8(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)
|
||||
#define munit_assert_int16(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)
|
||||
#define munit_assert_int32(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)
|
||||
#define munit_assert_int64(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)
|
||||
|
||||
#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_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_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)
|
||||
#define munit_assert_null(ptr) munit_assert_ptr(ptr, ==, NULL)
|
||||
#define munit_assert_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
|
||||
#define munit_assert_ptr_null(ptr) munit_assert_ptr(ptr, ==, NULL)
|
||||
#define munit_assert_ptr_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)
|
||||
|
||||
/*** Memory allocation ***/
|
||||
|
||||
void *munit_malloc_ex(const char *filename, int line, size_t size);
|
||||
|
||||
#define munit_malloc(size) munit_malloc_ex(__FILE__, __LINE__, (size))
|
||||
|
||||
#define munit_new(type) ((type *)munit_malloc(sizeof(type)))
|
||||
|
||||
#define munit_calloc(nmemb, size) munit_malloc((nmemb) * (size))
|
||||
|
||||
#define munit_newa(type, nmemb) ((type *)munit_calloc((nmemb), sizeof(type)))
|
||||
|
||||
/*** Random number generation ***/
|
||||
|
||||
void munit_rand_seed(munit_uint32_t seed);
|
||||
munit_uint32_t munit_rand_uint32(void);
|
||||
int munit_rand_int_range(int min, int max);
|
||||
double munit_rand_double(void);
|
||||
void munit_rand_memory(size_t size,
|
||||
munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
|
||||
|
||||
/*** 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
|
||||
} MunitResult;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char **values;
|
||||
} MunitParameterEnum;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *value;
|
||||
} MunitParameter;
|
||||
|
||||
const char *munit_parameters_get(const MunitParameter params[],
|
||||
const char *key);
|
||||
|
||||
typedef enum {
|
||||
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);
|
||||
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;
|
||||
} MunitTest;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
|
||||
|
||||
/* Note: I'm not very happy with this API; it's likely to change if I
|
||||
* figure out something better. Suggestions welcome. */
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc,
|
||||
char *const argv[MUNIT_ARRAY_PARAM(argc + 1)],
|
||||
const MunitArgument arguments[]);
|
||||
|
||||
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
|
||||
|
||||
#define assert_true(expr) munit_assert_true(expr)
|
||||
#define assert_false(expr) munit_assert_false(expr)
|
||||
#define assert_char(a, op, b) munit_assert_char(a, op, b)
|
||||
#define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)
|
||||
#define assert_short(a, op, b) munit_assert_short(a, op, b)
|
||||
#define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)
|
||||
#define assert_int(a, op, b) munit_assert_int(a, op, b)
|
||||
#define assert_uint(a, op, b) munit_assert_uint(a, op, b)
|
||||
#define assert_long(a, op, b) munit_assert_long(a, op, b)
|
||||
#define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)
|
||||
#define assert_llong(a, op, b) munit_assert_llong(a, op, b)
|
||||
#define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)
|
||||
#define assert_size(a, op, b) munit_assert_size(a, op, b)
|
||||
#define assert_float(a, op, b) munit_assert_float(a, op, b)
|
||||
#define assert_double(a, op, b) munit_assert_double(a, op, b)
|
||||
#define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)
|
||||
|
||||
#define assert_int8(a, op, b) munit_assert_int8(a, op, b)
|
||||
#define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)
|
||||
#define assert_int16(a, op, b) munit_assert_int16(a, op, b)
|
||||
#define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)
|
||||
#define assert_int32(a, op, b) munit_assert_int32(a, op, b)
|
||||
#define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)
|
||||
#define assert_int64(a, op, b) munit_assert_int64(a, op, b)
|
||||
#define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
|
||||
|
||||
#define assert_double_equal(a, b, precision) \
|
||||
munit_assert_double_equal(a, b, precision)
|
||||
#define assert_string_equal(a, b) munit_assert_string_equal(a, b)
|
||||
#define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
|
||||
#define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
|
||||
#define assert_memory_not_equal(size, a, b) \
|
||||
munit_assert_memory_not_equal(size, a, b)
|
||||
#define assert_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)
|
||||
#define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)
|
||||
|
||||
#define assert_null(ptr) munit_assert_null(ptr)
|
||||
#define assert_not_null(ptr) munit_assert_not_null(ptr)
|
||||
|
||||
#endif /* defined(MUNIT_ENABLE_ASSERT_ALIASES) */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !defined(MUNIT_H) */
|
||||
|
||||
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
|
||||
#if defined(assert)
|
||||
#undef assert
|
||||
#endif
|
||||
#define assert(expr) munit_assert(expr)
|
||||
#endif
|
449
tests/test.c
Normal file
449
tests/test.c
Normal file
|
@ -0,0 +1,449 @@
|
|||
/*
|
||||
* skiplist is MIT-licensed, but for this file:
|
||||
*
|
||||
* To the extent possible under law, the author(s) of this file have
|
||||
* waived all copyright and related or neighboring rights to this
|
||||
* work. See <https://creativecommons.org/publicdomain/zero/1.0/> for
|
||||
* details.
|
||||
*/
|
||||
|
||||
#define MUNIT_NO_FORK (1)
|
||||
#define MUNIT_ENABLE_ASSERT_ALIASES (1)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define __SL_DEBUG 0
|
||||
#if __SL_DEBUG > 0
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#if __SL_DEBUG >= 1
|
||||
#define __SLD_ASSERT(cond) assert(cond)
|
||||
#define __SLD_(b) b
|
||||
#elif __SL_DEBUG >= 2
|
||||
#define __SLD_P(...) printf(__VA_ARGS__)
|
||||
#elif __SL_DEBUG >= 3
|
||||
typedef struct dbg_node {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#define __SLD_RT_INS(e, n, t, c) __sld_rt_ins(e, n, t, c)
|
||||
#define __SLD_NC_INS(n, nn, t, c) __sld_nc_ins(n, nn, t, c)
|
||||
#define __SLD_RT_RMV(e, n, t, c) __sld_rt_rmv(e, n, t, c)
|
||||
#define __SLD_NC_RMV(n, nn, t, c) __sld_nc_rmv(n, nn, t, c)
|
||||
#define __SLD_BM(n) __sld_bm(n)
|
||||
#endif
|
||||
|
||||
#include "../include/skiplist.h"
|
||||
#include "munit.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable : 4127)
|
||||
#endif
|
||||
|
||||
struct user_data {
|
||||
size_t n_ele;
|
||||
};
|
||||
|
||||
typedef struct ex_node {
|
||||
sl_node snode;
|
||||
uint32_t key;
|
||||
uint32_t value;
|
||||
} ex_node_t;
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
ex_sl_t *slist = calloc(sizeof(ex_sl_t), 1);
|
||||
if (slist == NULL)
|
||||
return NULL;
|
||||
sl_init(slist, uint32_key_cmp);
|
||||
return (void *)(uintptr_t)slist;
|
||||
}
|
||||
|
||||
static void
|
||||
test_api_tear_down(void *fixture)
|
||||
{
|
||||
ex_sl_t *slist = (ex_sl_t *)fixture;
|
||||
assert_ptr_not_null(slist);
|
||||
sl_node *cursor = sl_begin(slist);
|
||||
while (cursor) {
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
assert_ptr_not_null(entry);
|
||||
assert_uint32(entry->key, ==, entry->value);
|
||||
cursor = sl_next(slist, cursor);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
sl_free(slist);
|
||||
free(fixture);
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_insert_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_insert_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_insert(const MunitParameter params[], void *data)
|
||||
{
|
||||
int ret;
|
||||
size_t inserted = 0;
|
||||
uint32_t n, key;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
ex_node_t *node;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(slist);
|
||||
n = munit_rand_int_range(4096, 8192);
|
||||
while (n--) {
|
||||
key = munit_rand_int_range(0, ((uint32_t)0 - 1) / 10);
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return MUNIT_ERROR;
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
if ((ret = sl_insert_nodup(slist, &node->snode)) == -1)
|
||||
continue; /* a random duplicate appeared */
|
||||
else {
|
||||
assert_int(ret, ==, 0);
|
||||
inserted++;
|
||||
}
|
||||
}
|
||||
assert_size(inserted, ==, sl_get_size(slist));
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_remove_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)test_api_setup(params, user_data);
|
||||
__populate_slist(slist);
|
||||
return (void *)slist;
|
||||
}
|
||||
static void
|
||||
test_api_remove_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_remove(const MunitParameter params[], void *data)
|
||||
{
|
||||
uint32_t key;
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
ex_node_t *node;
|
||||
(void)params;
|
||||
|
||||
assert_ptr_not_null(slist);
|
||||
key = munit_rand_int_range((((uint32_t)0 - 1) / 10) + 1, ((uint32_t)0 - 1));
|
||||
node = (ex_node_t *)calloc(sizeof(ex_node_t), 1);
|
||||
if (node == NULL)
|
||||
return MUNIT_ERROR;
|
||||
sl_init_node(&node->snode);
|
||||
node->key = key;
|
||||
node->value = key;
|
||||
if (sl_insert_nodup(slist, &node->snode) == -1)
|
||||
return MUNIT_ERROR;
|
||||
else {
|
||||
ex_node_t query;
|
||||
query.key = key;
|
||||
sl_node *cursor = sl_find(slist, &query.snode);
|
||||
assert_ptr_not_null(cursor);
|
||||
ex_node_t *entry = sl_get_entry(cursor, ex_node_t, snode);
|
||||
sl_erase_node(slist, &entry->snode);
|
||||
sl_release_node(&entry->snode);
|
||||
sl_wait_for_free(&entry->snode);
|
||||
sl_free_node(&entry->snode);
|
||||
free(entry);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_find_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
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);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_find(const MunitParameter params[], void *data)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/* */
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_update_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_update_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_update(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_delete_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_delete_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_delete(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_duplicates_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_duplicates_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_duplicates(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_size_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_size_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_size(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_iterators_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
return test_api_setup(params, user_data);
|
||||
}
|
||||
static void
|
||||
test_api_iterators_tear_down(void *fixture)
|
||||
{
|
||||
test_api_tear_down(fixture);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_iterators(const MunitParameter params[], void *data)
|
||||
{
|
||||
sl_raw *slist = (sl_raw *)data;
|
||||
(void)params;
|
||||
(void)slist;
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitTest api_test_suite[] = {
|
||||
{ (char *)"/api/insert", test_api_insert, test_api_insert_setup,
|
||||
test_api_insert_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (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 } };
|
||||
|
||||
static MunitTest scale_tests[] = { { NULL, NULL, NULL, NULL,
|
||||
MUNIT_TEST_OPTION_NONE, NULL } };
|
||||
|
||||
static MunitSuite other_test_suite[] = { { "/mt", mt_tests, NULL, 1,
|
||||
MUNIT_SUITE_OPTION_NONE },
|
||||
{ "/scale", scale_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
|
||||
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } };
|
||||
|
||||
static const MunitSuite main_test_suite = { (char *)"/api", api_test_suite,
|
||||
other_test_suite, 1, MUNIT_SUITE_OPTION_NONE };
|
||||
|
||||
int
|
||||
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
|
||||
{
|
||||
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