OSDN Git Service

2005-05-18 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / locale / cons / 12352.cc
1 // { dg-require-namedlocale "" }
2
3 // Copyright (C) 2003, 2005 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.
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 COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
22
23 #include <new>
24 #include <locale>
25 #include <cstdlib>
26 #include <cstring>
27 #include <testsuite_hooks.h>
28
29 int times_to_fail = 0;
30
31 void* allocate(std::size_t n)
32 {
33   if (!times_to_fail--)
34     return 0;
35
36   void* ret = std::malloc(n ? n : 1);
37   if (ret)
38     std::memset(ret, 0xbc, n);
39   return ret;
40 }
41
42 void deallocate(void* p)
43 {
44   if (p)
45     std::free(p);
46 }
47
48 void* operator new(std::size_t n) throw (std::bad_alloc)
49 {
50   void* ret = allocate(n);
51   if (!ret)
52     throw std::bad_alloc();
53   return ret;
54 }
55
56 void* operator new[](std::size_t n) throw (std::bad_alloc)
57 {
58   void* ret = allocate(n);
59   if (!ret)
60     throw std::bad_alloc();
61   return ret;
62 }
63
64 void operator delete(void* p) throw()
65 {
66   deallocate(p);
67 }
68
69 void operator delete[](void* p) throw()
70 {
71   deallocate(p);
72 }
73
74 void* operator new(std::size_t n, const std::nothrow_t&) throw()
75 {
76   return allocate(n);
77 }
78
79 void* operator new[](std::size_t n, const std::nothrow_t&) throw()
80 {
81   return allocate(n);
82 }
83
84 void operator delete(void* p, const std::nothrow_t&) throw()
85 {
86   deallocate(p);
87 }
88
89 void operator delete[](void* p, const std::nothrow_t&) throw()
90 {
91   deallocate(p);
92 }
93
94 // libstdc++/12352
95 void test01(int iters)
96 {
97   bool test __attribute__((unused)) = true;
98
99   for (int j = 0; j < iters; ++j)
100     {
101       for (int i = 0; i < 100; ++i)
102         {
103           times_to_fail = i;
104           try
105             {
106               std::locale loc1 = std::locale("");
107               std::locale loc2(loc1, std::locale::classic(),
108                                std::locale::numeric);
109             }
110           catch (std::exception&)
111             {
112             }
113         }
114     }
115 }
116
117 int main(int argc, char* argv[])
118 {
119   int iters = 1;
120   if (argc > 1)
121     iters = std::atoi(argv[1]);
122   if (iters < 1)
123     iters = 1;
124   test01(iters);
125
126   return 0;
127 }