From 62c1cad3da36ece4ef7366979dd91578b467e1fe Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 27 Jan 2019 18:30:35 +0900 Subject: [PATCH] Change Deque init to return pointer --- src/UDPC_Deque.c | 15 +++-- src/UDPC_Deque.h | 6 +- src/test/UDPC_UnitTest.c | 115 ++++++++++++++++++++------------------- 3 files changed, 70 insertions(+), 66 deletions(-) diff --git a/src/UDPC_Deque.c b/src/UDPC_Deque.c index 64c30d1..35a76a8 100644 --- a/src/UDPC_Deque.c +++ b/src/UDPC_Deque.c @@ -3,25 +3,28 @@ #include #include -int UDPC_Deque_init(UDPC_Deque *deque, uint32_t alloc_size) +UDPC_Deque* UDPC_Deque_init(uint32_t alloc_size) { + UDPC_Deque *deque = malloc(sizeof(UDPC_Deque)); UDPC_Deque_clear(deque); deque->alloc_size = alloc_size; deque->buf = malloc(alloc_size); if(deque->buf) { - return 1; + return deque; } else { - return 0; + free(deque); + return NULL; } } -void UDPC_Deque_destroy(UDPC_Deque *deque) +void UDPC_Deque_destroy(UDPC_Deque **deque) { - free(deque->buf); - deque->buf = NULL; + free((*deque)->buf); + free(*deque); + *deque = NULL; } int UDPC_Deque_realloc(UDPC_Deque *deque, uint32_t new_size) diff --git a/src/UDPC_Deque.h b/src/UDPC_Deque.h index 56285e2..6c8a6ca 100644 --- a/src/UDPC_Deque.h +++ b/src/UDPC_Deque.h @@ -13,14 +13,14 @@ typedef struct } UDPC_Deque; /*! - * \return non-zero on success + * \return non-null on success */ -int UDPC_Deque_init(UDPC_Deque *deque, uint32_t alloc_size); +UDPC_Deque* UDPC_Deque_init(uint32_t alloc_size); /*! * Frees resources used by a UDPC_Deque */ -void UDPC_Deque_destroy(UDPC_Deque *deque); +void UDPC_Deque_destroy(UDPC_Deque **deque); /*! * Fails if new_size is smaller than current size of Deque. diff --git a/src/test/UDPC_UnitTest.c b/src/test/UDPC_UnitTest.c index f24926a..22be834 100644 --- a/src/test/UDPC_UnitTest.c +++ b/src/test/UDPC_UnitTest.c @@ -14,123 +14,124 @@ int main() { arr[x] = x; } - UDPC_Deque deque; + UDPC_Deque *deque; // init - ASSERT_TRUE(UDPC_Deque_init(&deque, sizeof(int) * 32)); - ASSERT_TRUE(deque.buf); - ASSERT_EQ(deque.head, 0); - ASSERT_EQ(deque.tail, 0); - ASSERT_EQ(deque.size, 0); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 32); + deque = UDPC_Deque_init(sizeof(int) * 32); + ASSERT_TRUE(deque); + ASSERT_TRUE(deque->buf); + ASSERT_EQ(deque->head, 0); + ASSERT_EQ(deque->tail, 0); + ASSERT_EQ(deque->size, 0); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 32); // realloc smaller success - ASSERT_TRUE(UDPC_Deque_realloc(&deque, sizeof(int) * 16)); - ASSERT_TRUE(deque.buf); - ASSERT_EQ(deque.head, 0); - ASSERT_EQ(deque.tail, 0); - ASSERT_EQ(deque.size, 0); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 16); + ASSERT_TRUE(UDPC_Deque_realloc(deque, sizeof(int) * 16)); + ASSERT_TRUE(deque->buf); + ASSERT_EQ(deque->head, 0); + ASSERT_EQ(deque->tail, 0); + ASSERT_EQ(deque->size, 0); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 16); // push back success - ASSERT_TRUE(UDPC_Deque_push_back(&deque, arr, sizeof(int) * 4)); - ASSERT_EQ_MEM(arr, deque.buf, sizeof(int) * 4); - ASSERT_EQ(deque.size, sizeof(int) * 4); + ASSERT_TRUE(UDPC_Deque_push_back(deque, arr, sizeof(int) * 4)); + ASSERT_EQ_MEM(arr, deque->buf, sizeof(int) * 4); + ASSERT_EQ(deque->size, sizeof(int) * 4); // push front success - ASSERT_TRUE(UDPC_Deque_push_front(&deque, &arr[4], sizeof(int) * 4)); - ASSERT_EQ_MEM(&arr[4], &deque.buf[sizeof(int) * 12], sizeof(int) * 4); - ASSERT_EQ(deque.size, sizeof(int) * 8); + ASSERT_TRUE(UDPC_Deque_push_front(deque, &arr[4], sizeof(int) * 4)); + ASSERT_EQ_MEM(&arr[4], &deque->buf[sizeof(int) * 12], sizeof(int) * 4); + ASSERT_EQ(deque->size, sizeof(int) * 8); // realloc bigger success - ASSERT_TRUE(UDPC_Deque_realloc(&deque, sizeof(int) * 32)); - ASSERT_EQ_MEM(&arr[4], deque.buf, sizeof(int) * 4); - ASSERT_EQ_MEM(arr, &deque.buf[sizeof(int) * 4], sizeof(int) * 4); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 32); + ASSERT_TRUE(UDPC_Deque_realloc(deque, sizeof(int) * 32)); + ASSERT_EQ_MEM(&arr[4], deque->buf, sizeof(int) * 4); + ASSERT_EQ_MEM(arr, &deque->buf[sizeof(int) * 4], sizeof(int) * 4); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 32); // pop front success - UDPC_Deque_pop_front(&deque, sizeof(int) * 4); - ASSERT_EQ(deque.size, sizeof(int) * 4); + UDPC_Deque_pop_front(deque, sizeof(int) * 4); + ASSERT_EQ(deque->size, sizeof(int) * 4); // get front success size = sizeof(int) * 4; - ASSERT_TRUE(UDPC_Deque_get_front(&deque, (void**)&temp, &size)); + ASSERT_TRUE(UDPC_Deque_get_front(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 4); ASSERT_EQ_MEM(arr, temp, size); free(temp); // pop back success - UDPC_Deque_pop_back(&deque, sizeof(int) * 4); - ASSERT_EQ(deque.size, 0); + UDPC_Deque_pop_back(deque, sizeof(int) * 4); + ASSERT_EQ(deque->size, 0); // push front success - ASSERT_TRUE(UDPC_Deque_push_front(&deque, arr, sizeof(int) * 16)); - ASSERT_EQ(deque.size, sizeof(int) * 16); + ASSERT_TRUE(UDPC_Deque_push_front(deque, arr, sizeof(int) * 16)); + ASSERT_EQ(deque->size, sizeof(int) * 16); // get front success size = sizeof(int) * 16; - ASSERT_TRUE(UDPC_Deque_get_front(&deque, (void**)&temp, &size)); + ASSERT_TRUE(UDPC_Deque_get_front(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 16); ASSERT_EQ_MEM(arr, temp, size); free(temp); // get back success size = sizeof(int) * 16; - ASSERT_TRUE(UDPC_Deque_get_back(&deque, (void**)&temp, &size)); + ASSERT_TRUE(UDPC_Deque_get_back(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 16); ASSERT_EQ_MEM(arr, temp, size); free(temp); // realloc smaller fail - ASSERT_FALSE(UDPC_Deque_realloc(&deque, sizeof(int) * 8)); - ASSERT_EQ(deque.size, sizeof(int) * 16); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 32); + ASSERT_FALSE(UDPC_Deque_realloc(deque, sizeof(int) * 8)); + ASSERT_EQ(deque->size, sizeof(int) * 16); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 32); // realloc smaller success - ASSERT_TRUE(UDPC_Deque_realloc(&deque, sizeof(int) * 16)); - ASSERT_EQ(deque.size, sizeof(int) * 16); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 16); - ASSERT_EQ_MEM(deque.buf, arr, sizeof(int) * 16); + ASSERT_TRUE(UDPC_Deque_realloc(deque, sizeof(int) * 16)); + ASSERT_EQ(deque->size, sizeof(int) * 16); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 16); + ASSERT_EQ_MEM(deque->buf, arr, sizeof(int) * 16); // push back fail - ASSERT_FALSE(UDPC_Deque_push_back(&deque, arr, sizeof(int) * 16)); - ASSERT_EQ(deque.size, sizeof(int) * 16); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 16); - ASSERT_EQ_MEM(deque.buf, arr, sizeof(int) * 16); + ASSERT_FALSE(UDPC_Deque_push_back(deque, arr, sizeof(int) * 16)); + ASSERT_EQ(deque->size, sizeof(int) * 16); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 16); + ASSERT_EQ_MEM(deque->buf, arr, sizeof(int) * 16); // push front fail - ASSERT_FALSE(UDPC_Deque_push_back(&deque, arr, sizeof(int) * 16)); - ASSERT_EQ(deque.size, sizeof(int) * 16); - ASSERT_EQ(deque.alloc_size, sizeof(int) * 16); - ASSERT_EQ_MEM(deque.buf, arr, sizeof(int) * 16); + ASSERT_FALSE(UDPC_Deque_push_back(deque, arr, sizeof(int) * 16)); + ASSERT_EQ(deque->size, sizeof(int) * 16); + ASSERT_EQ(deque->alloc_size, sizeof(int) * 16); + ASSERT_EQ_MEM(deque->buf, arr, sizeof(int) * 16); // pop back - UDPC_Deque_pop_back(&deque, sizeof(int) * 8); + UDPC_Deque_pop_back(deque, sizeof(int) * 8); // get front success size = sizeof(int) * 8; - ASSERT_TRUE(UDPC_Deque_get_front(&deque, (void**)&temp, &size)); + ASSERT_TRUE(UDPC_Deque_get_front(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 8); ASSERT_EQ_MEM(temp, arr, sizeof(int) * 8); free(temp); // get front fail size = sizeof(int) * 16; - ASSERT_FALSE(UDPC_Deque_get_front(&deque, (void**)&temp, &size)); + ASSERT_FALSE(UDPC_Deque_get_front(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 8); ASSERT_EQ_MEM(temp, arr, sizeof(int) * 8); free(temp); // get back success size = sizeof(int) * 8; - ASSERT_TRUE(UDPC_Deque_get_back(&deque, (void**)&temp, &size)); + ASSERT_TRUE(UDPC_Deque_get_back(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 8); ASSERT_EQ_MEM(temp, arr, sizeof(int) * 8); free(temp); // get back fail size = sizeof(int) * 16; - ASSERT_FALSE(UDPC_Deque_get_back(&deque, (void**)&temp, &size)); + ASSERT_FALSE(UDPC_Deque_get_back(deque, (void**)&temp, &size)); ASSERT_EQ(size, sizeof(int) * 8); ASSERT_EQ_MEM(temp, arr, sizeof(int) * 8); free(temp); @@ -138,7 +139,7 @@ int main() // index success for(int x = 0; x < 8; ++x) { - ASSERT_TRUE(UDPC_Deque_index(&deque, sizeof(int), x, (void**)&temp)); + ASSERT_TRUE(UDPC_Deque_index(deque, sizeof(int), x, (void**)&temp)); if(temp) { ASSERT_EQ_MEM(temp, &arr[x], sizeof(int)); @@ -147,13 +148,13 @@ int main() } // index fail - ASSERT_FALSE(UDPC_Deque_index(&deque, sizeof(int), 8, (void**)&temp)); + ASSERT_FALSE(UDPC_Deque_index(deque, sizeof(int), 8, (void**)&temp)); ASSERT_FALSE(temp); // index_rev success for(int x = 0; x < 8; ++x) { - ASSERT_TRUE(UDPC_Deque_index_rev(&deque, sizeof(int), x, (void**)&temp)); + ASSERT_TRUE(UDPC_Deque_index_rev(deque, sizeof(int), x, (void**)&temp)); if(temp) { ASSERT_EQ_MEM(temp, &arr[7 - x], sizeof(int)); @@ -162,12 +163,12 @@ int main() } // index_rev fail - ASSERT_FALSE(UDPC_Deque_index_rev(&deque, sizeof(int), 8, (void**)&temp)); + ASSERT_FALSE(UDPC_Deque_index_rev(deque, sizeof(int), 8, (void**)&temp)); ASSERT_FALSE(temp); /* printf("asize %d, size %d, head %d, tail %d\n", - deque.alloc_size, deque.size, deque.head, deque.tail); + deque->alloc_size, deque->size, deque->head, deque->tail); */ UDPC_Deque_destroy(&deque);