[gtk/wip/otte/lottie: 293/339] testsuite: Update create_random_path()




commit 6d8adffae5538ff2ebeab48d7f4f26f7abbd5515
Author: Benjamin Otte <otte redhat com>
Date:   Fri Nov 27 04:46:19 2020 +0100

    testsuite: Update create_random_path()
    
    1. Allow specifying the max number of contours
    2. Be smarter about creating the paths:
       With 10% chance, create a "weird" path like the empty one or only
       points or things like that.
       Otherwise create a bunch of contours, with 2/3 a standard contour,
       with 1/3 a predetermined one.

 testsuite/gsk/path.c | 240 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 203 insertions(+), 37 deletions(-)
---
diff --git a/testsuite/gsk/path.c b/testsuite/gsk/path.c
index ceed9ca31f..1c98db2640 100644
--- a/testsuite/gsk/path.c
+++ b/testsuite/gsk/path.c
@@ -20,41 +20,188 @@
 #include <gtk/gtk.h>
 
 static GskPath *
-create_random_path (void)
+create_random_degenerate_path (guint max_contours)
 {
+#define N_DEGENERATE_PATHS 12
   GskPathBuilder *builder;
-  guint i, n;
+  guint i;
 
   builder = gsk_path_builder_new ();
-  n = g_test_rand_int_range (0, 20);
 
-  for (i = 0; i < n; i++)
-    {
-      switch (g_test_rand_int_range (0, 11))
-      {
-        case 0:
-          gsk_path_builder_move_to (builder,
+  switch (g_test_rand_int_range (0, N_DEGENERATE_PATHS))
+  {
+    case 0:
+      /* empty path */
+      break;
+
+    case 1:
+      /* a single point */
+      gsk_path_builder_move_to (builder, 
+                                g_test_rand_double_range (-1000, 1000),
+                                g_test_rand_double_range (-1000, 1000));
+      break;
+
+    case 2:
+      /* N points */
+      for (i = 0; i < MIN (10, max_contours); i++)
+        {
+          gsk_path_builder_move_to (builder, 
                                     g_test_rand_double_range (-1000, 1000),
                                     g_test_rand_double_range (-1000, 1000));
-          break;
-
-        case 1:
+        }
+      break;
+
+    case 3:
+      /* 1 closed point */
+      gsk_path_builder_move_to (builder, 
+                                g_test_rand_double_range (-1000, 1000),
+                                g_test_rand_double_range (-1000, 1000));
+      gsk_path_builder_close (builder);
+      break;
+
+    case 4:
+      /* the same point closed N times */
+      gsk_path_builder_move_to (builder, 
+                                g_test_rand_double_range (-1000, 1000),
+                                g_test_rand_double_range (-1000, 1000));
+      for (i = 0; i < MIN (10, max_contours); i++)
+        {
           gsk_path_builder_close (builder);
-          break;
+        }
+      break;
+
+    case 5:
+      /* a zero-width and zero-height rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      0, 0));
+      break;
+
+    case 6:
+      /* a zero-width rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      0,
+                                                      g_test_rand_double_range (-1000, 1000)));
+      break;
+
+    case 7:
+      /* a zero-height rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      0));
+      break;
+
+    case 8:
+      /* a negative-size rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 0),
+                                                      g_test_rand_double_range (-1000, 0)));
+      break;
+
+    case 9:
+      /* an absolutely random rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000)));
+      break;
+
+    case 10:
+      /* an absolutely random rect */
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000)));
+      break;
+
+    case 11:
+      /* an absolutely random circle */
+      gsk_path_builder_add_circle (builder,
+                                   &GRAPHENE_POINT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                         g_test_rand_double_range (-1000, 1000)),
+                                   g_test_rand_double_range (1, 1000));
+      break;
+
+    case N_DEGENERATE_PATHS:
+    default:
+      g_assert_not_reached ();
+  }
+
+  return gsk_path_builder_free_to_path (builder);
+}
+
+static GskPath *
+create_random_path (guint max_contours);
+
+static void
+add_shape_contour (GskPathBuilder *builder)
+{
+#define N_SHAPE_CONTOURS 3
+  switch (g_test_rand_int_range (0, N_SHAPE_CONTOURS))
+  {
+    case 0:
+      gsk_path_builder_add_rect (builder,
+                                 &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (-1000, 1000),
+                                                      g_test_rand_double_range (1, 1000),
+                                                      g_test_rand_double_range (1, 1000)));
+      break;
+
+    case 1:
+      gsk_path_builder_add_circle (builder,
+                                   &GRAPHENE_POINT_INIT (g_test_rand_double_range (-1000, 1000),
+                                                         g_test_rand_double_range (-1000, 1000)),
+                                   g_test_rand_double_range (1, 1000));
+      break;
+
+    case 2:
+      {
+        GskPath *path = create_random_path (1);
+        gsk_path_builder_add_path (builder, path);
+        gsk_path_unref (path);
+      }
+      break;
+
+    case N_SHAPE_CONTOURS:
+    default:
+      g_assert_not_reached ();
+      break;
+  }
+}
 
-        case 2:
-        case 3:
-        case 4:
-        case 5:
+static void
+add_standard_contour (GskPathBuilder *builder)
+{
+  guint i, n;
+
+  if (g_test_rand_bit ())
+    gsk_path_builder_move_to (builder,
+                              g_test_rand_double_range (-1000, 1000),
+                              g_test_rand_double_range (-1000, 1000));
+
+  /* that 20 is random, but should be enough to get some
+   * crazy self-intersecting shapes */
+  n = g_test_rand_int_range (1, 20);
+  for (i = 0; i < n; i++)
+    {
+      switch (g_test_rand_int_range (0, 2))
+      {
+        case 0:
           gsk_path_builder_line_to (builder,
                                     g_test_rand_double_range (-1000, 1000),
                                     g_test_rand_double_range (-1000, 1000));
           break;
 
-        case 6:
-        case 7:
-        case 8:
-        case 9:
+        case 1:
           gsk_path_builder_curve_to (builder,
                                      g_test_rand_double_range (-1000, 1000),
                                      g_test_rand_double_range (-1000, 1000),
@@ -64,20 +211,39 @@ create_random_path (void)
                                      g_test_rand_double_range (-1000, 1000));
           break;
 
-        case 10:
-          gsk_path_builder_add_rect (builder,
-                                     &GRAPHENE_RECT_INIT (g_test_rand_double_range (-1000, 1000),
-                                                          g_test_rand_double_range (-1000, 1000),
-                                                          g_test_rand_double_range (-1000, 1000),
-                                                          g_test_rand_double_range (-1000, 1000)));
-          break;
-
         default:
           g_assert_not_reached();
           break;
       }
     }
 
+  if (g_test_rand_bit ())
+    gsk_path_builder_close (builder);
+}
+
+static GskPath *
+create_random_path (guint max_contours)
+{
+  GskPathBuilder *builder;
+  guint i, n;
+
+  /* 5% chance for a weird shape */
+  if (g_test_rand_int_range (0, 20))
+    return create_random_degenerate_path (max_contours);
+
+  builder = gsk_path_builder_new ();
+  n = g_test_rand_int_range (1, 10);
+  n = MIN (n, max_contours);
+
+  for (i = 0; i < n; i++)
+    {
+      /* 2/3 of shapes are standard contours */
+      if (g_test_rand_int_range (0, 3))
+        add_standard_contour (builder);
+      else
+        add_shape_contour (builder);
+    }
+
   return gsk_path_builder_free_to_path (builder);
 }
 
@@ -93,9 +259,9 @@ test_create (void)
   for (i = 0; i < 1000; i++)
     {
       builder = gsk_path_builder_new ();
-      path1 = create_random_path ();
+      path1 = create_random_path (G_MAXUINT);
       gsk_path_builder_add_path (builder, path1);
-      path2 = create_random_path ();
+      path2 = create_random_path (G_MAXUINT);
       gsk_path_builder_add_path (builder, path2);
       built = gsk_path_builder_free_to_path (builder);
 
@@ -126,7 +292,7 @@ test_segment_start (void)
   float epsilon, length;
   guint i;
 
-  path = create_random_path ();
+  path = create_random_path (G_MAXUINT);
   measure = gsk_path_measure_new (path);
   length = gsk_path_measure_get_length (measure);
   epsilon = MAX (length / 1024, G_MINFLOAT);
@@ -159,7 +325,7 @@ test_segment_end (void)
   float epsilon, length;
   guint i;
 
-  path = create_random_path ();
+  path = create_random_path (G_MAXUINT);
   measure = gsk_path_measure_new (path);
   length = gsk_path_measure_get_length (measure);
   epsilon = MAX (length / 1024, G_MINFLOAT);
@@ -192,7 +358,7 @@ test_segment_chunk (void)
   float epsilon, length;
   guint i;
 
-  path = create_random_path ();
+  path = create_random_path (G_MAXUINT);
   measure = gsk_path_measure_new (path);
   length = gsk_path_measure_get_length (measure);
   epsilon = MAX (length / 1024, G_MINFLOAT);
@@ -227,7 +393,7 @@ test_segment (void)
 
   for (i = 0; i < 1000; i++)
     {
-      path = create_random_path ();
+      path = create_random_path (G_MAXUINT);
       measure = gsk_path_measure_new (path);
       length = gsk_path_measure_get_length (measure);
       /* chosen high enough to stop the testsuite from failing */
@@ -275,9 +441,9 @@ test_closest_point (void)
 
   for (i = 0; i < 10; i++)
     {
-      path1 = create_random_path ();
+      path1 = create_random_path (G_MAXUINT);
       measure1 = gsk_path_measure_new_with_tolerance (path1, tolerance);
-      path2 = create_random_path ();
+      path2 = create_random_path (G_MAXUINT);
       measure2 = gsk_path_measure_new_with_tolerance (path2, tolerance);
 
       builder = gsk_path_builder_new ();


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