From 8306616e3ab3b2cbcbaf7dc70931d2ed394a1aa5 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Fri, 15 May 2015 11:15:48 +0200 Subject: [PATCH] improve test/makemain.sh, move queu_pop code to vector class --- src/Queue.c | 8 +--- src/Vector.c | 9 +++++ src/Vector.h | 7 ++++ test/Makefile.base | 2 +- test/main.c | 92 +++++++++++++++++++++++----------------------- test/makeMain.sh | 6 +-- test/t.Queue.c | 3 +- test/t.Stack.c | 3 +- 8 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/Queue.c b/src/Queue.c index 794fa89..b85cf39 100644 --- a/src/Queue.c +++ b/src/Queue.c @@ -49,12 +49,8 @@ void* _queue_peek(Queue* queue) void queue_pop(Queue* queue) { - //remove first vector element and shift its internal array - safe_free(queue->array->datas[0]); - queue->array->datas++; - //NOTE: we remove first element, so capacity decrease too - queue->array->size--; - queue->array->capacity--; + vector_pop_first(queue->array); + } void queue_clear(Queue* queue) diff --git a/src/Vector.c b/src/Vector.c index 2d00985..a303d54 100644 --- a/src/Vector.c +++ b/src/Vector.c @@ -82,6 +82,15 @@ void vector_pop(Vector* vector) _vector_realloc(vector, vector->capacity >> 1); } +void vector_pop_first(Vector* vector) +{ + safe_free(vector->datas[0]); + vector->datas++; + vector->size--; + if (vector_size(vector) <= (vector->capacity >> 1)) + _vector_realloc(vector, vector->capacity >> 1); +} + void* _vector_get(Vector* vector, UInt index) { return vector->datas[index]; diff --git a/src/Vector.h b/src/Vector.h index d3fe04c..985ac10 100644 --- a/src/Vector.h +++ b/src/Vector.h @@ -105,6 +105,13 @@ void vector_pop( Vector* vector ///< "this" pointer. ); +/** + * @brief Remove the first element. + */ +void vector_pop_first( + Vector* vector ///< "this" pointer. +); + /** * @brief Get the element at given index. */ diff --git a/test/Makefile.base b/test/Makefile.base index 125781f..549baf8 100644 --- a/test/Makefile.base +++ b/test/Makefile.base @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -g -std=gnu99 +CFLAGS = -g -std=gnu99 -Wno-implicit-function-declaration LDFLAGS = -L../src/obj -lcgds INCLUDES = -I.. -I../src TARGET = testcgds diff --git a/test/main.c b/test/main.c index 7c08f3f..a6e926c 100644 --- a/test/main.c +++ b/test/main.c @@ -2,61 +2,61 @@ int main(int argc, char** argv) { - //file ./t.Stack.c : - t_stack_clear(); - t_stack_size(); - t_stack_push_pop_basic(); - t_stack_push_pop_evolved(); - t_stack_copy(); + //file ./t.Vector.c : + void t_vector_clear(); + void t_vector_size(); + void t_vector_push_pop_basic(); + void t_vector_push_pop_evolved(); + void t_vector_copy(); - //file ./t.Heap.c : - t_heap_clear(); - t_heap_size(); - t_heap_push_pop_basic(); - t_heap_push_pop_evolved(); - t_heap_copy(); + //file ./t.BufferTop.c : + void t_buffertop_clear(); + void t_buffertop_size(); + void t_buffertop_push_pop_basic(); + void t_buffertop_push_pop_evolved(); + void t_buffertop_copy(); //file ./t.Tree.c : - t_tree_clear(); - t_tree_size(); - t_tree_add_remove(); - t_tree_iterate(); - t_tree_copy(); + void t_tree_clear(); + void t_tree_size(); + void t_tree_add_remove(); + void t_tree_iterate(); + void t_tree_copy(); - //file ./t.PriorityQueue.c : - t_priorityqueue_clear(); - t_priorityqueue_size(); - t_priorityqueue_push_pop_basic(); - t_priorityqueue_push_pop_evolved(); - t_priorityqueue_copy(); + //file ./t.Heap.c : + void t_heap_clear(); + void t_heap_size(); + void t_heap_push_pop_basic(); + void t_heap_push_pop_evolved(); + void t_heap_copy(); //file ./t.List.c : - t_list_clear(); - t_list_size(); - t_list_push_pop_basic(); - t_list_push_pop_evolved(); - t_list_copy(); - - //file ./t.Vector.c : - t_vector_clear(); - t_vector_size(); - t_vector_push_pop_basic(); - t_vector_push_pop_evolved(); - t_vector_copy(); + void t_list_clear(); + void t_list_size(); + void t_list_push_pop_basic(); + void t_list_push_pop_evolved(); + void t_list_copy(); //file ./t.Queue.c : - t_queue_clear(); - t_queue_size(); - t_queue_push_pop_basic(); - t_queue_push_pop_evolved(); - t_queue_copy(); + void t_queue_clear(); + void t_queue_size(); + void t_queue_push_pop_basic(); + void t_queue_push_pop_evolved(); + void t_queue_copy(); - //file ./t.BufferTop.c : - t_buffertop_clear(); - t_buffertop_size(); - t_buffertop_push_pop_basic(); - t_buffertop_push_pop_evolved(); - t_buffertop_copy(); + //file ./t.Stack.c : + void t_stack_clear(); + void t_stack_size(); + void t_stack_push_pop_basic(); + void t_stack_push_pop_evolved(); + void t_stack_copy(); + + //file ./t.PriorityQueue.c : + void t_priorityqueue_clear(); + void t_priorityqueue_size(); + void t_priorityqueue_push_pop_basic(); + void t_priorityqueue_push_pop_evolved(); + void t_priorityqueue_copy(); return 0; } diff --git a/test/makeMain.sh b/test/makeMain.sh index 9583704..19bb852 100644 --- a/test/makeMain.sh +++ b/test/makeMain.sh @@ -10,11 +10,11 @@ printf '{\n' >> main.c #add functions for file in `find . -type f -name \*.c ! -name main.c`; do printf "\t//file $file :\n" >> main.c - functions=`grep "//FTEST" $file | sed 's/void \(.\+\) \/\/FTEST/\t\1;/g'` + functions=`grep '^ *void t_' $file | sed 's/^ *\(void [^(]*\).*/\t\1();/g'` printf "$functions" >> main.c - printf "\n\n" >> main.c + printf '\n\n' >> main.c done #finalize main.c -printf "\treturn 0;\n" >> main.c +printf '\treturn 0;\n' >> main.c printf '}\n' >> main.c diff --git a/test/t.Queue.c b/test/t.Queue.c index 0b368f1..cb86b9d 100644 --- a/test/t.Queue.c +++ b/test/t.Queue.c @@ -43,7 +43,8 @@ void t_queue_push_pop_basic() //FTEST int n = 10; Queue* q = queue_new(double); - for (int i = 0; i < n; i++) queue_push(q, (double) i); + for (int i = 0; i < n; i++) + queue_push(q, (double) i); // iterate and check values double ckValue = 0.0; while (!queue_empty(q)) diff --git a/test/t.Stack.c b/test/t.Stack.c index db5a0d5..bb879f9 100644 --- a/test/t.Stack.c +++ b/test/t.Stack.c @@ -44,7 +44,8 @@ void t_stack_push_pop_basic() //FTEST int n = 10; Stack* s = stack_new(double); - for (int i = 0; i < n; i++) stack_push(s, (double) i); + for (int i = 0; i < n; i++) + stack_push(s, (double) i); // iterate and check values double ckValue = n - 1; while (!stack_empty(s)) -- 2.44.0