| 71 | } |
| 72 | |
| 73 | static int testRobustEncoding() |
| 74 | { |
| 75 | // test that the conversion functions handle invalid |
| 76 | // unicode correctly/gracefully |
| 77 | |
| 78 | // we manipulate the format flags of stdout, remember |
| 79 | // the original state here to restore before return |
| 80 | std::ios::fmtflags const& flags = std::cout.flags(); |
| 81 | |
| 82 | int ret = 0; |
| 83 | char cstr[] = { static_cast<char>(-1), 0 }; |
| 84 | // this conversion could fail |
| 85 | std::wstring wstr = kwsys::Encoding::ToWide(cstr); |
| 86 | |
| 87 | wstr = kwsys::Encoding::ToWide(nullptr); |
| 88 | if (!wstr.empty()) { |
| 89 | wchar_t const* wcstr = wstr.c_str(); |
| 90 | std::cout << "ToWide(NULL) returned"; |
| 91 | for (size_t i = 0; i < wstr.size(); i++) { |
| 92 | std::cout << " " << std::hex << static_cast<int>(wcstr[i]); |
| 93 | } |
| 94 | std::cout << std::endl; |
| 95 | ret++; |
| 96 | } |
| 97 | wstr = kwsys::Encoding::ToWide(""); |
| 98 | if (!wstr.empty()) { |
| 99 | wchar_t const* wcstr = wstr.c_str(); |
| 100 | std::cout << "ToWide(\"\") returned"; |
| 101 | for (size_t i = 0; i < wstr.size(); i++) { |
| 102 | std::cout << " " << std::hex << static_cast<int>(wcstr[i]); |
| 103 | } |
| 104 | std::cout << std::endl; |
| 105 | ret++; |
| 106 | } |
| 107 | |
| 108 | #ifdef _WIN32 |
| 109 | // 16 bit wchar_t - we make an invalid surrogate pair |
| 110 | wchar_t cwstr[] = { 0xD801, 0xDA00, 0 }; |
| 111 | // this conversion could fail |
| 112 | std::string win_str = kwsys::Encoding::ToNarrow(cwstr); |
| 113 | #endif |
| 114 | |
| 115 | std::string str = kwsys::Encoding::ToNarrow(nullptr); |
| 116 | if (!str.empty()) { |
| 117 | std::cout << "ToNarrow(NULL) returned " << str << std::endl; |
| 118 | ret++; |
| 119 | } |
| 120 | |
| 121 | str = kwsys::Encoding::ToNarrow(L""); |
| 122 | if (!wstr.empty()) { |
| 123 | std::cout << "ToNarrow(\"\") returned " << str << std::endl; |
| 124 | ret++; |
| 125 | } |
| 126 | |
| 127 | std::cout.flags(flags); |
| 128 | return ret; |
| 129 | } |
| 130 |
no test coverage detected
searching dependent graphs…