vectorstream_test.cpp 5.3 KB

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