diff options
Diffstat (limited to 'libglusterfs/src')
-rw-r--r-- | libglusterfs/src/Makefile.am | 16 | ||||
-rw-r--r-- | libglusterfs/src/mem-pool.c | 9 | ||||
-rw-r--r-- | libglusterfs/src/unittest/mem_pool_unittest.c | 31 |
3 files changed, 42 insertions, 14 deletions
diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 25ee4c27a8b..de1b9b0b1f2 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -58,3 +58,19 @@ y.tab.h: graph.y CLEANFILES = graph.lex.c y.tab.c y.tab.h CONFIG_CLEAN_FILES = $(CONTRIB_BUILDDIR)/uuid/uuid_types.h + +#### UNIT TESTS ##### +CLEANFILES += *.gcda *.gcno *_xunit.xml +noinst_PROGRAMS = +TESTS = + +mem_pool_unittest_CPPFLAGS = $(libglusterfs_la_CPPFLAGS) +mem_pool_unittest_SOURCES = mem-pool.c \ + mem-pool.h \ + unittest/mem_pool_unittest.c \ + unittest/log_mock.c \ + unittest/global_mock.c +mem_pool_unittest_CFLAGS = $(UNITTEST_CFLAGS) +mem_pool_unittest_LDFLAGS = $(UNITTEST_LDFLAGS) +noinst_PROGRAMS += mem_pool_unittest +TESTS += mem_pool_unittest diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c index 356cfdb78ca..093592ec056 100644 --- a/libglusterfs/src/mem-pool.c +++ b/libglusterfs/src/mem-pool.c @@ -24,17 +24,24 @@ #define GLUSTERFS_ENV_MEM_ACCT_STR "GLUSTERFS_DISABLE_MEM_ACCT" +#include <cmockery/pbc.h> +#include <cmockery/cmockery_override.h> + void gf_mem_acct_enable_set (void *data) { glusterfs_ctx_t *ctx = NULL; + REQUIRE(data != NULL); + ctx = data; GF_ASSERT (ctx != NULL); ctx->mem_acct_enable = 1; + ENSURE(1 == ctx->mem_acct_enable); + return; } @@ -151,6 +158,8 @@ __gf_realloc (void *ptr, size_t size) if (!THIS->ctx->mem_acct_enable) return REALLOC (ptr, size); + REQUIRE(NULL != ptr); + tot_size = size + GF_MEM_HEADER_SIZE + GF_MEM_TRAILER_SIZE; orig_ptr = (char *)ptr - 8 - 4; diff --git a/libglusterfs/src/unittest/mem_pool_unittest.c b/libglusterfs/src/unittest/mem_pool_unittest.c index 3c0724d65e5..0d7aa199df0 100644 --- a/libglusterfs/src/unittest/mem_pool_unittest.c +++ b/libglusterfs/src/unittest/mem_pool_unittest.c @@ -35,8 +35,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 @@ -137,14 +137,14 @@ test_gf_mem_set_acct_info_asserts(void **state) // Check xl is NULL - expect_assert_failure(gf_mem_set_acct_info(NULL, &alloc_ptr, size, type)); + 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)); + 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); @@ -153,7 +153,7 @@ test_gf_mem_set_acct_info_asserts(void **state) 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)); + expect_assert_failure(gf_mem_set_acct_info(xl, &alloc_ptr, size, type, "")); helper_xlator_destroy(xl); } @@ -166,20 +166,23 @@ 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_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); @@ -220,7 +223,7 @@ 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); @@ -254,7 +257,7 @@ test_gf_calloc_mem_acct_enabled(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); @@ -287,7 +290,7 @@ 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); @@ -321,7 +324,7 @@ test_gf_malloc_mem_acct_enabled(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); @@ -354,7 +357,7 @@ test_gf_realloc_default_realloc(void **state) // 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); @@ -393,7 +396,7 @@ test_gf_realloc_mem_acct_enabled(void **state) // 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); |