]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/22_locale/global_templates/user_facet_hierarchies.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / global_templates / user_facet_hierarchies.cc
CommitLineData
a5544970 1// Copyright (C) 2007-2019 Free Software Foundation, Inc.
c5f416ab
BK
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
748086b7 6// Free Software Foundation; either version 3, or (at your option)
c5f416ab
BK
7// any later version.
8
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.
13
14// You should have received a copy of the GNU General Public License along
748086b7
JJ
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
c5f416ab
BK
17
18#include <string>
19#include <locale>
c5f416ab
BK
20#include <testsuite_hooks.h>
21
22// Based on Langer Kreft "Standard C++ IOStreams and Locales" p 316-318
23struct base_facet: public std::locale::facet
24{
25 virtual std::string msg() const
26 { return "base class"; }
27
28 static std::locale::id id;
29};
30
31std::locale::id base_facet::id;
32
33
34struct derived_facet: public base_facet
35{
36 virtual std::string msg() const
37 { return "derived class"; }
38
39 virtual std::string msg_repeater() const
40 { return "derived class derived class"; }
41
42};
43
44// PR libstdc++/30127
45// PR libstdc++/34449
46int main()
47{
c5f416ab
BK
48 using std::locale;
49 using std::has_facet;
50 using std::use_facet;
51
52 locale loc_c = locale::classic();
53 locale loc_base(loc_c, new base_facet);
54 locale loc_derived(loc_c, new derived_facet);
55
c5f416ab
BK
56 // Standard facets.
57 VERIFY( has_facet<std::ctype<char> >(loc_c) );
58 VERIFY( has_facet<std::ctype<char> >(loc_base) );
59 VERIFY( has_facet<std::ctype<char> >(loc_derived) );
60
61 // User defined base facet.
62 VERIFY( !has_facet<base_facet>(loc_c) );
63 VERIFY( has_facet<base_facet>(loc_base) );
64 VERIFY( has_facet<base_facet>(loc_derived) );
65
66 // User defined derived facet.
67 VERIFY( !has_facet<derived_facet>(loc_c) );
68 VERIFY( !has_facet<derived_facet>(loc_base) );
69 VERIFY( has_facet<derived_facet>(loc_derived) );
70
71
72 // 1
73 try
74 {
75 if (has_facet<derived_facet>(loc_base))
76 {
77 use_facet<derived_facet>(loc_base).msg_repeater();
78 VERIFY( false );
79 }
80 }
81 catch (...)
82 {
83 // Expect no exception.
84 VERIFY( true );
85 }
86
87 // 2
88 try
89 {
90 if (has_facet<base_facet>(loc_derived))
91 use_facet<base_facet>(loc_derived).msg();
92 else
93 VERIFY( true );
94 }
95 catch (...)
96 {
97 // Expect no exception.
98 VERIFY( true );
99 }
100
101 return 0;
102}