Debian Codegraph @

Utilities/KWSys/testFStream.cxx

raw
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
5 #if defined(_MSC_VER)
6 # pragma warning(disable : 4786)
7 #endif
8
9 #include KWSYS_HEADER(FStream.hxx)
10 #include <cstring>
11
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
18 #include <iostream>
19
20 static int testNoFile()
21 {
22 kwsys::ifstream in_file("NoSuchFile.txt");
23 if (in_file) {
24 return 1;
25 }
26
27 return 0;
28 }
29
30 static const int num_test_files = 7;
31 static const int max_test_file_size = 45;
32
33 static kwsys::FStream::BOM expected_bom[num_test_files] = {
34 kwsys::FStream::BOM_None, kwsys::FStream::BOM_None,
35 kwsys::FStream::BOM_UTF8, kwsys::FStream::BOM_UTF16LE,
36 kwsys::FStream::BOM_UTF16BE, kwsys::FStream::BOM_UTF32LE,
37 kwsys::FStream::BOM_UTF32BE
38 };
39
40 static unsigned char expected_bom_data[num_test_files][5] = {
41 { 0 },
42 { 0 },
43 { 3, 0xEF, 0xBB, 0xBF },
44 { 2, 0xFF, 0xFE },
45 { 2, 0xFE, 0xFF },
46 { 4, 0xFF, 0xFE, 0x00, 0x00 },
47 { 4, 0x00, 0x00, 0xFE, 0xFF },
48 };
49
50 static unsigned char file_data[num_test_files][max_test_file_size] = {
51 { 1, 'H' },
52 { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
53 { 11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' },
54 { 22, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20,
55 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00 },
56 { 22, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00,
57 0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64 },
58 { 44, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00,
59 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
60 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00,
61 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 },
62 { 44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00,
63 0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
64 0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00,
65 0x72, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64 },
66 };
67
68 static int testBOM()
69 {
70 // test various encodings in binary mode
71 for (int i = 0; i < num_test_files; i++) {
72 {
73 kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
74 out.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
75 *expected_bom_data[i]);
76 out.write(reinterpret_cast<const char*>(file_data[i] + 1),
77 file_data[i][0]);
78 }
79
80 kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
81 kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
82 if (bom != expected_bom[i]) {
83 std::cout << "Unexpected BOM " << i << std::endl;
84 return 1;
85 }
86 char data[max_test_file_size];
87 in.read(data, file_data[i][0]);
88 if (!in.good()) {
89 std::cout << "Unable to read data " << i << std::endl;
90 return 1;
91 }
92
93 if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
94 std::cout << "Incorrect read data " << i << std::endl;
95 return 1;
96 }
97 }
98
99 return 0;
100 }
101
102 static int testBOMIO()
103 {
104 // test various encodings in binary mode
105 for (int i = 0; i < num_test_files; i++) {
106 kwsys::fstream f("bomio.txt",
107 kwsys::fstream::in | kwsys::fstream::out |
108 kwsys::fstream::binary | kwsys::fstream::trunc);
109 f.write(reinterpret_cast<const char*>(expected_bom_data[i] + 1),
110 *expected_bom_data[i]);
111 f.write(reinterpret_cast<const char*>(file_data[i] + 1), file_data[i][0]);
112 if (!f.good()) {
113 std::cout << "Unable to write data " << i << std::endl;
114 return 1;
115 }
116 f.seekp(0);
117
118 kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(f);
119 if (bom != expected_bom[i]) {
120 std::cout << "Unexpected BOM " << i << std::endl;
121 return 1;
122 }
123 char data[max_test_file_size];
124 f.read(data, file_data[i][0]);
125 if (!f.good()) {
126 std::cout << "Unable to read data " << i << std::endl;
127 return 1;
128 }
129
130 if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) {
131 std::cout << "Incorrect read data " << i << std::endl;
132 return 1;
133 }
134 }
135
136 return 0;
137 }
138
139 int testFStream(int, char*[])
140 {
141 int ret = 0;
142
143 ret |= testNoFile();
144 ret |= testBOM();
145 ret |= testBOMIO();
146
147 return ret;
148 }