fix some tests
[calc.git] / tabular.c
index 0252a8e8ec1bf82aad5e3f6a723db40644fe35de..8100f048037643dfe531e770514dd2e699e8eaac 100644 (file)
--- a/tabular.c
+++ b/tabular.c
@@ -33,7 +33,9 @@ tab_t *resize_tab (tab_t *tab, int nb)
         tmp = (double *) callocordie (nb, sizeof (double));
         memcpy (tmp, tab->data, ((tab->size < nb) ? tab->size : nb) * sizeof (double));
     }
-    free (tab->data);
+    if (tab->data){
+        free (tab->data);
+    }
     tab->data = tmp;
     tab->size = nb;
     return tab;
@@ -43,6 +45,9 @@ tab_t *resize_tab (tab_t *tab, int nb)
 
 tab_t *copy_tab (tab_t *tab)
 {
+    if (tab == NULL) {
+        return NULL;
+    }
     tab_t *new = alloc_tab (tab->size);
     memcpy (new->data, tab->data, tab->size * sizeof (double));
     return new;
@@ -131,11 +136,13 @@ double pop_tab (tab_t *tab, int id)
     if ((!tab) || (((id < 1) || (id > tab->size)) && (id != -1))) {
         VERBOSE (WARNING, fprintf (stdout, "error out of bounds (%d/%d)\n", id, (tab) ? tab->size : 0));
     } else {
-        ret = tab->data[id - 1];
 
         /* special case for inserting an element at the end */
         id = (id == -1) ? tab->size : id;
 
+        /* read data */
+        ret = tab->data[id - 1];
+
         /* create larger tab */
         double *tmp = (double *) callocordie (tab->size - 1, sizeof (double));
         memcpy (tmp, tab->data, (id - 1) * sizeof (double));
@@ -154,8 +161,8 @@ double pop_tab (tab_t *tab, int id)
 void order_tab (tab_t *tab)
 {
     int i, j;
-    if ((!tab) || (tab->size < 3)) {
-        VERBOSE (WARNING, fprintf (stdout, "error not enough element in stack (%d)\n", tab->size));
+    if ((!tab) || (tab->size < 2)) {
+        VERBOSE (WARNING, fprintf (stdout, "error not enough element in stack (%d)\n", (tab) ? tab->size : 0));
         return;
     }