diff --git a/map/README.md b/map/README.md index e54835f..6cf869e 100644 --- a/map/README.md +++ b/map/README.md @@ -45,42 +45,42 @@ If you want to use structs anyway, you need to change the code a little bit. void example_str() { - const char *key, *value; - struct sc_map_str map; + const char *key, *value; + struct sc_map_str map; - sc_map_init_str(&map, 0, 0); + sc_map_init_str(&map, 0, 0); - sc_map_put_str(&map, "jack", "chicago"); - sc_map_put_str(&map, "jane", "new york"); - sc_map_put_str(&map, "janie", "atlanta"); + sc_map_put_str(&map, "jack", "chicago"); + sc_map_put_str(&map, "jane", "new york"); + sc_map_put_str(&map, "janie", "atlanta"); - sc_map_foreach (&map, key, value) { - printf("Key:[%s], Value:[%s] \n", key, value); - } + sc_map_foreach (&map, key, value) { + printf("Key:[%s], Value:[%s] \n", key, value); + } - sc_map_term_str(&map); + sc_map_term_str(&map); } void example_int_to_str() { - uint32_t key; - const char *value; - struct sc_map_64s map; + uint32_t key; + const char *value; + struct sc_map_64s map; - sc_map_init_64s(&map, 0, 0); + sc_map_init_64s(&map, 0, 0); - sc_map_put_64s(&map, 100, "chicago"); - sc_map_put_64s(&map, 200, "new york"); - sc_map_put_64s(&map, 300, "atlanta"); + sc_map_put_64s(&map, 100, "chicago"); + sc_map_put_64s(&map, 200, "new york"); + sc_map_put_64s(&map, 300, "atlanta"); - sc_map_del_64s(&map, 100, &value); - printf("Deleted : %s \n", value); + value = sc_map_del_64s(&map, 100); + printf("Deleted : %s \n", value); - sc_map_foreach (&map, key, value) { - printf("Key:[%d], Value:[%s] \n", key, value); - } + sc_map_foreach (&map, key, value) { + printf("Key:[%d], Value:[%s] \n", key, value); + } - sc_map_term_64s(&map); + sc_map_term_64s(&map); } int main(int argc, char *argv[]) diff --git a/map/map_example.c b/map/map_example.c index 7d499a4..1fa55a5 100644 --- a/map/map_example.c +++ b/map/map_example.c @@ -2,7 +2,7 @@ #include -void example_str() +void example_str(void) { const char *key, *value; struct sc_map_str map; @@ -14,13 +14,13 @@ void example_str() sc_map_put_str(&map, "janie", "atlanta"); sc_map_foreach (&map, key, value) { - printf("Key:[%s], Value:[%s] \n", key, value); - } + printf("Key:[%s], Value:[%s] \n", key, value); + } sc_map_term_str(&map); } -void example_int_to_str() +void example_int_to_str(void) { uint32_t key; const char *value; @@ -32,12 +32,12 @@ void example_int_to_str() sc_map_put_64s(&map, 200, "new york"); sc_map_put_64s(&map, 300, "atlanta"); - sc_map_del_64s(&map, 100, &value); + value = sc_map_del_64s(&map, 100); printf("Deleted : %s \n", value); sc_map_foreach (&map, key, value) { - printf("Key:[%d], Value:[%s] \n", key, value); - } + printf("Key:[%d], Value:[%s] \n", key, value); + } sc_map_term_64s(&map); } diff --git a/map/map_test.c b/map/map_test.c index 5a4bc62..067c9ca 100644 --- a/map/map_test.c +++ b/map/map_test.c @@ -24,6 +24,28 @@ void example(void) sc_map_term_str(&map); } +void example_int_to_str() +{ + uint32_t key; + const char *value; + struct sc_map_64s map; + + sc_map_init_64s(&map, 0, 0); + + sc_map_put_64s(&map, 100, "chicago"); + sc_map_put_64s(&map, 200, "new york"); + sc_map_put_64s(&map, 300, "atlanta"); + + value = sc_map_del_64s(&map, 100); + printf("Deleted : %s \n", value); + + sc_map_foreach (&map, key, value) { + printf("Key:[%d], Value:[%s] \n", key, value); + } + + sc_map_term_64s(&map); +} + static char *str_random(size_t size) { static char ch[] = "0123456789" @@ -45,7 +67,6 @@ static char *str_random(size_t size) void test_32() { - uint32_t val; struct sc_map_32 map; assert(sc_map_init_32(&map, 0, 0)); @@ -54,59 +75,75 @@ void test_32() assert(sc_map_init_32(&map, 0, 99) == false); assert(sc_map_init_32(&map, 16, 94)); - assert(sc_map_del_32(&map, 0, NULL) == false); - assert(sc_map_del_32(&map, 1, NULL) == false); + + sc_map_del_32(&map, 0); + assert(sc_map_found(&map) == false); + + sc_map_del_32(&map, 1); + assert(sc_map_found(&map) == false); for (int i = 0; i < 14; i++) { - assert(sc_map_put_32(&map, i, i) == true); + sc_map_put_32(&map, i, i); + assert(!sc_map_oom(&map)); } for (int i = 100; i < 200; i++) { - assert(sc_map_del_32(&map, i, NULL) == false); + sc_map_del_32(&map, i); + assert(sc_map_found(&map) == false); } sc_map_clear_32(&map); sc_map_clear_32(&map); for (int i = 0; i < 5; i++) { - assert(sc_map_put_32(&map, i, i) == true); + sc_map_put_32(&map, i, i); } - assert(sc_map_put_32(&map, 31, 15) == true); - assert(sc_map_put_32(&map, 15, 15) == true); - assert(sc_map_put_32(&map, 46, 15) == true); + sc_map_put_32(&map, 31, 15); + sc_map_put_32(&map, 15, 15); + sc_map_put_32(&map, 46, 15); - assert(sc_map_get_32(&map, 19, &val) == false); + sc_map_get_32(&map, 19); + assert(sc_map_found(&map) == false); for (int i = 0; i < 5; i++) { - assert(sc_map_put_32(&map, (5 * i) + i, i) == true); + sc_map_put_32(&map, (5 * i) + i, i); } - assert(sc_map_del_32(&map, 4, NULL) == true); - assert(sc_map_del_32(&map, 46, NULL) == true); - assert(sc_map_del_32(&map, 15, NULL) == true); + sc_map_del_32(&map, 4); + assert(sc_map_found(&map)); + + sc_map_del_32(&map, 46); + assert(sc_map_found(&map)); + + sc_map_del_32(&map, 15); + assert(sc_map_found(&map)); sc_map_clear_32(&map); for (int i = 1; i < 4; i++) { - assert(sc_map_put_32(&map, 16 * i, i) == true); + sc_map_put_32(&map, 16 * i, i); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_32(&map, 1024 * i, i) == true); + sc_map_put_32(&map, 1024 * i, i); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_32(&map, 512 * i, i) == true); + sc_map_put_32(&map, 512 * i, i); } - assert(sc_map_del_32(&map, 512, NULL) == true); - assert(sc_map_del_32(&map, 1024, NULL) == true); - assert(sc_map_del_32(&map, 48, NULL) == true); - + sc_map_del_32(&map, 512); + assert(sc_map_found(&map)); + + sc_map_del_32(&map, 1024); + assert(sc_map_found(&map)); + + sc_map_del_32(&map, 48); + assert(sc_map_found(&map)); + sc_map_term_32(&map); } void test_64() { - uint64_t val; struct sc_map_64 map; assert(sc_map_init_64(&map, 0, 0)); @@ -115,59 +152,74 @@ void test_64() assert(sc_map_init_64(&map, 0, 99) == false); assert(sc_map_init_64(&map, 16, 94)); - assert(sc_map_del_64(&map, 0, NULL) == false); - assert(sc_map_del_64(&map, 1, NULL) == false); + + sc_map_del_64(&map, 0); + assert(!sc_map_found(&map)); + + sc_map_del_64(&map, 1); + assert(!sc_map_found(&map)); for (int i = 0; i < 14; i++) { - assert(sc_map_put_64(&map, i, i) == true); + sc_map_put_64(&map, i, i); } for (int i = 100; i < 200; i++) { - assert(sc_map_del_64(&map, i, NULL) == false); + sc_map_del_64(&map, i); + assert(!sc_map_found(&map)); } sc_map_clear_64(&map); sc_map_clear_64(&map); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64(&map, i, i) == true); + sc_map_put_64(&map, i, i); } - assert(sc_map_put_64(&map, 31, 15) == true); - assert(sc_map_put_64(&map, 15, 15) == true); - assert(sc_map_put_64(&map, 46, 15) == true); + sc_map_put_64(&map, 31, 15); + sc_map_put_64(&map, 15, 15); + sc_map_put_64(&map, 46, 15); - assert(sc_map_get_64(&map, 19, &val) == false); + sc_map_get_64(&map, 19); + assert(!sc_map_found(&map)); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64(&map, (5 * i) + i, i) == true); + sc_map_put_64(&map, (5 * i) + i, i); } - assert(sc_map_del_64(&map, 4, NULL) == true); - assert(sc_map_del_64(&map, 46, NULL) == true); - assert(sc_map_del_64(&map, 15, NULL) == true); + sc_map_del_64(&map, 4) ; + assert(sc_map_found(&map)); + + sc_map_del_64(&map, 46); + assert(sc_map_found(&map)); + + sc_map_del_64(&map, 15); + assert(sc_map_found(&map)); sc_map_clear_64(&map); for (int i = 1; i < 4; i++) { - assert(sc_map_put_64(&map, 16 * i, i) == true); + sc_map_put_64(&map, 16 * i, i); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64(&map, 1024 * i, i) == true); + sc_map_put_64(&map, 1024 * i, i); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64(&map, 512 * i, i) == true); + sc_map_put_64(&map, 512 * i, i); } - assert(sc_map_del_64(&map, 512, NULL) == true); - assert(sc_map_del_64(&map, 1024, NULL) == true); - assert(sc_map_del_64(&map, 48, NULL) == true); + sc_map_del_64(&map, 512); + assert(sc_map_found(&map)); + + sc_map_del_64(&map, 1024); + assert(sc_map_found(&map)); + + sc_map_del_64(&map, 48); + assert(sc_map_found(&map)); sc_map_term_64(&map); } void test_64v() { - void *val; struct sc_map_64v map; assert(sc_map_init_64v(&map, 0, 0)); @@ -176,59 +228,74 @@ void test_64v() assert(sc_map_init_64v(&map, 0, 99) == false); assert(sc_map_init_64v(&map, 16, 94)); - assert(sc_map_del_64v(&map, 0, NULL) == false); - assert(sc_map_del_64v(&map, 1, NULL) == false); - + sc_map_del_64v(&map, 0); + assert(sc_map_found(&map) == false); + + sc_map_del_64v(&map, 1); + assert(sc_map_found(&map) == false); + for (int i = 0; i < 14; i++) { - assert(sc_map_put_64v(&map, i, NULL) == true); + sc_map_put_64v(&map, i, NULL); } for (int i = 100; i < 200; i++) { - assert(sc_map_del_64v(&map, i, NULL) == false); + sc_map_del_64v(&map, i); + assert(sc_map_found(&map) == false); } sc_map_clear_64v(&map); sc_map_clear_64v(&map); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64v(&map, i, NULL) == true); + sc_map_put_64v(&map, i, NULL); + assert(!sc_map_found(&map)); } - assert(sc_map_put_64v(&map, 31, NULL) == true); - assert(sc_map_put_64v(&map, 15, NULL) == true); - assert(sc_map_put_64v(&map, 46, NULL) == true); + sc_map_put_64v(&map, 31, NULL); + sc_map_put_64v(&map, 15, NULL); + sc_map_put_64v(&map, 46, NULL); - assert(sc_map_get_64v(&map, 19, &val) == false); + sc_map_get_64v(&map, 19); + assert(sc_map_found(&map) == false); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64v(&map, (5 * i) + i, NULL) == true); + sc_map_put_64v(&map, (5 * i) + i, NULL); } - assert(sc_map_del_64v(&map, 4, NULL) == true); - assert(sc_map_del_64v(&map, 46, NULL) == true); - assert(sc_map_del_64v(&map, 15, NULL) == true); + sc_map_del_64v(&map, 4); + assert(sc_map_found(&map)); + + sc_map_del_64v(&map, 46); + assert(sc_map_found(&map)); + + sc_map_del_64v(&map, 15); + assert(sc_map_found(&map)); sc_map_clear_64v(&map); for (int i = 1; i < 4; i++) { - assert(sc_map_put_64v(&map, 16 * i, NULL) == true); + sc_map_put_64v(&map, 16 * i, NULL); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64v(&map, 1024 * i, NULL) == true); + sc_map_put_64v(&map, 1024 * i, NULL); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64v(&map, 512 * i, NULL) == true); + sc_map_put_64v(&map, 512 * i, NULL); } - assert(sc_map_del_64v(&map, 512, NULL) == true); - assert(sc_map_del_64v(&map, 1024, NULL) == true); - assert(sc_map_del_64v(&map, 48, NULL) == true); + sc_map_del_64v(&map, 512); + assert(sc_map_found(&map)); + + sc_map_del_64v(&map, 1024); + assert(sc_map_found(&map)); + + sc_map_del_64v(&map, 48); + assert(sc_map_found(&map)); sc_map_term_64v(&map); } void test_64s() { - const char *val; struct sc_map_64s map; assert(sc_map_init_64s(&map, 0, 0)); @@ -237,52 +304,67 @@ void test_64s() assert(sc_map_init_64s(&map, 0, 99) == false); assert(sc_map_init_64s(&map, 16, 94)); - assert(sc_map_del_64s(&map, 0, NULL) == false); - assert(sc_map_del_64s(&map, 1, NULL) == false); + sc_map_del_64s(&map, 0); + assert(!sc_map_found(&map)); + + sc_map_del_64s(&map, 1); + assert(!sc_map_found(&map)); for (int i = 0; i < 14; i++) { - assert(sc_map_put_64s(&map, i, NULL) == true); + sc_map_put_64s(&map, i, NULL); } for (int i = 100; i < 200; i++) { - assert(sc_map_del_64s(&map, i, NULL) == false); + sc_map_del_64s(&map, i); + assert(!sc_map_found(&map)); } sc_map_clear_64s(&map); sc_map_clear_64s(&map); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64s(&map, i, NULL) == true); + sc_map_put_64s(&map, i, NULL); } - assert(sc_map_put_64s(&map, 31, NULL) == true); - assert(sc_map_put_64s(&map, 15, NULL) == true); - assert(sc_map_put_64s(&map, 46, NULL) == true); + sc_map_put_64s(&map, 31, NULL); + sc_map_put_64s(&map, 15, NULL); + sc_map_put_64s(&map, 46, NULL); - assert(sc_map_get_64s(&map, 19, &val) == false); + sc_map_get_64s(&map, 19); + assert(sc_map_found(&map) == false); for (int i = 0; i < 5; i++) { - assert(sc_map_put_64s(&map, (5 * i) + i, NULL) == true); + sc_map_put_64s(&map, (5 * i) + i, NULL); } - assert(sc_map_del_64s(&map, 4, NULL) == true); - assert(sc_map_del_64s(&map, 46, NULL) == true); - assert(sc_map_del_64s(&map, 15, NULL) == true); - + sc_map_del_64s(&map, 4); + assert(sc_map_found(&map)); + + sc_map_del_64s(&map, 46); + assert(sc_map_found(&map)); + + sc_map_del_64s(&map, 15); + assert(sc_map_found(&map)); + sc_map_clear_64s(&map); for (int i = 1; i < 4; i++) { - assert(sc_map_put_64s(&map, 16 * i, NULL) == true); + sc_map_put_64s(&map, 16 * i, NULL); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64s(&map, 1024 * i, NULL) == true); + sc_map_put_64s(&map, 1024 * i, NULL); } for (int i = 1; i < 4; i++) { - assert(sc_map_put_64s(&map, 512 * i, NULL) == true); + sc_map_put_64s(&map, 512 * i, NULL); } - assert(sc_map_del_64s(&map, 512, NULL) == true); - assert(sc_map_del_64s(&map, 1024, NULL) == true); - assert(sc_map_del_64s(&map, 48, NULL) == true); + sc_map_del_64s(&map, 512); + assert(sc_map_found(&map)); + + sc_map_del_64s(&map, 1024); + assert(sc_map_found(&map)); + + sc_map_del_64s(&map, 48); + assert(sc_map_found(&map)); sc_map_term_64s(&map); } @@ -291,7 +373,6 @@ void test_str() { const char *arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdef" "ghijklmnoprstuvyz"; - const char *val; struct sc_map_str map; assert(sc_map_init_str(&map, 0, 0)); @@ -300,62 +381,95 @@ void test_str() assert(sc_map_init_str(&map, 0, 99) == false); assert(sc_map_init_str(&map, 16, 94)); - assert(sc_map_del_str(&map, NULL, NULL) == false); - assert(sc_map_del_str(&map, "", NULL) == false); + + sc_map_del_str(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_del_str(&map, ""); + assert(!sc_map_found(&map)); for (int i = 0; i < 14; i++) { - assert(sc_map_put_str(&map, &arr[i], NULL) == true); + sc_map_put_str(&map, &arr[i], NULL); } for (int i = 15; i < 30; i++) { - assert(sc_map_del_str(&map, &arr[i], NULL) == false); + sc_map_del_str(&map, &arr[i]); + assert(!sc_map_found(&map)); } sc_map_clear_str(&map); sc_map_clear_str(&map); - assert(sc_map_put_str(&map, "h", NULL) == true); - assert(sc_map_put_str(&map, "z", NULL) == true); - assert(sc_map_get_str(&map, "13", &val) == false); - assert(sc_map_get_str(&map, NULL, &val) == false); - assert(sc_map_get_str(&map, "h", &val) == true); - assert(sc_map_get_str(&map, "z", &val) == true); - assert(sc_map_get_str(&map, "x", &val) == false); - assert(sc_map_put_str(&map, NULL, NULL) == true); - assert(sc_map_get_str(&map, NULL, &val) == true); - assert(sc_map_del_str(&map, NULL, &val) == true); - assert(sc_map_del_str(&map, "h", &val) == true); - assert(sc_map_del_str(&map, "13", &val) == false); + sc_map_put_str(&map, "h", NULL); + sc_map_put_str(&map, "z", NULL); + sc_map_get_str(&map, "13"); + assert(!sc_map_found(&map)); + + sc_map_get_str(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_get_str(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_get_str(&map, "z"); + assert(sc_map_found(&map)); + + sc_map_get_str(&map, "x"); + assert(!sc_map_found(&map)); + + sc_map_put_str(&map, NULL, NULL); + + sc_map_get_str(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_str(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_str(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_del_str(&map, "13"); + assert(!sc_map_found(&map)); + sc_map_clear_str(&map); assert(sc_map_size_str(&map) == 0); for (int i = 0; i < 5; i++) { - assert(sc_map_put_str(&map, &arr[i], NULL) == true); + sc_map_put_str(&map, &arr[i], NULL); } - assert(sc_map_put_str(&map, &arr[15], NULL) == true); - assert(sc_map_put_str(&map, &arr[7], NULL) == true); - assert(sc_map_put_str(&map, &arr[9], NULL) == true); + sc_map_put_str(&map, &arr[15], NULL); + sc_map_put_str(&map, &arr[7], NULL); + sc_map_put_str(&map, &arr[9], NULL); - assert(sc_map_get_str(&map, &arr[16], &val) == false); + sc_map_get_str(&map, &arr[16]); + assert(!sc_map_found(&map)); for (int i = 0; i < 5; i++) { - assert(sc_map_put_str(&map, &arr[(5 * i) + i], NULL) == true); + sc_map_put_str(&map, &arr[(5 * i) + i], NULL); } - assert(sc_map_del_str(&map, &arr[4], NULL) == true); - assert(sc_map_del_str(&map, &arr[6], NULL) == true); - assert(sc_map_del_str(&map, &arr[15], NULL) == true); + sc_map_del_str(&map, &arr[4]); + assert(sc_map_found(&map)); + + sc_map_del_str(&map, &arr[6]); + assert(sc_map_found(&map)); + + sc_map_del_str(&map, &arr[15]); + assert(sc_map_found(&map)); sc_map_clear_str(&map); - assert(sc_map_put_str(&map, "h", NULL) == true); - assert(sc_map_put_str(&map, "z", NULL) == true); - assert(sc_map_del_str(&map, "h", NULL) == true); + sc_map_put_str(&map, "h", NULL); + sc_map_put_str(&map, "z", NULL); + sc_map_del_str(&map, "h"); + assert(sc_map_found(&map)); + sc_map_clear_str(&map); - assert(sc_map_put_str(&map, "h", NULL) == true); - assert(sc_map_put_str(&map, "z", NULL) == true); - assert(sc_map_put_str(&map, "13", NULL) == true); - assert(sc_map_del_str(&map, "z", NULL) == true); + sc_map_put_str(&map, "h", NULL); + sc_map_put_str(&map, "z", NULL); + sc_map_put_str(&map, "13", NULL); + sc_map_del_str(&map, "z"); + assert(sc_map_found(&map)); sc_map_term_str(&map); } @@ -364,7 +478,6 @@ void test_sv() { const char *arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdef" "ghijklmnoprstuvyz"; - void *val; struct sc_map_sv map; assert(sc_map_init_sv(&map, 0, 0)); @@ -373,62 +486,93 @@ void test_sv() assert(sc_map_init_sv(&map, 0, 99) == false); assert(sc_map_init_sv(&map, 16, 94)); - assert(sc_map_del_sv(&map, NULL, NULL) == false); - assert(sc_map_del_sv(&map, "", NULL) == false); + sc_map_del_sv(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_del_sv(&map, ""); + assert(!sc_map_found(&map)); for (int i = 0; i < 14; i++) { - assert(sc_map_put_sv(&map, &arr[i], NULL) == true); + sc_map_put_sv(&map, &arr[i], NULL); } for (int i = 15; i < 30; i++) { - assert(sc_map_del_sv(&map, &arr[i], NULL) == false); + sc_map_del_sv(&map, &arr[i]); + assert(!sc_map_found(&map)); } sc_map_clear_sv(&map); sc_map_clear_sv(&map); - assert(sc_map_put_sv(&map, "h", NULL) == true); - assert(sc_map_put_sv(&map, "z", NULL) == true); - assert(sc_map_get_sv(&map, "13", &val) == false); - assert(sc_map_get_sv(&map, NULL, &val) == false); - assert(sc_map_get_sv(&map, "h", &val) == true); - assert(sc_map_get_sv(&map, "z", &val) == true); - assert(sc_map_get_sv(&map, "x", &val) == false); - assert(sc_map_put_sv(&map, NULL, NULL) == true); - assert(sc_map_get_sv(&map, NULL, &val) == true); - assert(sc_map_del_sv(&map, NULL, &val) == true); - assert(sc_map_del_sv(&map, "h", &val) == true); - assert(sc_map_del_sv(&map, "13", &val) == false); + sc_map_put_sv(&map, "h", NULL); + sc_map_put_sv(&map, "z", NULL); + + sc_map_get_sv(&map, "13"); + assert(!sc_map_found(&map)); + + sc_map_get_sv(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_get_sv(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_get_sv(&map, "z"); + assert(sc_map_found(&map)); + + sc_map_get_sv(&map, "x"); + assert(!sc_map_found(&map)); + + sc_map_put_sv(&map, NULL, NULL); + sc_map_get_sv(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_sv(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_sv(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_del_sv(&map, "13"); + assert(!sc_map_found(&map)); + sc_map_clear_sv(&map); assert(sc_map_size_sv(&map) == 0); for (int i = 0; i < 5; i++) { - assert(sc_map_put_sv(&map, &arr[i], NULL) == true); + sc_map_put_sv(&map, &arr[i], NULL); } - assert(sc_map_put_sv(&map, &arr[15], NULL) == true); - assert(sc_map_put_sv(&map, &arr[7], NULL) == true); - assert(sc_map_put_sv(&map, &arr[9], NULL) == true); + sc_map_put_sv(&map, &arr[15], NULL); + sc_map_put_sv(&map, &arr[7], NULL); + sc_map_put_sv(&map, &arr[9], NULL); - assert(sc_map_get_sv(&map, &arr[16], &val) == false); + sc_map_get_sv(&map, &arr[16]); + assert(!sc_map_found(&map)); for (int i = 0; i < 5; i++) { - assert(sc_map_put_sv(&map, &arr[(5 * i) + i], NULL) == true); + sc_map_put_sv(&map, &arr[(5 * i) + i], NULL); } - assert(sc_map_del_sv(&map, &arr[4], NULL) == true); - assert(sc_map_del_sv(&map, &arr[6], NULL) == true); - assert(sc_map_del_sv(&map, &arr[15], NULL) == true); + sc_map_del_sv(&map, &arr[4]); + assert(sc_map_found(&map)); + + sc_map_del_sv(&map, &arr[6]); + assert(sc_map_found(&map)); + + sc_map_del_sv(&map, &arr[15]); + assert(sc_map_found(&map)); sc_map_clear_sv(&map); - assert(sc_map_put_sv(&map, "h", NULL) == true); - assert(sc_map_put_sv(&map, "z", NULL) == true); - assert(sc_map_del_sv(&map, "h", NULL) == true); + sc_map_put_sv(&map, "h", NULL); + sc_map_put_sv(&map, "z", NULL); + sc_map_del_sv(&map, "h"); + assert(sc_map_found(&map)); sc_map_clear_sv(&map); - assert(sc_map_put_sv(&map, "h", NULL) == true); - assert(sc_map_put_sv(&map, "z", NULL) == true); - assert(sc_map_put_sv(&map, "13", NULL) == true); - assert(sc_map_del_sv(&map, "z", NULL) == true); + sc_map_put_sv(&map, "h", NULL) ; + sc_map_put_sv(&map, "z", NULL) ; + sc_map_put_sv(&map, "13", NULL); + sc_map_del_sv(&map, "z"); + assert(sc_map_found(&map)); sc_map_term_sv(&map); } @@ -437,7 +581,6 @@ void test_s64() { const char *arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdef" "ghijklmnoprstuvyz"; - uint64_t val; struct sc_map_s64 map; assert(sc_map_init_s64(&map, 0, 0)); @@ -446,63 +589,99 @@ void test_s64() assert(sc_map_init_s64(&map, 0, 99) == false); assert(sc_map_init_s64(&map, 16, 94)); - assert(sc_map_del_s64(&map, NULL, NULL) == false); - assert(sc_map_del_s64(&map, "", NULL) == false); + sc_map_del_s64(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_del_s64(&map, ""); + assert(!sc_map_found(&map)); for (int i = 0; i < 14; i++) { - assert(sc_map_put_s64(&map, &arr[i], 0) == true); + sc_map_put_s64(&map, &arr[i], 0); } for (int i = 15; i < 30; i++) { - assert(sc_map_del_s64(&map, &arr[i], NULL) == false); + sc_map_del_s64(&map, &arr[i]); + assert(!sc_map_found(&map)); } sc_map_clear_s64(&map); sc_map_clear_s64(&map); - assert(sc_map_put_s64(&map, "h", 0) == true); - assert(sc_map_put_s64(&map, "z", 0) == true); - assert(sc_map_get_s64(&map, "13", &val) == false); - assert(sc_map_get_s64(&map, NULL, &val) == false); - assert(sc_map_get_s64(&map, "h", &val) == true); - assert(sc_map_get_s64(&map, "z", &val) == true); - assert(sc_map_get_s64(&map, "x", &val) == false); - assert(sc_map_put_s64(&map, NULL, 0) == true); - assert(sc_map_get_s64(&map, NULL, &val) == true); - assert(sc_map_del_s64(&map, NULL, &val) == true); - assert(sc_map_del_s64(&map, "h", &val) == true); - assert(sc_map_del_s64(&map, "13", &val) == false); + sc_map_put_s64(&map, "h", 0); + sc_map_put_s64(&map, "z", 0); + + sc_map_get_s64(&map, "13"); + assert(!sc_map_found(&map)); + + sc_map_get_s64(&map, NULL); + assert(!sc_map_found(&map)); + + sc_map_get_s64(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_get_s64(&map, "z"); + assert(sc_map_found(&map)); + + sc_map_get_s64(&map, "x"); + assert(!sc_map_found(&map)); + + sc_map_put_s64(&map, NULL, 0); + assert(!sc_map_found(&map)); + + sc_map_get_s64(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_s64(&map, NULL); + assert(sc_map_found(&map)); + + sc_map_del_s64(&map, "h"); + assert(sc_map_found(&map)); + + sc_map_del_s64(&map, "13"); + assert(!sc_map_found(&map)); sc_map_clear_s64(&map); assert(sc_map_size_s64(&map) == 0); for (int i = 0; i < 5; i++) { - assert(sc_map_put_s64(&map, &arr[i], 0) == true); + sc_map_put_s64(&map, &arr[i], 0); } - assert(sc_map_put_s64(&map, &arr[15], 0) == true); - assert(sc_map_put_s64(&map, &arr[7], 0) == true); - assert(sc_map_put_s64(&map, &arr[9], 0) == true); + sc_map_put_s64(&map, &arr[15], 0); + sc_map_put_s64(&map, &arr[7], 0); + sc_map_put_s64(&map, &arr[9], 0); - assert(sc_map_get_s64(&map, &arr[16], &val) == false); + assert(!sc_map_oom(&map)); + + sc_map_get_s64(&map, &arr[16]); + assert(!sc_map_found(&map)); for (int i = 0; i < 5; i++) { - assert(sc_map_put_s64(&map, &arr[(5 * i) + i], 0) == true); + sc_map_put_s64(&map, &arr[(5 * i) + i], 0); + assert(!sc_map_oom(&map)); } - assert(sc_map_del_s64(&map, &arr[4], NULL) == true); - assert(sc_map_del_s64(&map, &arr[6], NULL) == true); - assert(sc_map_del_s64(&map, &arr[15], NULL) == true); + sc_map_del_s64(&map, &arr[4]); + assert(sc_map_found(&map)); + + sc_map_del_s64(&map, &arr[6]); + assert(sc_map_found(&map)); + + sc_map_del_s64(&map, &arr[15]); + assert(sc_map_found(&map)); sc_map_clear_s64(&map); - assert(sc_map_put_s64(&map, "h", 0) == true); - assert(sc_map_put_s64(&map, "z", 0) == true); - assert(sc_map_del_s64(&map, "h", 0) == true); + sc_map_put_s64(&map, "h", 0); + sc_map_put_s64(&map, "z", 0); + sc_map_del_s64(&map, "h"); + assert(sc_map_found(&map)); + sc_map_clear_s64(&map); - assert(sc_map_put_s64(&map, "h", 0) == true); - assert(sc_map_put_s64(&map, "z", 0) == true); - assert(sc_map_put_s64(&map, "13", 0) == true); - assert(sc_map_del_s64(&map, "z", 0) == true); - + sc_map_put_s64(&map, "h", 0); + sc_map_put_s64(&map, "z", 0); + sc_map_put_s64(&map, "13", 0); + sc_map_del_s64(&map, "z"); + assert(sc_map_found(&map)); + sc_map_term_s64(&map); } @@ -520,11 +699,11 @@ void test1() sc_map_init_str(&map, 0, 0); sc_map_put_str(&map, "100", "200"); - sc_map_get_str(&map, "100", &value); + value = sc_map_get_str(&map, "100"); assert(strcmp(value, "200") == 0); sc_map_term_str(&map); sc_map_put_str(&map, "100", "200"); - sc_map_get_str(&map, "100", &value); + value = sc_map_get_str(&map, "100"); assert(strcmp(value, "200") == 0); sc_map_term_str(&map); @@ -550,36 +729,45 @@ void test1() assert(false); } - assert(sc_map_put_str(&map, "key", "value")); - assert(sc_map_put_str(&map, "key", "value2")); - assert(sc_map_get_str(&map, "key", &value)); + sc_map_put_str(&map, "key", "value"); + sc_map_put_str(&map, "key", "value2"); + + value = sc_map_get_str(&map, "key"); + assert(sc_map_found(&map)); assert(strcmp(value, "value2") == 0); - assert(sc_map_del_str(&map, "key", NULL)); - assert(!sc_map_get_str(&map, "key", &value)); - assert(sc_map_put_str(&map, "key", "value3")); - assert(sc_map_del_str(&map, "key", &value)); + value = sc_map_del_str(&map, "key"); + assert(sc_map_found(&map)); + value = sc_map_get_str(&map, "key"); + assert(!sc_map_found(&map)); + sc_map_put_str(&map, "key", "value3"); + value = sc_map_del_str(&map, "key"); + assert(sc_map_found(&map)); assert(strcmp(value, "value3") == 0); - assert(!sc_map_del_str(&map, "key", &value)); + key = sc_map_del_str(&map, "key"); + assert(!sc_map_found(&map)); - assert(sc_map_put_str(&map, "key", "value")); + sc_map_put_str(&map, "key", "value"); assert(sc_map_size_str(&map) == 1); - assert(sc_map_put_str(&map, NULL, "nullvalue")); + sc_map_put_str(&map, NULL, "nullvalue"); assert(sc_map_size_str(&map) == 2); - assert(sc_map_get_str(&map, NULL, &value)); + value = sc_map_get_str(&map, NULL); + assert(sc_map_found(&map)); assert(strcmp(value, "nullvalue") == 0); - assert(sc_map_del_str(&map, NULL, NULL)); + sc_map_del_str(&map, NULL); + assert(sc_map_found(&map)); assert(sc_map_size_str(&map) == 1); sc_map_clear_str(&map); assert(sc_map_size_str(&map) == 0); for (int i = 0; i < 100; i++) { - assert(sc_map_put_str(&map, keys[i], values[i])); + sc_map_put_str(&map, keys[i], values[i]); } for (int i = 0; i < 100; i++) { - assert(sc_map_get_str(&map, keys[i], &value)); + value = sc_map_get_str(&map, keys[i]); + assert(sc_map_found(&map)); assert(strcmp(value, values[i]) == 0); } @@ -591,11 +779,12 @@ void test1() assert(sc_map_size_str(&map) == 0); for (int i = 0; i < 100; i++) { - assert(sc_map_put_str(&map, keys[i], values[i])); + sc_map_put_str(&map, keys[i], values[i]); } for (int i = 0; i < 100; i++) { - assert(sc_map_get_str(&map, keys[i], &value)); + value = sc_map_get_str(&map, keys[i]); + assert(sc_map_found(&map)); assert(strcmp(value, values[i]) == 0); } @@ -603,7 +792,7 @@ void test1() assert(sc_map_init_str(&map, 0, 0)); for (int i = 0; i < 100; i++) { - assert(sc_map_put_str(&map, keys[i], values[i])); + sc_map_put_str(&map, keys[i], values[i]); } bool found; @@ -671,21 +860,24 @@ retry: assert(sc_map_init_32(&map, 16, 50)); assert(sc_map_size_32(&map) == 0); - assert(sc_map_put_32(&map, 0, 0)); + sc_map_put_32(&map, 0, 0); sc_map_clear_32(&map); assert(sc_map_size_32(&map) == 0); for (int i = 0; i < 100; i++) { - assert(sc_map_put_32(&map, keys[i], values[i])); - assert(sc_map_get_32(&map, keys[i], &value)); + sc_map_put_32(&map, keys[i], values[i]); + value = sc_map_get_32(&map, keys[i]); + assert(sc_map_found(&map)); assert(value == values[i]); - assert(sc_map_put_32(&map, keys[i], values[i])); - assert(sc_map_del_32(&map, keys[i], &value)); + sc_map_put_32(&map, keys[i], values[i]); + + value = sc_map_del_32(&map, keys[i]); + assert(sc_map_found(&map)); assert(value == values[i]); } for (int i = 0; i < 128; i++) { - assert(sc_map_put_32(&map, keys[i], values[i])); + sc_map_put_32(&map, keys[i], values[i]); } assert(sc_map_size_32(&map) == 128); @@ -750,21 +942,24 @@ retry: assert(sc_map_init_64(&map, 16, 50)); assert(sc_map_size_64(&map) == 0); - assert(sc_map_put_64(&map, 0, 0)); + sc_map_put_64(&map, 0, 0); sc_map_clear_64(&map); assert(sc_map_size_64(&map) == 0); for (int i = 0; i < 100; i++) { - assert(sc_map_put_64(&map, keys[i], values[i])); - assert(sc_map_get_64(&map, keys[i], &value)); + sc_map_put_64(&map, keys[i], values[i]); + value = sc_map_get_64(&map, keys[i]); + assert(sc_map_found(&map)); assert(value == values[i]); - assert(sc_map_put_64(&map, keys[i], values[i])); - assert(sc_map_del_64(&map, keys[i], &value)); + + sc_map_put_64(&map, keys[i], values[i]); + value = sc_map_del_64(&map, keys[i]); + assert(sc_map_found(&map)); assert(value == values[i]); } for (int i = 0; i < 128; i++) { - assert(sc_map_put_64(&map, keys[i], values[i])); + sc_map_put_64(&map, keys[i], values[i]); } assert(sc_map_size_64(&map) == 128); @@ -813,39 +1008,43 @@ void test4() assert(sc_map_init_64s(&map64s, 1, 87)); for (int i = 0; i < 100; i++) { - assert(sc_map_put_64s(&map64s, i, NULL)); - assert(sc_map_get_64s(&map64s, i, &c)); + sc_map_put_64s(&map64s, i, NULL); + c = sc_map_get_64s(&map64s, i); + assert(sc_map_found(&map64s)); assert(c == NULL); } assert(sc_map_size_64s(&map64s) == 100); for (int i = 0; i < 100; i++) { - assert(sc_map_del_64s(&map64s, i, &c)); + c = sc_map_del_64s(&map64s, i); + assert(sc_map_found(&map64s)); assert(c == NULL); } assert(sc_map_size_64s(&map64s) == 0); - assert(sc_map_put_64s(&map64s, 3, NULL)); + sc_map_put_64s(&map64s, 3, NULL); assert(sc_map_size_64s(&map64s) == 1); sc_map_clear_64s(&map64s); assert(sc_map_size_64s(&map64s) == 0); sc_map_term_64s(&map64s); - void *v; + const char *v; struct sc_map_64v map64v; assert(sc_map_init_64v(&map64v, 1, 87)); for (int i = 0; i < 100; i++) { - assert(sc_map_put_64v(&map64v, i, NULL)); - assert(sc_map_get_64v(&map64v, i, &v)); - assert(c == NULL); + sc_map_put_64v(&map64v, i, NULL); + v = sc_map_get_64v(&map64v, i); + assert(sc_map_found(&map64v)); + assert(v == NULL); } assert(sc_map_size_64v(&map64v) == 100); for (int i = 0; i < 100; i++) { - assert(sc_map_del_64v(&map64v, i, &v)); + v = sc_map_del_64v(&map64v, i); + assert(sc_map_found(&map64v)); assert(v == NULL); } assert(sc_map_size_64v(&map64v) == 0); - assert(sc_map_put_64v(&map64v, 3, NULL)); + sc_map_put_64v(&map64v, 3, NULL); assert(sc_map_size_64v(&map64v) == 1); sc_map_clear_64v(&map64v); assert(sc_map_size_64v(&map64v) == 0); @@ -863,13 +1062,17 @@ void test4() struct sc_map_str mapstr; assert(sc_map_init_str(&mapstr, 0, 26)); for (int i = 0; i < 64; i++) { - assert(sc_map_put_str(&mapstr, keys[i], - (void *) (uintptr_t) i)); + sc_map_put_str(&mapstr, keys[i], + (void *) (uintptr_t) i); } - assert(sc_map_get_str(&mapstr, keys[0], (const char **) &v)); + + v = sc_map_get_str(&mapstr, keys[0]); + assert(sc_map_found(&mapstr)); assert(v == 0); assert(sc_map_size_str(&mapstr) == 64); - assert(sc_map_del_str(&mapstr, keys[12], (const char **) &v)); + + v = sc_map_del_str(&mapstr, keys[12]); + assert(sc_map_found(&mapstr)); assert(v == (void *) 12); assert(sc_map_size_str(&mapstr) == 63); sc_map_clear_str(&mapstr); @@ -878,12 +1081,15 @@ void test4() struct sc_map_sv mapsv; assert(sc_map_init_sv(&mapsv, 0, 26)); for (int i = 0; i < 64; i++) { - assert(sc_map_put_sv(&mapsv, keys[i], (void *) (uintptr_t) i)); + sc_map_put_sv(&mapsv, keys[i], (void *) (uintptr_t) i); } - assert(sc_map_get_sv(&mapsv, keys[0], &v)); + v = sc_map_get_sv(&mapsv, keys[0]); + assert(sc_map_found(&mapsv)); assert(v == 0); assert(sc_map_size_sv(&mapsv) == 64); - assert(sc_map_del_sv(&mapsv, keys[12], &v)); + + v = sc_map_del_sv(&mapsv, keys[12]); + assert(sc_map_found(&mapsv)); assert(v == (void *) 12); assert(sc_map_size_sv(&mapsv) == 63); sc_map_clear_sv(&mapsv); @@ -894,12 +1100,16 @@ void test4() assert(sc_map_init_s64(&maps64, 0, 26)); for (int i = 0; i < 64; i++) { - assert(sc_map_put_s64(&maps64, keys[i], i)); + sc_map_put_s64(&maps64, keys[i], i); } - assert(sc_map_get_s64(&maps64, keys[0], &val)); + + val = sc_map_get_s64(&maps64, keys[0]); + assert(sc_map_found(&maps64)); assert(val == 0); assert(sc_map_size_s64(&maps64) == 64); - assert(sc_map_del_s64(&maps64, keys[12], &val)); + + val = sc_map_del_s64(&maps64, keys[12]); + assert(sc_map_found(&maps64)); assert(val == 12); assert(sc_map_size_s64(&maps64) == 63); sc_map_clear_s64(&maps64); @@ -950,7 +1160,6 @@ static void test6() { const int count = 120000; uint32_t val; - bool b, exists; struct sc_map_32 map; srand(time(NULL)); @@ -965,28 +1174,23 @@ static void test6() sc_map_init_32(&map, 0, 0); for (int i = 0; i < count; i++) { - b = sc_map_put_32(&map, keys[i], values[i]); - assert(b); + sc_map_put_32(&map, keys[i], values[i]); if (i % 7 == 0 || i % 17 == 0 || i % 79 == 0) { - b = sc_map_del_32(&map, keys[i], &val); - assert(b); + val = sc_map_del_32(&map, keys[i]); + assert(sc_map_found(&map)); assert(val == values[i]); } } for (int i = 0; i < count; i++) { - exists = true; if (i % 7 == 0 || i % 17 == 0 || i % 79 == 0) { - exists = false; + continue; } - b = sc_map_get_32(&map, keys[i], &val); - assert(b == exists); - - if (b) { - assert(val == values[i]); - } + val = sc_map_get_32(&map, keys[i]); + assert(sc_map_found(&map)); + assert(val == values[i]); } sc_map_term_32(&map); @@ -1018,18 +1222,18 @@ void fail_test_32() assert(sc_map_init_32(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_32(&map, i, i); + sc_map_put_32(&map, i, i); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_32(&map, 44444, 44444)); + sc_map_put_32(&map, 44444, 44444); for (size_t i = 0; i < SC_MAP_MAX; i++) { - success = sc_map_put_32(&map, i, i); + sc_map_put_32(&map, i, i); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_term_32(&map); @@ -1045,18 +1249,18 @@ void fail_test_64() assert(sc_map_init_64(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_64(&map, i, i); + sc_map_put_64(&map, i, i); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_64(&map, 44444, 44444)); + sc_map_put_64(&map, 44444, 44444); for (size_t i = 0; i < SC_MAP_MAX; i++) { - success = sc_map_put_64(&map, i, i); + sc_map_put_64(&map, i, i); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_term_64(&map); @@ -1072,18 +1276,18 @@ void fail_test_64v() assert(sc_map_init_64v(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_64v(&map, i, NULL); + sc_map_put_64v(&map, i, NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_64v(&map, 44444, NULL)); + sc_map_put_64v(&map, 44444, NULL); for (size_t i = 0; i < SC_MAP_MAX; i++) { - success = sc_map_put_64v(&map, i, NULL); + sc_map_put_64v(&map, i, NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_term_64v(&map); @@ -1099,18 +1303,18 @@ void fail_test_64s() assert(sc_map_init_64s(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_64s(&map, i, NULL); + sc_map_put_64s(&map, i, NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_64s(&map, 44444, NULL)); + sc_map_put_64s(&map, 44444, NULL); for (size_t i = 0; i < SC_MAP_MAX; i++) { - success = sc_map_put_64s(&map, i, NULL); + sc_map_put_64s(&map, i, NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_term_64s(&map); @@ -1128,24 +1332,25 @@ void fail_test_str() assert(sc_map_init_str(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_str(&map, &s[i], NULL); + sc_map_put_str(&map, &s[i], NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_str(&map, &s[21], NULL)); + sc_map_put_str(&map, &s[21], NULL); sc_map_clear_str(&map); for (size_t i = 0; i < SC_MAP_MAX; i++) { char *c = str_random(32); - success = sc_map_put_str(&map, c, NULL); - if (!success) { + + sc_map_put_str(&map, c, NULL); + if (sc_map_oom(&map)) { free(c); break; } } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_foreach_key (&map, v) { @@ -1167,24 +1372,24 @@ void fail_test_sv() assert(sc_map_init_sv(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_sv(&map, &s[i], NULL); + sc_map_put_sv(&map, &s[i], NULL); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_sv(&map, &s[21], NULL)); + sc_map_put_sv(&map, &s[21], NULL); sc_map_clear_sv(&map); for (size_t i = 0; i < SC_MAP_MAX; i++) { char *c = str_random(32); - success = sc_map_put_sv(&map, c, NULL); - if (!success) { + sc_map_put_sv(&map, c, NULL); + if (sc_map_oom(&map)) { free(c); break; } } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_foreach_key (&map, v) { @@ -1206,24 +1411,24 @@ void fail_test_s64() assert(sc_map_init_s64(&map, 10, 0)); fail_calloc = true; - bool success = true; + for (int i = 0; i < 20; i++) { - success = sc_map_put_s64(&map, &s[i], 0); + sc_map_put_s64(&map, &s[i], 0); } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; - assert(sc_map_put_s64(&map, &s[21], 0)); + sc_map_put_s64(&map, &s[21], 0); sc_map_clear_s64(&map); for (size_t i = 0; i < SC_MAP_MAX; i++) { char *c = str_random(32); - success = sc_map_put_s64(&map, c, 0); - if (!success) { + sc_map_put_s64(&map, c, 0); + if (sc_map_oom(&map)) { free(c); break; } } - assert(!success); + assert(sc_map_oom(&map)); fail_calloc = false; sc_map_foreach_key (&map, v) { @@ -1260,6 +1465,7 @@ void fail_test_s64(void) int main() { example(); + example_int_to_str(); fail_test_32(); fail_test_64(); fail_test_64v(); diff --git a/map/sc_map.c b/map/sc_map.c index e06ebc6..ede65e1 100644 --- a/map/sc_map.c +++ b/map/sc_map.c @@ -205,20 +205,26 @@ return true; \ } \ \ - bool sc_map_put_##name(struct sc_map_##name *m, K key, V value) \ + V sc_map_put_##name(struct sc_map_##name *m, K key, V value) \ { \ + V ret; \ uint32_t pos, mod, h; \ \ + m->oom = false; \ + \ if (!sc_map_remap_##name(m)) { \ - return false; \ + m->oom = true; \ + return 0; \ } \ \ if (key == 0) { \ + ret = (m->used) ? m->mem[-1].value : 0; \ + m->found = m->used; \ m->size += !m->used; \ m->used = true; \ m->mem[-1].value = value; \ \ - return true; \ + return ret; \ } \ \ mod = m->cap - 1; \ @@ -233,20 +239,23 @@ continue; \ } \ \ + m->found = m->mem[pos].key != 0; \ + ret = m->found ? m->mem[pos].value : 0; \ sc_map_assign_##name(&m->mem[pos], key, value, h); \ - return true; \ + \ + return ret; \ } \ } \ \ /** NOLINTNEXTLINE */ \ - bool sc_map_get_##name(struct sc_map_##name *m, K key, V *value) \ + V sc_map_get_##name(struct sc_map_##name *m, K key) \ { \ const uint32_t mod = m->cap - 1; \ uint32_t h, pos; \ \ if (key == 0) { \ - *value = m->mem[-1].value; \ - return m->used; \ + m->found = m->used; \ + return m->used ? m->mem[-1].value : 0; \ } \ \ h = hash_fn(key); \ @@ -254,33 +263,31 @@ \ while (true) { \ if (m->mem[pos].key == 0) { \ - return false; \ + m->found = false; \ + return 0; \ } else if (!sc_map_cmp_##name(&m->mem[pos], key, h)) { \ pos = (pos + 1) & (mod); \ continue; \ } \ \ - *value = m->mem[pos].value; \ - return true; \ + m->found = true; \ + return m->mem[pos].value; \ } \ } \ \ /** NOLINTNEXTLINE */ \ - bool sc_map_del_##name(struct sc_map_##name *m, K key, V *value) \ + V sc_map_del_##name(struct sc_map_##name *m, K key) \ { \ const uint32_t mod = m->cap - 1; \ uint32_t pos, prev, it, p, h; \ + V ret; \ \ if (key == 0) { \ - bool ret = m->used; \ + m->found = m->used; \ m->size -= m->used; \ m->used = false; \ \ - if (value != NULL) { \ - *value = m->mem[-1].value; \ - } \ - \ - return ret; \ + return m->found ? m->mem[-1].value : 0; \ } \ \ h = hash_fn(key); \ @@ -288,15 +295,15 @@ \ while (true) { \ if (m->mem[pos].key == 0) { \ - return false; \ + m->found = false; \ + return 0; \ } else if (!sc_map_cmp_##name(&m->mem[pos], key, h)) { \ pos = (pos + 1) & (mod); \ continue; \ } \ \ - if (value != NULL) { \ - *value = m->mem[pos].value; \ - } \ + m->found = true; \ + ret = m->mem[pos].value; \ \ m->size--; \ m->mem[pos].key = 0; \ @@ -320,7 +327,7 @@ } \ } \ \ - return true; \ + return ret; \ } \ } diff --git a/map/sc_map.h b/map/sc_map.h index 41ff42e..ab368df 100644 --- a/map/sc_map.h +++ b/map/sc_map.h @@ -65,6 +65,8 @@ uint32_t load_fac; \ uint32_t remap; \ bool used; \ + bool oom; \ + bool found; \ }; \ \ /** \ @@ -123,9 +125,9 @@ * @param map map \ * @param K key \ * @param V value \ - * @return 'true' on success, 'false' on out of memory. \ + * @return previous value if exists \ */ \ - bool sc_map_put_##name(struct sc_map_##name *map, K key, V val); \ + V sc_map_put_##name(struct sc_map_##name *map, K key, V val); \ \ /** \ * Get element \ @@ -143,7 +145,7 @@ * @return 'true' if key exists, 'false' otherwise \ */ \ /** NOLINTNEXTLINE */ \ - bool sc_map_get_##name(struct sc_map_##name *map, K key, V *val); \ + V sc_map_get_##name(struct sc_map_##name *map, K key); \ \ /** \ * Delete element \ @@ -156,13 +158,23 @@ * \ * @param map map \ * @param K key \ - * @param V pointer to put current value \ - * - if key does not exist, value is undefined \ - * - Pass NULL if you don't want to get previous 'value' \ - * @return 'true' if key exists, 'false' otherwise \ + * @return current value if exists \ */ \ /** NOLINTNEXTLINE */ \ - bool sc_map_del_##name(struct sc_map_##name *map, K key, V *val); + V sc_map_del_##name(struct sc_map_##name *map, K key); + +/** + * @param map map + * @return - if put operation overrides a value, returns true + * - if del operation deletes a key, returns true + */ +#define sc_map_found(map) ((map)->found) + +/** + * @param map map + * @return true if put operation failed with out of memory + */ +#define sc_map_oom(map) ((map)->oom) /** * Foreach loop