Change Deque init to return pointer

This commit is contained in:
Stephen Seo 2019-01-27 18:30:35 +09:00
parent 599ac8a23e
commit 62c1cad3da
3 changed files with 70 additions and 66 deletions

View file

@ -3,25 +3,28 @@
#include <stdlib.h>
#include <string.h>
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)

View file

@ -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.

View file

@ -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);