| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // REQUIRES: locale.en_US.UTF-8 |
| 10 | |
| 11 | // <ios> |
| 12 | |
| 13 | // template <class charT, class traits> class basic_ios |
| 14 | |
| 15 | // locale imbue(const locale& loc); |
| 16 | |
| 17 | #include <ios> |
| 18 | #include <streambuf> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | #include "platform_support.h" // locale name macros |
| 23 | |
| 24 | struct testbuf |
| 25 | : public std::streambuf |
| 26 | { |
| 27 | }; |
| 28 | |
| 29 | bool f1_called = false; |
| 30 | bool f2_called = false; |
| 31 | bool f3_called = false; |
| 32 | |
| 33 | void f1(std::ios_base::event ev, std::ios_base& stream, int index) |
| 34 | { |
| 35 | if (ev == std::ios_base::imbue_event) |
| 36 | { |
| 37 | assert(!f1_called); |
| 38 | assert( f2_called); |
| 39 | assert( f3_called); |
| 40 | assert(stream.getloc().name() == LOCALE_en_US_UTF_8); |
| 41 | assert(index == 4); |
| 42 | f1_called = true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void f2(std::ios_base::event ev, std::ios_base& stream, int index) |
| 47 | { |
| 48 | if (ev == std::ios_base::imbue_event) |
| 49 | { |
| 50 | assert(!f1_called); |
| 51 | assert(!f2_called); |
| 52 | assert( f3_called); |
| 53 | assert(stream.getloc().name() == LOCALE_en_US_UTF_8); |
| 54 | assert(index == 5); |
| 55 | f2_called = true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void f3(std::ios_base::event ev, std::ios_base& stream, int index) |
| 60 | { |
| 61 | if (ev == std::ios_base::imbue_event) |
| 62 | { |
| 63 | assert(!f1_called); |
| 64 | assert(!f2_called); |
| 65 | assert(!f3_called); |
| 66 | assert(stream.getloc().name() == LOCALE_en_US_UTF_8); |
| 67 | assert(index == 6); |
| 68 | f3_called = true; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | int main(int, char**) |
| 73 | { |
| 74 | { |
| 75 | std::ios ios(0); |
| 76 | ios.register_callback(fn: f1, index: 4); |
| 77 | ios.register_callback(fn: f2, index: 5); |
| 78 | ios.register_callback(fn: f3, index: 6); |
| 79 | std::locale l = ios.imbue(std::locale(LOCALE_en_US_UTF_8)); |
| 80 | assert(l.name() == std::string("C" )); |
| 81 | assert(ios.getloc().name() == std::string(LOCALE_en_US_UTF_8)); |
| 82 | assert(f1_called); |
| 83 | assert(f2_called); |
| 84 | assert(f3_called); |
| 85 | } |
| 86 | f1_called = false; |
| 87 | f2_called = false; |
| 88 | f3_called = false; |
| 89 | { |
| 90 | testbuf sb; |
| 91 | std::ios ios(&sb); |
| 92 | ios.register_callback(fn: f1, index: 4); |
| 93 | ios.register_callback(fn: f2, index: 5); |
| 94 | ios.register_callback(fn: f3, index: 6); |
| 95 | std::locale l = ios.imbue(std::locale(LOCALE_en_US_UTF_8)); |
| 96 | assert(l.name() == std::string("C" )); |
| 97 | assert(ios.getloc().name() == std::string(LOCALE_en_US_UTF_8)); |
| 98 | assert(sb.getloc().name() == std::string(LOCALE_en_US_UTF_8)); |
| 99 | assert(f1_called); |
| 100 | assert(f2_called); |
| 101 | assert(f3_called); |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |