Re: Refactoring path_calc_values

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

 



Patch to add tests for GeglPath, current implementation of gegl_path_calc_values fails.

    Damien
From e7e24fb50e01fecf0c02e0cc3439833a16d08c77 Mon Sep 17 00:00:00 2001
From: Damien de Lemeny <d.delemeny@xxxxxxxxx>
Date: Mon, 31 May 2010 15:09:01 +0200
Subject: [PATCH] Add test case for GeglPath

Refactor test-path
---
 tests/Makefile.am |    1 +
 tests/test-path.c |  134 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 0 deletions(-)
 create mode 100644 tests/test-path.c

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..ae50142
--- /dev/null
+++ b/tests/test-path.c
@@ -0,0 +1,134 @@
+#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;
+}
+
+int main(int argc, char *argv[])
+{
+  int result=SUCCESS;
+  int i=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;
+    }
+  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;
+    }
+  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;
+}
-- 
1.6.3.3

_______________________________________________
Gegl-developer mailing list
Gegl-developer@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

[Index of Archives]     [Yosemite News]     [Yosemite Photos]     [gtk]     [GIMP Users]     [KDE]     [Gimp's Home]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux