1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filetype/filetype.cpp
3 // Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
4 // Author: Mike Wetherell
6 // Copyright: (c) 2005 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
16 // for all others, include the necessary headers
22 #include <sys/socket.h>
27 #include "wx/wfstream.h"
28 #include "wx/filename.h"
29 #include "wx/socket.h"
30 #include "wx/sckstrm.h"
31 #include "wx/mstream.h"
35 ///////////////////////////////////////////////////////////////////////////////
38 class FileKindTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE(FileKindTestCase
);
42 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
49 CPPUNIT_TEST(MemoryStream
);
51 CPPUNIT_TEST(SocketStream
);
53 CPPUNIT_TEST_SUITE_END();
64 void TestFILE(wxFFile
& file
, bool expected
);
65 void TestFd(wxFile
& file
, bool expected
);
68 // test a wxFFile and wxFFileInput/OutputStreams of a known type
70 void FileKindTestCase::TestFILE(wxFFile
& file
, bool expected
)
72 CPPUNIT_ASSERT(file
.IsOpened());
73 CPPUNIT_ASSERT((wxGetFileKind(file
.fp()) == wxFILE_KIND_DISK
) == expected
);
74 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
76 wxFFileInputStream
inStream(file
);
77 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
79 wxFFileOutputStream
outStream(file
);
80 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
83 // test a wxFile and wxFileInput/OutputStreams of a known type
85 void FileKindTestCase::TestFd(wxFile
& file
, bool expected
)
87 CPPUNIT_ASSERT(file
.IsOpened());
88 CPPUNIT_ASSERT((wxGetFileKind(file
.fd()) == wxFILE_KIND_DISK
) == expected
);
89 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
91 wxFileInputStream
inStream(file
);
92 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
94 wxFileOutputStream
outStream(file
);
95 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
100 ~TempFile() { if (!m_name
.IsEmpty()) wxRemoveFile(m_name
); }
104 // test with an ordinary file
106 void FileKindTestCase::File()
108 TempFile tmp
; // put first
110 tmp
.m_name
= wxFileName::CreateTempFileName(_T("wxft"), &file
);
114 wxFFile
ffile(tmp
.m_name
);
115 TestFILE(ffile
, true);
120 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
121 void FileKindTestCase::Pipe()
127 _pipe(afd
, 256, O_BINARY
);
130 wxFile
file0(afd
[0]);
131 wxFile
file1(afd
[1]);
132 TestFd(file0
, false);
135 wxFFile
ffile(fdopen(afd
[0], "r"));
136 TestFILE(ffile
, false);
140 // test with a socket
143 void FileKindTestCase::Socket()
145 int s
= socket(PF_INET
, SOCK_STREAM
, 0);
151 wxFFile
ffile(fdopen(s
, "r"));
152 TestFILE(ffile
, false);
156 // Socket streams should be non-seekable
159 void FileKindTestCase::SocketStream()
161 wxSocketClient client
;
162 wxSocketInputStream
inStream(client
);
163 CPPUNIT_ASSERT(!inStream
.IsSeekable());
164 wxSocketOutputStream
outStream(client
);
165 CPPUNIT_ASSERT(!outStream
.IsSeekable());
167 wxBufferedInputStream
nonSeekableBufferedInput(inStream
);
168 CPPUNIT_ASSERT(!nonSeekableBufferedInput
.IsSeekable());
169 wxBufferedOutputStream
nonSeekableBufferedOutput(outStream
);
170 CPPUNIT_ASSERT(!nonSeekableBufferedOutput
.IsSeekable());
174 // Memory streams should be seekable
176 void FileKindTestCase::MemoryStream()
179 wxMemoryInputStream
inStream(buf
, sizeof(buf
));
180 CPPUNIT_ASSERT(inStream
.IsSeekable());
181 wxMemoryOutputStream
outStream(buf
, sizeof(buf
));
182 CPPUNIT_ASSERT(outStream
.IsSeekable());
184 wxBufferedInputStream
seekableBufferedInput(inStream
);
185 CPPUNIT_ASSERT(seekableBufferedInput
.IsSeekable());
186 wxBufferedOutputStream
seekableBufferedOutput(outStream
);
187 CPPUNIT_ASSERT(seekableBufferedOutput
.IsSeekable());
190 // Stdin will usually be a terminal, if so then test it
192 void FileKindTestCase::Stdin()
195 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL
);
196 if (isatty(fileno(stdin
)))
197 CPPUNIT_ASSERT(wxGetFileKind(stdin
) == wxFILE_KIND_TERMINAL
);
200 // register in the unnamed registry so that these tests are run by default
201 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase
);
203 // also include in it's own registry so that these tests can be run alone
204 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase
, "FileKindTestCase");
206 #endif // wxUSE_STREAMS