static void wmem_test_strutls(void) { wmem_allocator_t *allocator; const char *orig_str; char *new_str; char **split_str; allocator = wmem_allocator_force_new(WMEM_ALLOCATOR_STRICT); orig_str = "TEST1"; new_str = wmem_strdup(allocator, orig_str); g_assert_cmpstr(new_str, ==, orig_str); new_str[0] = 'X'; g_assert_cmpstr(new_str, >, orig_str); wmem_strict_check_canaries(allocator); orig_str = "TEST123456789"; new_str = wmem_strndup(allocator, orig_str, 6); g_assert_cmpstr(new_str, ==, "TEST12"); g_assert_cmpstr(new_str, <, orig_str); new_str[0] = 'X'; g_assert_cmpstr(new_str, >, orig_str); wmem_strict_check_canaries(allocator); new_str = wmem_strdup_printf(allocator, "abc %s %% %d", "boo", 23); g_assert_cmpstr(new_str, ==, "abc boo % 23"); wmem_strict_check_canaries(allocator); new_str = wmem_strconcat(allocator, "ABC", NULL); g_assert_cmpstr(new_str, ==, "ABC"); new_str = wmem_strconcat(allocator, "ABC", "DEF", NULL); g_assert_cmpstr(new_str, ==, "ABCDEF"); wmem_strict_check_canaries(allocator); new_str = wmem_strconcat(allocator, "", "", "ABCDEF", "", "GH", NULL); g_assert_cmpstr(new_str, ==, "ABCDEFGH"); wmem_strict_check_canaries(allocator); split_str = wmem_strsplit(allocator, "A-C", "-", 2); g_assert_cmpstr(split_str[0], ==, "A"); g_assert_cmpstr(split_str[1], ==, "C"); split_str = wmem_strsplit(allocator, "--aslkf-asio--asfj-as--", "-", 10); g_assert_cmpstr(split_str[0], ==, "aslkf"); g_assert_cmpstr(split_str[1], ==, "asio"); g_assert_cmpstr(split_str[2], ==, "asfj"); g_assert_cmpstr(split_str[3], ==, "as"); split_str = wmem_strsplit(allocator, "--aslkf-asio--asfj-as--", "-", 4); g_assert_cmpstr(split_str[0], ==, "aslkf"); g_assert_cmpstr(split_str[1], ==, "asio"); g_assert_cmpstr(split_str[2], ==, "-asfj-as--"); wmem_destroy_allocator(allocator); }
static void wmem_test_stack(void) { wmem_allocator_t *allocator; wmem_stack_t *stack; unsigned int i; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); stack = wmem_stack_new(allocator); g_assert(stack); g_assert(wmem_stack_count(stack) == 0); for (i=0; i<CONTAINER_ITERS; i++) { wmem_stack_push(stack, GINT_TO_POINTER(i)); g_assert(wmem_stack_count(stack) == i+1); g_assert(wmem_stack_peek(stack) == GINT_TO_POINTER(i)); } wmem_strict_check_canaries(allocator); for (i=CONTAINER_ITERS; i>0; i--) { g_assert(wmem_stack_peek(stack) == GINT_TO_POINTER(i-1)); g_assert(wmem_stack_pop(stack) == GINT_TO_POINTER(i-1)); g_assert(wmem_stack_count(stack) == i-1); } g_assert(wmem_stack_count(stack) == 0); wmem_destroy_stack(stack); wmem_destroy_allocator(allocator); }
static void wmem_test_queue(void) { wmem_allocator_t *allocator; wmem_queue_t *queue; unsigned int i; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); queue = wmem_queue_new(allocator); g_assert(queue); g_assert(wmem_queue_count(queue) == 0); for (i=0; i<CONTAINER_ITERS; i++) { wmem_queue_push(queue, GINT_TO_POINTER(i)); g_assert(wmem_queue_count(queue) == i+1); g_assert(wmem_queue_peek(queue) == GINT_TO_POINTER(0)); } wmem_strict_check_canaries(allocator); for (i=0; i<CONTAINER_ITERS; i++) { g_assert(wmem_queue_peek(queue) == GINT_TO_POINTER(i)); g_assert(wmem_queue_pop(queue) == GINT_TO_POINTER(i)); g_assert(wmem_queue_count(queue) == CONTAINER_ITERS-i-1); } g_assert(wmem_queue_count(queue) == 0); wmem_destroy_queue(queue); wmem_destroy_allocator(allocator); }
static void wmem_test_miscutls(void) { wmem_allocator_t *allocator; const char *source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *ret; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); ret = (char*) wmem_memdup(allocator, source, 5); ret[4] = '\0'; g_assert_cmpstr(ret, ==, "ABCD"); ret = (char*) wmem_memdup(allocator, source, 1); g_assert(ret[0] == 'A'); wmem_strict_check_canaries(allocator); ret = (char*) wmem_memdup(allocator, source, 10); ret[9] = '\0'; g_assert_cmpstr(ret, ==, "ABCDEFGHI"); wmem_destroy_allocator(allocator); }
static void wmem_test_strbuf(void) { wmem_allocator_t *allocator; wmem_strbuf_t *strbuf; int i; char *str; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); strbuf = wmem_strbuf_new(allocator, "TEST"); g_assert(strbuf); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TEST"); g_assert(wmem_strbuf_get_len(strbuf) == 4); wmem_strbuf_append(strbuf, "FUZZ"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ"); g_assert(wmem_strbuf_get_len(strbuf) == 8); wmem_strbuf_append_printf(strbuf, "%d%s", 3, "a"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3a"); g_assert(wmem_strbuf_get_len(strbuf) == 10); wmem_strbuf_append_c(strbuf, 'q'); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq"); g_assert(wmem_strbuf_get_len(strbuf) == 11); wmem_strbuf_append_unichar(strbuf, g_utf8_get_char("\xC2\xA9")); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq\xC2\xA9"); g_assert(wmem_strbuf_get_len(strbuf) == 13); wmem_strbuf_truncate(strbuf, 32); wmem_strbuf_truncate(strbuf, 24); wmem_strbuf_truncate(strbuf, 16); wmem_strbuf_truncate(strbuf, 13); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq\xC2\xA9"); g_assert(wmem_strbuf_get_len(strbuf) == 13); wmem_strbuf_truncate(strbuf, 3); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TES"); g_assert(wmem_strbuf_get_len(strbuf) == 3); strbuf = wmem_strbuf_sized_new(allocator, 10, 10); g_assert(strbuf); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, ""); g_assert(wmem_strbuf_get_len(strbuf) == 0); wmem_strbuf_append(strbuf, "FUZZ"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ"); g_assert(wmem_strbuf_get_len(strbuf) == 4); wmem_strbuf_append_printf(strbuf, "%d%s", 3, "abcdefghijklmnop"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append(strbuf, "abcdefghijklmnopqrstuvwxyz"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append_c(strbuf, 'q'); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append_unichar(strbuf, g_utf8_get_char("\xC2\xA9")); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); str = wmem_strbuf_finalize(strbuf); g_assert_cmpstr(str, ==, "FUZZ3abcd"); g_assert(strlen(str) == 9); wmem_free_all(allocator); strbuf = wmem_strbuf_new(allocator, "TEST"); for (i=0; i<1024; i++) { if (g_test_rand_bit()) { wmem_strbuf_append(strbuf, "ABC"); } else { wmem_strbuf_append_printf(strbuf, "%d%d", 3, 777); } wmem_strict_check_canaries(allocator); } g_assert(strlen(wmem_strbuf_get_str(strbuf)) == wmem_strbuf_get_len(strbuf)); wmem_destroy_allocator(allocator); }
static void wmem_test_list(void) { wmem_allocator_t *allocator; wmem_list_t *list; wmem_list_frame_t *frame; unsigned int i; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); list = wmem_list_new(allocator); g_assert(list); g_assert(wmem_list_count(list) == 0); frame = wmem_list_head(list); g_assert(frame == NULL); for (i=0; i<CONTAINER_ITERS; i++) { wmem_list_prepend(list, GINT_TO_POINTER(i)); g_assert(wmem_list_count(list) == i+1); frame = wmem_list_head(list); g_assert(frame); g_assert(wmem_list_frame_data(frame) == GINT_TO_POINTER(i)); } wmem_strict_check_canaries(allocator); i = CONTAINER_ITERS - 1; frame = wmem_list_head(list); while (frame) { g_assert(wmem_list_frame_data(frame) == GINT_TO_POINTER(i)); i--; frame = wmem_list_frame_next(frame); } i = 0; frame = wmem_list_tail(list); while (frame) { g_assert(wmem_list_frame_data(frame) == GINT_TO_POINTER(i)); i++; frame = wmem_list_frame_prev(frame); } i = CONTAINER_ITERS - 2; while (wmem_list_count(list) > 1) { wmem_list_remove(list, GINT_TO_POINTER(i)); i--; } wmem_list_remove(list, GINT_TO_POINTER(CONTAINER_ITERS - 1)); g_assert(wmem_list_count(list) == 0); g_assert(wmem_list_head(list) == NULL); g_assert(wmem_list_tail(list) == NULL); for (i=0; i<CONTAINER_ITERS; i++) { wmem_list_append(list, GINT_TO_POINTER(i)); g_assert(wmem_list_count(list) == i+1); frame = wmem_list_head(list); g_assert(frame); } wmem_strict_check_canaries(allocator); i = 0; frame = wmem_list_head(list); while (frame) { g_assert(wmem_list_frame_data(frame) == GINT_TO_POINTER(i)); i++; frame = wmem_list_frame_next(frame); } i = CONTAINER_ITERS - 1; frame = wmem_list_tail(list); while (frame) { g_assert(wmem_list_frame_data(frame) == GINT_TO_POINTER(i)); i--; frame = wmem_list_frame_prev(frame); } wmem_destroy_allocator(allocator); list = wmem_list_new(NULL); for (i=0; i<CONTAINER_ITERS; i++) { wmem_list_prepend(list, GINT_TO_POINTER(i)); } g_assert(wmem_list_count(list) == CONTAINER_ITERS); wmem_destroy_list(list); }
static void wmem_test_array(void) { wmem_allocator_t *allocator; wmem_array_t *array; unsigned int i, j, k; guint32 val, *buf; guint32 vals[8]; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); array = wmem_array_new(allocator, sizeof(guint32)); g_assert(array); g_assert(wmem_array_get_count(array) == 0); for (i=0; i<CONTAINER_ITERS; i++) { val = i; wmem_array_append_one(array, val); g_assert(wmem_array_get_count(array) == i+1); val = *(guint32*)wmem_array_index(array, i); g_assert(val == i); } wmem_strict_check_canaries(allocator); for (i=0; i<CONTAINER_ITERS; i++) { val = *(guint32*)wmem_array_index(array, i); g_assert(val == i); } array = wmem_array_sized_new(allocator, sizeof(guint32), 73); for (i=0; i<CONTAINER_ITERS; i++) { for (j=0; j<8; j++) { vals[j] = i+j; } wmem_array_append(array, vals, 8); g_assert(wmem_array_get_count(array) == 8*(i+1)); } wmem_strict_check_canaries(allocator); buf = (guint32*)wmem_array_get_raw(array); for (i=0; i<CONTAINER_ITERS; i++) { for (j=0; j<8; j++) { g_assert(buf[i*8 + j] == i+j); } } wmem_array_sort(array, wmem_test_compare_guint32); for (i=0, k=0; i<8; i++) { for (j=0; j<=i; j++, k++) { val = *(guint32*)wmem_array_index(array, k); g_assert(val == i); } } for (j=k; k<8*(CONTAINER_ITERS+1)-j; k++) { val = *(guint32*)wmem_array_index(array, k); g_assert(val == ((k-j)/8)+8); } for (i=0; i<7; i++) { for (j=0; j<7-i; j++, k++) { val = *(guint32*)wmem_array_index(array, k); g_assert(val == CONTAINER_ITERS+i); } } g_assert(k == wmem_array_get_count(array)); wmem_destroy_allocator(allocator); }