TestIcu.cxx 917 Bytes
Newer Older
1 2 3 4 5 6
/*
 * Unit tests for src/util/
 */

#include "config.h"
#include "lib/icu/Converter.hxx"
7
#include "util/AllocatedString.hxx"
8

9
#include <gtest/gtest.h>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

#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" },
};

26 27 28 29
TEST(IcuConverter, InvalidCharset)
{
	EXPECT_ANY_THROW(IcuConverter::Create("doesntexist"));
}
30

31 32 33 34 35
TEST(IcuConverter, Latin1)
{
	IcuConverter *const converter =
		IcuConverter::Create("iso-8859-1");
	ASSERT_NE(converter, nullptr);
36

37 38 39
	for (const auto i : invalid_utf8) {
		EXPECT_ANY_THROW(converter->FromUTF8(i));
	}
40

41 42 43
	for (const auto i : latin1_tests) {
		auto f = converter->FromUTF8(i.utf8);
		EXPECT_STREQ(f.c_str(), i.other);
44

45 46
		auto t = converter->ToUTF8(i.other);
		EXPECT_STREQ(t.c_str(), i.utf8);
47 48
	}

49 50
	delete converter;
}
51 52

#endif