changeset 12627:f47180dc07fc

Tests for module 'unistr/u8-check'.
author Bruno Haible <bruno@clisp.org>
date Sat, 09 Jan 2010 19:40:38 +0100
parents de23328471d5
children e70a738da0ce
files ChangeLog modules/unistr/u8-check-tests tests/unistr/test-u8-check.c
diffstat 3 files changed, 204 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2010-01-09  Bruno Haible  <bruno@clisp.org>
 
+	Tests for module 'unistr/u8-check'.
+	* modules/unistr/u8-check-tests: New file.
+	* tests/unistr/test-u8-check.c: New file.
+
 	* tests/unictype/test-categ_byname.c: Include <stdbool.h>.
 	(category_equals): New function.
 	(main): Add more tests.
new file mode 100644
--- /dev/null
+++ b/modules/unistr/u8-check-tests
@@ -0,0 +1,12 @@
+Files:
+tests/unistr/test-u8-check.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-u8-check
+check_PROGRAMS += test-u8-check
+test_u8_check_SOURCES = unistr/test-u8-check.c
new file mode 100644
--- /dev/null
+++ b/tests/unistr/test-u8-check.c
@@ -0,0 +1,188 @@
+/* Test of u8_check() function.
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
+
+#include <config.h>
+
+#include "unistr.h"
+
+#include "macros.h"
+
+int
+main ()
+{
+  /* Test empty string.  */
+  {
+    static const uint8_t input[] = "";
+    ASSERT (u8_check (input, 0) == NULL);
+  }
+
+  /* Test valid non-empty string.  */
+  {
+    static const uint8_t input[] = /* "Данило Шеган" */
+      "\320\224\320\260\320\275\320\270\320\273\320\276 \320\250\320\265\320\263\320\260\320\275";
+    ASSERT (u8_check (input, sizeof (input) - 1) == NULL);
+  }
+
+  /* Test out-of-range character with 4 bytes: U+110000.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\364\220\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test out-of-range character with 5 bytes: U+200000.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\370\210\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test out-of-range character with 6 bytes: U+4000000.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\374\204\200\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test invalid lead byte.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\376\200\200\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\377\200\200\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test overlong 2-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\301\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test overlong 3-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\340\200\277";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test overlong 4-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\360\200\277\277";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test invalid bytes in 2-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\302\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == NULL);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\302\100";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\302\300";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test invalid bytes in 3-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == NULL);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\100\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\300\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\200\100";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\200\300";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test invalid bytes in 4-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == NULL);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\100\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\300\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\100\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\300\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\200\100";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\200\300";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test truncated/incomplete 2-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\302";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test truncated/incomplete 3-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\342\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test truncated/incomplete 4-byte character.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\362\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test missing lead byte.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\200\200\200\200\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  /* Test surrogate codepoints.  */
+  {
+    static const uint8_t input[] = "\320\224\320\260\355\240\200\355\260\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+  {
+    static const uint8_t input[] = "\320\224\320\260\355\260\200";
+    ASSERT (u8_check (input, sizeof (input) - 1) == input + 4);
+  }
+
+  return 0;
+}