Ticket #955: testIostreams.cpp

File testIostreams.cpp, 1.9 KB (added by anonymous, 15 years ago)

source code exhibiting push problem with zlib_compressor

Line 
1#include <iostream>
2#include <sstream>
3
4#include <boost/iostreams/filtering_stream.hpp>
5#include <boost/iostreams/copy.hpp>
6
7#ifdef USE_BZIP2
8# include <boost/iostreams/filter/bzip2.hpp>
9#else
10# include <boost/iostreams/filter/zlib.hpp>
11#endif
12
13int main (int argc, char** argv)
14 {
15 int loops(NUM_LOOPS);
16 if (argc > 1)
17 {
18 loops = atoi(argv[1]);
19 }
20
21 std::string testData =
22 "\n\
23 <node name='car'>\n\
24 <interface name='CarInterface'>\n\
25 <method name='accelerate'/>\n\
26 <method name='decelerate'/>\n\
27 <method name='turnLeft'/>\n\
28 <method name='turnRight'/>\n\
29 <signal name='crashed'/>\n\
30 </interface>\n\
31 </node>\n\
32 ";
33
34 std::cerr << "Test data : " << testData << std::endl;
35
36 int i(0);
37 try
38 {
39 for (i = 0; i < loops; ++i)
40 {
41 if (!(i % 1000))
42 {
43 std::cerr << i << ' ';
44 }
45
46 std::istringstream ifs(testData);
47 std::ostringstream toFS;
48
49 boost::iostreams::filtering_ostream ofs;
50
51#ifdef USE_BZIP2
52 // pushing bzip2 has no problems
53 ofs.push(boost::iostreams::bzip2_compressor());
54#else
55 // pushing zlib causes the bad_alloc exception after many loops
56 // consuming more memory each loop
57 ofs.push(boost::iostreams::zlib_compressor());
58#endif
59
60 ofs.push(toFS);
61
62 boost::iostreams::copy(ifs, ofs);
63 ofs.reset();
64 }
65 }
66 catch (const std::exception& e)
67 {
68 std::cerr << std::endl << "Caught std::exception on iteration " << i << ": " << e.what() << std::endl;
69 }
70
71 if (i == loops)
72 {
73 std::cerr << std::endl << "TEST OK on " << i << " iterations" << std::endl;
74 }
75 else
76 {
77 std::cerr << std::endl << "TEST FAILED on iteration " << i << std::endl;
78 }
79 return 0;
80 }