testFStream.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #if defined(_MSC_VER)
  5. # pragma warning(disable : 4786)
  6. #endif
  7. #include KWSYS_HEADER(FStream.hxx)
  8. #include <cstring>
  9. // Work-around CMake dependency scanning limitation. This must
  10. // duplicate the above list of headers.
  11. #if 0
  12. # include "FStream.hxx.in"
  13. #endif
  14. #include <iostream>
  15. static int testNoFile()
  16. {
  17. kwsys::ifstream in_file("NoSuchFile.txt");
  18. if (in_file) {
  19. return 1;
  20. }
  21. return 0;
  22. }
  23. static const int num_test_files = 7;
  24. static const int max_test_file_size = 45;
  25. static kwsys::FStream::BOM expected_bom[num_test_files] = {
  26. kwsys::FStream::BOM_None, kwsys::FStream::BOM_None,
  27. kwsys::FStream::BOM_UTF8, kwsys::FStream::BOM_UTF16LE,
  28. kwsys::FStream::BOM_UTF16BE, kwsys::FStream::BOM_UTF32LE,
  29. kwsys::FStream::BOM_UTF32BE
  30. };
  31. static unsigned char expected_bom_data[num_test_files][5] = {
  32. { 0 },
  33. { 0 },
  34. { 3, 0xEF, 0xBB, 0xBF },
  35. { 2, 0xFF, 0xFE },
  36. { 2, 0xFE, 0xFF },
  37. { 4, 0xFF, 0xFE, 0x00, 0x00 },
  38. { 4, 0x00, 0x00, 0xFE, 0xFF },
  39. };
  40. static unsigned char file_data[num_test_files][max_test_file_size] = {
  41. { 1, 'H' },
  42. { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
  43. { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
  44. { 22, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20,
  45. 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00 },
  46. { 22, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00,
  47. 0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64 },
  48. { 44, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00,
  49. 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  50. 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00,
  51. 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 },
  52. { 44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00,
  53. 0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
  54. 0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
  55. 0x72, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64 },
  56. };
  57. static int testBOM()
  58. {
  59. // test various encodings in binary mode
  60. for (int i = 0; i < num_test_files; i++) {
  61. {
  62. kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
  63. out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
  64. *expected_bom_data[i]);
  65. out.write(reinterpret_cast<const char*>(file_data[i] + 1),
  66. file_data[i][0]);
  67. }
  68. kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
  69. kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
  70. if (bom != expected_bom[i]) {
  71. std::cout << "Unexpected BOM " << i << std::endl;
  72. return 1;
  73. }
  74. char data[max_test_file_size];
  75. in.read(data, file_data[i][0]);
  76. if (!in.good()) {
  77. std::cout << "Unable to read data " << i << std::endl;
  78. return 1;
  79. }
  80. if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
  81. std::cout << "Incorrect read data " << i << std::endl;
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. static int testBOMIO()
  88. {
  89. // test various encodings in binary mode
  90. for (int i = 0; i < num_test_files; i++) {
  91. kwsys::fstream f("bomio.txt",
  92. kwsys::fstream::in | kwsys::fstream::out |
  93. kwsys::fstream::binary | kwsys::fstream::trunc);
  94. f.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
  95. *expected_bom_data[i]);
  96. f.write(reinterpret_cast<const char*>(file_data[i] + 1), file_data[i][0]);
  97. if (!f.good()) {
  98. std::cout << "Unable to write data " << i << std::endl;
  99. return 1;
  100. }
  101. f.seekp(0);
  102. kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(f);
  103. if (bom != expected_bom[i]) {
  104. std::cout << "Unexpected BOM " << i << std::endl;
  105. return 1;
  106. }
  107. char data[max_test_file_size];
  108. f.read(data, file_data[i][0]);
  109. if (!f.good()) {
  110. std::cout << "Unable to read data " << i << std::endl;
  111. return 1;
  112. }
  113. if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
  114. std::cout << "Incorrect read data " << i << std::endl;
  115. return 1;
  116. }
  117. }
  118. return 0;
  119. }
  120. int testFStream(int, char* [])
  121. {
  122. int ret = 0;
  123. ret |= testNoFile();
  124. ret |= testBOM();
  125. ret |= testBOMIO();
  126. return ret;
  127. }