Mercurial > code
view C++/tests/Unicode/main.cpp @ 421:d046379137f3
Js:
- add deleteProperty
- add hasProperty
- add more documentation
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 08 Oct 2015 14:15:27 +0200 |
parents | f083259de5e6 |
children |
line wrap: on
line source
/* * main.cpp -- main test file for Utf8 * * Copyright (c) 2013-2015 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); printf("%s\n", r.c_str()); 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); } TEST(Misc, forEachInvalid) { std::string s{"a" "\xFF" "b"}; try { unicode::forEach(s, [&] (char32_t) { }); FAIL() << "exception expected"; } catch (...) { } } int main(int argc, char **argv) { InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }