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 // <locale>
11
12 // template <class Facet> locale(const locale& other, Facet* f);
13
14 #include <locale>
15 #include <new>
16 #include <cassert>
17
18 #include "platform_support.h" // locale name macros
19
20 int new_called = 0;
21
operator new(std::size_t s)22 void* operator new(std::size_t s) throw(std::bad_alloc)
23 {
24 ++new_called;
25 return std::malloc(s);
26 }
27
operator delete(void * p)28 void operator delete(void* p) throw()
29 {
30 --new_called;
31 std::free(p);
32 }
33
check(const std::locale & loc)34 void check(const std::locale& loc)
35 {
36 assert(std::has_facet<std::collate<char> >(loc));
37 assert(std::has_facet<std::collate<wchar_t> >(loc));
38
39 assert(std::has_facet<std::ctype<char> >(loc));
40 assert(std::has_facet<std::ctype<wchar_t> >(loc));
41 assert((std::has_facet<std::codecvt<char, char, std::mbstate_t> >(loc)));
42 assert((std::has_facet<std::codecvt<char16_t, char, std::mbstate_t> >(loc)));
43 assert((std::has_facet<std::codecvt<char32_t, char, std::mbstate_t> >(loc)));
44 assert((std::has_facet<std::codecvt<wchar_t, char, std::mbstate_t> >(loc)));
45
46 assert((std::has_facet<std::moneypunct<char> >(loc)));
47 assert((std::has_facet<std::moneypunct<wchar_t> >(loc)));
48 assert((std::has_facet<std::money_get<char> >(loc)));
49 assert((std::has_facet<std::money_get<wchar_t> >(loc)));
50 assert((std::has_facet<std::money_put<char> >(loc)));
51 assert((std::has_facet<std::money_put<wchar_t> >(loc)));
52
53 assert((std::has_facet<std::numpunct<char> >(loc)));
54 assert((std::has_facet<std::numpunct<wchar_t> >(loc)));
55 assert((std::has_facet<std::num_get<char> >(loc)));
56 assert((std::has_facet<std::num_get<wchar_t> >(loc)));
57 assert((std::has_facet<std::num_put<char> >(loc)));
58 assert((std::has_facet<std::num_put<wchar_t> >(loc)));
59
60 assert((std::has_facet<std::time_get<char> >(loc)));
61 assert((std::has_facet<std::time_get<wchar_t> >(loc)));
62 assert((std::has_facet<std::time_put<char> >(loc)));
63 assert((std::has_facet<std::time_put<wchar_t> >(loc)));
64
65 assert((std::has_facet<std::messages<char> >(loc)));
66 assert((std::has_facet<std::messages<wchar_t> >(loc)));
67 }
68
69 struct my_facet
70 : public std::locale::facet
71 {
testmy_facet72 int test() const {return 5;}
73
74 static std::locale::id id;
75 };
76
77 std::locale::id my_facet::id;
78
main()79 int main()
80 {
81 {
82 {
83 std::locale loc(LOCALE_ru_RU_UTF_8);
84 check(loc);
85 std::locale loc2(loc, new my_facet);
86 check(loc2);
87 assert((std::has_facet<my_facet>(loc2)));
88 const my_facet& f = std::use_facet<my_facet>(loc2);
89 assert(f.test() == 5);
90 }
91 assert(new_called == 0);
92 }
93 {
94 {
95 std::locale loc;
96 check(loc);
97 std::locale loc2(loc, (std::ctype<char>*)0);
98 check(loc2);
99 assert(loc == loc2);
100 }
101 assert(new_called == 0);
102 }
103 }
104