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
);
50 CPPUNIT_TEST(SocketStream
);
51 CPPUNIT_TEST_SUITE_END();
60 void TestFILE(wxFFile
& file
, bool expected
);
61 void TestFd(wxFile
& file
, bool expected
);
64 // test a wxFFile and wxFFileInput/OutputStreams of a known type
66 void FileKindTestCase::TestFILE(wxFFile
& file
, bool expected
)
68 CPPUNIT_ASSERT(file
.IsOpened());
69 CPPUNIT_ASSERT((wxGetFileKind(file
.fp()) == wxFILE_KIND_DISK
) == expected
);
70 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
72 wxFFileInputStream
inStream(file
);
73 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
75 wxFFileOutputStream
outStream(file
);
76 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
79 // test a wxFile and wxFileInput/OutputStreams of a known type
81 void FileKindTestCase::TestFd(wxFile
& file
, bool expected
)
83 CPPUNIT_ASSERT(file
.IsOpened());
84 CPPUNIT_ASSERT((wxGetFileKind(file
.fd()) == wxFILE_KIND_DISK
) == expected
);
85 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
87 wxFileInputStream
inStream(file
);
88 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
90 wxFileOutputStream
outStream(file
);
91 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
96 ~TempFile() { if (!m_name
.IsEmpty()) wxRemoveFile(m_name
); }
100 // test with an ordinary file
102 void FileKindTestCase::File()
104 TempFile tmp
; // put first
106 tmp
.m_name
= wxFileName::CreateTempFileName(_T("wxft"), &file
);
110 wxFFile
ffile(tmp
.m_name
);
111 TestFILE(ffile
, true);
116 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
117 void FileKindTestCase::Pipe()
123 _pipe(afd
, 256, O_BINARY
);
126 wxFile
file0(afd
[0]);
127 wxFile
file1(afd
[1]);
128 TestFd(file0
, false);
131 wxFFile
ffile(fdopen(afd
[0], "r"));
132 TestFILE(ffile
, false);
136 // test with a socket
139 void FileKindTestCase::Socket()
141 int s
= socket(PF_INET
, SOCK_STREAM
, 0);
147 wxFFile
ffile(fdopen(s
, "r"));
148 TestFILE(ffile
, false);
152 // Socket streams should be non-seekable
155 void FileKindTestCase::SocketStream()
157 wxSocketClient client
;
158 wxSocketInputStream
inStream(client
);
159 CPPUNIT_ASSERT(!inStream
.IsSeekable());
160 wxSocketOutputStream
outStream(client
);
161 CPPUNIT_ASSERT(!outStream
.IsSeekable());
163 wxBufferedInputStream
nonSeekableBufferedInput(inStream
);
164 CPPUNIT_ASSERT(!nonSeekableBufferedInput
.IsSeekable());
165 wxBufferedOutputStream
nonSeekableBufferedOutput(outStream
);
166 CPPUNIT_ASSERT(!nonSeekableBufferedOutput
.IsSeekable());
170 // Memory streams should be seekable
172 void FileKindTestCase::MemoryStream()
175 wxMemoryInputStream
inStream(buf
, sizeof(buf
));
176 CPPUNIT_ASSERT(inStream
.IsSeekable());
177 wxMemoryOutputStream
outStream(buf
, sizeof(buf
));
178 CPPUNIT_ASSERT(outStream
.IsSeekable());
180 wxBufferedInputStream
seekableBufferedInput(inStream
);
181 CPPUNIT_ASSERT(seekableBufferedInput
.IsSeekable());
182 wxBufferedOutputStream
seekableBufferedOutput(outStream
);
183 CPPUNIT_ASSERT(seekableBufferedOutput
.IsSeekable());
186 // Stdin will usually be a terminal, if so then test it
188 void FileKindTestCase::Stdin()
191 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL
);
192 if (isatty(fileno(stdin
)))
193 CPPUNIT_ASSERT(wxGetFileKind(stdin
) == wxFILE_KIND_TERMINAL
);
196 // register in the unnamed registry so that these tests are run by default
197 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase
);
199 // also include in it's own registry so that these tests can be run alone
200 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase
, "FileKindTestCase");
202 #endif // wxUSE_STREAMS