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