summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/localization/locales/locale.convenience/conversions/conversions.string/from_bytes.pass.cpp
blob: e527f31a1e2a8f5263f68d79da3bb8c5a565c933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <locale>

// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>

// wide_string from_bytes(char byte);
// wide_string from_bytes(const char* ptr);
// wide_string from_bytes(const byte_string& str);
// wide_string from_bytes(const char* first, const char* last);

#include <locale>
#include <codecvt>
#include <cassert>

template <class CharT, size_t = sizeof(CharT)>
struct TestHelper;
template <class CharT>
struct TestHelper<CharT, 2> {
  static void test();
};
template <class CharT>
struct TestHelper<CharT, 4> {
  static void test();
};

template <class CharT>
void TestHelper<CharT, 2>::test() {
  static_assert((std::is_same<CharT, wchar_t>::value), "");
  {
    std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
    std::string bs("\xE1\x80\x85\x00");
    std::wstring ws = myconv.from_bytes('a');
    assert(ws == L"a");
    ws = myconv.from_bytes(bs.c_str());
    assert(ws == L"\x1005");
    ws = myconv.from_bytes(bs);
    assert(ws == L"\x1005");
    ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
    assert(ws == L"\x1005");
    ws = myconv.from_bytes("");
    assert(ws.size() == 0);
  }
}

template <class CharT>
void TestHelper<CharT, 4>::test() {
  static_assert((std::is_same<CharT, wchar_t>::value), "");
  {
    std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
    std::string bs("\xF1\x80\x80\x83");
    std::wstring ws = myconv.from_bytes('a');
    assert(ws == L"a");
    ws = myconv.from_bytes(bs.c_str());
    assert(ws == L"\x40003");
    ws = myconv.from_bytes(bs);
    assert(ws == L"\x40003");
    ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
    assert(ws == L"\x40003");
    ws = myconv.from_bytes("");
    assert(ws.size() == 0);
  }
}

int main() { TestHelper<wchar_t>::test(); }
OpenPOWER on IntegriCloud