diff options
Diffstat (limited to 'test/test-utf.cc')
| -rw-r--r-- | test/test-utf.cc | 64 |
1 files changed, 64 insertions, 0 deletions
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; +} + |
