[gegl/paint-system: 1/7] Add tests for GeglPath



commit 3abecf07cd33b97275b14a5fbd997795e07a5646
Author: Damien de Lemeny <d delemeny gmail com>
Date:   Mon May 31 15:09:01 2010 +0200

    Add tests for GeglPath
    
    * test gegl_path_length
    * test gegl_path_calc forward & backward
    * test gegl_path_calc_values

 tests/Makefile.am |    1 +
 tests/test-path.c |  184 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 185 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c801ca0..41e0100 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -10,6 +10,7 @@ TESTS = \
 	test-color-op			\
 	test-gegl-rectangle		\
 	test-misc			\
+	test-path		\
 	test-proxynop-processing
 noinst_PROGRAMS = $(TESTS)
 
diff --git a/tests/test-path.c b/tests/test-path.c
new file mode 100644
index 0000000..3cc1d1e
--- /dev/null
+++ b/tests/test-path.c
@@ -0,0 +1,184 @@
+#include <stdio.h>
+#include <math.h>
+#include <glib.h>
+
+#include "gegl.h"
+#include "property-types/gegl-path.h"
+
+#define SUCCESS  0
+#define FAILURE -1
+
+#define EPSILON 0.00001
+
+#define NSMP 3
+static gboolean 
+equals(double a, double b)
+{
+  return fabs(a-b) < EPSILON;
+}
+static void
+distribute(double length, int num_samples, gdouble *result)
+{
+  int i=0;
+  gdouble spacing = 0;
+  if (num_samples>1)
+    spacing = length/(num_samples-1);
+  for (i=0; i<num_samples-1; i++)
+    result[i]=spacing*i;
+  if (num_samples>1)
+    result[num_samples-1]= length;
+}
+
+static int 
+test_path_get_length (GeglPath *path, gdouble exp_length)
+{  
+  gdouble length;
+  length = gegl_path_get_length(path);
+
+  if (! equals(length, exp_length))
+    {
+      g_printerr("test_path_get_length()\n");
+      g_printerr("The length of path is incorrect.\n");
+      g_printerr("length is %f, should be %f\n", length, exp_length);
+      return FALSE;
+    }
+  return TRUE;
+}
+
+static int 
+test_path_calc_values (GeglPath *path, int num_samples,
+                       gdouble *exp_x, gdouble *exp_y)
+{
+  int i=0;
+  gdouble x[num_samples], y[num_samples];
+  gdouble length;
+  /* gegl_path_calc_values:
+   * Compute @num_samples for a path into the provided arrays @xs and @ys
+   * the returned values include the start and end positions of the path.
+   */
+  length = gegl_path_get_length(path);
+
+  gegl_path_calc_values(path,num_samples,x,y);
+
+  for (i=0; i<num_samples; i++)
+    if (! (equals(x[i],exp_x[i]) && equals(y[i],exp_y[i])) )
+    {
+      g_printerr("test_path_calc_values()\n");
+      g_printerr("Sample %d is incorrect.\n",i);
+      for ( i=0;i<NSMP;i++)
+        printf("Sample %d : x=%f exp_x=%f  y=%f exp_y=%f\n",
+                i, x[i], exp_x[i], y[i], exp_y[i]);
+      return FALSE;
+    }
+  return TRUE;
+}
+
+static int 
+test_path_calc (GeglPath *path, gdouble pos,
+                gdouble exp_x, gdouble exp_y)
+{
+  int i=0;
+  gdouble x=0.0, y=0.0;
+  /* gegl_path_calc_values:
+   * Compute @num_samples for a path into the provided arrays @xs and @ys
+   * the returned values include the start and end positions of the path.
+   */
+  gegl_path_calc(path,pos,&x,&y);
+
+  if (! (equals(x,exp_x) && equals(y,exp_y)) )
+    {
+      g_printerr("test_path_calc()\n");
+      g_printerr("Calculated point is incorrect.\n",i);
+      printf("x=%f exp_x=%f  y=%f exp_y=%f\n",
+              x, exp_x, y, exp_y);
+      return FALSE;
+    }
+  return TRUE;
+}
+int main(int argc, char *argv[])
+{
+  int result=SUCCESS;
+  int i=1, j=1;
+
+  g_type_init();
+  gegl_init (&argc, &argv);
+  GeglPath *path = NULL;
+  gdouble exp_x[NSMP],exp_y[NSMP];
+
+  distribute (0.0, NSMP ,exp_y);
+  distribute (1.0, NSMP ,exp_x);
+
+  path = gegl_path_new ();
+  gegl_path_append (path, 'M', 0.0, 0.0);
+  gegl_path_append (path, 'L', 0.5, 0.0);
+  gegl_path_append (path, 'L', 1.0, 0.0);
+  if(! test_path_get_length(path, 1.0) )
+    {
+      g_printerr("The gegl_path_get_length() test #%d failed.\n",i);
+      result += FAILURE;
+    }
+  /* path_calc forwards */
+  for ( j=0;j<NSMP;j++)
+    if(! test_path_calc(path, exp_x[j], exp_x[j], exp_y[j]) )
+      {
+        g_printerr("The gegl_path_calc() test #%d.%d failed.\n",i,j+1);
+        result += FAILURE;
+      }
+  /* path_calc backwards */
+  for ( j=NSMP-1;j>-1;j--)
+    if(! test_path_calc(path, exp_x[j], exp_x[j], exp_y[j]) )
+      {
+        g_printerr("The gegl_path_calc() reverse test #%d.%d failed.\n",i,j+1);
+        result += FAILURE;
+      }
+  if(! test_path_calc_values(path, NSMP, exp_x, exp_y) )
+    {
+      g_printerr("The gegl_path_calc_values() test #%d failed.\n",i);
+      result += FAILURE;
+    }
+
+  i++;
+
+  path = gegl_path_new ();
+  gegl_path_append (path, 'M', 0.0, 0.0);
+  gegl_path_append (path, 'L', 0.5, 0.0);
+  gegl_path_append (path, 'M', 0.5, 0.0);
+  gegl_path_append (path, 'L', 1.0, 0.0);
+  if(! test_path_get_length(path, 1.0) )
+    {
+      g_printerr("The gegl_path_get_length() test #%d failed.\n",i);
+      result += FAILURE;
+    }
+  /* path_calc forwards */
+  for ( j=0;j<NSMP;j++)
+    if(! test_path_calc(path, exp_x[j], exp_x[j], exp_y[j]) )
+      {
+        g_printerr("The gegl_path_calc() test #%d.%d failed.\n",i,j+1);
+        result += FAILURE;
+      }
+  /* path_calc backwards */
+  for ( j=NSMP-1;j>-1;j--)
+    if(! test_path_calc(path, exp_x[j], exp_x[j], exp_y[j]) )
+      {
+        g_printerr("The gegl_path_calc() reverse test #%d.%d failed.\n",i,j+1);
+        result += FAILURE;
+      }
+  if(! test_path_calc_values(path, NSMP, exp_x, exp_y) )
+    {
+      g_printerr("The gegl_path_calc_values() test #%d failed.\n",i);
+      result += FAILURE;
+    }
+
+  /* path1   :    |--+--+--|--+--+--|
+   * path2   :    |--+--+--|
+   *                       |--+--+--|
+   * 1sampl  :    ^ ?
+   * 2sampl  :    ^                 ^
+   * 3sampl  :    ^        ^        ^
+   * 4sampl  :    ^     ^     ^     ^
+   */
+
+  gegl_exit ();
+
+  return result;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]