Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "cvector.h"
- /*
- * Example program for debugging cvector,
- * this program will check for errors in
- * 'random' data that will be moved and
- * copied various times etc: if errors are
- * found, there is work to be done!
- *
- */
- void err_exit(char *error) {
- printf("\n"
- "error: %s\n");
- exit(1);
- }
- int main(int argc, char *argv[]) {
- struct t_cvector v, *pv;
- int i, n, value, *pvalue;
- // DEBUG cvector_alloc(...)
- // ALLOC EXAMPLE 1 (STATIC DATA - NOTE THE 3 (THREE) PARAMETERS)
- cvector_alloc(&v, sizeof(int), 4);
- if(!v.capacity)
- err_exit((char*)"cvector_alloc failed (allocating data and iterators).");
- // ALLOC EXAMPLE 2 (DYNAMICALLY ALLOCATED DATA - NOTE THE ZERO ON 3RD PARAMETER)
- if(!(pv = cvector_alloc(0, sizeof(int), 32)))
- err_exit((char*)"cvector_alloc failed (allocating vector).");
- if(!pv->capacity) {
- cvector_free(pv);
- free(pv);
- err_exit((char*)"cvector_alloc failed (allocating data and iterators).");
- }
- // DEBUG cvector_push(...)
- n = v.capacity + 2;
- for(i=0;i<n;i++) {
- value = 1 + (i * 1);
- printf("vector_push(...) value %d", value);
- cvector_push_back(&v, &value);
- printf("\t(element:capacity\t%d:%d)\n", v.elements - 1, v.capacity);
- }
- // DEBUG cvector_erase(...)
- cvector_erase(&v, 2);
- // DEBUG cvector_at(...) ***CHECK IF ERASE WAS SUCCESSFULL!***
- printf("cvector_erase(&v, 2); -> check if erase was successfull\n");
- for(i=0;i<v.elements;i++) {
- pvalue = (int*)cvector_at(&v, i);
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
- }
- // DEBUG cvector_swap(...) ***BEFORE*** cvector_resize(...)
- cvector_swap(&v, 0, 1);
- printf("cvector_swap(&v, 0, 1)\n");
- for(i=0;i<v.elements;i++) {
- pvalue = (int*)cvector_at(&v, i);
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
- }
- // DEBUG cvector_resize(...)
- value = 777;
- printf("cvector_resize(v, elements + 2)\t(elements:capacity:data\t%d:%d:%d)\n", v.elements, v.capacity, value);
- cvector_resize(&v, v.elements + 2, &value);
- printf("result:\t\t\t\t(elements:capacity:data\t%d:%d:%d)\n", v.elements, v.capacity, value);
- // DEBUG cvector_at(...) ***CHECK IF THE SWAP BEFORE THE RESIZE IS PERSISTING!***
- for(i=0;i<v.elements;i++) {
- pvalue = (int*)cvector_at(&v, i);
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
- }
- // DEBUG cvector_erase(...)
- cvector_erase(&v, 1);
- // DEBUG cvector_at(...) ***CHECK IF ERASE WAS SUCCESSFULL!***
- printf("cvector_erase(&v, 1); -> check if erase was successfull\n");
- for(i=0;i<v.elements;i++) {
- pvalue = (int*)cvector_at(&v, i);
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
- }
- // DEBUG cvector_insert(...)
- value = 666;
- cvector_insert(&v, 1, &value);
- // DEBUG cvector_at(...) ***CHECK IF INSERT WAS SUCCESSFULL!***
- printf("cvector_insert(&v, 1, 666); -> check if insert was successfull\n");
- for(i=0;i<v.elements;i++) {
- pvalue = (int*)cvector_at(&v, i);
- value = (int)*pvalue;
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, value);
- }
- /*
- // DEBUG cvector_assign(...)
- cvector_assign(pv, &v);
- //*pv = v;
- // DEBUG cvector_at(...) ***CHECK IF ASSIGN WAS SUCCESSFULL***
- printf("cvector_assign(pv, &v); -> check if assign was successfull\n");
- for(i=0;i<pv->elements;i++) {
- pvalue = (int*)cvector_at(pv, i);
- printf("vector_at(pv, %d) element %d\t(address:value\t\t%08x:%d)\n", i, i, pvalue, value);
- }
- //*/
- // DEBUG cvector_clear(...)
- // PRINT DESCRIPTION OF LAST MODIFICATIONS SO WE CAN COMPARE TO RESULTING
- // LIST (UNTIL CAPACITY REACHED)
- cvector_clear(&v);
- printf("cvector_clear() -> then list data until capacity reached:\n");
- // DEBUG cvector_at(...)
- for(i=0;i<v.capacity;i++) {
- pvalue = (int*)cvector_at(&v, i);
- printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
- }
- // free data and iterators in the static vector stucture
- cvector_free(&v);
- // first free data and iterators in the dynamically allocated vector structure,
- // then free the dynamically allocated vector structure itself
- cvector_free(pv);
- free(pv);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement