CMake/testFStream.cxx
KWSys Upstream 773b36e5d4 KWSys 2016-11-09 (18c65411)
Code extracted from:

    http://public.kitware.com/KWSys.git

at commit 18c654114de3aa65429542f95308720bc68f9231 (master).

Upstream Shortlog
-----------------

Brad King (14):
      37306a1c FStream: Quiet unused argument warning
      15e90a3c Sort includes to stabilize include order w.r.t. clang-format
      26509227 Copyright.txt: Add notice of copyright by contributors
      fc42d3f2 Add temporary script to filter license notices
      c41c1bc4 Simplify KWSys per-source license notices
      1d4c0b4a Remove temporary script that filtered license notices
      a4f5ef79 SystemInformation: Remove stray comment
      8649a886 kwsysPrivate: Protect KWSYS_HEADER macro from clang-format
      89b98af5 Configure clang-format for KWSys source tree
      547dacad Add a script to run clang-format on the entire source tree
      aa94be0c CONTRIBUTING: Add a section on coding style
      6604c4b6 Empty commit at end of history preceding clang-format style transition
      2b3e2b1c Tell Git to not export 'clang-format' infrastructure
      18c65411 FStream: Include Configure.hxx before other headers

Kitware Robot (1):
      6c973b46 Revise C++ coding style using clang-format
2016-11-09 09:22:56 -05:00

117 lines
3.5 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#include "kwsysPrivate.h"
#if defined(_MSC_VER)
#pragma warning(disable : 4786)
#endif
#include KWSYS_HEADER(FStream.hxx)
#include <string.h>
#ifdef __BORLANDC__
#include <mem.h> /* memcmp */
#endif
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
#if 0
#include "FStream.hxx.in"
#endif
#include <iostream>
//----------------------------------------------------------------------------
static int testNoFile()
{
kwsys::ifstream in_file("NoSuchFile.txt");
if (in_file) {
return 1;
}
return 0;
}
static const int num_test_files = 7;
static const int max_test_file_size = 45;
static kwsys::FStream::BOM expected_bom[num_test_files] = {
kwsys::FStream::BOM_None, kwsys::FStream::BOM_None,
kwsys::FStream::BOM_UTF8, kwsys::FStream::BOM_UTF16LE,
kwsys::FStream::BOM_UTF16BE, kwsys::FStream::BOM_UTF32LE,
kwsys::FStream::BOM_UTF32BE
};
static unsigned char expected_bom_data[num_test_files][5] = {
{ 0 },
{ 0 },
{ 3, 0xEF, 0xBB, 0xBF },
{ 2, 0xFF, 0xFE },
{ 2, 0xFE, 0xFF },
{ 4, 0xFF, 0xFE, 0x00, 0x00 },
{ 4, 0x00, 0x00, 0xFE, 0xFF },
};
static unsigned char file_data[num_test_files][max_test_file_size] = {
{ 1, 'H' },
{ 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
{ 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
{ 22, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20,
0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00 },
{ 22, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00,
0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64 },
{ 44, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00,
0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00,
0x00, 0x6C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 },
{ 44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00,
0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
0x72, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64 },
};
//----------------------------------------------------------------------------
static int testBOM()
{
// test various encodings in binary mode
for (int i = 0; i < num_test_files; i++) {
{
kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
*expected_bom_data[i]);
out.write(reinterpret_cast<const char*>(file_data[i] + 1),
file_data[i][0]);
}
kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
if (bom != expected_bom[i]) {
std::cout << "Unexpected BOM " << i << std::endl;
return 1;
}
char data[max_test_file_size];
in.read(data, file_data[i][0]);
if (!in.good()) {
std::cout << "Unable to read data " << i << std::endl;
return 1;
}
if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
std::cout << "Incorrect read data " << i << std::endl;
return 1;
}
}
return 0;
}
//----------------------------------------------------------------------------
int testFStream(int, char* [])
{
int ret = 0;
ret |= testNoFile();
ret |= testBOM();
return ret;
}