cmake/Source/kwsys/testFStream.cxx

149 lines
4.4 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
2014-08-03 19:52:23 +02:00
#include "kwsysPrivate.h"
#if defined(_MSC_VER)
2018-08-09 18:06:22 +02:00
# pragma warning(disable : 4786)
2014-08-03 19:52:23 +02:00
#endif
#include KWSYS_HEADER(FStream.hxx)
2020-08-30 11:54:41 +02:00
#include <cstring>
2014-08-03 19:52:23 +02:00
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
#if 0
2018-08-09 18:06:22 +02:00
# include "FStream.hxx.in"
2014-08-03 19:52:23 +02:00
#endif
2015-11-17 17:22:37 +01:00
#include <iostream>
2014-08-03 19:52:23 +02:00
static int testNoFile()
{
kwsys::ifstream in_file("NoSuchFile.txt");
2017-04-14 19:02:05 +02:00
if (in_file) {
2014-08-03 19:52:23 +02:00
return 1;
2017-04-14 19:02:05 +02:00
}
2014-08-03 19:52:23 +02:00
return 0;
}
2016-03-13 13:35:51 +01:00
static const int num_test_files = 7;
static const int max_test_file_size = 45;
2017-04-14 19:02:05 +02:00
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,
2015-04-27 22:25:09 +02:00
kwsys::FStream::BOM_UTF32BE
};
2017-04-14 19:02:05 +02:00
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 },
2015-04-27 22:25:09 +02:00
};
2017-04-14 19:02:05 +02:00
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 },
2015-04-27 22:25:09 +02:00
};
static int testBOM()
{
// test various encodings in binary mode
2017-04-14 19:02:05 +02:00
for (int i = 0; i < num_test_files; i++) {
2015-04-27 22:25:09 +02:00
{
kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
2017-04-14 19:02:05 +02:00
out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
2015-04-27 22:25:09 +02:00
*expected_bom_data[i]);
2017-04-14 19:02:05 +02:00
out.write(reinterpret_cast<const char*>(file_data[i] + 1),
2015-04-27 22:25:09 +02:00
file_data[i][0]);
2017-04-14 19:02:05 +02:00
}
2015-04-27 22:25:09 +02:00
kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
2017-04-14 19:02:05 +02:00
if (bom != expected_bom[i]) {
2015-11-17 17:22:37 +01:00
std::cout << "Unexpected BOM " << i << std::endl;
2015-04-27 22:25:09 +02:00
return 1;
2017-04-14 19:02:05 +02:00
}
2016-03-13 13:35:51 +01:00
char data[max_test_file_size];
2015-04-27 22:25:09 +02:00
in.read(data, file_data[i][0]);
2017-04-14 19:02:05 +02:00
if (!in.good()) {
2015-11-17 17:22:37 +01:00
std::cout << "Unable to read data " << i << std::endl;
2015-04-27 22:25:09 +02:00
return 1;
2017-04-14 19:02:05 +02:00
}
2015-04-27 22:25:09 +02:00
2017-04-14 19:02:05 +02:00
if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
2015-11-17 17:22:37 +01:00
std::cout << "Incorrect read data " << i << std::endl;
2015-04-27 22:25:09 +02:00
return 1;
}
2017-04-14 19:02:05 +02:00
}
2015-04-27 22:25:09 +02:00
return 0;
}
2021-09-14 00:13:48 +02:00
static int testBOMIO()
{
// test various encodings in binary mode
for (int i = 0; i < num_test_files; i++) {
kwsys::fstream f("bomio.txt",
kwsys::fstream::in | kwsys::fstream::out |
kwsys::fstream::binary | kwsys::fstream::trunc);
f.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
*expected_bom_data[i]);
f.write(reinterpret_cast<const char*>(file_data[i] + 1), file_data[i][0]);
if (!f.good()) {
std::cout << "Unable to write data " << i << std::endl;
return 1;
}
f.seekp(0);
kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(f);
if (bom != expected_bom[i]) {
std::cout << "Unexpected BOM " << i << std::endl;
return 1;
}
char data[max_test_file_size];
f.read(data, file_data[i][0]);
if (!f.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;
}
2023-05-23 16:38:00 +02:00
int testFStream(int, char*[])
2014-08-03 19:52:23 +02:00
{
int ret = 0;
ret |= testNoFile();
2015-04-27 22:25:09 +02:00
ret |= testBOM();
2021-09-14 00:13:48 +02:00
ret |= testBOMIO();
2014-08-03 19:52:23 +02:00
return ret;
}