1
0

testIOS.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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(stl/vector)
  12. #include KWSYS_HEADER(ios/sstream)
  13. #include KWSYS_HEADER(ios/fstream)
  14. #include KWSYS_HEADER(ios/iostream)
  15. // Work-around CMake dependency scanning limitation. This must
  16. // duplicate the above list of headers.
  17. #if 0
  18. # include "kwsys_stl_string.hxx.in"
  19. # include "kwsys_stl_vector.h.in"
  20. # include "kwsys_ios_sstream.h.in"
  21. # include "kwsys_ios_fstream.h.in"
  22. # include "kwsys_ios_iostream.h.in"
  23. #endif
  24. #include <string.h> /* strlen */
  25. int testIOS(int, char*[])
  26. {
  27. kwsys_ios::ostringstream ostr;
  28. const char hello[] = "hello";
  29. ostr << hello;
  30. if(ostr.str() != hello)
  31. {
  32. kwsys_ios::cerr << "failed to write hello to ostr" << kwsys_ios::endl;
  33. return 1;
  34. }
  35. const char world[] = "world";
  36. kwsys_ios::ostringstream ostr2;
  37. ostr2.write( hello, strlen(hello) ); /* I could do sizeof */
  38. ostr2.put( '\0' );
  39. ostr2.write( world, strlen(world) );
  40. if(ostr2.str().size() != strlen(hello) + 1 + strlen(world) )
  41. {
  42. kwsys_ios::cerr << "failed to write hello to ostr2" << kwsys_ios::endl;
  43. return 1;
  44. }
  45. 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 };
  46. const size_t narray = sizeof(array); // 180
  47. kwsys_ios::stringstream strstr;
  48. strstr.write( (char*)array, narray );
  49. //strstr.seekp( narray / 2 ); // set position of put pointer in mid string
  50. if(strstr.str().size() != narray )
  51. {
  52. kwsys_ios::cerr << "failed to write array to strstr" << kwsys_ios::endl;
  53. return 1;
  54. }
  55. kwsys_ios::istringstream istr(" 10 20 str ");
  56. kwsys_stl::string s;
  57. int x;
  58. if(istr >> x)
  59. {
  60. if(x != 10)
  61. {
  62. kwsys_ios::cerr << "x != 10" << kwsys_ios::endl;
  63. return 1;
  64. }
  65. }
  66. else
  67. {
  68. kwsys_ios::cerr << "Failed to read 10 from istr" << kwsys_ios::endl;
  69. return 1;
  70. }
  71. if(istr >> x)
  72. {
  73. if(x != 20)
  74. {
  75. kwsys_ios::cerr << "x != 20" << kwsys_ios::endl;
  76. return 1;
  77. }
  78. }
  79. else
  80. {
  81. kwsys_ios::cerr << "Failed to read 20 from istr" << kwsys_ios::endl;
  82. return 1;
  83. }
  84. if(istr >> s)
  85. {
  86. if(s != "str")
  87. {
  88. kwsys_ios::cerr << "s != \"str\"" << kwsys_ios::endl;
  89. return 1;
  90. }
  91. }
  92. else
  93. {
  94. kwsys_ios::cerr << "Failed to read str from istr" << kwsys_ios::endl;
  95. return 1;
  96. }
  97. if(istr >> s)
  98. {
  99. kwsys_ios::cerr << "Able to read past end of stream" << kwsys_ios::endl;
  100. return 1;
  101. }
  102. else
  103. {
  104. // Clear the failure.
  105. istr.clear(istr.rdstate() & ~kwsys_ios::ios::eofbit);
  106. istr.clear(istr.rdstate() & ~kwsys_ios::ios::failbit);
  107. }
  108. istr.str("30");
  109. if(istr >> x)
  110. {
  111. if(x != 30)
  112. {
  113. kwsys_ios::cerr << "x != 30" << kwsys_ios::endl;
  114. return 1;
  115. }
  116. }
  117. else
  118. {
  119. kwsys_ios::cerr << "Failed to read 30 from istr" << kwsys_ios::endl;
  120. return 1;
  121. }
  122. kwsys_ios::stringstream sstr;
  123. sstr << "40 str2";
  124. if(sstr >> x)
  125. {
  126. if(x != 40)
  127. {
  128. kwsys_ios::cerr << "x != 40" << kwsys_ios::endl;
  129. return 1;
  130. }
  131. }
  132. else
  133. {
  134. kwsys_ios::cerr << "Failed to read 40 from sstr" << kwsys_ios::endl;
  135. return 1;
  136. }
  137. if(sstr >> s)
  138. {
  139. if(s != "str2")
  140. {
  141. kwsys_ios::cerr << "s != \"str2\"" << kwsys_ios::endl;
  142. return 1;
  143. }
  144. }
  145. else
  146. {
  147. kwsys_ios::cerr << "Failed to read str2 from sstr" << kwsys_ios::endl;
  148. return 1;
  149. }
  150. // Just try to compile this.
  151. if(x == 12345)
  152. {
  153. kwsys_ios::ifstream fin("/does_not_exist",
  154. kwsys_ios::ios::in | kwsys_ios_binary);
  155. }
  156. kwsys_ios::cout << "IOS tests passed" << kwsys_ios::endl;
  157. return 0;
  158. }