2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / numpunct / members / char / cache_2.cc
blob6be2ec04f8834ad6ea71772e6a6030950a172b9a
1 // 2003-07-06 Benjamin Kosnik <bkoz@redhat.com>
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 #include <locale>
22 #include <sstream>
23 #include <ostream>
24 #include <testsuite_hooks.h>
26 class numpunct_checked : public std::numpunct<char>
28 typedef std::numpunct<char> base;
30 public:
31 explicit
32 numpunct_checked(std::size_t refs = 0): base(refs) { }
34 string_type
35 base_truename() const
36 { return base::do_truename(); }
38 protected:
39 virtual string_type
40 do_truename() const
41 { return base::do_truename() + "st"; }
44 // Changing caches deletes old cache, adds new one.
45 void test01()
47 using namespace std;
49 bool test __attribute__((unused)) = true;
50 const string empty;
51 const string basestr("true");
52 const string derivedstr("truest");
54 const locale loc(locale::classic(), new numpunct_checked);
55 stringbuf sbuf;
56 ostream os(&sbuf);
57 os.setf(ios_base::boolalpha);
59 // Pre-cache sanity check.
60 const numpunct<char>& np = use_facet<numpunct<char> >(loc);
61 VERIFY( np.truename() == derivedstr );
63 // Cache.
64 os.imbue(loc);
65 os << true;
66 VERIFY( sbuf.str() == derivedstr );
68 // Re-cache.
69 sbuf.str(empty);
70 os.imbue(locale::classic());
71 os << true;
72 VERIFY( sbuf.str() == basestr );
74 // Cache new locale again.
75 sbuf.str(empty);
76 os.imbue(loc);
77 os << true;
78 VERIFY( sbuf.str() == derivedstr );
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 );
86 int main()
88 test01();
89 return 0;