Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / test / api / test-buffer.c
index 228f0f3..4b48d5c 100644 (file)
@@ -71,7 +71,7 @@ fixture_init (fixture_t *fixture, gconstpointer user_data)
 
     case BUFFER_ONE_BY_ONE:
       for (i = 1; i < G_N_ELEMENTS (utf32) - 1; i++)
-       hb_buffer_add (b, utf32[i], i);
+      hb_buffer_add (b, utf32[i], i);
       break;
 
     case BUFFER_UTF32:
@@ -856,6 +856,74 @@ test_buffer_empty (void)
   g_assert (!hb_buffer_allocation_successful (b));
 }
 
+typedef struct {
+  const char *contents;
+  hb_buffer_serialize_format_t format;
+  unsigned int num_items;
+  hb_bool_t success;
+} serialization_test_t;
+
+static const serialization_test_t serialization_tests[] = {
+  { "<U+0640=0|U+0635=1>", HB_BUFFER_SERIALIZE_FORMAT_TEXT, 2, 1 },
+  { "[{\"u\":1600,\"cl\":0},{\"u\":1589,\"cl\":1}]", HB_BUFFER_SERIALIZE_FORMAT_JSON, 2, 1 },
+
+  /* Mixed glyphs/Unicodes -> parse fail */
+  { "[{\"u\":1600,\"cl\":0},{\"g\":1589,\"cl\":1}]", HB_BUFFER_SERIALIZE_FORMAT_JSON, 0, 0 },
+  { "<U+0640=0|uni0635=1>", HB_BUFFER_SERIALIZE_FORMAT_TEXT, 0, 0 },
+};
+
+static void
+test_buffer_serialize_deserialize (void)
+{
+  hb_buffer_t *b;
+  unsigned int i;
+
+  for (i = 0; i < G_N_ELEMENTS (serialization_tests); i++)
+  {
+    unsigned int consumed;
+    char round_trip[1024];
+
+    b = hb_buffer_create ();
+    hb_buffer_set_replacement_codepoint (b, (hb_codepoint_t) -1);
+
+    const serialization_test_t *test = &serialization_tests[i];
+    g_test_message ("serialize test #%d", i);
+
+    (void) hb_buffer_deserialize_unicode (b, test->contents, -1, NULL, test->format);
+
+    // Expected parse failure, got one, don't round-trip
+    if (test->success != 0)
+    {
+      unsigned int num_glyphs = hb_buffer_get_length (b);
+      g_assert_cmpint (num_glyphs, ==, test->num_items);
+
+      hb_buffer_serialize_unicode (b, 0, num_glyphs, round_trip,
+                                  sizeof(round_trip), &consumed, test->format,
+                                  HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
+      g_assert_cmpstr (round_trip, ==, test->contents);
+    }
+
+    hb_buffer_destroy (b);
+
+  }
+
+  char test[1024];
+  unsigned int consumed;
+  hb_buffer_t *indeterminate = hb_buffer_get_empty ();
+  hb_buffer_serialize (indeterminate, 0, (unsigned) -1,
+                      test, sizeof(test), &consumed, NULL,
+                      HB_BUFFER_SERIALIZE_FORMAT_JSON,
+                      HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
+  g_assert_cmpstr ( test, ==, "[]");
+
+  hb_buffer_serialize (indeterminate, 0, (unsigned) - 1,
+                      test, sizeof(test), &consumed, NULL,
+                      HB_BUFFER_SERIALIZE_FORMAT_TEXT,
+                      HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
+  g_assert_cmpstr ( test, ==, "!!");
+
+}
+
 int
 main (int argc, char **argv)
 {
@@ -880,6 +948,7 @@ main (int argc, char **argv)
   hb_test_add (test_buffer_utf16_conversion);
   hb_test_add (test_buffer_utf32_conversion);
   hb_test_add (test_buffer_empty);
+  hb_test_add (test_buffer_serialize_deserialize);
 
   return hb_test_run();
 }