[v3] add locale::global reference checks to testsuite

Benjamin Kosnik bkoz@redhat.com
Wed Oct 9 22:15:00 GMT 2002


Verify correct behavior...... everything seems cool now.

2002-10-09  Benjamin Kosnik  <bkoz@redhat.com>	

	* src/locale.cc: Fix comments, move ctors together.
	* testsuite/22_locale/static_members.cc (test03): Add.

Index: src/locale.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/src/locale.cc,v
retrieving revision 1.66
diff -c -p -r1.66 locale.cc
*** src/locale.cc	8 Oct 2002 23:32:23 -0000	1.66
--- src/locale.cc	10 Oct 2002 05:12:57 -0000
*************** namespace std 
*** 148,172 ****
      0
    };
  
-   locale::~locale() throw()
-   { _M_impl->_M_remove_reference(); }
- 
-   void
-   locale::_M_coalesce(const locale& __base, const locale& __add, 
- 		      category __cat)
-   {
-     __cat = _S_normalize_category(__cat);  
-     _M_impl = new _Impl(*__base._M_impl, 1);  
- 
-     try 
-       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
-     catch (...) 
-       { 
- 	_M_impl->_M_remove_reference(); 
- 	__throw_exception_again;
-       }
-   }
- 
    locale::locale() throw()
    { 
      _S_initialize(); 
--- 148,153 ----
*************** namespace std 
*** 178,183 ****
--- 159,165 ----
  
    // This is used to initialize global and classic locales, and
    // assumes that the _Impl objects are constructed correctly.
+   // The lack of a reference increment is intentional.
    locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
    { }
  
*************** namespace std 
*** 309,314 ****
--- 291,299 ----
    locale::locale(const locale& __base, const locale& __add, category __cat)
    { _M_coalesce(__base, __add, __cat); }
  
+   locale::~locale() throw()
+   { _M_impl->_M_remove_reference(); }
+ 
    bool
    locale::operator==(const locale& __rhs) const throw()
    {
*************** namespace std 
*** 381,387 ****
  	try 
  	  {
  	    // 26 Standard facets, 2 references.
! 	    // One reference for _M_classic, one for _M_global
  	    _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
  	    _S_global = _S_classic; 	    
  	    new (&c_locale) locale(_S_classic);
--- 366,372 ----
  	try 
  	  {
  	    // 26 Standard facets, 2 references.
! 	    // One reference for _S_classic, one for _S_global
  	    _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
  	    _S_global = _S_classic; 	    
  	    new (&c_locale) locale(_S_classic);
*************** namespace std 
*** 397,402 ****
--- 382,403 ----
  	  }
        }
      return c_locale;
+   }
+ 
+   void
+   locale::_M_coalesce(const locale& __base, const locale& __add, 
+ 		      category __cat)
+   {
+     __cat = _S_normalize_category(__cat);  
+     _M_impl = new _Impl(*__base._M_impl, 1);  
+ 
+     try 
+       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
+     catch (...) 
+       { 
+ 	_M_impl->_M_remove_reference(); 
+ 	__throw_exception_again;
+       }
    }
  
    locale::category
Index: testsuite/22_locale/static_members.cc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/testsuite/22_locale/static_members.cc,v
retrieving revision 1.4
diff -c -p -r1.4 static_members.cc
*** testsuite/22_locale/static_members.cc	8 Oct 2002 23:32:23 -0000	1.4
--- testsuite/22_locale/static_members.cc	10 Oct 2002 05:12:57 -0000
***************
*** 23,30 ****
  #include <cwchar> // for mbstate_t
  #include <locale>
  #include <iostream>
! //#include <testsuite_hooks.h>
! #define VERIFY(x) test &= x
  
  typedef std::codecvt<char, char, std::mbstate_t> ccodecvt;
  class gnu_codecvt: public ccodecvt { }; 
--- 23,29 ----
  #include <cwchar> // for mbstate_t
  #include <locale>
  #include <iostream>
! #include <testsuite_hooks.h>
  
  typedef std::codecvt<char, char, std::mbstate_t> ccodecvt;
  class gnu_codecvt: public ccodecvt { }; 
*************** class gnu_codecvt: public ccodecvt { }; 
*** 32,43 ****
  void test01()
  {
    using namespace std;
- 
    bool test = true;
    string str1, str2;
  
    // Construct a locale object with the C facet.
-   const locale loc_env("");
    const locale loc01 = locale::classic();
  
    // Construct a locale object with the specialized facet.
--- 31,41 ----
  void test01()
  {
    using namespace std;
    bool test = true;
+ 
    string str1, str2;
  
    // Construct a locale object with the C facet.
    const locale loc01 = locale::classic();
  
    // Construct a locale object with the specialized facet.
*************** void test01()
*** 54,66 ****
    // global
    locale loc03;
    VERIFY ( loc03 == loc01);
!   locale loc04 = locale::global(loc02);
    locale loc05;
    VERIFY (loc05 != loc03);
    VERIFY (loc05 == loc02);
  
!   // Reset global locale.
!   locale::global(loc_env);
  }
  
  // Sanity check locale::global(loc) and setlocale.
--- 52,64 ----
    // global
    locale loc03;
    VERIFY ( loc03 == loc01);
!   locale global_orig = locale::global(loc02);
    locale loc05;
    VERIFY (loc05 != loc03);
    VERIFY (loc05 == loc02);
  
!   // Reset global settings.
!   locale::global(global_orig);
  }
  
  // Sanity check locale::global(loc) and setlocale.
*************** void test02()
*** 77,96 ****
  
    // Get underlying current locale and environment settings.
    const string lc_all_orig = std::setlocale(LC_ALL, NULL);
!   const locale loc_orig("");
  
    // setlocale to en_PH
    string lc_all_ph = std::setlocale(LC_ALL, ph.c_str());
  
    const locale loc_env("");
!   VERIFY( loc_env == loc_orig );
  
!   locale::global(loc_mx);
    string lc_all_mx = std::setlocale(LC_ALL, NULL);
    VERIFY( lc_all_mx == mx.c_str() );
  
!   // Restore global info.
!   locale::global(loc_orig);
  }
  
  int main ()
--- 75,176 ----
  
    // Get underlying current locale and environment settings.
    const string lc_all_orig = std::setlocale(LC_ALL, NULL);
!   const locale env_orig("");
  
    // setlocale to en_PH
    string lc_all_ph = std::setlocale(LC_ALL, ph.c_str());
  
    const locale loc_env("");
!   VERIFY( loc_env == env_orig );
  
!   locale global_orig = locale::global(loc_mx);
    string lc_all_mx = std::setlocale(LC_ALL, NULL);
    VERIFY( lc_all_mx == mx.c_str() );
  
!   // Restore global settings.
!   locale::global(global_orig);
! }
! 
! // Static counter for use in checking ctors/dtors.
! static std::size_t counter;
! 
! class surf : public std::locale::facet
! {
! public:
!   static std::locale::id 	       	id;
!   surf(size_t refs = 0): std::locale::facet(refs) { ++counter; }
!   ~surf() { --counter; }
! };
! 
! std::locale::id surf::id;
! 
! typedef surf facet_type;
! 
! // Verify lifetimes of global objects.
! void test03()
! {
!   using namespace std;
!   bool test = true;
! 
!   string name;
!   locale global_orig;
!   // 1: Destroyed when out of scope.
!   {
!     {
!       {
! 	VERIFY( counter == 0 );
! 	{
! 	  locale loc01(locale::classic(), new facet_type);
! 	  VERIFY( counter == 1 );
! 	  global_orig = locale::global(loc01);
! 	  name = loc01.name();
! 	}
! 	VERIFY( counter == 1 );
! 	locale loc02 = locale();
! 	// Weak, but it's something...
! 	VERIFY( loc02.name() == name );
!       }
!       VERIFY( counter == 1 );
!       // NB: loc03 should be a copy of the previous global locale.
!       locale loc03 = locale::global(global_orig);
!       VERIFY( counter == 1 );
!       VERIFY( loc03.name() == name );
!     }
!     VERIFY( counter == 0 );
!     locale loc04 = locale();
!     VERIFY( loc04 == global_orig );
!   }
! 
!   // 2: Not destroyed when out of scope, deliberately leaked.
!   {
!     {
!       {
! 	VERIFY( counter == 0 );
! 	{
! 	  locale loc01(locale::classic(), new facet_type(1));
! 	  VERIFY( counter == 1 );
! 	  global_orig = locale::global(loc01);
! 	  name = loc01.name();
! 	}
! 	VERIFY( counter == 1 );
! 	locale loc02 = locale();
! 	// Weak, but it's something...
! 	VERIFY( loc02.name() == name );
!       }
!       VERIFY( counter == 1 );
!       // NB: loc03 should be a copy of the previous global locale.
!       locale loc03 = locale::global(global_orig);
!       VERIFY( counter == 1 );
!       VERIFY( loc03.name() == name );
!     }
!     VERIFY( counter == 1 );
!     locale loc04 = locale();
!     VERIFY( loc04 == global_orig );
!   }
!   VERIFY( counter == 1 );
! 
!   // Restore global settings.
!   locale::global(global_orig);
  }
  
  int main ()
*************** int main ()
*** 98,102 ****
--- 178,183 ----
    test01();
    test02();
  
+   test03();
    return 0;
  }



More information about the Gcc-patches mailing list