[pango/wip/otte/testrandom] tests: Add a spice up wrapping test




commit 179869cc14582883d0432f32f4aa23a2eae314a5
Author: Benjamin Otte <otte redhat com>
Date:   Fri Nov 12 03:14:34 2021 +0100

    tests: Add a spice up wrapping test
    
    * Generate 20 random strings that look like sentences.
    * For each of them, measure the size at 100 random widths.
    * assert that for sorted widths, the measured widths are increasing
      and the heights are decreasing.

 tests/meson.build  |   3 +-
 tests/testrandom.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 188 insertions(+), 1 deletion(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index b962a758..1668058b 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -44,7 +44,8 @@ if cairo_dep.found()
     [ 'testmisc', [ 'testmisc.c' ], [ libpangocairo_dep, glib_dep, harfbuzz_dep ] ],
     [ 'cxx-test', [ 'cxx-test.cpp' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
     [ 'test-harfbuzz', [ 'test-harfbuzz.c' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
-    [ 'test-break', [ 'test-break.c', 'test-common.c' ], [libpangocairo_dep, glib_dep, harfbuzz_dep ] ]
+    [ 'test-break', [ 'test-break.c', 'test-common.c' ], [libpangocairo_dep, glib_dep, harfbuzz_dep ] ],
+    [ 'testrandom', [ 'testrandom.c' ], [ libpangocairo_dep, gio_dep ] ],
   ]
 
   if pango_cairo_backends.contains('png')
diff --git a/tests/testrandom.c b/tests/testrandom.c
new file mode 100644
index 00000000..ea014903
--- /dev/null
+++ b/tests/testrandom.c
@@ -0,0 +1,186 @@
+/* Pango
+ * testmisc.c: Test program for miscellaneous things
+ *
+ * Copyright (C) 2021 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+#include <pango/pangocairo.h>
+
+static char **words;
+static gsize n_words;
+
+static const char *
+random_word (void)
+{
+  return words[g_test_rand_int_range (0, n_words)];
+}
+
+static char *
+create_random_sentence (void)
+{
+  GString *string = g_string_new (NULL);
+  gsize i, n_words;
+
+  n_words = g_test_rand_int_range (3, 15);
+  for (i = 0; i < n_words; i++)
+    {
+      if (i > 0)
+        {
+          if (g_random_int_range (0, 10) == 0)
+            g_string_append_c (string, ',');
+          g_string_append_c (string, ' ');
+        }
+      g_string_append (string, random_word ());
+    }
+
+  g_string_append_c (string, "!.?"[g_test_rand_int_range(0, 3)]);
+
+  return g_string_free (string, FALSE);
+}
+
+typedef struct
+{
+  int set_width;
+  int width;
+  int height;
+} Size;
+
+static int
+compare_size (gconstpointer a,
+              gconstpointer b,
+              gpointer      unused)
+{
+  return ((const Size *) a)->set_width - ((const Size *) b)->set_width;
+}
+
+static void
+layout_check_size (PangoLayout *layout,
+                   int          width,
+                   Size        *out_size)
+{
+  out_size->set_width = width;
+  pango_layout_set_width (layout, width);
+  pango_layout_get_size (layout, &out_size->width, &out_size->height);
+}
+
+static void
+test_wrap_char (void)
+{
+#define N_SENTENCES 20
+  PangoFontDescription *desc;
+  PangoContext *context;
+  PangoLayout *layout;
+  char *sentence;
+  Size min, max;
+  Size sizes[100];
+  gsize i, j;
+
+  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+  desc = pango_font_description_from_string ("Sans 10");
+  layout = pango_layout_new (context);
+  pango_layout_set_font_description (layout, desc);
+  pango_font_description_free (desc);
+
+  for (j = 0; j < N_SENTENCES; j++)
+    {
+      sentence = create_random_sentence ();
+      pango_layout_set_text (layout, sentence, -1);
+      g_free (sentence);
+      pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
+
+      layout_check_size (layout, -1, &max);
+      layout_check_size (layout, 0, &min);
+      g_assert_cmpint (min.width, <=, max.width);
+      g_assert_cmpint (min.height, >=, max.height);
+
+      for (i = 0; i < G_N_ELEMENTS (sizes); i++)
+        {
+          layout_check_size (layout, min.width / 2 + g_test_rand_int_range (0, max.width), &sizes[i]);
+        }
+
+      g_qsort_with_data (sizes, G_N_ELEMENTS (sizes), sizeof (Size), compare_size, NULL);
+
+      g_assert_cmpint (sizes[0].width, >=, min.width);
+      g_assert_cmpint (sizes[0].height, <=, min.height);
+
+      for (i = 1; i < G_N_ELEMENTS (sizes); i++)
+        {
+          g_assert_cmpint (sizes[i-1].set_width, <=, sizes[i].set_width);
+          g_assert_cmpint (sizes[i-1].width, <=, sizes[i].width);
+          g_assert_cmpint (sizes[i-1].height, >=, sizes[i].height);
+          if (sizes[i-1].width == sizes[i].width)
+            g_assert_cmpint (sizes[i-1].height, ==, sizes[i].height);
+        }
+
+      if (sizes[i-1].set_width >= max.width)
+        {
+          g_assert_cmpint (sizes[i-1].width, ==, max.width);
+          g_assert_cmpint (sizes[i-1].height, ==, max.height);
+        }
+      else
+        {
+          g_assert_cmpint (sizes[i-1].width, <, max.width);
+          g_assert_cmpint (sizes[i-1].height, >, max.height);
+        }
+    }
+
+  g_object_unref (layout);
+  g_object_unref (context);
+}
+
+static char **
+init_words (void)
+{
+  GFile *file;
+  GBytes *bytes;
+  char **result;
+
+  file = g_file_new_for_path ("/usr/share/dict/words");
+  bytes = g_file_load_bytes (file, NULL, NULL, NULL);
+  if (bytes)
+    {
+      result = g_strsplit (g_bytes_get_data (bytes, NULL), "\n", -1);
+      g_bytes_unref (bytes);
+    }
+  else
+    result = g_strsplit ("lorem ipsum dolor sit amet consectetur adipisci elit sed eiusmod tempor incidunt 
labore et dolore magna aliqua ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut 
aliquid ex ea commodi consequat", " ", -1);
+
+  return result;
+}
+
+int
+main (int argc, char *argv[])
+{
+  int result;
+
+  g_test_init (&argc, &argv, NULL);
+
+  words = init_words ();
+  n_words = g_strv_length (words);
+
+  g_test_add_func ("/layout/wrap-char", test_wrap_char);
+
+  result = g_test_run ();
+
+  g_strfreev (words);
+
+  return result;
+}


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