bufferstream_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/streams/bufferstream.hpp>
  12. #include <sstream>
  13. #include <cstring>
  14. namespace boost{
  15. namespace interprocess{
  16. //Force instantiations to catch compile-time errors
  17. template class basic_bufferbuf<char>;
  18. template class basic_bufferstream<char>;
  19. template class basic_ibufferstream<char>;
  20. template class basic_obufferstream<char>;
  21. }}
  22. using namespace boost::interprocess;
  23. static int bufferstream_test()
  24. {
  25. //Static big enough buffer
  26. {
  27. const int BufSize = 10001;
  28. //This will be zero-initialized
  29. static char buffer [BufSize];
  30. bufferstream bufstream;
  31. if(bufstream.tellg() != std::streampos(0)){
  32. return 1;
  33. }
  34. if(bufstream.tellp() != std::streampos(0)){
  35. return 1;
  36. }
  37. std::stringstream std_stringstream;
  38. std::string str1, str2, str3("testline:");
  39. int number1, number2;
  40. //Make sure we have null in the last byte
  41. bufstream.buffer(buffer, BufSize-1);
  42. for(int i = 0; i < 100; ++i){
  43. bufstream << "testline: " << i << std::endl;
  44. std_stringstream << "testline: " << i << std::endl;
  45. }
  46. if(std::strcmp(buffer, std_stringstream.str().c_str()) != 0){
  47. return 1;
  48. }
  49. //We shouldn't have reached the end of the buffer writing
  50. if(bufstream.bad()){
  51. assert(0);
  52. return 1;
  53. }
  54. bufstream.buffer(buffer, BufSize-1);
  55. for(int i = 0; i < 100; ++i){
  56. bufstream >> str1 >> number1;
  57. std_stringstream >> str2 >> number2;
  58. if((str1 != str2) || (str1 != str3)){
  59. assert(0); return 1;
  60. }
  61. if((number1 != number2) || (number1 != i)){
  62. assert(0); return 1;
  63. }
  64. }
  65. //We shouldn't have reached the end of the buffer reading
  66. if(bufstream.eof()){
  67. assert(0);
  68. return 1;
  69. }
  70. }
  71. //Static small buffer. Check if buffer
  72. //overflow protection works.
  73. {
  74. const int BufSize = 101;
  75. //This will be zero-initialized
  76. static char buffer [BufSize];
  77. bufferstream bufstream;
  78. std::stringstream std_stringstream;
  79. std::string str1;
  80. int number1;
  81. //Make sure we have null in the last byte
  82. bufstream.buffer(buffer, BufSize-1);
  83. for(int i = 0; i < 100; ++i){
  84. bufstream << "testline: " << i << std::endl;
  85. std_stringstream << "testline: " << i << std::endl;
  86. }
  87. //Contents should be different
  88. if(std::strcmp(buffer, std_stringstream.str().c_str()) == 0){
  89. return 1;
  90. }
  91. //The stream shouldn't be in good health
  92. if(bufstream.good()){
  93. assert(0);
  94. return 1;
  95. }
  96. //The bad flag should be active. This indicates overflow attempt
  97. if(!bufstream.bad()){
  98. assert(0);
  99. return 1;
  100. }
  101. //Now let's test read overflow
  102. bufstream.clear();
  103. bufstream.buffer(buffer, BufSize-1);
  104. for(int i = 0; i < 100; ++i){
  105. bufstream >> str1 >> number1;
  106. }
  107. //The stream shouldn't be in good health
  108. if(bufstream.good()){
  109. assert(0);
  110. return 1;
  111. }
  112. //The eof flag indicates we have reached the end of the
  113. //buffer while reading
  114. if(!bufstream.eof()){
  115. assert(0);
  116. return 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. int main ()
  122. {
  123. if(bufferstream_test()){
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. #include <boost/interprocess/detail/config_end.hpp>