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 <stdio.h>
20
21namespace boost {
22namespace interprocess {
23
24//Force instantiations to catch compile-time errors
25typedef basic_string<char> my_string;
26typedef basic_vectorstream<my_string > my_stringstream_t;
27typedef vector<char> my_vector;
28typedef basic_vectorstream<my_vector> my_vectorstream_t;
29template class basic_vectorstream<my_string>;
30template class basic_vectorstream<std::vector<char> >;
31
32}}
33
34using namespace boost::interprocess;
35
36static int vectorstream_test()
37{
38 { //Test high watermarking initialization
39 my_stringstream_t my_stringstream;
40
41 if(my_stringstream.tellg() != std::streampos(0)){
42 return 1;
43 }
44 if(my_stringstream.tellp() != std::streampos(0)){
45 return 1;
46 }
47
48 int a (0);
49 my_stringstream << 11;
50 my_stringstream >> a;
51 if(a != 11)
52 return 1;
53 }
54 { //Test high watermarking initialization
55 my_vectorstream_t my_stringstream;
56 int a (0);
57 my_stringstream << 13;
58 my_stringstream >> a;
59 if(a != 13)
60 return 1;
61 }
62
63 //Pre-reserved string
64 {
65 my_stringstream_t my_stringstream;
66 std::stringstream std_stringstream;
67 std::string str1, str2, str3("testline:");
68 int number1, number2;
69
70 my_stringstream.reserve(size: 10000);
71 for(int i = 0; i < 100; ++i){
72 my_stringstream << "testline: " << i << std::endl;
73 std_stringstream << "testline: " << i << std::endl;
74 }
75
76 if(std::strcmp(s1: my_stringstream.vector().c_str(), s2: std_stringstream.str().c_str()) != 0){
77 return 1;
78 }
79
80 for(int i = 0; i < 100; ++i){
81 my_stringstream >> str1 >> number1;
82 std_stringstream >> str2 >> number2;
83 if((str1 != str2) || (str1 != str3)){
84 assert(0); return 1;
85 }
86 if((number1 != number2) || (number1 != i)){
87 assert(0); return 1;
88 }
89 }
90 }
91 //Pre-reserved vector
92 {
93 basic_vectorstream<std::vector<char> > my_vectorstream;
94 std::vector<char> myvector;
95 std::stringstream std_stringstream;
96 std::string str1, str2, str3("testline:");
97 int number1, number2;
98
99 my_vectorstream.reserve(size: 10000);
100 for(int i = 0; i < 100; ++i){
101 my_vectorstream << "testline: " << i << std::endl;
102 std_stringstream << "testline: " << i << std::endl;
103 }
104 //Add final null to form a c string
105 my_vectorstream.swap_vector(vect&: myvector);
106 myvector.push_back(x: 0);
107 if(std::strcmp(s1: &(myvector[0]), s2: std_stringstream.str().c_str()) != 0){
108 return 1;
109 }
110 myvector.pop_back();
111 my_vectorstream.swap_vector(vect&: myvector);
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(s1: my_stringstream.vector().c_str(), s2: 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
177int main ()
178{
179 if(vectorstream_test()){
180 return 1;
181 }
182 return 0;
183}
184

source code of boost/libs/interprocess/test/vectorstream_test.cpp