Mercurial > libunicode
view test/main.cpp @ 2:84765c6f4872
New style
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Feb 2017 18:07:27 +0100 |
parents | 0d9603b420c2 |
children | 34a7a99dbcfa |
line wrap: on
line source
/* * main.cpp -- main test file for unicode * * Copyright (c) 2013-2017 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(conversion_32_to_8, ascii) { try { std::u32string u32{'a', 'b', 'c'}; std::string s = unicode::to_utf8(u32); ASSERT_EQ("abc", s); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(conversion_32_to_8, valid) { try { std::u32string u32{'a', U'é', 'c', U'𠀀'}; std::string s = unicode::to_utf8(u32); std::string expected = u8"aéc𠀀"; ASSERT_EQ(expected, s); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(conversion_32_to_8, invalid) { std::u32string u32{'a', 0xFFFFFFFF, 'c'}; ASSERT_ANY_THROW(unicode::to_utf8(u32)); } /* * Conversion UTF8 -> UTF32 * ------------------------------------------------------------------ */ TEST(Conversion_8_to_32, ascii) { try { std::string s{"abc"}; std::u32string expected{'a', 'b', 'c'}; std::u32string result = unicode::to_utf32(s); ASSERT_EQ(expected, result); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST(Conversion_8_to_32, valid) { try { std::string s{u8"aéc𠀀"}; std::u32string expected{'a', U'é', 'c', U'𠀀'}; std::u32string result = unicode::to_utf32(s); ASSERT_EQ(expected, result); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * UTF32 to upper * ------------------------------------------------------------------ */ TEST(toupper_32, 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(toupper_32, 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(toupper_32, 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(tolower_32, 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(tolower_32, 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(tolower_32, 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(toupper_8, 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(toupper_8, 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(toupper_8, invalid) { std::string s{"a" "\xFF""b"}; ASSERT_ANY_THROW(unicode::toupper(s)); } /* * UTF8 to lower * ------------------------------------------------------------------ */ TEST(tolower_8, 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(tolower_8, 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(tolower_8, invalid) { std::string s{"A" "\xFF""B"}; ASSERT_ANY_THROW(unicode::tolower(s)); } /* * Checks functions * ------------------------------------------------------------------ */ TEST(checks, isspace) { ASSERT_TRUE(unicode::isspace(' ')); ASSERT_FALSE(unicode::isspace(/* é */ 233)); } TEST(checks, isalpha) { ASSERT_TRUE(unicode::isalpha(U'é')); ASSERT_FALSE(unicode::isalpha(U'€')); } TEST(checks, isupper) { ASSERT_FALSE(unicode::isupper('a')); ASSERT_FALSE(unicode::isupper(U'é')); ASSERT_TRUE(unicode::isupper('A')); ASSERT_TRUE(unicode::isupper(U'É')); } TEST(checks, islower) { ASSERT_TRUE(unicode::islower('a')); ASSERT_TRUE(unicode::islower(U'é')); ASSERT_FALSE(unicode::islower('A')); ASSERT_FALSE(unicode::islower(U'É')); } /* * Miscellaneous * ------------------------------------------------------------------ */ TEST(misc, nbytes_point) { ASSERT_EQ(1, unicode::nbytes_point('a')); ASSERT_EQ(2, unicode::nbytes_point(U'é')); ASSERT_EQ(3, unicode::nbytes_point(U'€')); ASSERT_EQ(4, unicode::nbytes_point(U'𠀀')); } TEST(misc, nbytes_utf8) { std::string s1{u8"a"}; std::string s2{u8"é"}; std::string s3{u8"€"}; std::string s4{u8"𠀀"}; ASSERT_EQ(1, unicode::nbytes_utf8(s1[0])); ASSERT_EQ(2, unicode::nbytes_utf8(s2[0])); ASSERT_EQ(3, unicode::nbytes_utf8(s3[0])); ASSERT_EQ(4, unicode::nbytes_utf8(s4[0])); } TEST(misc, for_each) { std::string s{u8"aé€𠀀"}; int current = 0; unicode::for_each(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, for_each_invalid) { std::string s{"a" "\xFF" "b"}; ASSERT_ANY_THROW(unicode::for_each(s, [&] (char32_t) { })); } int main(int argc, char** argv) { InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }