From 4658bd8208613bad3c987ee5083e1b6c5481f037 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 1 Dec 2014 20:55:05 +0100 Subject: test/TestIcu: new unit test for lib/icu/* --- test/TestIcu.cxx | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 test/TestIcu.cxx (limited to 'test') diff --git a/test/TestIcu.cxx b/test/TestIcu.cxx new file mode 100644 index 000000000..9d525d698 --- /dev/null +++ b/test/TestIcu.cxx @@ -0,0 +1,82 @@ +/* + * Unit tests for src/util/ + */ + +#include "config.h" +#include "lib/icu/Converter.hxx" +#include "util/Error.hxx" + +#include +#include +#include +#include + +#include +#include + +#ifdef HAVE_ICU_CONVERTER + +static const char *const invalid_utf8[] = { + "\xfc", +}; + +struct StringPair { + const char *utf8, *other; +}; + +static constexpr StringPair latin1_tests[] = { + { "foo", "foo" }, + { "\xc3\xbc", "\xfc" }, +}; + +class TestIcuConverter : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(TestIcuConverter); + CPPUNIT_TEST(TestInvalidCharset); + CPPUNIT_TEST(TestLatin1); + CPPUNIT_TEST_SUITE_END(); + +public: + void TestInvalidCharset() { + CPPUNIT_ASSERT_EQUAL((IcuConverter *)nullptr, + IcuConverter::Create("doesntexist", + IgnoreError())); + } + + void TestLatin1() { + IcuConverter *const converter = + IcuConverter::Create("iso-8859-1", IgnoreError()); + CPPUNIT_ASSERT(converter != nullptr); + + for (const auto i : invalid_utf8) { + auto f = converter->FromUTF8(i); + CPPUNIT_ASSERT_EQUAL(true, f.empty()); + } + + for (const auto i : latin1_tests) { + auto f = converter->FromUTF8(i.utf8); + CPPUNIT_ASSERT_EQUAL(true, f == i.other); + + auto t = converter->ToUTF8(i.other); + CPPUNIT_ASSERT_EQUAL(true, t == i.utf8); + } + + delete converter; + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestIcuConverter); + +#endif + +int +main(gcc_unused int argc, gcc_unused char **argv) +{ +#ifdef HAVE_ICU_CONVERTER + CppUnit::TextUi::TestRunner runner; + auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); + runner.addTest(registry.makeTest()); + return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; +#else + return EXIT_SUCCESS; +#endif +} -- cgit v1.2.3