1// { dg-require-namedlocale "" }
2// { dg-require-namedlocale "en_US" }
3
4// Copyright (C) 2003-2015 Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING3.  If not see
19// <http://www.gnu.org/licenses/>.
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
29int times_to_fail = 0;
30
31void* 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
42void deallocate(void* p)
43{
44  if (p)
45    std::free(p);
46}
47
48void* 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
56void* 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
64void operator delete(void* p) throw()
65{
66  deallocate(p);
67}
68
69void operator delete[](void* p) throw()
70{
71  deallocate(p);
72}
73
74void* operator new(std::size_t n, const std::nothrow_t&) throw()
75{
76  return allocate(n);
77}
78
79void* operator new[](std::size_t n, const std::nothrow_t&) throw()
80{
81  return allocate(n);
82}
83
84void operator delete(void* p, const std::nothrow_t&) throw()
85{
86  deallocate(p);
87}
88
89void operator delete[](void* p, const std::nothrow_t&) throw()
90{
91  deallocate(p);
92}
93
94// libstdc++/12352
95void 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	      std::locale loc3 = std::locale("en_US");
110	      std::locale loc4(loc3, std::locale::classic(),
111			       std::locale::numeric);
112	    }
113	  catch (std::exception&)
114	    {
115	    }
116	}
117    }
118}
119
120int main(int argc, char* argv[])
121{
122  int iters = 1;
123  if (argc > 1)
124    iters = std::atoi(argv[1]);
125  if (iters < 1)
126    iters = 1;
127  test01(iters);
128
129  return 0;
130}
131