testIOS.cxx 4.2 KB

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