summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/22_locale/locale/global_locale_objects/2.cc
blob: b76bd0aaf42f3d7c06aefa14bb1a066e50be76f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// { dg-require-namedlocale "en_PH.ISO8859-1" }
// { dg-require-namedlocale "es_MX.ISO8859-1" }

// 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>

// Copyright (C) 2000-2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// 22.1.1.5 locale static members [lib.locale.statics]

#include <cwchar> // for mbstate_t
#include <locale>
#include <testsuite_hooks.h>

// Sanity check locale::global(loc) and setlocale.
void test02()
{
  using namespace std;
  
  const string ph(ISO_8859(1,en_PH));
  const string mx(ISO_8859(1,es_MX));
  const char* orig = setlocale(LC_ALL, 0);
  const char* testph = setlocale(LC_ALL, ph.c_str());
  const char* testmx = setlocale(LC_ALL, mx.c_str());
  setlocale(LC_ALL, orig);

  // If the underlying locale doesn't support these names, setlocale
  // won't be reset. Therefore, disable unless we know these specific
  // named locales work.
  if (testph && testmx)
    {
      const locale loc_ph = locale(ph.c_str());
      const locale loc_mx = locale(mx.c_str());
      
      // Use setlocale between two calls to locale("")
      const locale loc_env_1 = locale("");
      setlocale(LC_ALL, ph.c_str());
      const locale loc_env_2 = locale("");
      VERIFY( loc_env_1 == loc_env_2 );
      
      // Change global locale.
      locale global_orig = locale::global(loc_mx);
      const char* lc_all_mx = setlocale(LC_ALL, 0);
      if (lc_all_mx)
	{
	  VERIFY( mx == lc_all_mx );
	}
      
      // Restore global settings.
      locale::global(global_orig);
    }
}

int main ()
{
  test02();
  return 0;
}