5 #include "config_test.h"
6 #include <JM/jm_vector.h>
7 #include <JM/jm_stack.h>
9 void print_int(int i,void* data) {
13 void print_dbl(double d,void* data) {
17 int compar_int(int* a, int* b) {
21 int return_code = CTEST_RETURN_SUCCESS;
23 void log_error(const char* fmt, ...) {
28 return_code = CTEST_RETURN_FAIL;
35 jm_vector(int) stackv;
38 jm_vector(int)* v = &stackv;
40 s = jm_stack_alloc(double)(100, 0 );
42 jm_vector_init(int)(v,VINIT_SIZE,0);
43 jm_vector_zero(int)(v);
44 jm_vector_set_item(int)(v, 2, TESTVAL);
45 for( i = 0; i < 32; i++) {
48 jm_vector_push_back(int)(v,x);
49 jm_stack_push(double)(s,x);
50 top = (int)jm_stack_top(double)(s);
51 printf("pushed item %d=%d (stack top %g), vector size: %u, capacity: %u\n", i, x, jm_stack_top(double)(s),
52 (unsigned)jm_vector_get_size(int)(v), (unsigned)jm_vector_reserve(int)(v,0));
53 if(top != x) log_error("Stack top does not match the pushed value \n");
54 if(jm_vector_get_size(int)(v) != VINIT_SIZE+i+1) log_error("Vector size %d is not as expected %d\n", jm_vector_get_size(int)(v), VINIT_SIZE+i+1);
57 size_t index = jm_vector_find_index(int)(v, &k,jm_compare_int);
59 if( index != 2) log_error("Index of '%d' should be '2' but got %d\n", TESTVAL, k );
61 for( i = 0; i < 22; i++) {
62 jm_stack_pop(double)(s);
64 jm_vector_resize_int(v,10);
65 printf("Printing the vector \n");
66 jm_vector_foreach_c(int)(v,print_int,0);
67 jm_vector_qsort(int)(v, jm_compare_int);
68 printf("Printing sorted vector \n");
69 jm_vector_foreach_c(int)(v,print_int,0);
70 printf("Index of %d is %u\n", k, (unsigned)jm_vector_bsearch_index(int)(v, &k, jm_compare_int));
71 printf("Printing the stack \n");
72 jm_stack_foreach(double)(s,print_dbl,0);
74 jm_vector_free_data(int)(v);
75 jm_stack_free(double)(s);