xref: /llvm-project/libcxx/test/std/localization/locales/locale/locale.members/combine.pass.cpp (revision f0fc8c4878ed3ed60bd7536f97ef2673eb691ae7)
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 // XFAIL: availability-char8_t_support-missing
10 
11 // This test runs in C++20, but we have deprecated codecvt<char(16|32), char, mbstate_t> in C++20.
12 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
13 
14 // <locale>
15 
16 // template <class Facet> locale combine(const locale& other) const;
17 
18 #include <locale>
19 #include <stdexcept>
20 #include <cassert>
21 
22 #include "count_new.h"
23 
24 #include "test_macros.h"
25 
26 template<class CharT>
check_for(const std::locale & loc)27 void check_for(const std::locale& loc)
28 {
29     assert(std::has_facet<std::collate<CharT> >(loc));
30 
31     assert(std::has_facet<std::ctype<CharT> >(loc));
32 
33     assert((std::has_facet<std::codecvt<CharT, char, std::mbstate_t> >(loc)));
34 
35     assert(std::has_facet<std::moneypunct<CharT> >(loc));
36     assert(std::has_facet<std::money_get<CharT> >(loc));
37     assert(std::has_facet<std::money_put<CharT> >(loc));
38 
39     assert(std::has_facet<std::numpunct<CharT> >(loc));
40     assert(std::has_facet<std::num_get<CharT> >(loc));
41     assert(std::has_facet<std::num_put<CharT> >(loc));
42 
43     assert(std::has_facet<std::time_get<CharT> >(loc));
44     assert(std::has_facet<std::time_put<CharT> >(loc));
45 
46     assert(std::has_facet<std::messages<CharT> >(loc));
47 }
48 
check(const std::locale & loc)49 void check(const std::locale& loc)
50 {
51     check_for<char>(loc);
52 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
53     check_for<wchar_t>(loc);
54 #endif
55 
56     assert((std::has_facet<std::codecvt<char16_t, char, std::mbstate_t> >(loc)));
57     assert((std::has_facet<std::codecvt<char32_t, char, std::mbstate_t> >(loc)));
58 #if TEST_STD_VER > 17
59     assert((std::has_facet<std::codecvt<char16_t, char8_t, std::mbstate_t> >(loc)));
60     assert((std::has_facet<std::codecvt<char32_t, char8_t, std::mbstate_t> >(loc)));
61 #endif
62 }
63 
64 struct my_facet
65     : public std::locale::facet
66 {
testmy_facet67     int test() const {return 5;}
68 
69     static std::locale::id id;
70 };
71 
72 std::locale::id my_facet::id;
73 
main(int,char **)74 int main(int, char**)
75 {
76 {
77     globalMemCounter.reset();
78     {
79         std::locale loc;
80         std::locale loc2(loc, new my_facet);
81         std::locale loc3 = loc.combine<my_facet>(loc2);
82         check(loc3);
83         assert(loc3.name() == "*");
84         assert((std::has_facet<my_facet>(loc3)));
85         const my_facet& f = std::use_facet<my_facet>(loc3);
86         assert(f.test() == 5);
87     }
88     assert(globalMemCounter.checkOutstandingNewEq(0));
89 }
90 #ifndef TEST_HAS_NO_EXCEPTIONS
91 {
92     {
93         std::locale loc;
94         std::locale loc2;
95         try
96         {
97             std::locale loc3 = loc.combine<my_facet>(loc2);
98             assert(false);
99         }
100         catch (std::runtime_error&)
101         {
102         }
103     }
104     assert(globalMemCounter.checkOutstandingNewEq(0));
105 }
106 #endif
107 
108   return 0;
109 }
110