Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / localization / locales / locale.convenience / conversions / conversions.string / from_bytes.pass.cpp
blobac614b087b90a66beeb39e3af1b2e6e63504902b
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // <locale>
11 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
13 // wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
15 // wide_string from_bytes(char byte);
16 // wide_string from_bytes(const char* ptr);
17 // wide_string from_bytes(const byte_string& str);
18 // wide_string from_bytes(const char* first, const char* last);
20 // XFAIL: no-wide-characters
22 #include <locale>
23 #include <codecvt>
24 #include <cassert>
26 #include "test_macros.h"
28 template <class CharT, std::size_t = sizeof(CharT)>
29 struct TestHelper;
30 template <class CharT>
31 struct TestHelper<CharT, 2> {
32 static void test();
34 template <class CharT>
35 struct TestHelper<CharT, 4> {
36 static void test();
39 template <class CharT>
40 void TestHelper<CharT, 2>::test() {
41 static_assert((std::is_same<CharT, wchar_t>::value), "");
43 std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
44 std::string bs("\xE1\x80\x85");
45 std::wstring ws = myconv.from_bytes('a');
46 assert(ws == L"a");
47 ws = myconv.from_bytes(bs.c_str());
48 assert(ws == L"\u1005");
49 ws = myconv.from_bytes(bs);
50 assert(ws == L"\u1005");
51 ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
52 assert(ws == L"\u1005");
53 ws = myconv.from_bytes("");
54 assert(ws.size() == 0);
58 template <class CharT>
59 void TestHelper<CharT, 4>::test() {
60 static_assert((std::is_same<CharT, wchar_t>::value), "");
62 std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
63 std::string bs("\xF1\x80\x80\x83");
64 std::wstring ws = myconv.from_bytes('a');
65 assert(ws == L"a");
66 ws = myconv.from_bytes(bs.c_str());
67 assert(ws == L"\U00040003");
68 ws = myconv.from_bytes(bs);
69 assert(ws == L"\U00040003");
70 ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
71 assert(ws == L"\U00040003");
72 ws = myconv.from_bytes("");
73 assert(ws.size() == 0);
77 int main(int, char**) {
78 TestHelper<wchar_t>::test();
79 return 0;