• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  //////////////////////////////////////////////////////////////////////////////
2  //
3  // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
4  // Software License, Version 1.0. (See accompanying file
5  // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  //
7  // See http://www.boost.org/libs/interprocess for documentation.
8  //
9  //////////////////////////////////////////////////////////////////////////////
10  
11  #include <boost/interprocess/streams/bufferstream.hpp>
12  #include <sstream>
13  #include <cstring>
14  
15  namespace boost{
16  namespace interprocess{
17  
18  //Force instantiations to catch compile-time errors
19  template class basic_bufferbuf<char>;
20  template class basic_bufferstream<char>;
21  template class basic_ibufferstream<char>;
22  template class basic_obufferstream<char>;
23  
24  }}
25  
26  using namespace boost::interprocess;
27  
bufferstream_test()28  static int bufferstream_test()
29  {
30     //Static big enough buffer
31     {
32        const int BufSize = 10001;
33        //This will be zero-initialized
34        static char buffer [BufSize];
35        bufferstream bufstream;
36        if(bufstream.tellg() != std::streampos(0)){
37           return 1;
38        }
39        if(bufstream.tellp() != std::streampos(0)){
40           return 1;
41        }
42        std::stringstream std_stringstream;
43        std::string str1, str2, str3("testline:");
44        int number1, number2;
45  
46        //Make sure we have null in the last byte
47        bufstream.buffer(buffer, BufSize-1);
48        for(int i = 0; i < 100; ++i){
49           bufstream         << "testline: " << i << std::endl;
50           std_stringstream  << "testline: " << i << std::endl;
51        }
52  
53        if(std::strcmp(buffer, std_stringstream.str().c_str()) != 0){
54           return 1;
55        }
56  
57        //We shouldn't have reached the end of the buffer writing
58        if(bufstream.bad()){
59           assert(0);
60           return 1;
61        }
62  
63        bufstream.buffer(buffer, BufSize-1);
64        for(int i = 0; i < 100; ++i){
65           bufstream         >> str1 >> number1;
66           std_stringstream  >> str2 >> number2;
67           if((str1 != str2) || (str1 != str3)){
68              assert(0); return 1;
69           }
70           if((number1 != number2) || (number1 != i)){
71              assert(0); return 1;
72           }
73        }
74        //We shouldn't have reached the end of the buffer reading
75        if(bufstream.eof()){
76           assert(0);
77           return 1;
78        }
79     }
80  
81     //Static small buffer. Check if buffer
82     //overflow protection works.
83     {
84        const int BufSize = 101;
85        //This will be zero-initialized
86        static char buffer [BufSize];
87        bufferstream bufstream;
88        std::stringstream std_stringstream;
89        std::string str1;
90        int number1;
91  
92        //Make sure we have null in the last byte
93        bufstream.buffer(buffer, BufSize-1);
94        for(int i = 0; i < 100; ++i){
95           bufstream         << "testline: " << i << std::endl;
96           std_stringstream  << "testline: " << i << std::endl;
97        }
98  
99        //Contents should be different
100        if(std::strcmp(buffer, std_stringstream.str().c_str()) == 0){
101           return 1;
102        }
103        //The stream shouldn't be in good health
104        if(bufstream.good()){
105           assert(0);
106           return 1;
107        }
108           //The bad flag should be active. This indicates overflow attempt
109        if(!bufstream.bad()){
110           assert(0);
111           return 1;
112        }
113  
114        //Now let's test read overflow
115        bufstream.clear();
116        bufstream.buffer(buffer, BufSize-1);
117        for(int i = 0; i < 100; ++i){
118           bufstream         >> str1 >> number1;
119        }
120        //The stream shouldn't be in good health
121        if(bufstream.good()){
122           assert(0);
123           return 1;
124        }
125        //The eof flag indicates we have reached the end of the
126        //buffer while reading
127        if(!bufstream.eof()){
128           assert(0);
129           return 1;
130        }
131     }
132     return 0;
133  }
134  
main()135  int main ()
136  {
137     if(bufferstream_test()){
138        return 1;
139     }
140     return 0;
141  }
142  
143