]>
Commit | Line | Data |
---|---|---|
8a17a7b4 BK |
1 | // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com> |
2 | ||
a5544970 | 3 | // Copyright (C) 2000-2019 Free Software Foundation, Inc. |
8a17a7b4 BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
8a17a7b4 BK |
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 | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
8a17a7b4 BK |
19 | |
20 | // 22.1.1.5 locale static members [lib.locale.statics] | |
21 | ||
4bc95009 | 22 | #include <cwchar> // for mbstate_t |
8a17a7b4 | 23 | #include <locale> |
402a402c | 24 | #include <testsuite_hooks.h> |
8a17a7b4 | 25 | |
402a402c BK |
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; | |
402a402c BK |
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 | ||
22620c14 JW |
75 | // 2: Not destroyed when out of scope, deliberately "leaked". |
76 | const facet_type* ptr = 0; | |
402a402c BK |
77 | { |
78 | { | |
79 | { | |
80 | VERIFY( counter == 0 ); | |
81 | { | |
22620c14 JW |
82 | ptr = new facet_type(1); |
83 | locale loc01(locale::classic(), ptr); | |
402a402c BK |
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 | ||
22620c14 JW |
105 | // Clean up. |
106 | delete ptr; | |
107 | ||
402a402c BK |
108 | // Restore global settings. |
109 | locale::global(global_orig); | |
8a17a7b4 BK |
110 | } |
111 | ||
112 | int main () | |
113 | { | |
402a402c | 114 | test03(); |
8a17a7b4 BK |
115 | return 0; |
116 | } |