datasheet-test: Add test for datasheet_resize_column().
[pspp-builds.git] / tests / data / datasheet-test.c
index 5eeb355346cf98fb82ee227d5925cf3a2f420a71..a3fdb608d09ea7422bc9769b059badd609d64e2a 100644 (file)
@@ -70,7 +70,7 @@ struct datasheet_test_params
     int n_widths;
 
     /* State. */
-    int next_value;
+    unsigned int next_value;
   };
 
 static bool
@@ -258,11 +258,16 @@ check_datasheet (struct mc *mc, struct datasheet *ds,
               ds_put_format (&s, "row %zu:", row);
               for (col = 0; col < n_columns; col++)
                 {
+                  int width = caseproto_get_width (proto, col);
                   union value v;
-                  value_init (&v, 0);
+                  value_init (&v, width);
                   if (!datasheet_get_value (ds, row, col, &v))
                     NOT_REACHED ();
-                  ds_put_format (&s, " %g", v.f);
+                  if (width == 0)
+                    ds_put_format (&s, " %g", v.f);
+                  else
+                    ds_put_format (&s, " '%.*s'",
+                                   width, value_str (&v, width));
                 }
               mc_error (mc, "%s", ds_cstr (&s));
             }
@@ -449,10 +454,10 @@ datasheet_mc_init (struct mc *mc)
 }
 
 static void
-value_from_param (union value *value, int width, int idx)
+value_from_param (union value *value, int width, unsigned int idx)
 {
   if (width == 0)
-    value->f = idx;
+    value->f = idx & 0xffff;
   else
     {
       unsigned int hash = hash_int (idx, 0);
@@ -465,6 +470,21 @@ value_from_param (union value *value, int width, int idx)
     }
 }
 
+struct resize_cb_aux
+  {
+    int old_width;
+    int new_width;
+  };
+
+static void
+resize_cb (const union value *old_value, union value *new_value, void *aux_)
+{
+  struct resize_cb_aux *aux = aux_;
+
+  value_from_param (new_value, aux->new_width,
+                    value_hash (old_value, aux->old_width, 0));
+}
+
 /* "mutate" function for struct mc_class. */
 static void
 datasheet_mc_mutate (struct mc *mc, const void *ods_)
@@ -519,6 +539,46 @@ datasheet_mc_mutate (struct mc *mc, const void *ods_)
             caseproto_unref (proto);
           }
 
+  /* Resize each column to each possible new size. */
+  for (pos = 0; pos < n_columns; pos++)
+    for (width_idx = 0; width_idx < params->n_widths; width_idx++)
+      {
+        int owidth = caseproto_get_width (oproto, pos);
+        int width = params->widths[width_idx];
+        if (mc_include_state (mc))
+          {
+            struct resize_cb_aux aux;
+            struct caseproto *proto;
+            struct datasheet *ds;
+            size_t i;
+
+            mc_name_operation (mc, "resize column %zu (of %zu) "
+                               "from width %d to %d",
+                               pos, n_columns, owidth, width);
+            clone_model (ods, odata, &ds, data);
+
+            aux.old_width = owidth;
+            aux.new_width = width;
+            if (!datasheet_resize_column (ds, pos, width, resize_cb, &aux))
+              NOT_REACHED ();
+            proto = caseproto_set_width (caseproto_ref (oproto), pos, width);
+
+            for (i = 0; i < n_rows; i++)
+              {
+                union value *old_value = &data[i][pos];
+                union value new_value;
+                value_init (&new_value, width);
+                resize_cb (old_value, &new_value, &aux);
+                value_swap (old_value, &new_value);
+                value_destroy (&new_value, owidth);
+              }
+
+            check_datasheet (mc, ds, data, n_rows, proto);
+            release_data (n_rows, proto, data);
+            caseproto_unref (proto);
+          }
+      }
+
   /* Delete all possible numbers of columns from all possible
      positions. */
   for (pos = 0; pos < n_columns; pos++)