]>
Commit | Line | Data |
---|---|---|
3c70014d MW |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/filetype/filetype.cpp | |
0912690b | 3 | // Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable |
3c70014d MW |
4 | // Author: Mike Wetherell |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 2005 Mike Wetherell | |
7 | // Licence: wxWidgets licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "testprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | // for all others, include the necessary headers | |
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/wx.h" | |
19 | #endif | |
20 | ||
b4d47ca0 VZ |
21 | #if wxUSE_STREAMS |
22 | ||
3c70014d MW |
23 | #ifdef __UNIX__ |
24 | #include <sys/socket.h> | |
25 | #endif | |
26 | ||
27 | #include "wx/file.h" | |
28 | #include "wx/ffile.h" | |
29 | #include "wx/wfstream.h" | |
30 | #include "wx/filename.h" | |
31 | #include "wx/socket.h" | |
32 | #include "wx/sckstrm.h" | |
33 | #include "wx/mstream.h" | |
34 | ||
b4d47ca0 VZ |
35 | #ifdef __VISUALC__ |
36 | #define isatty _isatty | |
37 | #define fdopen _fdopen | |
38 | #define fileno _fileno | |
39 | #endif | |
3c70014d MW |
40 | |
41 | /////////////////////////////////////////////////////////////////////////////// | |
42 | // The test case | |
43 | ||
0912690b | 44 | class FileKindTestCase : public CppUnit::TestCase |
3c70014d | 45 | { |
0912690b | 46 | CPPUNIT_TEST_SUITE(FileKindTestCase); |
3c70014d MW |
47 | CPPUNIT_TEST(File); |
48 | #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__ | |
49 | CPPUNIT_TEST(Pipe); | |
50 | #endif | |
51 | #if defined __UNIX__ | |
52 | CPPUNIT_TEST(Socket); | |
53 | #endif | |
54 | CPPUNIT_TEST(Stdin); | |
55 | CPPUNIT_TEST(MemoryStream); | |
f4dd8614 | 56 | #if wxUSE_SOCKETS |
3c70014d | 57 | CPPUNIT_TEST(SocketStream); |
f4dd8614 | 58 | #endif |
3c70014d MW |
59 | CPPUNIT_TEST_SUITE_END(); |
60 | ||
61 | void File(); | |
62 | void Pipe(); | |
63 | void Socket(); | |
64 | void Stdin(); | |
65 | void MemoryStream(); | |
f4dd8614 | 66 | #if wxUSE_SOCKETS |
3c70014d | 67 | void SocketStream(); |
f4dd8614 | 68 | #endif |
3c70014d MW |
69 | |
70 | void TestFILE(wxFFile& file, bool expected); | |
71 | void TestFd(wxFile& file, bool expected); | |
72 | }; | |
73 | ||
74 | // test a wxFFile and wxFFileInput/OutputStreams of a known type | |
75 | // | |
0912690b | 76 | void FileKindTestCase::TestFILE(wxFFile& file, bool expected) |
3c70014d MW |
77 | { |
78 | CPPUNIT_ASSERT(file.IsOpened()); | |
0912690b MW |
79 | CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected); |
80 | CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected); | |
3c70014d MW |
81 | |
82 | wxFFileInputStream inStream(file); | |
83 | CPPUNIT_ASSERT(inStream.IsSeekable() == expected); | |
84 | ||
85 | wxFFileOutputStream outStream(file); | |
86 | CPPUNIT_ASSERT(outStream.IsSeekable() == expected); | |
87 | } | |
88 | ||
89 | // test a wxFile and wxFileInput/OutputStreams of a known type | |
90 | // | |
0912690b | 91 | void FileKindTestCase::TestFd(wxFile& file, bool expected) |
3c70014d MW |
92 | { |
93 | CPPUNIT_ASSERT(file.IsOpened()); | |
0912690b MW |
94 | CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected); |
95 | CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected); | |
3c70014d MW |
96 | |
97 | wxFileInputStream inStream(file); | |
98 | CPPUNIT_ASSERT(inStream.IsSeekable() == expected); | |
99 | ||
100 | wxFileOutputStream outStream(file); | |
101 | CPPUNIT_ASSERT(outStream.IsSeekable() == expected); | |
102 | } | |
103 | ||
104 | struct TempFile | |
105 | { | |
106 | ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); } | |
107 | wxString m_name; | |
108 | }; | |
109 | ||
110 | // test with an ordinary file | |
111 | // | |
0912690b | 112 | void FileKindTestCase::File() |
3c70014d MW |
113 | { |
114 | TempFile tmp; // put first | |
115 | wxFile file; | |
9a83f860 | 116 | tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file); |
3c70014d MW |
117 | TestFd(file, true); |
118 | file.Close(); | |
119 | ||
120 | wxFFile ffile(tmp.m_name); | |
121 | TestFILE(ffile, true); | |
122 | } | |
123 | ||
124 | // test with a pipe | |
125 | // | |
126 | #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__ | |
0912690b | 127 | void FileKindTestCase::Pipe() |
3c70014d MW |
128 | { |
129 | int afd[2]; | |
130 | #ifdef __UNIX__ | |
131 | pipe(afd); | |
132 | #else | |
133 | _pipe(afd, 256, O_BINARY); | |
134 | #endif | |
135 | ||
136 | wxFile file0(afd[0]); | |
137 | wxFile file1(afd[1]); | |
138 | TestFd(file0, false); | |
139 | file0.Detach(); | |
140 | ||
141 | wxFFile ffile(fdopen(afd[0], "r")); | |
142 | TestFILE(ffile, false); | |
143 | } | |
144 | #endif | |
145 | ||
146 | // test with a socket | |
147 | // | |
148 | #if defined __UNIX__ | |
0912690b | 149 | void FileKindTestCase::Socket() |
3c70014d MW |
150 | { |
151 | int s = socket(PF_INET, SOCK_STREAM, 0); | |
152 | ||
153 | wxFile file(s); | |
154 | TestFd(file, false); | |
155 | file.Detach(); | |
156 | ||
157 | wxFFile ffile(fdopen(s, "r")); | |
158 | TestFILE(ffile, false); | |
159 | } | |
160 | #endif | |
161 | ||
162 | // Socket streams should be non-seekable | |
163 | // | |
164 | #if wxUSE_SOCKETS | |
0912690b | 165 | void FileKindTestCase::SocketStream() |
3c70014d MW |
166 | { |
167 | wxSocketClient client; | |
168 | wxSocketInputStream inStream(client); | |
169 | CPPUNIT_ASSERT(!inStream.IsSeekable()); | |
170 | wxSocketOutputStream outStream(client); | |
171 | CPPUNIT_ASSERT(!outStream.IsSeekable()); | |
172 | ||
173 | wxBufferedInputStream nonSeekableBufferedInput(inStream); | |
174 | CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable()); | |
175 | wxBufferedOutputStream nonSeekableBufferedOutput(outStream); | |
176 | CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable()); | |
177 | } | |
178 | #endif | |
179 | ||
180 | // Memory streams should be seekable | |
181 | // | |
0912690b | 182 | void FileKindTestCase::MemoryStream() |
3c70014d MW |
183 | { |
184 | char buf[20]; | |
185 | wxMemoryInputStream inStream(buf, sizeof(buf)); | |
186 | CPPUNIT_ASSERT(inStream.IsSeekable()); | |
187 | wxMemoryOutputStream outStream(buf, sizeof(buf)); | |
188 | CPPUNIT_ASSERT(outStream.IsSeekable()); | |
189 | ||
190 | wxBufferedInputStream seekableBufferedInput(inStream); | |
191 | CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable()); | |
192 | wxBufferedOutputStream seekableBufferedOutput(outStream); | |
193 | CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable()); | |
194 | } | |
195 | ||
196 | // Stdin will usually be a terminal, if so then test it | |
197 | // | |
0912690b | 198 | void FileKindTestCase::Stdin() |
3c70014d MW |
199 | { |
200 | if (isatty(0)) | |
0912690b | 201 | CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL); |
3c70014d | 202 | if (isatty(fileno(stdin))) |
0912690b | 203 | CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL); |
3c70014d MW |
204 | } |
205 | ||
206 | // register in the unnamed registry so that these tests are run by default | |
0912690b | 207 | CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase); |
3c70014d MW |
208 | |
209 | // also include in it's own registry so that these tests can be run alone | |
0912690b | 210 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase"); |
3c70014d MW |
211 | |
212 | #endif // wxUSE_STREAMS |