Debian Codegraph @

libstdc++-v3/testsuite/22_locale/locale/global_locale_objects/3.cc

raw symbols SCIP
1 // 2000-09-13 Benjamin Kosnik <[email protected]>
2
3 // Copyright (C) 2000-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 22.1.1.5 locale static members [lib.locale.statics]
21
22 #include <cwchar> // for mbstate_t
23 #include <locale>
24 #include <testsuite_hooks.h>
25
26 // Static counter for use in checking ctors/dtors.
27 static std::size_t counter;
28
29 class surf : public std::locale::facet
30 {
31 public:
32 static std::locale::id id;
33 surf(size_t refs = 0): std::locale::facet(refs) { ++counter; }
34 ~surf() { --counter; }
35 };
36
37 std::locale::id surf::id;
38
39 typedef surf facet_type;
40
41 // Verify lifetimes of global objects.
42 void test03()
43 {
44 using namespace std;
45
46 string name;
47 locale global_orig;
48 // 1: Destroyed when out of scope.
49 {
50 {
51 {
52 VERIFY( counter == 0 );
53 {
54 locale loc01(locale::classic(), new facet_type);
55 VERIFY( counter == 1 );
56 global_orig = locale::global(loc01);
57 name = loc01.name();
58 }
59 VERIFY( counter == 1 );
60 locale loc02 = locale();
61 // Weak, but it's something...
62 VERIFY( loc02.name() == name );
63 }
64 VERIFY( counter == 1 );
65 // NB: loc03 should be a copy of the previous global locale.
66 locale loc03 = locale::global(global_orig);
67 VERIFY( counter == 1 );
68 VERIFY( loc03.name() == name );
69 }
70 VERIFY( counter == 0 );
71 locale loc04 = locale();
72 VERIFY( loc04 == global_orig );
73 }
74
75 // 2: Not destroyed when out of scope, deliberately "leaked".
76 const facet_type* ptr = 0;
77 {
78 {
79 {
80 VERIFY( counter == 0 );
81 {
82 ptr = new facet_type(1);
83 locale loc01(locale::classic(), ptr);
84 VERIFY( counter == 1 );
85 global_orig = locale::global(loc01);
86 name = loc01.name();
87 }
88 VERIFY( counter == 1 );
89 locale loc02 = locale();
90 // Weak, but it's something...
91 VERIFY( loc02.name() == name );
92 }
93 VERIFY( counter == 1 );
94 // NB: loc03 should be a copy of the previous global locale.
95 locale loc03 = locale::global(global_orig);
96 VERIFY( counter == 1 );
97 VERIFY( loc03.name() == name );
98 }
99 VERIFY( counter == 1 );
100 locale loc04 = locale();
101 VERIFY( loc04 == global_orig );
102 }
103 VERIFY( counter == 1 );
104
105 // Clean up.
106 delete ptr;
107
108 // Restore global settings.
109 locale::global(global_orig);
110 }
111
112 int main ()
113 {
114 test03();
115 return 0;
116 }