1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // REQUIRES: locale.ru_RU.UTF-8
11 // UNSUPPORTED: sanitizer-new-delete
12 
13 // <locale>
14 
15 // template <class Facet> locale(const locale& other, Facet* f);
16 
17 #include <locale>
18 #include <new>
19 #include <cassert>
20 
21 #include "platform_support.h" // locale name macros
22 
23 int new_called = 0;
24 
operator new(std::size_t s)25 void* operator new(std::size_t s) throw(std::bad_alloc)
26 {
27     ++new_called;
28     return std::malloc(s);
29 }
30 
operator delete(void * p)31 void  operator delete(void* p) throw()
32 {
33     --new_called;
34     std::free(p);
35 }
36 
check(const std::locale & loc)37 void check(const std::locale& loc)
38 {
39     assert(std::has_facet<std::collate<char> >(loc));
40     assert(std::has_facet<std::collate<wchar_t> >(loc));
41 
42     assert(std::has_facet<std::ctype<char> >(loc));
43     assert(std::has_facet<std::ctype<wchar_t> >(loc));
44     assert((std::has_facet<std::codecvt<char, char, std::mbstate_t> >(loc)));
45     assert((std::has_facet<std::codecvt<char16_t, char, std::mbstate_t> >(loc)));
46     assert((std::has_facet<std::codecvt<char32_t, char, std::mbstate_t> >(loc)));
47     assert((std::has_facet<std::codecvt<wchar_t, char, std::mbstate_t> >(loc)));
48 
49     assert((std::has_facet<std::moneypunct<char> >(loc)));
50     assert((std::has_facet<std::moneypunct<wchar_t> >(loc)));
51     assert((std::has_facet<std::money_get<char> >(loc)));
52     assert((std::has_facet<std::money_get<wchar_t> >(loc)));
53     assert((std::has_facet<std::money_put<char> >(loc)));
54     assert((std::has_facet<std::money_put<wchar_t> >(loc)));
55 
56     assert((std::has_facet<std::numpunct<char> >(loc)));
57     assert((std::has_facet<std::numpunct<wchar_t> >(loc)));
58     assert((std::has_facet<std::num_get<char> >(loc)));
59     assert((std::has_facet<std::num_get<wchar_t> >(loc)));
60     assert((std::has_facet<std::num_put<char> >(loc)));
61     assert((std::has_facet<std::num_put<wchar_t> >(loc)));
62 
63     assert((std::has_facet<std::time_get<char> >(loc)));
64     assert((std::has_facet<std::time_get<wchar_t> >(loc)));
65     assert((std::has_facet<std::time_put<char> >(loc)));
66     assert((std::has_facet<std::time_put<wchar_t> >(loc)));
67 
68     assert((std::has_facet<std::messages<char> >(loc)));
69     assert((std::has_facet<std::messages<wchar_t> >(loc)));
70 }
71 
72 struct my_facet
73     : public std::locale::facet
74 {
testmy_facet75     int test() const {return 5;}
76 
77     static std::locale::id id;
78 };
79 
80 std::locale::id my_facet::id;
81 
main()82 int main()
83 {
84 {
85     {
86         std::locale loc(LOCALE_ru_RU_UTF_8);
87         check(loc);
88         std::locale loc2(loc, new my_facet);
89         check(loc2);
90         assert((std::has_facet<my_facet>(loc2)));
91         const my_facet& f = std::use_facet<my_facet>(loc2);
92         assert(f.test() == 5);
93     }
94     assert(new_called == 0);
95 }
96 {
97     {
98         std::locale loc;
99         check(loc);
100         std::locale loc2(loc, (std::ctype<char>*)0);
101         check(loc2);
102         assert(loc == loc2);
103     }
104     assert(new_called == 0);
105 }
106 }
107