OSDN Git Service

2005-05-18 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / facet / 2.cc
1 // { dg-require-namedlocale "" }
2
3 // 2000-08-31 Benjamin Kosnik <bkoz@redhat.com>
4
5 // Copyright (C) 2000, 2002, 2003, 2005 Free Software Foundation
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
22
23 // 22.1.1.1.2 - class locale::facet [lib.locale.facet]
24
25 #include <cwchar> // for mbstate_t
26 #include <locale>
27 #include <stdexcept>
28 #include <string>
29 #include <iterator>
30 #include <limits>
31 #include <testsuite_hooks.h>
32
33 // Static counter for use in checking ctors/dtors.
34 static std::size_t counter;
35
36 class surf : public std::locale::facet
37 {
38 public:
39   static std::locale::id                id;
40   surf(size_t refs = 0): std::locale::facet(refs) { ++counter; }
41   ~surf() { --counter; }
42 };
43
44 std::locale::id surf::id;
45
46 typedef surf facet_type;
47
48 void test02()
49 {
50   using namespace std;
51   bool test __attribute__((unused)) = true;
52
53   // 1: Destroyed when out of scope.
54   VERIFY( counter == 0 );
55   {
56     locale loc01(locale::classic(), new facet_type);
57     VERIFY( counter == 1 );
58   }
59   VERIFY( counter == 0 );
60
61   // 2: Not destroyed when out of scope, deliberately leaked.
62   VERIFY( counter == 0 );
63   {
64     // Default refs argument is zero.
65     locale loc02(locale::classic(), new facet_type(1));
66     VERIFY( counter == 1 );
67   }
68   VERIFY( counter == 1 );
69
70   // 3: Pathological.
71   counter = 0;
72   {
73     // Test bounds.
74     facet_type* f = new facet_type(numeric_limits<size_t>::max());
75     VERIFY( counter == 1 );
76     // Add a reference.
77     locale loc01(locale::classic(), f);
78     {
79       // Add another reference...
80       locale loc02(locale::classic(), f);
81     }
82     VERIFY( counter == 1 );
83   }
84
85   // 4: Named locale should destroy facets when it goes out of scope.
86   // Not quite sure how to test for this w/o valgrind at the moment.
87   {
88     locale loc03 = locale("es_MX");
89   }
90 }
91
92 int main ()
93 {
94   test02();
95   return 0;
96 }