1// 2000-09-13 Benjamin Kosnik <bkoz@redhat.com> 2 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. 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19// USA. 20 21// 22.1.1.5 locale static members [lib.locale.statics] 22 23#include <cwchar> // for mbstate_t 24#include <locale> 25#include <testsuite_hooks.h> 26 27// Static counter for use in checking ctors/dtors. 28static std::size_t counter; 29 30class surf : public std::locale::facet 31{ 32public: 33 static std::locale::id id; 34 surf(size_t refs = 0): std::locale::facet(refs) { ++counter; } 35 ~surf() { --counter; } 36}; 37 38std::locale::id surf::id; 39 40typedef surf facet_type; 41 42// Verify lifetimes of global objects. 43void test03() 44{ 45 using namespace std; 46 bool test __attribute__((unused)) = true; 47 48 string name; 49 locale global_orig; 50 // 1: Destroyed when out of scope. 51 { 52 { 53 { 54 VERIFY( counter == 0 ); 55 { 56 locale loc01(locale::classic(), new facet_type); 57 VERIFY( counter == 1 ); 58 global_orig = locale::global(loc01); 59 name = loc01.name(); 60 } 61 VERIFY( counter == 1 ); 62 locale loc02 = locale(); 63 // Weak, but it's something... 64 VERIFY( loc02.name() == name ); 65 } 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 ); 71 } 72 VERIFY( counter == 0 ); 73 locale loc04 = locale(); 74 VERIFY( loc04 == global_orig ); 75 } 76 77 // 2: Not destroyed when out of scope, deliberately leaked. 78 { 79 { 80 { 81 VERIFY( counter == 0 ); 82 { 83 locale loc01(locale::classic(), new facet_type(1)); 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 105 // Restore global settings. 106 locale::global(global_orig); 107} 108 109int main () 110{ 111 test03(); 112 return 0; 113} 114