• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright 2002, 2003 Daryle Walker
3 
4 Copyright 2019 Glen Joseph Fernandes
5 (glenjofe@gmail.com)
6 
7 Distributed under the Boost Software License, Version 1.0.
8 (http://www.boost.org/LICENSE_1_0.txt)
9 */
10 #include <boost/io/ios_state.hpp>
11 #include <boost/core/lightweight_test.hpp>
12 #include <boost/config.hpp>
13 #include <iomanip>
14 #include <ios>
15 #include <istream>
16 #include <locale>
17 #include <ostream>
18 #include <sstream>
19 #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
20 #include <stdexcept>
21 #endif
22 #include <streambuf>
23 #include <string>
24 #include <cstddef>
25 
26 class backward_bool_names
27     : public std::numpunct<char> {
28     typedef std::numpunct<char> base_type;
29 
30 public:
backward_bool_names(std::size_t refs=0)31     explicit backward_bool_names(std::size_t refs = 0)
32         : base_type( refs ) { }
33 
34 protected:
~backward_bool_names()35     ~backward_bool_names() { }
36 
do_truename() const37     base_type::string_type do_truename() const {
38         return "eurt";
39     }
40 
do_falsename() const41     base_type::string_type do_falsename() const {
42         return "eslaf";
43     }
44 };
45 
saver_tests_1(int index,std::istream & input,std::ostream & output,std::ostream & err)46 void saver_tests_1(int index,
47     std::istream& input,
48     std::ostream& output,
49     std::ostream& err)
50 {
51     boost::io::ios_flags_saver     ifls(output);
52     boost::io::ios_precision_saver iprs(output);
53     boost::io::ios_width_saver     iws(output);
54     boost::io::ios_tie_saver       its(input);
55     boost::io::ios_rdbuf_saver     irs(output);
56     boost::io::ios_fill_saver      ifis(output);
57     boost::io::ios_locale_saver    ils(output);
58     boost::io::ios_iword_saver     iis(output, index);
59     boost::io::ios_pword_saver     ipws(output, index);
60     std::locale loc(std::locale::classic(), new backward_bool_names);
61     input.tie(&err);
62     output.rdbuf(err.rdbuf());
63     output.iword(index) = 69L;
64     output.pword(index) = &err;
65     output.setf(std::ios_base::showpos | std::ios_base::boolalpha);
66     output.setf(std::ios_base::internal, std::ios_base::adjustfield);
67     output.fill('@');
68     output.precision( 9 );
69     output << "Hello world";
70     output << std::setw(10) << -16;
71     output << std::setw(15) << 34.5678901234;
72     output.imbue(loc);
73     output << true;
74     BOOST_TEST(&err == output.pword(index));
75     BOOST_TEST(69L == output.iword(index));
76     try {
77         boost::io::ios_exception_saver ies(output);
78         boost::io::ios_iostate_saver ias(output);
79         output.exceptions(std::ios_base::eofbit | std::ios_base::badbit);
80         output.setstate(std::ios_base::eofbit);
81         BOOST_ERROR("previous line should have thrown");
82 #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
83     } catch (std::exception&) {
84 #else
85     } catch (std::ios_base::failure&) {
86 #endif
87         BOOST_TEST(output.exceptions() == std::ios_base::goodbit );
88     }
89 }
90 
saver_tests_2(int index,std::istream & input,std::ostream & output,std::ostream & err)91 void saver_tests_2(int index,
92     std::istream& input,
93     std::ostream& output,
94     std::ostream& err)
95 {
96     boost::io::ios_tie_saver   its(input, &err);
97     boost::io::ios_rdbuf_saver irs(output, err.rdbuf());
98     boost::io::ios_iword_saver iis(output, index, 69L);
99     boost::io::ios_pword_saver ipws(output, index, &err);
100     boost::io::ios_flags_saver ifls(output,
101         (output.flags() & ~std::ios_base::adjustfield) |
102         std::ios_base::showpos |
103         std::ios_base::boolalpha |
104         (std::ios_base::internal & std::ios_base::adjustfield));
105     boost::io::ios_precision_saver iprs(output, 9);
106     boost::io::ios_fill_saver ifis(output, '@');
107     output << "Hello world";
108     boost::io::ios_width_saver iws(output, 12);
109     output << -16 + 34.5678901234;
110     std::locale loc(std::locale::classic(), new backward_bool_names);
111     boost::io::ios_locale_saver ils(output, loc);
112     output << true;
113     BOOST_TEST(&err == output.pword(index));
114     BOOST_TEST(69L == output.iword(index));
115     try {
116         boost::io::ios_exception_saver ies(output, std::ios_base::eofbit);
117         boost::io::ios_iostate_saver ias(output,
118             output.rdstate() | std::ios_base::eofbit );
119         BOOST_ERROR("previous line should have thrown");
120 #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
121     } catch (std::exception&) {
122 #else
123     } catch (std::ios_base::failure&) {
124 #endif
125         BOOST_TEST(output.exceptions() == std::ios_base::goodbit);
126     }
127 }
128 
main()129 int main()
130 {
131     int index = std::ios_base::xalloc();
132     std::ostringstream out;
133     std::ostringstream err;
134     std::istringstream in;
135     std::ios_base::fmtflags out_flags = out.flags();
136     std::streamsize         out_precision = out.precision();
137     std::streamsize         out_width = out.width();
138     std::ios_base::iostate  out_iostate = out.rdstate();
139     std::ios_base::iostate  out_exceptions = out.exceptions();
140     std::ostream*           in_tie = in.tie();
141     std::streambuf*         out_sb = out.rdbuf();
142     char                    out_fill = out.fill();
143     std::locale             out_locale = out.getloc();
144     out.iword(index) = 42L;
145     out.pword(index) = &in;
146     saver_tests_1(index, in, out, err);
147     BOOST_TEST(&in == out.pword(index));
148     BOOST_TEST(42L == out.iword(index));
149     BOOST_TEST(out_locale == out.getloc());
150     BOOST_TEST(out_fill == out.fill());
151     BOOST_TEST(out_sb == out.rdbuf());
152     BOOST_TEST(in_tie == in.tie());
153     BOOST_TEST(out_exceptions == out.exceptions());
154     BOOST_TEST(out_iostate == out.rdstate());
155     BOOST_TEST(out_width == out.width());
156     BOOST_TEST(out_precision == out.precision());
157     BOOST_TEST(out_flags == out.flags());
158     saver_tests_2(index, in, out, err);
159     BOOST_TEST(&in == out.pword(index));
160     BOOST_TEST(42L == out.iword(index));
161     BOOST_TEST(out_locale == out.getloc());
162     BOOST_TEST(out_fill == out.fill());
163     BOOST_TEST(out_sb == out.rdbuf());
164     BOOST_TEST(in_tie == in.tie());
165     BOOST_TEST(out_exceptions == out.exceptions());
166     BOOST_TEST(out_iostate == out.rdstate());
167     BOOST_TEST(out_width == out.width());
168     BOOST_TEST(out_precision == out.precision());
169     BOOST_TEST(out_flags == out.flags());
170     return boost::report_errors();
171 }
172