• 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 // template <class Facet> locale combine(const locale& other) const;
13 
14 #include <locale>
15 #include <stdexcept>
16 #include <cassert>
17 
18 #include "count_new.hpp"
19 
20 #include "test_macros.h"
21 
check(const std::locale & loc)22 void check(const std::locale& loc)
23 {
24     assert(std::has_facet<std::collate<char> >(loc));
25     assert(std::has_facet<std::collate<wchar_t> >(loc));
26 
27     assert(std::has_facet<std::ctype<char> >(loc));
28     assert(std::has_facet<std::ctype<wchar_t> >(loc));
29     assert((std::has_facet<std::codecvt<char, char, std::mbstate_t> >(loc)));
30     assert((std::has_facet<std::codecvt<char16_t, char, std::mbstate_t> >(loc)));
31     assert((std::has_facet<std::codecvt<char32_t, char, std::mbstate_t> >(loc)));
32     assert((std::has_facet<std::codecvt<wchar_t, char, std::mbstate_t> >(loc)));
33 
34     assert((std::has_facet<std::moneypunct<char> >(loc)));
35     assert((std::has_facet<std::moneypunct<wchar_t> >(loc)));
36     assert((std::has_facet<std::money_get<char> >(loc)));
37     assert((std::has_facet<std::money_get<wchar_t> >(loc)));
38     assert((std::has_facet<std::money_put<char> >(loc)));
39     assert((std::has_facet<std::money_put<wchar_t> >(loc)));
40 
41     assert((std::has_facet<std::numpunct<char> >(loc)));
42     assert((std::has_facet<std::numpunct<wchar_t> >(loc)));
43     assert((std::has_facet<std::num_get<char> >(loc)));
44     assert((std::has_facet<std::num_get<wchar_t> >(loc)));
45     assert((std::has_facet<std::num_put<char> >(loc)));
46     assert((std::has_facet<std::num_put<wchar_t> >(loc)));
47 
48     assert((std::has_facet<std::time_get<char> >(loc)));
49     assert((std::has_facet<std::time_get<wchar_t> >(loc)));
50     assert((std::has_facet<std::time_put<char> >(loc)));
51     assert((std::has_facet<std::time_put<wchar_t> >(loc)));
52 
53     assert((std::has_facet<std::messages<char> >(loc)));
54     assert((std::has_facet<std::messages<wchar_t> >(loc)));
55 }
56 
57 struct my_facet
58     : public std::locale::facet
59 {
testmy_facet60     int test() const {return 5;}
61 
62     static std::locale::id id;
63 };
64 
65 std::locale::id my_facet::id;
66 
main()67 int main()
68 {
69 {
70     {
71         std::locale loc;
72         std::locale loc2(loc, new my_facet);
73         std::locale loc3 = loc.combine<my_facet>(loc2);
74         check(loc3);
75         assert(loc3.name() == "*");
76         assert((std::has_facet<my_facet>(loc3)));
77         const my_facet& f = std::use_facet<my_facet>(loc3);
78         assert(f.test() == 5);
79     }
80     assert(globalMemCounter.checkOutstandingNewEq(0));
81 }
82 #ifndef TEST_HAS_NO_EXCEPTIONS
83 {
84     {
85         std::locale loc;
86         std::locale loc2;
87         try
88         {
89             std::locale loc3 = loc.combine<my_facet>(loc2);
90             assert(false);
91         }
92         catch (std::runtime_error&)
93         {
94         }
95     }
96     assert(globalMemCounter.checkOutstandingNewEq(0));
97 }
98 #endif
99 }
100