testIOS.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Configure.hxx)
  12. #include <sstream>
  13. #include <fstream>
  14. #include <iostream>
  15. #include <vector>
  16. #include <string.h> /* strlen */
  17. // Work-around CMake dependency scanning limitation. This must
  18. // duplicate the above list of headers.
  19. #if 0
  20. # include "Configure.hxx.in"
  21. #endif
  22. int testIOS(int, char*[])
  23. {
  24. std::ostringstream ostr;
  25. const char hello[] = "hello";
  26. ostr << hello;
  27. if(ostr.str() != hello)
  28. {
  29. std::cerr << "failed to write hello to ostr" << std::endl;
  30. return 1;
  31. }
  32. const char world[] = "world";
  33. std::ostringstream ostr2;
  34. ostr2.write( hello, strlen(hello) ); /* I could do sizeof */
  35. ostr2.put( '\0' );
  36. ostr2.write( world, strlen(world) );
  37. if(ostr2.str().size() != strlen(hello) + 1 + strlen(world) )
  38. {
  39. std::cerr << "failed to write hello to ostr2" << std::endl;
  40. return 1;
  41. }
  42. static const unsigned char array[] = { 0xff,0x4f,0xff,0x51,0x00,0x29,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x01,0x01,0xff,0x52,0x00,0x0c,0x00,0x00,0x00,0x01,0x00,0x05,0x04,0x04,0x00,0x01,0xff,0x5c,0x00,0x13,0x40,0x40,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0xff,0x64,0x00,0x2c,0x00,0x00,0x43,0x72,0x65,0x61,0x74,0x65,0x64,0x20,0x62,0x79,0x20,0x49,0x54,0x4b,0x2f,0x47,0x44,0x43,0x4d,0x2f,0x4f,0x70,0x65,0x6e,0x4a,0x50,0x45,0x47,0x20,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x31,0x2e,0x30,0xff,0x90,0x00,0x0a,0x00,0x00,0x00,0x00,0x06,0x2c,0x00,0x01,0xff,0x93,0xcf,0xb0,0x18,0x08,0x7f,0xc6,0x99,0xbf,0xff,0xc0,0xf8,0xc1,0xc1,0xf3,0x05,0x81,0xf2,0x83,0x0a,0xa5,0xff,0x10,0x90,0xbf,0x2f,0xff,0x04,0xa8,0x7f,0xc0,0xf8,0xc4,0xc1,0xf3,0x09,0x81,0xf3,0x0c,0x19,0x34 };
  43. const size_t narray = sizeof(array); // 180
  44. std::stringstream strstr;
  45. strstr.write( (char*)array, narray );
  46. //strstr.seekp( narray / 2 ); // set position of put pointer in mid string
  47. if(strstr.str().size() != narray )
  48. {
  49. std::cerr << "failed to write array to strstr" << std::endl;
  50. return 1;
  51. }
  52. std::istringstream istr(" 10 20 str ");
  53. std::string s;
  54. int x;
  55. if(istr >> x)
  56. {
  57. if(x != 10)
  58. {
  59. std::cerr << "x != 10" << std::endl;
  60. return 1;
  61. }
  62. }
  63. else
  64. {
  65. std::cerr << "Failed to read 10 from istr" << std::endl;
  66. return 1;
  67. }
  68. if(istr >> x)
  69. {
  70. if(x != 20)
  71. {
  72. std::cerr << "x != 20" << std::endl;
  73. return 1;
  74. }
  75. }
  76. else
  77. {
  78. std::cerr << "Failed to read 20 from istr" << std::endl;
  79. return 1;
  80. }
  81. if(istr >> s)
  82. {
  83. if(s != "str")
  84. {
  85. std::cerr << "s != \"str\"" << std::endl;
  86. return 1;
  87. }
  88. }
  89. else
  90. {
  91. std::cerr << "Failed to read str from istr" << std::endl;
  92. return 1;
  93. }
  94. if(istr >> s)
  95. {
  96. std::cerr << "Able to read past end of stream" << std::endl;
  97. return 1;
  98. }
  99. else
  100. {
  101. // Clear the failure.
  102. istr.clear(istr.rdstate() & ~std::ios::eofbit);
  103. istr.clear(istr.rdstate() & ~std::ios::failbit);
  104. }
  105. istr.str("30");
  106. if(istr >> x)
  107. {
  108. if(x != 30)
  109. {
  110. std::cerr << "x != 30" << std::endl;
  111. return 1;
  112. }
  113. }
  114. else
  115. {
  116. std::cerr << "Failed to read 30 from istr" << std::endl;
  117. return 1;
  118. }
  119. std::stringstream sstr;
  120. sstr << "40 str2";
  121. if(sstr >> x)
  122. {
  123. if(x != 40)
  124. {
  125. std::cerr << "x != 40" << std::endl;
  126. return 1;
  127. }
  128. }
  129. else
  130. {
  131. std::cerr << "Failed to read 40 from sstr" << std::endl;
  132. return 1;
  133. }
  134. if(sstr >> s)
  135. {
  136. if(s != "str2")
  137. {
  138. std::cerr << "s != \"str2\"" << std::endl;
  139. return 1;
  140. }
  141. }
  142. else
  143. {
  144. std::cerr << "Failed to read str2 from sstr" << std::endl;
  145. return 1;
  146. }
  147. // Just try to compile this.
  148. if(x == 12345)
  149. {
  150. std::ifstream fin("/does_not_exist",
  151. std::ios::in | std::ios::binary);
  152. }
  153. std::cout << "IOS tests passed" << std::endl;
  154. return 0;
  155. }