summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-08-06 22:07:03 +0200
committerJoel Klinghed <the_jk@yahoo.com>2017-08-06 22:24:59 +0200
commitbe1dc86b5434e84a88ba2e62873d87c62f8880c0 (patch)
treeba73db824c64890d196f843b0b72e11b0185e7fc /test
parentd935594d14eb65b55db0d4e96d66a7404f2130d0 (diff)
Add utf module
Currently only has valid_utf8 and read_utf8
Diffstat (limited to 'test')
-rw-r--r--test/.gitignore1
-rw-r--r--test/Makefile.am5
-rw-r--r--test/test-utf.cc64
3 files changed, 69 insertions, 1 deletions
diff --git a/test/.gitignore b/test/.gitignore
index 8bf01f1..f1a5a11 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -10,3 +10,4 @@
/test-observers
/test-htmlattrtext
/test-package
+/test-utf
diff --git a/test/Makefile.am b/test/Makefile.am
index abc1f9c..5248547 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,7 +3,7 @@ MAINTAINERCLEANFILES = Makefile.in
AM_CXXFLAGS = @DEFINES@ -I$(top_srcdir)/src
TESTS = test-url test-http test-args test-xdg test-paths test-strings \
- test-observers test-htmlattrtext test-package
+ test-observers test-htmlattrtext test-package test-utf
check_PROGRAMS = $(TESTS)
@@ -32,3 +32,6 @@ test_htmlattrtext_LDADD = $(top_builddir)/src/libattrstr.a
test_package_SOURCES = test-package.cc test.hh
test_package_LDADD = $(top_builddir)/src/libtp.a
+
+test_utf_SOURCES = test-utf.cc test.hh
+test_utf_LDADD = $(top_builddir)/src/libtp.a
diff --git a/test/test-utf.cc b/test/test-utf.cc
new file mode 100644
index 0000000..7096a47
--- /dev/null
+++ b/test/test-utf.cc
@@ -0,0 +1,64 @@
+// -*- mode: c++; c-basic-offset: 2; -*-
+
+#include "common.hh"
+#include "test.hh"
+
+#include <stdarg.h>
+
+#include "utf.hh"
+
+namespace {
+
+bool test_empty() {
+ ASSERT_EQ(true, valid_utf8(""));
+ ASSERT_EQ(true, valid_utf8("", 0));
+ ASSERT_EQ(true, valid_utf8("\xff", 0));
+ auto str = "";
+ ASSERT_EQ(str, read_utf8(str, 0, nullptr));
+ ASSERT_EQ(str, read_utf8(str, 10, nullptr));
+ return true;
+}
+
+bool test_good(char const* str, ...) {
+ ASSERT_EQ(true, valid_utf8(str));
+ va_list args;
+ va_start(args, str);
+ auto pos = str;
+ auto end = str + strlen(str) + 1;
+ uint32_t value;
+ while (true) {
+ uint32_t expected = va_arg(args, uint32_t);
+ auto next = read_utf8(pos, end - pos, &value);
+ if (expected == 0) {
+ ASSERT_EQ(pos, next);
+ ASSERT_EQ(expected, value);
+ break;
+ } else {
+ ASSERT_EQ(false, pos == next);
+ ASSERT_EQ(false, next == nullptr);
+ ASSERT_EQ(expected, value);
+ }
+ pos = next;
+ }
+ va_end(args);
+ return true;
+}
+
+bool test_bad(char const* str) {
+ ASSERT_EQ(false, valid_utf8(str));
+ return true;
+}
+
+} // namespace
+
+int main(void) {
+ BEFORE;
+ RUN(test_empty());
+ RUN(test_good("$", 0x24, 0));
+ RUN(test_good("\xc2\xa2", 0xa2, 0));
+ RUN(test_good("\xe2\x82\xac", 0x20ac, 0));
+ RUN(test_good("\xf0\x90\x8d\x88", 0x10348, 0));
+ RUN(test_bad("\xf0\x82\x82\xac"));
+ AFTER;
+}
+