Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / dynamic_bitset / dyn_bitset_unit_tests4.cpp
1 // -----------------------------------------------------------
2 //              Copyright (c) 2001 Jeremy Siek
3 //           Copyright (c) 2003-2006 Gennaro Prota
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 //    (See accompanying file LICENSE_1_0.txt or copy at
7 //          http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // -----------------------------------------------------------
10
11 #include <fstream>
12 #include <string>
13 #include <cstddef>   // for std::size_t
14 #include <stdexcept> // for std::logic_error
15 #include <assert.h>
16
17 #include "boost/config.hpp"
18 #if !defined (BOOST_NO_STRINGSTREAM)
19 # include <sstream>
20 #endif
21
22 #include "bitset_test.hpp"
23 #include "boost/dynamic_bitset/dynamic_bitset.hpp"
24 #include "boost/detail/workaround.hpp"
25
26
27 // Codewarrior 8.3 for Win fails without this.
28 // Thanks Howard Hinnant ;)
29 #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
30 # pragma parse_func_templ off
31 #endif
32
33
34 #if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE
35 # define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
36 #endif
37
38 #if !defined BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
39 std::wstring widen_string( const std::string & str,
40                            const std::locale & loc = std::locale() )
41 {
42   std::wstring result;
43   const std::string::size_type len = str.length();
44   if(len != 0) {
45
46     typedef std::ctype<wchar_t> ct_type;
47     typedef std::wstring::traits_type tr_type;
48     const ct_type & ct = BOOST_USE_FACET(ct_type, loc);
49
50     result.resize(len);
51     for (std::size_t i = 0; i < len; ++i)
52         tr_type::assign(result[i], ct.widen(str[i]));
53
54   }
55   return result;
56 }
57 #endif
58
59 template <typename Block>
60 void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
61 {
62
63   typedef boost::dynamic_bitset<Block> bitset_type;
64   typedef bitset_test<bitset_type> Tests;
65
66   //=====================================================================
67   // Test stream operator<<
68   {
69
70     // The test "variables" are: the stream type and its state, the
71     // exception mask, the width, the fill char and the padding side (left/right)
72
73     std::ios::iostate masks[] = {
74                                   std::ios::goodbit,
75                                   std::ios::eofbit,
76                                   std::ios::failbit,
77                                   std::ios::eofbit | std::ios::failbit
78                                 };
79
80     static std::string strings[] = {
81                                   std::string(""),
82                                   std::string("0"),
83                                   std::string("1"),
84                                   std::string("11100"),
85                                   get_long_string()
86                                 };
87
88     char fill_chars[] =         { '*', 'x', ' ' };
89
90     std::size_t num_masks   = sizeof(masks) / sizeof(masks[0]);
91     std::size_t num_strings = sizeof(strings) / sizeof(strings[0]);
92     std::size_t num_chars   = sizeof(fill_chars) / sizeof(fill_chars[0]);
93
94     std::fstream not_good_stream("dynamic_bitset_tests - this file shouldn't exist",
95                                  std::ios::in);
96
97
98     for (std::size_t mi = 0; mi < num_masks; ++mi) {
99       for (std::size_t si = 0; si < num_strings; ++si) {
100
101         std::streamsize slen = (std::streamsize)(strings[si].length());
102
103         assert( (std::numeric_limits<std::streamsize>::max)()
104                  >=(std::streamsize)(1+slen*2) );
105
106         for (std::size_t ci = 0; ci < num_chars; ++ci) {
107
108           // note how "negative widths" are tested too
109           const std::streamsize widths[] = { -1 - slen/2, 0, slen/2, 1 + slen*2 };
110           std::size_t num_widths = sizeof(widths) / sizeof(widths[0]);
111
112           for (std::size_t wi = 0; wi < num_widths; ++wi) {
113             std::streamsize w = widths[wi];
114             {
115               // test 0 - stream !good()
116               if(not_good_stream.good())
117                   throw std::logic_error("Error in operator << tests"
118                                          " - please, double check");
119               bitset_type b(strings[si]);
120               not_good_stream.width(w);
121               not_good_stream.fill(fill_chars[ci]);
122               try { not_good_stream.exceptions(masks[mi]); } catch(...) {}
123
124               Tests::stream_inserter(b, not_good_stream, "<unused_string>");
125             }
126             {
127               // test 1a - file stream
128               bitset_type b(strings[si]);
129               std::ofstream file(test_file_name(), std::ios::trunc);
130               file.width(w);
131               file.fill(fill_chars[ci]);
132               file.exceptions(masks[mi]);
133               Tests::stream_inserter(b, file, test_file_name());
134
135             }
136             {
137               //NOTE: there are NO string stream tests
138             }
139 #if !defined (BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS)
140             {
141               // test 1b - wide file stream
142               bitset_type b(strings[si]);
143               std::wofstream file(test_file_name());
144               file.width(w);
145               file.fill(fill_chars[ci]);
146               file.exceptions(masks[mi]);
147               Tests::stream_inserter(b, file, test_file_name());
148             }
149 #endif
150           }
151         }
152       }
153     } // for (; mi..)
154
155   }
156
157   //=====================================================================
158   // Test stream operator>>
159   {
160
161   // The test "variables" are: the stream type, the exception mask,
162   // the actual contents (and/or state) of the stream, and width.
163   //
164   // With few exceptions, each test case consists of writing a different
165   // assortment of digits and "whitespaces" to a text stream and then checking
166   // that what was written gets read back unchanged. That's NOT guaranteed by
167   // the standard, unless the assortment always ends with a '\n' and satisfies
168   // other conditions (see C99, 7.19.2/2), however it works in practice and is
169   // a good "real life" test. Some characters, such as '\v' and '\f', are not
170   // used exactly because they are the ones which will most likely give problems
171   // on some systems (for instance '\f' could actually be written as a sequence
172   // of new-lines, and we could never be able to read it back)
173   //
174   // Note how the bitset object is not initially empty. That helps checking
175   // that it isn't erroneously clear()ed by operator>>.
176
177
178   std::ios::iostate masks[] = {
179                                   std::ios::goodbit,
180                                   std::ios::eofbit,
181                                   std::ios::failbit,
182                                   std::ios::eofbit | std::ios::failbit
183                                    };
184
185   const std::string spaces = "\t\n "; //"\t\n\v\f ";
186
187   const std::string long_string = get_long_string();
188   /*const*/ static std::string strings[] = {
189                   // NOTE: "const" gives the usual problems with Borland
190                   //       (in Tests::stream_extractor instantiation)
191
192
193 #if !(defined __BORLANDC__     \
194       && BOOST_WORKAROUND(BOOST_RWSTD_VER, BOOST_TESTED_AT(0x20101)))
195                                         // Borland 5.5.1 with RW library crashes
196             // empty string
197             std::string(""),
198             // no bitset
199             spaces,
200 #endif
201             // no bitset
202             std::string("x"),
203             std::string("\t  xyz"),
204
205             // bitset of size 1
206             std::string("0"),
207             std::string("1"),
208
209             std::string("  0  "),
210             std::string("  1  "),
211             spaces + "1",
212             "1" + spaces,
213             spaces + "1" + spaces,
214             std::string("  x1x  "),
215             std::string("  1x  "),
216
217             // long bitset
218             long_string,
219             "  " + long_string + " xyz",
220             spaces + long_string,
221             spaces + long_string + spaces
222     };
223
224
225   //-----------------------------------------------------
226
227   std::stringstream not_good_stream;
228   not_good_stream << "test";
229   std::string sink;
230   not_good_stream >> sink; // now the stream should be in eof state
231
232   const std::size_t num_masks = sizeof(masks) / sizeof(masks[0]);
233   const std::size_t num_strings = sizeof(strings) / sizeof(strings[0]);
234
235   for (std::size_t mi = 0; mi < num_masks; ++mi) {
236     for (std::size_t si = 0; si < num_strings; ++si) {
237
238       const std::streamsize slen = (std::streamsize)(strings[si].length());
239       assert((std::numeric_limits<std::streamsize>::max)() >= (std::streamsize)(1+slen*2));
240
241       std::streamsize widths[] = { -1, 0, slen/2, slen, 1 + slen*2 };
242       std::size_t num_widths = sizeof(widths) / sizeof(widths[0]);
243
244       for(std::size_t wi = 0; wi < num_widths; ++wi) {
245         const std::streamsize w = widths[wi];
246
247         // test 0 - !good() stream
248         {
249           if(not_good_stream.good())
250             throw std::logic_error("Error in operator >> tests"
251                                    " - please, double check");
252           bitset_type b(1, 15ul); // note: b is not empty
253           not_good_stream.width(w);
254           try { not_good_stream.exceptions(masks[mi]); } catch(...) {}
255           std::string irrelevant;
256           Tests::stream_extractor(b, not_good_stream, irrelevant);
257         }
258         // test 1a - (narrow) file stream
259         {
260           bitset_type b(1, 255ul);
261           {
262             std::ofstream f(test_file_name());
263             f << strings[si];
264           }
265
266           std::ifstream f(test_file_name());
267           f.width(w);
268           f.exceptions(masks[mi]);
269           Tests::stream_extractor(b, f, strings[si]);
270         }
271 #if !defined(BOOST_NO_STRINGSTREAM)
272         // test 2a - stringstream
273         {
274           bitset_type b(1, 255ul);
275           std::istringstream stream(strings[si]);
276           stream.width(w);
277           stream.exceptions(masks[mi]);
278           Tests::stream_extractor(b, stream, strings[si]);
279         }
280 #endif
281
282 #if !defined(BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS)
283         // test 1b - wchar_t file stream
284         {
285           std::wstring wstr = widen_string(strings[si]);
286           bitset_type b(1, 255ul);
287           {
288             std::basic_ofstream<wchar_t> of(test_file_name());
289             of << wstr;
290           }
291
292           std::basic_ifstream<wchar_t> f(test_file_name());
293           f.width(w);
294           f.exceptions(masks[mi]);
295           Tests::stream_extractor(b, f, wstr);
296         }
297         // test 2b - wstringstream
298         {
299           bitset_type b(1, 255ul);
300           std::wstring wstr = widen_string(strings[si]);
301
302           std::wistringstream wstream(wstr);
303           wstream.width(w);
304           wstream.exceptions(masks[mi]);
305           Tests::stream_extractor(b, wstream, wstr);
306         }
307 #endif // BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
308
309       }
310     }
311
312   } // for ( mi = 0; ...)
313
314
315   }
316   //=====================================================================
317   // << Any other tests go here >>
318   //         .....
319
320 }
321
322
323 int
324 test_main(int, char*[])
325 {
326   run_test_cases<unsigned char>();
327   run_test_cases<unsigned short>();
328   run_test_cases<unsigned int>();
329   run_test_cases<unsigned long>();
330 # ifdef BOOST_HAS_LONG_LONG
331   run_test_cases< ::boost::ulong_long_type>();
332 # endif
333
334   return 0;
335 }