OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / numpunct / members / char / cache_2.cc
1 // 2003-07-06  Benjamin Kosnik  <bkoz@redhat.com>
2
3 // Copyright (C) 2003 Free Software Foundation, Inc.
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 #include <locale>
22 #include <sstream>
23 #include <ostream>
24 #include <testsuite_hooks.h>
25
26 class numpunct_checked : public std::numpunct<char>
27 {
28   typedef std::numpunct<char> base;
29
30 public:
31   explicit 
32   numpunct_checked(std::size_t refs = 0): base(refs) { }
33
34   string_type
35   base_truename() const
36   { return base::do_truename(); }
37
38 protected:
39   virtual string_type  
40   do_truename() const
41   { return base::do_truename() + "st"; }
42 };
43
44 // Changing caches deletes old cache, adds new one.
45 void test01()
46 {
47   using namespace std;
48
49   bool test __attribute__((unused)) = true;
50   const string  empty;
51   const string  basestr("true");
52   const string  derivedstr("truest");
53
54   const locale  loc(locale::classic(), new numpunct_checked);   
55   stringbuf     sbuf;
56   ostream       os(&sbuf);
57   os.setf(ios_base::boolalpha);
58
59   // Pre-cache sanity check.
60   const numpunct<char>& np = use_facet<numpunct<char> >(loc);
61   VERIFY( np.truename() == derivedstr );
62   
63   // Cache.
64   os.imbue(loc);
65   os << true;
66   VERIFY( sbuf.str() == derivedstr );
67
68   // Re-cache.
69   sbuf.str(empty);
70   os.imbue(locale::classic());
71   os << true;
72   VERIFY( sbuf.str() == basestr );
73
74   // Cache new locale again.
75   sbuf.str(empty);
76   os.imbue(loc);
77   os << true;
78   VERIFY( sbuf.str() == derivedstr );
79
80   // Post-cache sanity check, make sure that base class is still fine.
81   VERIFY( np.truename() == derivedstr );
82   const numpunct_checked& npd = static_cast<const numpunct_checked&>(np);
83   VERIFY( npd.base_truename() == basestr );
84 }
85
86 int main()
87 {
88   test01();
89   return 0;
90 }