From 29e3940c14a540ab146b34cf14527c6add463a7b Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 21 Feb 2019 12:34:35 +0900 Subject: [PATCH] Add push_..._realloc to Deque --- src/UDPC_Deque.c | 24 ++++++++++++++++++++++++ src/UDPC_Deque.h | 14 +++++++++++++- src/test/UDPC_UnitTest.c | 21 +++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/UDPC_Deque.c b/src/UDPC_Deque.c index e1cee32..c49ba11 100644 --- a/src/UDPC_Deque.c +++ b/src/UDPC_Deque.c @@ -140,6 +140,30 @@ int UDPC_Deque_push_front(UDPC_Deque *deque, const void *data, uint32_t size) return 1; } +int UDPC_Deque_push_back_realloc(UDPC_Deque *deque, const void *data, uint32_t size) +{ + if(UDPC_Deque_push_back(deque, data, size) == 0) + { + if(UDPC_Deque_realloc(deque, deque->alloc_size * 2) != 0) + { + return UDPC_Deque_push_back(deque, data, size); + } + } + return 0; +} + +int UDPC_Deque_push_front_realloc(UDPC_Deque *deque, const void *data, uint32_t size) +{ + if(UDPC_Deque_push_front(deque, data, size) == 0) + { + if(UDPC_Deque_realloc(deque, deque->alloc_size * 2) != 0) + { + return UDPC_Deque_push_front(deque, data, size); + } + } + return 0; +} + uint32_t UDPC_Deque_get_available(UDPC_Deque *deque) { return deque->alloc_size - deque->size; diff --git a/src/UDPC_Deque.h b/src/UDPC_Deque.h index 9434cff..91be55a 100644 --- a/src/UDPC_Deque.h +++ b/src/UDPC_Deque.h @@ -40,7 +40,19 @@ int UDPC_Deque_push_back(UDPC_Deque *deque, const void *data, uint32_t size); */ int UDPC_Deque_push_front(UDPC_Deque *deque, const void *data, uint32_t size); -// TODO add push_back/push_front variants that realloc on not enough free space +/*! + * \brief Same as push_back, but realloc if not enough free space + * Note when realloc occurs, the allocated space is doubled. + * \return non-zero on data pushed into Deque success + */ +int UDPC_Deque_push_back_realloc(UDPC_Deque *deque, const void *data, uint32_t size); + +/*! + * \brief Same as push_front, but realloc if not enough free space + * Note when realloc occurs, the allocated space is doubled. + * \return non-zero on data pushed into Deque success + */ +int UDPC_Deque_push_front_realloc(UDPC_Deque *deque, const void *data, uint32_t size); /*! * \return size in bytes of available data diff --git a/src/test/UDPC_UnitTest.c b/src/test/UDPC_UnitTest.c index d345a90..32ac414 100644 --- a/src/test/UDPC_UnitTest.c +++ b/src/test/UDPC_UnitTest.c @@ -206,6 +206,27 @@ void TEST_DEQUE() } ASSERT_EQ(deque->size, 0); + // test push_back_realloc + ASSERT_EQ(deque->alloc_size, 16 * sizeof(int)); + for(int x = 0; x < 16; ++x) + { + ASSERT_NEQ(UDPC_Deque_push_back(deque, &x, sizeof(int)), 0); + } + int tempInt = 20; + ASSERT_EQ(UDPC_Deque_push_back(deque, &tempInt, sizeof(int)), 0); + ASSERT_NEQ(UDPC_Deque_push_back_realloc(deque, &tempInt, sizeof(int)), 0); + ASSERT_EQ(deque->alloc_size, 32 * sizeof(int)); + ASSERT_EQ_MEM(UDPC_Deque_get_back_ptr(deque, sizeof(int)), &tempInt, sizeof(int)); + + UDPC_Deque_pop_back(deque, sizeof(int)); + ASSERT_NEQ(UDPC_Deque_realloc(deque, 16 * sizeof(int)), 0); + + // test push_front_realloc + ASSERT_EQ(UDPC_Deque_push_front(deque, &tempInt, sizeof(int)), 0); + ASSERT_NEQ(UDPC_Deque_push_front_realloc(deque, &tempInt, sizeof(int)), 0); + ASSERT_EQ(deque->alloc_size, 32 * sizeof(int)); + ASSERT_EQ_MEM(UDPC_Deque_get_front_ptr(deque, sizeof(int)), &tempInt, sizeof(int)); + /* for(int x = 0; x < deque->tail / sizeof(int); ++x) {