| 1 | // Copyright (C) 2015-2025 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. |
| 8 | |
| 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. |
| 13 | |
| 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/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <locale> |
| 21 | #include <iterator> |
| 22 | #include <string> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | const auto expected = (const char*)u8"£¥€"; |
| 26 | const std::size_t expected_len = std::char_traits<char>::length(expected); |
| 27 | |
| 28 | template<typename C> |
| 29 | void test(const C* from) |
| 30 | { |
| 31 | auto len = std::char_traits<C>::length(from); |
| 32 | std::mbstate_t state{}; |
| 33 | char buf[16] = { }; |
| 34 | using test_type = std::codecvt<C, char, std::mbstate_t>; |
| 35 | const test_type& cvt = std::use_facet<test_type>(std::locale::classic()); |
| 36 | auto from_end = from + len; |
| 37 | auto from_next = from; |
| 38 | auto buf_end = std::end(buf); |
| 39 | auto buf_next = buf; |
| 40 | auto res = cvt.out(state, from, from_end, from_next, buf, buf_end, buf_next); |
| 41 | VERIFY( res == std::codecvt_base::ok ); |
| 42 | VERIFY( from_next == from_end ); |
| 43 | VERIFY( (buf_next - buf) == expected_len ); |
| 44 | VERIFY( 0 == std::char_traits<char>::compare(buf, expected, expected_len) ); |
| 45 | |
| 46 | C buf2[16]; |
| 47 | auto exp_end = expected + expected_len; |
| 48 | auto exp_next = expected; |
| 49 | auto buf2_end = std::end(buf2); |
| 50 | auto buf2_next = buf2; |
| 51 | res = cvt.in(state, expected, exp_end, exp_next, buf2, buf2_end, buf2_next); |
| 52 | VERIFY( res == std::codecvt_base::ok ); |
| 53 | VERIFY( exp_next == exp_end ); |
| 54 | VERIFY( (buf2_next - buf2) == len ); |
| 55 | VERIFY( 0 == std::char_traits<C>::compare(buf2, from, len) ); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | test01() |
| 60 | { |
| 61 | test(u"£¥€"); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test02() |
| 66 | { |
| 67 | test(U"£¥€"); |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | main() |
| 72 | { |
| 73 | test01(); |
| 74 | test02(); |
| 75 | } |