1 | // |
2 | // FileStreamTest.cpp |
3 | // |
4 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "FileStreamTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/FileStream.h" |
15 | #include "Poco/File.h" |
16 | #include "Poco/TemporaryFile.h" |
17 | #include "Poco/Exception.h" |
18 | |
19 | |
20 | FileStreamTest::FileStreamTest(const std::string& rName): CppUnit::TestCase(rName) |
21 | { |
22 | } |
23 | |
24 | |
25 | FileStreamTest::~FileStreamTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void FileStreamTest::testRead() |
31 | { |
32 | #if defined(POCO_OS_FAMILY_WINDOWS) |
33 | char tmp[]={'\xc4', '\xd6', '\xdc', '\xe4', '\xf6', '\xfc', '\0'}; |
34 | std::string file(tmp); |
35 | file.append(".txt" ); |
36 | #else |
37 | std::string file("testfile.txt" ); |
38 | #endif |
39 | |
40 | Poco::TemporaryFile::registerForDeletion(file); |
41 | |
42 | Poco::FileOutputStream fos(file, std::ios::binary); |
43 | fos << "sometestdata" ; |
44 | fos.close(); |
45 | |
46 | Poco::FileInputStream fis(file); |
47 | assertTrue (fis.good()); |
48 | std::string read; |
49 | fis >> read; |
50 | assertTrue (!read.empty()); |
51 | } |
52 | |
53 | |
54 | void FileStreamTest::testWrite() |
55 | { |
56 | #if defined(POCO_OS_FAMILY_WINDOWS) |
57 | char tmp[]={'\xdf', '\xc4', '\xd6', '\xdc', '\xe4', '\xf6', '\xfc', '\0'}; |
58 | std::string file(tmp); |
59 | file = "dummy_" + file + (".txt" ); |
60 | #else |
61 | std::string file("dummy_file.txt" ); |
62 | #endif |
63 | |
64 | Poco::TemporaryFile::registerForDeletion(file); |
65 | |
66 | Poco::FileOutputStream fos(file); |
67 | assertTrue (fos.good()); |
68 | fos << "hiho" ; |
69 | fos.close(); |
70 | |
71 | Poco::FileInputStream fis(file); |
72 | assertTrue (fis.good()); |
73 | std::string read; |
74 | fis >> read; |
75 | assertTrue (read == "hiho" ); |
76 | } |
77 | |
78 | |
79 | void FileStreamTest::testReadWrite() |
80 | { |
81 | #if defined(POCO_OS_FAMILY_WINDOWS) |
82 | char tmp[]={'\xdf', '\xc4', '\xd6', '\xdc', '\xe4', '\xf6', '\xfc', '\0'}; |
83 | std::string file(tmp); |
84 | file = "dummy_" + file + (".txt" ); |
85 | #else |
86 | char tmp[]={'\xc3', '\x9f', '\xc3', '\x84', '\xc3', '\x96', '\xc3', '\x9c', '\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc', '\0'}; |
87 | std::string file(tmp); |
88 | file = "dummy_" + file + ".txt" ; |
89 | #endif |
90 | |
91 | Poco::TemporaryFile::registerForDeletion(file); |
92 | |
93 | Poco::FileStream fos(file); |
94 | assertTrue (fos.good()); |
95 | fos << "hiho" ; |
96 | fos.seekg(0, std::ios::beg); |
97 | std::string read; |
98 | fos >> read; |
99 | assertTrue (read == "hiho" ); |
100 | } |
101 | |
102 | |
103 | void FileStreamTest::testOpen() |
104 | { |
105 | Poco::FileOutputStream ostr; |
106 | ostr.open("test.txt" , std::ios::out); |
107 | assertTrue (ostr.good()); |
108 | ostr.close(); |
109 | } |
110 | |
111 | |
112 | void FileStreamTest::testOpenModeIn() |
113 | { |
114 | Poco::File f("nonexistent.txt" ); |
115 | if (f.exists()) |
116 | f.remove(); |
117 | |
118 | try |
119 | { |
120 | Poco::FileInputStream istr("nonexistent.txt" ); |
121 | fail("nonexistent file - must throw" ); |
122 | } |
123 | catch (Poco::Exception&) |
124 | { |
125 | } |
126 | |
127 | f.createFile(); |
128 | Poco::FileInputStream istr("nonexistent.txt" ); |
129 | assertTrue (istr.good()); |
130 | } |
131 | |
132 | |
133 | void FileStreamTest::testOpenModeOut() |
134 | { |
135 | Poco::File f("test.txt" ); |
136 | if (f.exists()) |
137 | f.remove(); |
138 | |
139 | Poco::FileOutputStream ostr1("test.txt" ); |
140 | ostr1 << "Hello, world!" ; |
141 | ostr1.close(); |
142 | |
143 | assertTrue (f.exists()); |
144 | assertTrue (f.getSize() != 0); |
145 | |
146 | Poco::FileStream str1("test.txt" ); |
147 | str1.close(); |
148 | |
149 | assertTrue (f.exists()); |
150 | assertTrue (f.getSize() != 0); |
151 | |
152 | Poco::FileOutputStream ostr2("test.txt" ); |
153 | ostr2.close(); |
154 | |
155 | assertTrue (f.exists()); |
156 | assertTrue (f.getSize() == 0); |
157 | |
158 | f.remove(); |
159 | } |
160 | |
161 | |
162 | void FileStreamTest::testOpenModeTrunc() |
163 | { |
164 | Poco::File f("test.txt" ); |
165 | if (f.exists()) |
166 | f.remove(); |
167 | |
168 | Poco::FileOutputStream ostr1("test.txt" ); |
169 | ostr1 << "Hello, world!" ; |
170 | ostr1.close(); |
171 | |
172 | assertTrue (f.exists()); |
173 | assertTrue (f.getSize() != 0); |
174 | |
175 | Poco::FileStream str1("test.txt" , std::ios::trunc); |
176 | str1.close(); |
177 | |
178 | assertTrue (f.exists()); |
179 | assertTrue (f.getSize() == 0); |
180 | |
181 | f.remove(); |
182 | } |
183 | |
184 | |
185 | void FileStreamTest::testOpenModeAte() |
186 | { |
187 | Poco::FileOutputStream ostr("test.txt" ); |
188 | ostr << "0123456789" ; |
189 | ostr.close(); |
190 | |
191 | Poco::FileStream str1("test.txt" , std::ios::ate); |
192 | int c = str1.get(); |
193 | assertTrue (str1.eof()); |
194 | |
195 | str1.clear(); |
196 | str1.seekg(0); |
197 | c = str1.get(); |
198 | assertTrue (c == '0'); |
199 | |
200 | str1.close(); |
201 | |
202 | Poco::FileStream str2("test.txt" , std::ios::ate); |
203 | str2 << "abcdef" ; |
204 | str2.seekg(0); |
205 | std::string s; |
206 | str2 >> s; |
207 | assertTrue (s == "0123456789abcdef" ); |
208 | str2.close(); |
209 | } |
210 | |
211 | |
212 | void FileStreamTest::testOpenModeApp() |
213 | { |
214 | Poco::FileOutputStream ostr("test.txt" ); |
215 | ostr << "0123456789" ; |
216 | ostr.close(); |
217 | |
218 | Poco::FileStream str1("test.txt" , std::ios::app); |
219 | |
220 | str1 << "abc" ; |
221 | |
222 | str1.seekp(0); |
223 | |
224 | str1 << "def" ; |
225 | |
226 | str1.close(); |
227 | |
228 | Poco::FileInputStream istr("test.txt" ); |
229 | std::string s; |
230 | istr >> s; |
231 | assertTrue (s == "0123456789abcdef" ); |
232 | istr.close(); |
233 | } |
234 | |
235 | |
236 | void FileStreamTest::testSeek() |
237 | { |
238 | Poco::FileStream str("test.txt" , std::ios::trunc); |
239 | str << "0123456789abcdef" ; |
240 | |
241 | str.seekg(0); |
242 | int c = str.get(); |
243 | assertTrue (c == '0'); |
244 | |
245 | str.seekg(10); |
246 | assertTrue (str.tellg() == std::streampos(10)); |
247 | c = str.get(); |
248 | assertTrue (c == 'a'); |
249 | assertTrue (str.tellg() == std::streampos(11)); |
250 | |
251 | str.seekg(-1, std::ios::end); |
252 | assertTrue (str.tellg() == std::streampos(15)); |
253 | c = str.get(); |
254 | assertTrue (c == 'f'); |
255 | assertTrue (str.tellg() == std::streampos(16)); |
256 | |
257 | str.seekg(-1, std::ios::cur); |
258 | assertTrue (str.tellg() == std::streampos(15)); |
259 | c = str.get(); |
260 | assertTrue (c == 'f'); |
261 | assertTrue (str.tellg() == std::streampos(16)); |
262 | |
263 | str.seekg(-4, std::ios::cur); |
264 | assertTrue (str.tellg() == std::streampos(12)); |
265 | c = str.get(); |
266 | assertTrue (c == 'c'); |
267 | assertTrue (str.tellg() == std::streampos(13)); |
268 | |
269 | str.seekg(1, std::ios::cur); |
270 | assertTrue (str.tellg() == std::streampos(14)); |
271 | c = str.get(); |
272 | assertTrue (c == 'e'); |
273 | assertTrue (str.tellg() == std::streampos(15)); |
274 | } |
275 | |
276 | |
277 | void FileStreamTest::testMultiOpen() |
278 | { |
279 | Poco::FileStream str("test.txt" , std::ios::trunc); |
280 | str << "0123456789\n" ; |
281 | str << "abcdefghij\n" ; |
282 | str << "klmnopqrst\n" ; |
283 | str.close(); |
284 | |
285 | std::string s; |
286 | str.open("test.txt" , std::ios::in); |
287 | std::getline(str, s); |
288 | assertTrue (s == "0123456789" ); |
289 | str.close(); |
290 | |
291 | str.open("test.txt" , std::ios::in); |
292 | std::getline(str, s); |
293 | assertTrue (s == "0123456789" ); |
294 | str.close(); |
295 | } |
296 | |
297 | |
298 | void FileStreamTest::setUp() |
299 | { |
300 | } |
301 | |
302 | |
303 | void FileStreamTest::tearDown() |
304 | { |
305 | } |
306 | |
307 | |
308 | CppUnit::Test* FileStreamTest::suite() |
309 | { |
310 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FileStreamTest" ); |
311 | |
312 | CppUnit_addTest(pSuite, FileStreamTest, testRead); |
313 | CppUnit_addTest(pSuite, FileStreamTest, testWrite); |
314 | CppUnit_addTest(pSuite, FileStreamTest, testReadWrite); |
315 | CppUnit_addTest(pSuite, FileStreamTest, testOpen); |
316 | CppUnit_addTest(pSuite, FileStreamTest, testOpenModeIn); |
317 | CppUnit_addTest(pSuite, FileStreamTest, testOpenModeOut); |
318 | CppUnit_addTest(pSuite, FileStreamTest, testOpenModeTrunc); |
319 | CppUnit_addTest(pSuite, FileStreamTest, testOpenModeAte); |
320 | CppUnit_addTest(pSuite, FileStreamTest, testOpenModeApp); |
321 | CppUnit_addTest(pSuite, FileStreamTest, testSeek); |
322 | CppUnit_addTest(pSuite, FileStreamTest, testMultiOpen); |
323 | |
324 | return pSuite; |
325 | } |
326 | |