SHARE
TWEET

debug.c

a guest Oct 31st, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "cvector.h"
  4.  
  5. /*
  6.  * Example program for debugging cvector,
  7.  * this program will check for errors in
  8.  * 'random' data that will be moved and
  9.  * copied various times etc: if errors are
  10.  * found, there is work to be done!
  11.  *
  12.  */
  13.  
  14. void err_exit(char *error) {
  15.  printf("\n"
  16.         "error: %s\n");
  17.  exit(1);
  18. }
  19.  
  20. int main(int argc, char *argv[]) {
  21.  struct t_cvector v, *pv;
  22.  int i, n, value, *pvalue;
  23.  
  24.  // DEBUG cvector_alloc(...)
  25.  // ALLOC EXAMPLE 1 (STATIC DATA - NOTE THE 3 (THREE) PARAMETERS)
  26.  cvector_alloc(&v, sizeof(int), 4);
  27.  
  28.  if(!v.capacity)
  29.   err_exit((char*)"cvector_alloc failed (allocating data and iterators).");
  30.  
  31.  // ALLOC EXAMPLE 2 (DYNAMICALLY ALLOCATED DATA - NOTE THE ZERO ON 3RD PARAMETER)
  32.  if(!(pv = cvector_alloc(0, sizeof(int), 32)))
  33.   err_exit((char*)"cvector_alloc failed (allocating vector).");
  34.  
  35.  if(!pv->capacity) {
  36.   cvector_free(pv);
  37.   free(pv);
  38.   err_exit((char*)"cvector_alloc failed (allocating data and iterators).");
  39.  }
  40.  
  41.  // DEBUG cvector_push(...)
  42.  n = v.capacity + 2;
  43.  
  44.  for(i=0;i<n;i++) {
  45.   value = 1 + (i * 1);
  46.   printf("vector_push(...) value %d", value);
  47.   cvector_push_back(&v, &value);
  48.   printf("\t(element:capacity\t%d:%d)\n", v.elements - 1, v.capacity);
  49.  }
  50.  
  51.  // DEBUG cvector_erase(...)
  52.  cvector_erase(&v, 2);
  53.  
  54.  // DEBUG cvector_at(...) ***CHECK IF ERASE WAS SUCCESSFULL!***
  55.  printf("cvector_erase(&v, 2); -> check if erase was successfull\n");
  56.  
  57.  for(i=0;i<v.elements;i++) {
  58.   pvalue = (int*)cvector_at(&v, i);
  59.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
  60.  }
  61.  
  62.  // DEBUG cvector_swap(...) ***BEFORE*** cvector_resize(...)
  63.  cvector_swap(&v, 0, 1);
  64.  printf("cvector_swap(&v, 0, 1)\n");
  65.  
  66.  for(i=0;i<v.elements;i++) {
  67.   pvalue = (int*)cvector_at(&v, i);
  68.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
  69.  }
  70.  
  71.  // DEBUG cvector_resize(...)
  72.  value = 777;
  73.  
  74.  printf("cvector_resize(v, elements + 2)\t(elements:capacity:data\t%d:%d:%d)\n", v.elements, v.capacity, value);
  75.  cvector_resize(&v, v.elements + 2, &value);
  76.  printf("result:\t\t\t\t(elements:capacity:data\t%d:%d:%d)\n", v.elements, v.capacity, value);
  77.  
  78.  // DEBUG cvector_at(...) ***CHECK IF THE SWAP BEFORE THE RESIZE IS PERSISTING!***
  79.  for(i=0;i<v.elements;i++) {
  80.   pvalue = (int*)cvector_at(&v, i);
  81.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
  82.  }
  83.  
  84.  // DEBUG cvector_erase(...)
  85.  cvector_erase(&v, 1);
  86.  
  87.  // DEBUG cvector_at(...) ***CHECK IF ERASE WAS SUCCESSFULL!***
  88.  printf("cvector_erase(&v, 1); -> check if erase was successfull\n");
  89.  
  90.  for(i=0;i<v.elements;i++) {
  91.   pvalue = (int*)cvector_at(&v, i);
  92.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
  93.  }
  94.  
  95.  // DEBUG cvector_insert(...)
  96.  value = 666;
  97.  cvector_insert(&v, 1, &value);
  98.  
  99.  // DEBUG cvector_at(...) ***CHECK IF INSERT WAS SUCCESSFULL!***
  100.  printf("cvector_insert(&v, 1, 666); -> check if insert was successfull\n");
  101.  
  102.  for(i=0;i<v.elements;i++) {
  103.   pvalue = (int*)cvector_at(&v, i);
  104.   value =  (int)*pvalue;
  105.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, value);
  106.  }
  107.  
  108.  /*
  109.  // DEBUG cvector_assign(...)
  110.  cvector_assign(pv, &v);
  111.  //*pv = v;
  112.  
  113.  // DEBUG cvector_at(...) ***CHECK IF ASSIGN WAS SUCCESSFULL***
  114.  printf("cvector_assign(pv, &v); -> check if assign was successfull\n");
  115.  
  116.  for(i=0;i<pv->elements;i++) {
  117.   pvalue = (int*)cvector_at(pv, i);
  118.   printf("vector_at(pv, %d) element %d\t(address:value\t\t%08x:%d)\n", i, i, pvalue, value);
  119.  }
  120.  //*/
  121.  
  122.  // DEBUG cvector_clear(...)
  123.  // PRINT DESCRIPTION OF LAST MODIFICATIONS SO WE CAN COMPARE TO RESULTING
  124.  // LIST (UNTIL CAPACITY REACHED)
  125.  cvector_clear(&v);
  126.  printf("cvector_clear() -> then list data until capacity reached:\n");
  127.  
  128.  // DEBUG cvector_at(...)
  129.  for(i=0;i<v.capacity;i++) {
  130.   pvalue = (int*)cvector_at(&v, i);
  131.   printf("vector_at(...) element %d\t(address:value\t\t%08x:%d)\n", i, pvalue, *pvalue);
  132.  }
  133.  
  134.  // free data and iterators in the static vector stucture
  135.  cvector_free(&v);
  136.  
  137.  // first free data and iterators in the dynamically allocated vector structure,
  138.  // then free the dynamically allocated vector structure itself
  139.  cvector_free(pv);
  140.  free(pv);
  141.  
  142.  return 0;
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top