• 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/containers/string.hpp>
12 #include <boost/interprocess/containers/vector.hpp>
13 #include <boost/interprocess/streams/vectorstream.hpp>
14 #include <boost/interprocess/streams/bufferstream.hpp>
15 #include <sstream>
16 #include <cstring>
17 #include <vector>
18 #include <iostream>
19 #include <boost/date_time/posix_time/posix_time_types.hpp>
20 #include <stdio.h>
21 
22 namespace boost {
23 namespace interprocess {
24 
25 //Force instantiations to catch compile-time errors
26 typedef basic_string<char> my_string;
27 typedef basic_vectorstream<my_string > my_stringstream_t;
28 typedef vector<char> my_vector;
29 typedef basic_vectorstream<my_vector>  my_vectorstream_t;
30 template class basic_vectorstream<my_string>;
31 template class basic_vectorstream<std::vector<char> >;
32 
33 }}
34 
35 using namespace boost::interprocess;
36 
vectorstream_test()37 static int vectorstream_test()
38 {
39    {  //Test high watermarking initialization
40       my_stringstream_t my_stringstream;
41 
42       if(my_stringstream.tellg() != std::streampos(0)){
43          return 1;
44       }
45       if(my_stringstream.tellp() != std::streampos(0)){
46          return 1;
47       }
48 
49       int a (0);
50       my_stringstream << 11;
51       my_stringstream >> a;
52       if(a != 11)
53          return 1;
54    }
55    {  //Test high watermarking initialization
56       my_vectorstream_t my_stringstream;
57       int a (0);
58       my_stringstream << 13;
59       my_stringstream >> a;
60       if(a != 13)
61          return 1;
62    }
63 
64    //Pre-reserved string
65    {
66       my_stringstream_t my_stringstream;
67       std::stringstream std_stringstream;
68       std::string str1, str2, str3("testline:");
69       int number1, number2;
70 
71       my_stringstream.reserve(10000);
72       for(int i = 0; i < 100; ++i){
73          my_stringstream  << "testline: " << i << std::endl;
74          std_stringstream << "testline: " << i << std::endl;
75       }
76 
77       if(std::strcmp(my_stringstream.vector().c_str(), std_stringstream.str().c_str()) != 0){
78          return 1;
79       }
80 
81       for(int i = 0; i < 100; ++i){
82          my_stringstream  >> str1 >> number1;
83          std_stringstream >> str2 >> number2;
84          if((str1 != str2) || (str1 != str3)){
85             assert(0); return 1;
86          }
87          if((number1 != number2) || (number1 != i)){
88             assert(0); return 1;
89          }
90       }
91    }
92    //Pre-reserved vector
93    {
94       basic_vectorstream<std::vector<char> > my_vectorstream;
95       std::vector<char> myvector;
96       std::stringstream std_stringstream;
97       std::string str1, str2, str3("testline:");
98       int number1, number2;
99 
100       my_vectorstream.reserve(10000);
101       for(int i = 0; i < 100; ++i){
102          my_vectorstream  << "testline: " << i << std::endl;
103          std_stringstream << "testline: " << i << std::endl;
104       }
105       //Add final null to form a c string
106       myvector.push_back(0);
107       if(std::strcmp(&(my_vectorstream.vector()[0]), std_stringstream.str().c_str()) != 0){
108          return 1;
109       }
110       myvector.pop_back();
111       for(int i = 0; i < 100; ++i){
112          my_vectorstream  >> str1 >> number1;
113          std_stringstream >> str2 >> number2;
114          if((str1 != str2) || (str1 != str3)){
115             assert(0); return 1;
116          }
117          if((number1 != number2) || (number1 != i)){
118             assert(0); return 1;
119          }
120       }
121    }
122 
123    //No pre-reserved or pre-reserved string
124    {
125       my_stringstream_t my_stringstream;
126       std::stringstream std_stringstream;
127       std::string str1, str2, str3("testline:");
128       int number1, number2;
129 
130       for(int i = 0; i < 100; ++i){
131          my_stringstream  << "testline: " << i << std::endl;
132          std_stringstream << "testline: " << i << std::endl;
133       }
134       if(std::strcmp(my_stringstream.vector().c_str(), std_stringstream.str().c_str()) != 0){
135          assert(0);   return 1;
136       }
137       for(int i = 0; i < 100; ++i){
138          my_stringstream  >> str1 >> number1;
139          std_stringstream >> str2 >> number2;
140          if((str1 != str2) || (str1 != str3)){
141             assert(0); return 1;
142          }
143          if((number1 != number2) || (number1 != i)){
144             assert(0); return 1;
145          }
146       }
147    }
148 
149    //Test seek
150    {
151       my_stringstream_t my_stringstream;
152       my_stringstream << "ABCDEFGHIJKLM";
153       my_stringstream.seekp(0);
154       my_stringstream << "PQRST";
155       string s("PQRSTFGHIJKLM");
156       if(s != my_stringstream.vector()){
157          return 1;
158       }
159       my_stringstream.seekp(0, std::ios_base::end);
160       my_stringstream << "NOPQRST";
161       s ="PQRSTFGHIJKLMNOPQRST";
162       if(s != my_stringstream.vector()){
163          return 1;
164       }
165       int size = static_cast<int>(my_stringstream.vector().size());
166       my_stringstream.seekp(-size, std::ios_base::cur);
167       my_stringstream << "ABCDE";
168       s ="ABCDEFGHIJKLMNOPQRST";
169       if(s != my_stringstream.vector()){
170          return 1;
171       }
172    }
173    return 0;
174 }
175 
main()176 int main ()
177 {
178    if(vectorstream_test()){
179       return 1;
180    }
181    return 0;
182 }
183