testFStream.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <string.h>
  9. #ifdef __BORLANDC__
  10. #include <mem.h> /* memcmp */
  11. #endif
  12. // Work-around CMake dependency scanning limitation. This must
  13. // duplicate the above list of headers.
  14. #if 0
  15. #include "FStream.hxx.in"
  16. #endif
  17. #include <iostream>
  18. static int testNoFile()
  19. {
  20. kwsys::ifstream in_file("NoSuchFile.txt");
  21. if (in_file) {
  22. return 1;
  23. }
  24. return 0;
  25. }
  26. static const int num_test_files = 7;
  27. static const int max_test_file_size = 45;
  28. static kwsys::FStream::BOM expected_bom[num_test_files] = {
  29. kwsys::FStream::BOM_None, kwsys::FStream::BOM_None,
  30. kwsys::FStream::BOM_UTF8, kwsys::FStream::BOM_UTF16LE,
  31. kwsys::FStream::BOM_UTF16BE, kwsys::FStream::BOM_UTF32LE,
  32. kwsys::FStream::BOM_UTF32BE
  33. };
  34. static unsigned char expected_bom_data[num_test_files][5] = {
  35. { 0 },
  36. { 0 },
  37. { 3, 0xEF, 0xBB, 0xBF },
  38. { 2, 0xFF, 0xFE },
  39. { 2, 0xFE, 0xFF },
  40. { 4, 0xFF, 0xFE, 0x00, 0x00 },
  41. { 4, 0x00, 0x00, 0xFE, 0xFF },
  42. };
  43. static unsigned char file_data[num_test_files][max_test_file_size] = {
  44. { 1, 'H' },
  45. { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
  46. { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
  47. { 22, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20,
  48. 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00 },
  49. { 22, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00,
  50. 0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64 },
  51. { 44, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00,
  52. 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  53. 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00,
  54. 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 },
  55. { 44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00,
  56. 0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
  57. 0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
  58. 0x72, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64 },
  59. };
  60. static int testBOM()
  61. {
  62. // test various encodings in binary mode
  63. for (int i = 0; i < num_test_files; i++) {
  64. {
  65. kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
  66. out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
  67. *expected_bom_data[i]);
  68. out.write(reinterpret_cast<const char*>(file_data[i] + 1),
  69. file_data[i][0]);
  70. }
  71. kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
  72. kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
  73. if (bom != expected_bom[i]) {
  74. std::cout << "Unexpected BOM " << i << std::endl;
  75. return 1;
  76. }
  77. char data[max_test_file_size];
  78. in.read(data, file_data[i][0]);
  79. if (!in.good()) {
  80. std::cout << "Unable to read data " << i << std::endl;
  81. return 1;
  82. }
  83. if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
  84. std::cout << "Incorrect read data " << i << std::endl;
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. }
  90. int testFStream(int, char* [])
  91. {
  92. int ret = 0;
  93. ret |= testNoFile();
  94. ret |= testBOM();
  95. return ret;
  96. }