• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // locale() throw();
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     int ok;
72     {
73         std::locale loc;
74         assert(new_called == 0);
75         assert(loc.name() == "C");
76         assert(new_called == 0);
77         check(loc);
78         assert(new_called == 0);
79         assert(std::locale::global(std::locale(LOCALE_en_US_UTF_8)) == loc);
80         ok = new_called;
81         std::locale loc2;
82         assert(new_called == ok);
83         check(loc2);
84         assert(new_called == ok);
85         assert(loc2 == std::locale(LOCALE_en_US_UTF_8));
86         assert(new_called == ok);
87     }
88     assert(new_called == ok);
89 }
90