Mercurial > code
view modules/unicode/test/main.cpp @ 525:17a733c5661a
Unicode: resurrection
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 01 Jun 2016 17:13:26 +0200 |
parents | |
children | f48bb09bccc7 |
line wrap: on
line source
/* * main.cpp -- main test file for unicode * * Copyright (c) 2013-2016 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.hpp> 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); std::string expected = u8"aéc𠀀"; ASSERT_EQ(expected, s); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(Conversion32to8, invalid) { std::u32string u32{'a', 0xFFFFFFFF, 'c'}; ASSERT_ANY_THROW(unicode::toUtf8(u32)); } /* * 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{u8"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{u8"aéc"}; std::string r = unicode::toupper(s); ASSERT_EQ(u8"AÉC", r); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(Toupper8, invalid) { std::string s{"a" "\xFF""b"}; ASSERT_ANY_THROW(unicode::toupper(s)); } /* * 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{u8"AÉC"}; std::string r = unicode::tolower(s); ASSERT_EQ(u8"aéc", r); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(Tolower8, invalid) { std::string s{"A" "\xFF""B"}; ASSERT_ANY_THROW(unicode::tolower(s)); } /* * 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{u8"a"}; std::string s2{u8"é"}; std::string s3{u8"€"}; std::string s4{u8"𠀀"}; 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{u8"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); } TEST(Misc, forEachInvalid) { std::string s{"a" "\xFF" "b"}; ASSERT_ANY_THROW(unicode::forEach(s, [&] (char32_t) { })); } int main(int argc, char **argv) { InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }