testFStream.cxx 4.0 KB

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