2014-12-01 20:55:05 +01:00
|
|
|
/*
|
|
|
|
* Unit tests for src/util/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "lib/icu/Converter.hxx"
|
2015-06-25 22:43:55 +02:00
|
|
|
#include "util/AllocatedString.hxx"
|
|
|
|
#include "util/StringAPI.hxx"
|
2014-12-01 20:55:05 +01:00
|
|
|
|
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2016-04-12 22:18:36 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2014-12-01 20:55:05 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#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() {
|
2016-04-12 22:18:36 +02:00
|
|
|
try {
|
|
|
|
IcuConverter::Create("doesntexist");
|
|
|
|
CPPUNIT_FAIL("Exception expected");
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-04-12 22:18:36 +02:00
|
|
|
}
|
2014-12-01 20:55:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestLatin1() {
|
|
|
|
IcuConverter *const converter =
|
2016-04-12 22:18:36 +02:00
|
|
|
IcuConverter::Create("iso-8859-1");
|
2014-12-01 20:55:05 +01:00
|
|
|
CPPUNIT_ASSERT(converter != nullptr);
|
|
|
|
|
|
|
|
for (const auto i : invalid_utf8) {
|
2016-04-13 10:39:29 +02:00
|
|
|
try {
|
|
|
|
auto f = converter->FromUTF8(i);
|
|
|
|
CPPUNIT_FAIL("Exception expected");
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-04-13 10:39:29 +02:00
|
|
|
}
|
2014-12-01 20:55:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto i : latin1_tests) {
|
|
|
|
auto f = converter->FromUTF8(i.utf8);
|
2015-06-25 22:43:55 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(f.c_str(),
|
|
|
|
i.other));
|
2014-12-01 20:55:05 +01:00
|
|
|
|
|
|
|
auto t = converter->ToUTF8(i.other);
|
2015-06-25 22:43:55 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(t.c_str(),
|
|
|
|
i.utf8));
|
2014-12-01 20:55:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|