from_bytes.pass.cpp (2116B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <locale> 11 12 // wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc> 13 14 // wide_string from_bytes(char byte); 15 // wide_string from_bytes(const char* ptr); 16 // wide_string from_bytes(const byte_string& str); 17 // wide_string from_bytes(const char* first, const char* last); 18 19 #include <locale> 20 #include <codecvt> 21 #include <cassert> 22 23 template <class CharT, size_t = sizeof(CharT)> 24 struct TestHelper; 25 template <class CharT> 26 struct TestHelper<CharT, 2> { 27 static void test(); 28 }; 29 template <class CharT> 30 struct TestHelper<CharT, 4> { 31 static void test(); 32 }; 33 34 template <class CharT> 35 void TestHelper<CharT, 2>::test() { 36 static_assert((std::is_same<CharT, wchar_t>::value), ""); 37 { 38 std::wstring_convert<std::codecvt_utf8<CharT> > myconv; 39 std::string bs("\xE1\x80\x85\x00"); 40 std::wstring ws = myconv.from_bytes('a'); 41 assert(ws == L"a"); 42 ws = myconv.from_bytes(bs.c_str()); 43 assert(ws == L"\x1005"); 44 ws = myconv.from_bytes(bs); 45 assert(ws == L"\x1005"); 46 ws = myconv.from_bytes(bs.data(), bs.data() + bs.size()); 47 assert(ws == L"\x1005"); 48 ws = myconv.from_bytes(""); 49 assert(ws.size() == 0); 50 } 51 } 52 53 template <class CharT> 54 void TestHelper<CharT, 4>::test() { 55 static_assert((std::is_same<CharT, wchar_t>::value), ""); 56 { 57 std::wstring_convert<std::codecvt_utf8<CharT> > myconv; 58 std::string bs("\xF1\x80\x80\x83"); 59 std::wstring ws = myconv.from_bytes('a'); 60 assert(ws == L"a"); 61 ws = myconv.from_bytes(bs.c_str()); 62 assert(ws == L"\x40003"); 63 ws = myconv.from_bytes(bs); 64 assert(ws == L"\x40003"); 65 ws = myconv.from_bytes(bs.data(), bs.data() + bs.size()); 66 assert(ws == L"\x40003"); 67 ws = myconv.from_bytes(""); 68 assert(ws.size() == 0); 69 } 70 } 71 72 int main() { TestHelper<wchar_t>::test(); }