Fix 22_locale/locale/cons/12658_thread-2.cc on hppa.
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / codecvt / utf8-char8_t.cc
blob7af7e3c2359ea0d5e616434e6e945beee00d2fe2
1 // Copyright (C) 2015-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++11 } }
19 // { dg-require-cstdint "" }
20 // { dg-options "-fchar8_t" }
22 #include <locale>
23 #include <iterator>
24 #include <string>
25 #include <testsuite_hooks.h>
27 const char8_t expected[] = u8"£¥€";
28 const std::size_t expected_len = std::char_traits<char8_t>::length(expected);
30 template<typename C>
31 void test(const C* from)
33 auto len = std::char_traits<C>::length(from);
34 std::mbstate_t state{};
35 char8_t buf[16] = { };
36 using test_type = std::codecvt<C, char8_t, std::mbstate_t>;
37 const test_type& cvt = std::use_facet<test_type>(std::locale::classic());
38 auto from_end = from + len;
39 auto from_next = from;
40 auto buf_end = std::end(buf);
41 auto buf_next = buf;
42 auto res = cvt.out(state, from, from_end, from_next, buf, buf_end, buf_next);
43 VERIFY( res == std::codecvt_base::ok );
44 VERIFY( from_next == from_end );
45 VERIFY( (buf_next - buf) == expected_len );
46 VERIFY( 0 == std::char_traits<char8_t>::compare(buf, expected, expected_len) );
48 C buf2[16];
49 auto exp_end = expected + expected_len;
50 auto exp_next = expected;
51 auto buf2_end = std::end(buf2);
52 auto buf2_next = buf2;
53 res = cvt.in(state, expected, exp_end, exp_next, buf2, buf2_end, buf2_next);
54 VERIFY( res == std::codecvt_base::ok );
55 VERIFY( exp_next == exp_end );
56 VERIFY( (buf2_next - buf2) == len );
57 VERIFY( 0 == std::char_traits<C>::compare(buf2, from, len) );
60 void
61 test01()
63 test(u"£¥€");
66 void
67 test02()
69 test(U"£¥€");
72 int
73 main()
75 test01();
76 test02();