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 // explicit locale(const string& std_name);
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 
main()69 int main()
70 {
71     {
72         std::locale loc(std::string(LOCALE_ru_RU_UTF_8));
73         check(loc);
74         std::locale loc2(std::string(LOCALE_ru_RU_UTF_8));
75         check(loc2);
76         assert(loc == loc2);
77         std::locale loc3(std::string(LOCALE_zh_CN_UTF_8));
78         check(loc3);
79         assert(!(loc == loc3));
80         assert(loc != loc3);
81     }
82     assert(new_called == 0);
83 }
84