From 63b83df5dc664afb5722c2e317b531decec9a5d3 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 1 Jul 2024 08:00:46 +0000 Subject: [PATCH 1/2] WIP: add two tests --- CMakeLists.txt | 12 +++++----- tests/test.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dc0c4a..0a274cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" ON) # Add shared library add_library(sparsemap_SHARED SHARED ${LIB_SRC} ${HEADERS}) -# Set target properties for shared library (adjust if needed) +# Set target properties for the shared library (adjust if needed) set_target_properties(sparsemap_SHARED PROPERTIES VERSION 1.0.0 # Set library version LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" # Set output directory @@ -87,8 +87,8 @@ target_link_libraries(soak PUBLIC m) add_custom_target(run_soak COMMAND soak WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) # Add fuzzer program -add_executable(fuzzer tests/fuzzer.c) -target_link_libraries(fuzzer PRIVATE sparsemap) -target_include_directories(fuzzer PRIVATE ${HEADER_DIR} lib) -target_link_libraries(fuzzer PUBLIC m) -add_custom_target(run_fuzzer COMMAND fuzzer WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +# add_executable(fuzzer tests/fuzzer.c) +# target_link_libraries(fuzzer PRIVATE sparsemap) +# target_include_directories(fuzzer PRIVATE ${HEADER_DIR} lib) +# target_link_libraries(fuzzer PUBLIC m) +# add_custom_target(run_fuzzer COMMAND fuzzer WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/tests/test.c b/tests/test.c index 87c7c06..646ec10 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1348,6 +1348,68 @@ test_scale_fuzz(const MunitParameter params[], void *data) return MUNIT_OK; } +static void * +test_scale_alternating_setup(const MunitParameter params[], void *user_data) +{ + (void)params; + (void)user_data; + sparsemap_t *map = sparsemap(10 * 1024); + assert_ptr_not_null(map); + return (void *)map; +} +static void +test_scale_alternating_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + assert_ptr_not_null(map); + munit_free(map); +} +static MunitResult +test_scale_alternating(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + for (int i = 0; i < 8192; i++) { + if (i % 2) + sparsemap_set(map, i, true); + } + return MUNIT_OK; +} + +static void * +test_scale_8192_setup(const MunitParameter params[], void *user_data) +{ + (void)params; + (void)user_data; + sparsemap_t *map = sparsemap(10 * 1024); + assert_ptr_not_null(map); + return (void *)map; +} +static void +test_scale_8192_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + assert_ptr_not_null(map); + munit_free(map); +} +static MunitResult +test_scale_8192(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + for (int i = 0; i < 16384; i++) { + sparsemap_set(map, i, true); + if (i > 2049) { + sparsemap_set(map, i - 2049, false); + } + //assert_true(sparsemap_get_starting_offset(map) == (i / 2048) * 2048); + if (i % 2048 == 0) printf("%d\t%d\t%d\t%zu\n", i, i / 2048, i % 2048, sparsemap_get_starting_offset(map)); + } + return MUNIT_OK; +} + static void * test_scale_spans_come_spans_go_setup(const MunitParameter params[], void *user_data) { @@ -1595,6 +1657,8 @@ static MunitTest scale_test_suite[] = { { (char *)"/ondrej", test_scale_ondrej, test_scale_ondrej_setup, test_scale_ondrej_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, #endif { (char *)"/fuzz", test_scale_fuzz, test_scale_fuzz_setup, test_scale_fuzz_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/alternating", test_scale_alternating, test_scale_alternating_setup, test_scale_alternating_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/8192", test_scale_8192, test_scale_8192_setup, test_scale_8192_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/spans_come_spans_go", test_scale_spans_come_spans_go, test_scale_spans_come_spans_go_setup, test_scale_spans_come_spans_go_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/best-case", test_scale_best_case, test_scale_best_case_setup, test_scale_best_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/worst-case", test_scale_worst_case, test_scale_worst_case_setup, test_scale_worst_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, -- 2.45.2 From 745e205c80e9e6e9e7b4a8729ff388e3e04609d2 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 2 Jul 2024 09:05:18 -0400 Subject: [PATCH 2/2] tests to ensure dynamic sizing is working --- src/sparsemap.c | 27 ++++++++--------- tests/test.c | 77 ++++++++++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/sparsemap.c b/src/sparsemap.c index 4b1d479..3190a9f 100644 --- a/src/sparsemap.c +++ b/src/sparsemap.c @@ -292,23 +292,21 @@ __sm_chunk_increase_capacity(__sm_chunk_t *chunk, size_t capacity) static bool __sm_chunk_is_empty(__sm_chunk_t *chunk) { - /* The __sm_chunk_t is empty if all flags (in m_data[0]) are zero. */ - if (chunk->m_data[0] == 0) { - return true; - } - - /* It's also empty if all flags are Zero or None. */ - register uint8_t *p = (uint8_t *)chunk->m_data; - for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) { - if (*p) { - for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) { - size_t flags = SM_CHUNK_GET_FLAGS(*p, j); - if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) { - return false; + if (chunk->m_data[0] != 0) { + /* A chunk is considered empty if all flags are SM_PAYLOAD_ZERO or _NONE. */ + register uint8_t *p = (uint8_t *)chunk->m_data; + for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) { + if (*p) { + for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) { + size_t flags = SM_CHUNK_GET_FLAGS(*p, j); + if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) { + return false; + } } } } } + /* The __sm_chunk_t is empty if all flags (in m_data[0]) are zero. */ return true; } @@ -1307,11 +1305,10 @@ sparsemap_get_starting_offset(sparsemap_t *map) return 0; } uint8_t *p = __sm_get_chunk_data(map, 0); - sm_idx_t start = *(sm_idx_t *)p; + sparsemap_idx_t relative_position = *(sm_idx_t *)p; p += sizeof(sm_idx_t); __sm_chunk_t chunk; __sm_chunk_init(&chunk, p); - sparsemap_idx_t relative_position = start; for (size_t m = 0; m < sizeof(sm_bitvec_t); m++, p++) { for (int n = 0; n < SM_FLAGS_PER_INDEX_BYTE; n++) { size_t flags = SM_CHUNK_GET_FLAGS(*p, n); diff --git a/tests/test.c b/tests/test.c index 646ec10..dc57b83 100644 --- a/tests/test.c +++ b/tests/test.c @@ -652,6 +652,39 @@ test_api_get_ending_offset(const MunitParameter params[], void *data) return MUNIT_OK; } +static void * +test_api_get_starting_offset_rolling_setup(const MunitParameter params[], void *user_data) +{ + (void)params; + (void)user_data; + sparsemap_t *map = sparsemap(10 * 1024); + assert_ptr_not_null(map); + return (void *)map; +} +static void +test_api_get_starting_offset_rolling_tear_down(void *fixture) +{ + sparsemap_t *map = (sparsemap_t *)fixture; + assert_ptr_not_null(map); + munit_free(map); +} +static MunitResult +test_api_get_starting_offset_rolling(const MunitParameter params[], void *data) +{ + sparsemap_t *map = (sparsemap_t *)data; + (void)params; + + for (sparsemap_idx_t i = 0; i < 10 * 2048; i++) { + sparsemap_set(map, i, true); + if (i > 2047) { + sparsemap_set(map, i - 2048, false); + assert_true(sparsemap_get_starting_offset(map) == i - 2047); + // printf("%d\t%d\t%zu\n", i, i - 2047, sparsemap_get_starting_offset(map)); + } + } + return MUNIT_OK; +} + static void * test_api_scan_setup(const MunitParameter params[], void *user_data) { @@ -1214,6 +1247,7 @@ static MunitTest api_test_suite[] = { { (char *)"/count", test_api_count, test_api_count_setup, test_api_count_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/get_data", test_api_get_data, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/get_starting_offset", test_api_get_starting_offset, test_api_get_starting_offset_setup, test_api_get_starting_offset_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/get_starting_offset/rolling", test_api_get_starting_offset_rolling, test_api_get_starting_offset_rolling_setup, test_api_get_starting_offset_rolling_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/get_ending_offset", test_api_get_ending_offset, test_api_get_ending_offset_setup, test_api_get_ending_offset_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/scan", test_api_scan, test_api_scan_setup, test_api_scan_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/split", test_api_split, test_api_split_setup, test_api_split_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, @@ -1364,48 +1398,20 @@ test_scale_alternating_tear_down(void *fixture) assert_ptr_not_null(map); munit_free(map); } +extern char *bytes_as(double bytes, char *s, size_t size); static MunitResult test_scale_alternating(const MunitParameter params[], void *data) { sparsemap_t *map = (sparsemap_t *)data; (void)params; - for (int i = 0; i < 8192; i++) { - if (i % 2) - sparsemap_set(map, i, true); - } - return MUNIT_OK; -} - -static void * -test_scale_8192_setup(const MunitParameter params[], void *user_data) -{ - (void)params; - (void)user_data; - sparsemap_t *map = sparsemap(10 * 1024); - assert_ptr_not_null(map); - return (void *)map; -} -static void -test_scale_8192_tear_down(void *fixture) -{ - sparsemap_t *map = (sparsemap_t *)fixture; - assert_ptr_not_null(map); - munit_free(map); -} -static MunitResult -test_scale_8192(const MunitParameter params[], void *data) -{ - sparsemap_t *map = (sparsemap_t *)data; - (void)params; - - for (int i = 0; i < 16384; i++) { - sparsemap_set(map, i, true); - if (i > 2049) { - sparsemap_set(map, i - 2049, false); + for (sparsemap_idx_t i = 0; i < (1000 * 8192); i++) { + if (i % 2) { + if (sparsemap_set(map, i, true) != i) { + // printf("%zu\n", i); + break; + } } - //assert_true(sparsemap_get_starting_offset(map) == (i / 2048) * 2048); - if (i % 2048 == 0) printf("%d\t%d\t%d\t%zu\n", i, i / 2048, i % 2048, sparsemap_get_starting_offset(map)); } return MUNIT_OK; } @@ -1658,7 +1664,6 @@ static MunitTest scale_test_suite[] = { #endif { (char *)"/fuzz", test_scale_fuzz, test_scale_fuzz_setup, test_scale_fuzz_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/alternating", test_scale_alternating, test_scale_alternating_setup, test_scale_alternating_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, - { (char *)"/8192", test_scale_8192, test_scale_8192_setup, test_scale_8192_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/spans_come_spans_go", test_scale_spans_come_spans_go, test_scale_spans_come_spans_go_setup, test_scale_spans_come_spans_go_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/best-case", test_scale_best_case, test_scale_best_case_setup, test_scale_best_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/worst-case", test_scale_worst_case, test_scale_worst_case_setup, test_scale_worst_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL }, -- 2.45.2