view C++/tests/Unicode/main.cpp @ 357:3174c463af95

Pack: disable because it will be redesigned / deleted
author David Demelier <markand@malikania.fr>
date Tue, 28 Apr 2015 11:04:54 +0200
parents 7fe8d4094983
children b78d6d8f2872
line wrap: on
line source

/*
 * main.cpp -- main test file for Utf8
 *
 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * /!\ Be sure to keep this file with UTF-8 encoding /!\
 */

#include <gtest/gtest.h>

#include <Unicode.h>

using namespace testing;

/* --------------------------------------------------------
 * Conversion UTF32 -> UTF8
 * -------------------------------------------------------- */

TEST(Conversion32to8, ascii)
{
	try {
		std::u32string u32{'a', 'b', 'c'};
		std::string s = Unicode::toUtf8(u32);

		ASSERT_EQ("abc", s);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Conversion32to8, valid)
{
	try {
		std::u32string u32{'a', U'é', 'c', U'𠀀'};
		std::string s = Unicode::toUtf8(u32);

		ASSERT_EQ("aéc𠀀", s);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Conversion32to8, invalid)
{
	try {
		std::u32string u32{'a', 0xFFFFFFFF, 'c'};
		std::string s = Unicode::toUtf8(u32);

		FAIL() << "expected a failure";
	} catch (const std::exception &ex) {
		SUCCEED();
	}
}

/* --------------------------------------------------------
 * Conversion UTF8 -> UTF32
 * -------------------------------------------------------- */

TEST(Conversion8to32, ascii)
{
	try {
		std::string s{"abc"};
		std::u32string expected{'a', 'b', 'c'};
		std::u32string result = Unicode::toUtf32(s);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Conversion8to32, valid)
{
	try {
		std::string s{"aéc𠀀"};
		std::u32string expected{'a', U'é', 'c', U'𠀀'};
		std::u32string result = Unicode::toUtf32(s);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/* --------------------------------------------------------
 * UTF32 to upper
 * -------------------------------------------------------- */

TEST(Toupper32, ascii)
{
	try {
		std::u32string u32{'a', 'b', 'c'};
		std::u32string expected{'A', 'B', 'C'};
		std::u32string result = Unicode::toupper(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Toupper32, valid)
{
	try {
		std::u32string u32{U'ä', U'ç', U'ë'};
		std::u32string expected{U'Ä', U'Ç', U'Ë'};
		std::u32string result = Unicode::toupper(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Toupper32, invalid)
{
	try {
		std::u32string u32{'a', 0xFFFFFFFF, 'b'};
		std::u32string expected{'A', 0xFFFFFFFF, 'B'};
		std::u32string result = Unicode::toupper(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/* --------------------------------------------------------
 * UTF32 to lower
 * -------------------------------------------------------- */

TEST(Tolower32, ascii)
{
	try {
		std::u32string u32{'A', 'B', 'C'};
		std::u32string expected{'a', 'b', 'c'};
		std::u32string result = Unicode::tolower(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Tolower32, valid)
{
	try {
		std::u32string u32{U'Ä', U'Ç', U'Ë'};
		std::u32string expected{U'ä', U'ç', U'ë'};
		std::u32string result = Unicode::tolower(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Tolower32, invalid)
{
	try {
		std::u32string u32{'A', 0xFFFFFFFF, 'B'};
		std::u32string expected{'a', 0xFFFFFFFF, 'b'};
		std::u32string result = Unicode::tolower(u32);

		ASSERT_EQ(expected, result);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/* --------------------------------------------------------
 * UTF8 to upper
 * -------------------------------------------------------- */

TEST(Toupper8, ascii)
{
	try {
		std::string s{"abc"};
		std::string r = Unicode::toupper(s);

		ASSERT_EQ("ABC", r);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Toupper8, valid)
{
	try {
		std::string s{"aéc"};
		std::string r = Unicode::toupper(s);

		ASSERT_EQ("AÉC", r);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Toupper8, invalid)
{
	try {
		std::string s{"a" "\xFF""b"};
		std::string r = Unicode::toupper(s);

		FAIL() << "expected a failure";
	} catch (const std::exception &ex) {
		SUCCEED();
	}
}

/* --------------------------------------------------------
 * UTF8 to lower
 * -------------------------------------------------------- */

TEST(Tolower8, ascii)
{
	try {
		std::string s{"ABC"};
		std::string r = Unicode::tolower(s);

		ASSERT_EQ("abc", r);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Tolower8, valid)
{
	try {
		std::string s{"AÉC"};
		std::string r = Unicode::tolower(s);

		ASSERT_EQ("aéc", r);
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST(Tolower8, invalid)
{
	try {
		std::string s{"A" "\xFF""B"};
		std::string r = Unicode::tolower(s);

		FAIL() << "expected a failure";
	} catch (const std::exception &ex) {
		SUCCEED();
	}
}

/* --------------------------------------------------------
 * Check functions
 * -------------------------------------------------------- */

TEST(Check, isspace)
{
	ASSERT_TRUE(Unicode::isspace(' '));
	ASSERT_FALSE(Unicode::isspace(/* é */ 233));
}

TEST(Check, isalpha)
{
	ASSERT_TRUE(Unicode::isalpha(U'é'));
	ASSERT_FALSE(Unicode::isalpha(U'€'));
}

TEST(Check, isupper)
{
	ASSERT_FALSE(Unicode::isupper('a'));
	ASSERT_FALSE(Unicode::isupper(U'é'));
	ASSERT_TRUE(Unicode::isupper('A'));
	ASSERT_TRUE(Unicode::isupper(U'É'));
}

TEST(Check, islower)
{
	ASSERT_TRUE(Unicode::islower('a'));
	ASSERT_TRUE(Unicode::islower(U'é'));
	ASSERT_FALSE(Unicode::islower('A'));
	ASSERT_FALSE(Unicode::islower(U'É'));
}

/* --------------------------------------------------------
 * Miscellaneous
 * -------------------------------------------------------- */

TEST(Misc, nbytesPoint)
{
	ASSERT_EQ(1, Unicode::nbytesPoint('a'));
	ASSERT_EQ(2, Unicode::nbytesPoint(U'é'));
	ASSERT_EQ(3, Unicode::nbytesPoint(U'€'));
	ASSERT_EQ(4, Unicode::nbytesPoint(U'𠀀'));
}

TEST(Misc, nbytesUtf8)
{
	std::string s1{"a"};
	std::string s2{"é"};
	std::string s3{"€"};
	std::string s4{"𠀀"};

	ASSERT_EQ(1, Unicode::nbytesUtf8(s1[0]));
	ASSERT_EQ(2, Unicode::nbytesUtf8(s2[0]));
	ASSERT_EQ(3, Unicode::nbytesUtf8(s3[0]));
	ASSERT_EQ(4, Unicode::nbytesUtf8(s4[0]));
}

TEST(Misc, forEach)
{
	std::string s{"aé€𠀀"};
	int current = 0;

	Unicode::forEach(s, [&] (char32_t code) {
		if (current == 0) {
			ASSERT_EQ(U'a', code);
		} else if (current == 1) {
			ASSERT_EQ(U'é', code);
		} else if (current == 2) {
			ASSERT_EQ(U'€', code);
		} else if (current == 3) {
			ASSERT_EQ(U'𠀀', code);
		}

		current++;
	});

	ASSERT_EQ(4, current);
}

int main(int argc, char **argv)
{
	InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}