Skip to content

Commit

Permalink
add ordering test
Browse files Browse the repository at this point in the history
  • Loading branch information
Anotra committed Feb 26, 2024
1 parent 21c32c1 commit 21ed41c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
7 changes: 6 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
project(anotest)
add_executable(${PROJECT_NAME} main.c
locks-pthread.c
ordering.c
)
target_link_libraries(${PROJECT_NAME} PRIVATE anomap)


if (WITH_LOCKS)
add_test(locks ${PROJECT_NAME} locks)
endif()
endif()

add_test(ordering ${PROJECT_NAME} ordering)
9 changes: 8 additions & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#include <stdio.h>
#include <string.h>

int test_locks(void);
#define TEST_FUNC(name) \
int test_ ## name(void)


TEST_FUNC(locks);
TEST_FUNC(ordering);


#define TEST_FEATURE(name)\
do {\
Expand All @@ -16,5 +22,6 @@ int main(int argc, char *argv[]) {
if (argc != 2)
return 1;
TEST_FEATURE(locks);
TEST_FEATURE(ordering);
return -1;
}
56 changes: 56 additions & 0 deletions test/ordering.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdlib.h>
#include <stdio.h>
#include "anomap.h"

ANOMAP_DECLARE_COMPARE_FUNCTION(_cmp_int, int)

int
test_ordering(void) {
struct anomap *map = anomap_create(sizeof (int), 0,
_cmp_int, anomap_preserve_order);

for (int i=0; i<100; i+=2)
anomap_do(map, anomap_insert, &i, NULL);
for (int i=1; i<100; i+=2)
anomap_do(map, anomap_insert, &i, NULL);

#define DO_ADVANCE_AND_CHECK \
do { \
if (!anomap_advance(map, &index, &p)) \
return 1; \
int key; \
if (!anomap_at_index(map, index, &key, NULL)) \
return 2; \
if (key != i) \
return 3; \
} while (0)

size_t index;
anomap_position p;
{ //forward
p = anomap_head;
for (int i=0; i<100; i+=2)
DO_ADVANCE_AND_CHECK;
for (int i=1; i<100; i+=2)
DO_ADVANCE_AND_CHECK;
if (anomap_advance(map, &index, &p))
return 4;
}
{ //reverse
p = anomap_tail;
for (int i=99; i>=0; i-=2)
DO_ADVANCE_AND_CHECK;
for (int i=98; i>=0; i-=2)
DO_ADVANCE_AND_CHECK;
if (anomap_advance(map, &index, &p))
return 4;
}
{ //check order of indicies
for (size_t i=0; i<anomap_length(map); i++) {
int key;
anomap_at_index(map, i, &key, NULL);
if (key != i) return 5;
}
}
return 0;
}

0 comments on commit 21ed41c

Please sign in to comment.