diff options
Diffstat (limited to 'libglusterfs/src/unittest/mem_pool_unittest.c')
| -rw-r--r-- | libglusterfs/src/unittest/mem_pool_unittest.c | 205 |
1 files changed, 108 insertions, 97 deletions
diff --git a/libglusterfs/src/unittest/mem_pool_unittest.c b/libglusterfs/src/unittest/mem_pool_unittest.c index 3c0724d65e5..9ca324329ba 100644 --- a/libglusterfs/src/unittest/mem_pool_unittest.c +++ b/libglusterfs/src/unittest/mem_pool_unittest.c @@ -8,17 +8,24 @@ cases as published by the Free Software Foundation. */ -#include "mem-pool.h" -#include "logging.h" -#include "xlator.h" +#include "glusterfs/mem-pool.h" +#include "glusterfs/logging.h" +#include "glusterfs/xlator.h" #include <stdarg.h> #include <stddef.h> #include <setjmp.h> #include <inttypes.h> #include <string.h> -#include <cmockery/pbc.h> -#include <cmockery/cmockery.h> +#include <cmocka_pbc.h> +#include <cmocka.h> + +#ifndef assert_ptr_equal +#define assert_ptr_equal(a, b) \ + _assert_int_equal(cast_ptr_to_largest_integral_type(a), \ + cast_ptr_to_largest_integral_type(b), __FILE__, \ + __LINE__) +#endif /* * memory header for gf_mem_set_acct_info @@ -35,8 +42,8 @@ typedef struct __attribute__((packed)) { * Prototypes to private functions */ int -gf_mem_set_acct_info (xlator_t *xl, char **alloc_ptr, - size_t size, uint32_t type); +gf_mem_set_acct_info(xlator_t *xl, char **alloc_ptr, size_t size, uint32_t type, + const char *typestr); /* * Helper functions @@ -51,19 +58,20 @@ helper_xlator_init(uint32_t num_types) xl = test_calloc(1, sizeof(xlator_t)); assert_non_null(xl); - xl->mem_acct.num_types = num_types; - xl->mem_acct.rec = test_calloc(num_types, sizeof(struct mem_acct_rec)); - assert_non_null(xl->mem_acct.rec); + xl->mem_acct->num_types = num_types; + xl->mem_acct = test_calloc(sizeof(struct mem_acct) + + sizeof(struct mem_acct_rec) * num_types); + assert_non_null(xl->mem_acct); xl->ctx = test_calloc(1, sizeof(glusterfs_ctx_t)); assert_non_null(xl->ctx); for (i = 0; i < num_types; i++) { - ret = LOCK_INIT(&(xl->mem_acct.rec[i].lock)); - assert_int_equal(ret, 0); + ret = LOCK_INIT(&(xl->mem_acct->rec[i].lock)); + assert_int_equal(ret, 0); } - ENSURE(num_types == xl->mem_acct.num_types); + ENSURE(num_types == xl->mem_acct->num_types); ENSURE(NULL != xl); return xl; @@ -74,32 +82,28 @@ helper_xlator_destroy(xlator_t *xl) { int i, ret; - for (i = 0; i < xl->mem_acct.num_types; i++) { - ret = LOCK_DESTROY(&(xl->mem_acct.rec[i].lock)); - assert_int_equal(ret, 0); + for (i = 0; i < xl->mem_acct->num_types; i++) { + ret = LOCK_DESTROY(&(xl->mem_acct->rec[i].lock)); + assert_int_equal(ret, 0); } - free(xl->mem_acct.rec); + free(xl->mem_acct->rec); free(xl->ctx); free(xl); return 0; } static void -helper_check_memory_headers( char *mem, - xlator_t *xl, - size_t size, - uint32_t type) +helper_check_memory_headers(char *mem, xlator_t *xl, size_t size, uint32_t type) { mem_header_t *p; - p = (mem_header_t *)mem, - assert_int_equal(p->type, type); + p = (mem_header_t *)mem, assert_int_equal(p->type, type); assert_int_equal(p->size, size); assert_true(p->xl == xl); assert_int_equal(p->header_magic, GF_MEM_HEADER_MAGIC); - assert_true(*(uint32_t *)(mem+sizeof(mem_header_t)+size) == GF_MEM_TRAILER_MAGIC); - + assert_true(*(uint32_t *)(mem + sizeof(mem_header_t) + size) == + GF_MEM_TRAILER_MAGIC); } /* @@ -108,7 +112,7 @@ helper_check_memory_headers( char *mem, static void test_gf_mem_acct_enable_set(void **state) { - (void) state; + (void)state; glusterfs_ctx_t test_ctx; expect_assert_failure(gf_mem_acct_enable_set(NULL)); @@ -135,25 +139,27 @@ test_gf_mem_set_acct_info_asserts(void **state) size = 8196; type = 0; - // Check xl is NULL - expect_assert_failure(gf_mem_set_acct_info(NULL, &alloc_ptr, size, type)); - // Check xl->mem_acct.rec = NULL - expect_assert_failure(gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type)); - // Check type <= xl->mem_acct.num_types + expect_assert_failure( + gf_mem_set_acct_info(NULL, &alloc_ptr, size, type, "")); + // Check xl->mem_acct = NULL + expect_assert_failure( + gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type, "")); + // Check type <= xl->mem_acct->num_types type = 100; - expect_assert_failure(gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type)); + expect_assert_failure( + gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type, "")); // Check alloc is NULL - assert_int_equal(-1, gf_mem_set_acct_info(&xltest, NULL, size, type)); + assert_int_equal(-1, gf_mem_set_acct_info(&xltest, NULL, size, type, "")); // Initialize xl xl = helper_xlator_init(10); // Test number of types type = 100; - assert_true(NULL != xl->mem_acct.rec); - assert_true(type > xl->mem_acct.num_types); - expect_assert_failure(gf_mem_set_acct_info(xl, &alloc_ptr, size, type)); + assert_true(NULL != xl->mem_acct); + assert_true(type > xl->mem_acct->num_types); + expect_assert_failure(gf_mem_set_acct_info(xl, &alloc_ptr, size, type, "")); helper_xlator_destroy(xl); } @@ -166,25 +172,28 @@ test_gf_mem_set_acct_info_memory(void **state) char *temp_ptr; size_t size; uint32_t type; + const char *typestr = "TEST"; size = 8196; type = 9; // Initialize xl xl = helper_xlator_init(10); + assert_null(xl->mem_acct->rec[type].typestr); // Test allocation temp_ptr = test_calloc(1, size + GF_MEM_HEADER_SIZE + GF_MEM_TRAILER_SIZE); assert_non_null(temp_ptr); alloc_ptr = temp_ptr; - gf_mem_set_acct_info(xl, &alloc_ptr, size, type); + gf_mem_set_acct_info(xl, &alloc_ptr, size, type, typestr); - //Check values - assert_int_equal(xl->mem_acct.rec[type].size, size); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].max_size, size); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 1); + // Check values + assert_ptr_equal(typestr, xl->mem_acct->rec[type].typestr); + assert_int_equal(xl->mem_acct->rec[type].size, size); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].max_size, size); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1); // Check memory helper_check_memory_headers(temp_ptr, xl, size, type); @@ -220,16 +229,16 @@ test_gf_calloc_default_calloc(void **state) // Call __gf_calloc size = 1024; type = 3; - mem = __gf_calloc(1, size, type); + mem = __gf_calloc(1, size, type, "3"); assert_non_null(mem); memset(mem, 0x5A, size); // Check xl did not change - assert_int_equal(xl->mem_acct.rec[type].size, 0); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].max_size, 0); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].size, 0); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].max_size, 0); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0); free(mem); helper_xlator_destroy(xl); @@ -249,21 +258,21 @@ test_gf_calloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_calloc size = 1024; type = 3; - mem = __gf_calloc(1, size, type); + mem = __gf_calloc(1, size, type, "3"); assert_non_null(mem); memset(mem, 0x5A, size); // Check xl values - assert_int_equal(xl->mem_acct.rec[type].size, size); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].max_size, size); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].size, size); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].max_size, size); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1); // Check memory helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type); @@ -287,16 +296,16 @@ test_gf_malloc_default_malloc(void **state) // Call __gf_malloc size = 1024; type = 3; - mem = __gf_malloc(size, type); + mem = __gf_malloc(size, type, "3"); assert_non_null(mem); memset(mem, 0x5A, size); // Check xl did not change - assert_int_equal(xl->mem_acct.rec[type].size, 0); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].max_size, 0); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].size, 0); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].max_size, 0); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0); free(mem); helper_xlator_destroy(xl); @@ -316,21 +325,21 @@ test_gf_malloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc size = 1024; type = 3; - mem = __gf_malloc(size, type); + mem = __gf_malloc(size, type, "3"); assert_non_null(mem); memset(mem, 0x5A, size); // Check xl values - assert_int_equal(xl->mem_acct.rec[type].size, size); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 1); - assert_int_equal(xl->mem_acct.rec[type].max_size, size); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].size, size); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1); + assert_int_equal(xl->mem_acct->rec[type].max_size, size); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1); // Check memory helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type); @@ -349,12 +358,12 @@ test_gf_realloc_default_realloc(void **state) // Initialize xl xl = helper_xlator_init(10); assert_int_equal(xl->ctx->mem_acct_enable, 0); - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc then realloc size = 10; type = 3; - mem = __gf_malloc(size, type); + mem = __gf_malloc(size, type, "3"); assert_non_null(mem); memset(mem, 0xA5, size); @@ -364,11 +373,11 @@ test_gf_realloc_default_realloc(void **state) memset(mem, 0x5A, size); // Check xl did not change - assert_int_equal(xl->mem_acct.rec[type].size, 0); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 0); - assert_int_equal(xl->mem_acct.rec[type].max_size, 0); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].size, 0); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0); + assert_int_equal(xl->mem_acct->rec[type].max_size, 0); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0); free(mem); helper_xlator_destroy(xl); @@ -388,12 +397,12 @@ test_gf_realloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc then realloc size = 1024; type = 3; - mem = __gf_malloc(size, type); + mem = __gf_malloc(size, type, "3"); assert_non_null(mem); memset(mem, 0xA5, size); @@ -409,11 +418,11 @@ test_gf_realloc_mem_acct_enabled(void **state) // not to the realloc + the malloc. // Is this a bug? // - assert_int_equal(xl->mem_acct.rec[type].size, size+1024); - assert_int_equal(xl->mem_acct.rec[type].num_allocs, 2); - assert_int_equal(xl->mem_acct.rec[type].total_allocs, 2); - assert_int_equal(xl->mem_acct.rec[type].max_size, size+1024); - assert_int_equal(xl->mem_acct.rec[type].max_num_allocs, 2); + assert_int_equal(xl->mem_acct->rec[type].size, size + 1024); + assert_int_equal(xl->mem_acct->rec[type].num_allocs, 2); + assert_int_equal(xl->mem_acct->rec[type].total_allocs, 2); + assert_int_equal(xl->mem_acct->rec[type].max_size, size + 1024); + assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 2); // Check memory helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type); @@ -433,7 +442,7 @@ test_gf_realloc_ptr(void **state) assert_int_equal(xl->ctx->mem_acct_enable, 0); // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Tests according to the manpage for realloc @@ -454,19 +463,21 @@ test_gf_realloc_ptr(void **state) helper_xlator_destroy(xl); } -int main(void) { - const UnitTest tests[] = { - unit_test(test_gf_mem_acct_enable_set), - unit_test(test_gf_mem_set_acct_info_asserts), - unit_test(test_gf_mem_set_acct_info_memory), - unit_test(test_gf_calloc_default_calloc), - unit_test(test_gf_calloc_mem_acct_enabled), - unit_test(test_gf_malloc_default_malloc), - unit_test(test_gf_malloc_mem_acct_enabled), - unit_test(test_gf_realloc_default_realloc), - unit_test(test_gf_realloc_mem_acct_enabled), - unit_test(test_gf_realloc_ptr), +int +main(void) +{ + const struct CMUnitTest libglusterfs_mem_pool_tests[] = { + cmocka_unit_test(test_gf_mem_acct_enable_set), + cmocka_unit_test(test_gf_mem_set_acct_info_asserts), + cmocka_unit_test(test_gf_mem_set_acct_info_memory), + cmocka_unit_test(test_gf_calloc_default_calloc), + cmocka_unit_test(test_gf_calloc_mem_acct_enabled), + cmocka_unit_test(test_gf_malloc_default_malloc), + cmocka_unit_test(test_gf_malloc_mem_acct_enabled), + cmocka_unit_test(test_gf_realloc_default_realloc), + cmocka_unit_test(test_gf_realloc_mem_acct_enabled), + cmocka_unit_test(test_gf_realloc_ptr), }; - return run_tests(tests, "libglusterfs_mem_pool"); + return cmocka_run_group_tests(libglusterfs_mem_pool_tests, NULL, NULL); } |
