Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / global_templates / user_facet_hierarchies.cc
blob94c41771502932aea101dde4f1a4665e686b3920
1 // Copyright (C) 2007, 2008 Free Software Foundation
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
19 #include <string>
20 #include <locale>
21 #include <testsuite_hooks.h>
23 // Based on Langer Kreft "Standard C++ IOStreams and Locales" p 316-318
24 struct base_facet: public std::locale::facet
26 virtual std::string msg() const
27 { return "base class"; }
29 static std::locale::id id;
32 std::locale::id base_facet::id;
35 struct derived_facet: public base_facet
37 virtual std::string msg() const
38 { return "derived class"; }
40 virtual std::string msg_repeater() const
41 { return "derived class derived class"; }
45 // PR libstdc++/30127
46 // PR libstdc++/34449
47 int main()
49 bool test __attribute__((unused)) = true;
51 using std::locale;
52 using std::has_facet;
53 using std::use_facet;
55 locale loc_c = locale::classic();
56 locale loc_base(loc_c, new base_facet);
57 locale loc_derived(loc_c, new derived_facet);
59 // Standard facets.
60 VERIFY( has_facet<std::ctype<char> >(loc_c) );
61 VERIFY( has_facet<std::ctype<char> >(loc_base) );
62 VERIFY( has_facet<std::ctype<char> >(loc_derived) );
64 // User defined base facet.
65 VERIFY( !has_facet<base_facet>(loc_c) );
66 VERIFY( has_facet<base_facet>(loc_base) );
67 VERIFY( has_facet<base_facet>(loc_derived) );
69 // User defined derived facet.
70 VERIFY( !has_facet<derived_facet>(loc_c) );
71 VERIFY( !has_facet<derived_facet>(loc_base) );
72 VERIFY( has_facet<derived_facet>(loc_derived) );
75 // 1
76 try
78 if (has_facet<derived_facet>(loc_base))
80 use_facet<derived_facet>(loc_base).msg_repeater();
81 VERIFY( false );
84 catch (...)
86 // Expect no exception.
87 VERIFY( true );
90 // 2
91 try
93 if (has_facet<base_facet>(loc_derived))
94 use_facet<base_facet>(loc_derived).msg();
95 else
96 VERIFY( true );
98 catch (...)
100 // Expect no exception.
101 VERIFY( true );
104 return 0;