testFStream.cxx 3.54 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4 5
#include "kwsysPrivate.h"

#if defined(_MSC_VER)
6
#pragma warning(disable : 4786)
7 8 9
#endif

#include KWSYS_HEADER(FStream.hxx)
10 11
#include <string.h>
#ifdef __BORLANDC__
12
#include <mem.h> /* memcmp */
13
#endif
14 15 16 17

// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
18
#include "FStream.hxx.in"
19 20
#endif

21
#include <iostream>
22 23 24 25 26

//----------------------------------------------------------------------------
static int testNoFile()
{
  kwsys::ifstream in_file("NoSuchFile.txt");
27
  if (in_file) {
28
    return 1;
29
  }
30 31 32 33

  return 0;
}

34 35 36
static const int num_test_files = 7;
static const int max_test_file_size = 45;

37 38 39 40
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,
41 42 43
  kwsys::FStream::BOM_UTF32BE
};

44 45 46 47 48 49 50 51
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 },
52 53
};

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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 },
70 71 72 73 74 75
};

//----------------------------------------------------------------------------
static int testBOM()
{
  // test various encodings in binary mode
76
  for (int i = 0; i < num_test_files; i++) {
77 78
    {
      kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
79
      out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
80
                *expected_bom_data[i]);
81
      out.write(reinterpret_cast<const char*>(file_data[i] + 1),
82
                file_data[i][0]);
83
    }
84 85 86

    kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
    kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
87
    if (bom != expected_bom[i]) {
88
      std::cout << "Unexpected BOM " << i << std::endl;
89
      return 1;
90
    }
91
    char data[max_test_file_size];
92
    in.read(data, file_data[i][0]);
93
    if (!in.good()) {
94
      std::cout << "Unable to read data " << i << std::endl;
95
      return 1;
96
    }
97

98
    if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
99
      std::cout << "Incorrect read data " << i << std::endl;
100 101
      return 1;
    }
102
  }
103 104 105 106

  return 0;
}

107
//----------------------------------------------------------------------------
108
int testFStream(int, char* [])
109 110 111 112
{
  int ret = 0;

  ret |= testNoFile();
113
  ret |= testBOM();
114 115 116

  return ret;
}