2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / locale / global_locale_objects / 3.cc
blob5e3ecedee42501c6fb08a59e66811006847b0d23
1 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2000, 2002, 2003 Free Software Foundation
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 2, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // 22.1.1.5 locale static members [lib.locale.statics]
23 #include <cwchar> // for mbstate_t
24 #include <locale>
25 #include <testsuite_hooks.h>
27 // Static counter for use in checking ctors/dtors.
28 static std::size_t counter;
30 class surf : public std::locale::facet
32 public:
33 static std::locale::id id;
34 surf(size_t refs = 0): std::locale::facet(refs) { ++counter; }
35 ~surf() { --counter; }
38 std::locale::id surf::id;
40 typedef surf facet_type;
42 // Verify lifetimes of global objects.
43 void test03()
45 using namespace std;
46 bool test __attribute__((unused)) = true;
48 string name;
49 locale global_orig;
50 // 1: Destroyed when out of scope.
54 VERIFY( counter == 0 );
56 locale loc01(locale::classic(), new facet_type);
57 VERIFY( counter == 1 );
58 global_orig = locale::global(loc01);
59 name = loc01.name();
61 VERIFY( counter == 1 );
62 locale loc02 = locale();
63 // Weak, but it's something...
64 VERIFY( loc02.name() == name );
66 VERIFY( counter == 1 );
67 // NB: loc03 should be a copy of the previous global locale.
68 locale loc03 = locale::global(global_orig);
69 VERIFY( counter == 1 );
70 VERIFY( loc03.name() == name );
72 VERIFY( counter == 0 );
73 locale loc04 = locale();
74 VERIFY( loc04 == global_orig );
77 // 2: Not destroyed when out of scope, deliberately leaked.
81 VERIFY( counter == 0 );
83 locale loc01(locale::classic(), new facet_type(1));
84 VERIFY( counter == 1 );
85 global_orig = locale::global(loc01);
86 name = loc01.name();
88 VERIFY( counter == 1 );
89 locale loc02 = locale();
90 // Weak, but it's something...
91 VERIFY( loc02.name() == name );
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 );
99 VERIFY( counter == 1 );
100 locale loc04 = locale();
101 VERIFY( loc04 == global_orig );
103 VERIFY( counter == 1 );
105 // Restore global settings.
106 locale::global(global_orig);
109 int main ()
111 test03();
112 return 0;