1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filetype/filetype.cpp
3 // Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
4 // Author: Mike Wetherell
5 // Copyright: (c) 2005 Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
15 // for all others, include the necessary headers
23 #include <sys/socket.h>
28 #include "wx/wfstream.h"
29 #include "wx/filename.h"
30 #include "wx/socket.h"
31 #include "wx/sckstrm.h"
32 #include "wx/mstream.h"
35 #define isatty _isatty
36 #define fdopen _fdopen
37 #define fileno _fileno
40 ///////////////////////////////////////////////////////////////////////////////
43 class FileKindTestCase
: public CppUnit::TestCase
45 CPPUNIT_TEST_SUITE(FileKindTestCase
);
47 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
54 CPPUNIT_TEST(MemoryStream
);
56 CPPUNIT_TEST(SocketStream
);
58 CPPUNIT_TEST_SUITE_END();
69 void TestFILE(wxFFile
& file
, bool expected
);
70 void TestFd(wxFile
& file
, bool expected
);
73 // test a wxFFile and wxFFileInput/OutputStreams of a known type
75 void FileKindTestCase::TestFILE(wxFFile
& file
, bool expected
)
77 CPPUNIT_ASSERT(file
.IsOpened());
78 CPPUNIT_ASSERT((wxGetFileKind(file
.fp()) == wxFILE_KIND_DISK
) == expected
);
79 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
81 wxFFileInputStream
inStream(file
);
82 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
84 wxFFileOutputStream
outStream(file
);
85 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
88 // test a wxFile and wxFileInput/OutputStreams of a known type
90 void FileKindTestCase::TestFd(wxFile
& file
, bool expected
)
92 CPPUNIT_ASSERT(file
.IsOpened());
93 CPPUNIT_ASSERT((wxGetFileKind(file
.fd()) == wxFILE_KIND_DISK
) == expected
);
94 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
96 wxFileInputStream
inStream(file
);
97 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
99 wxFileOutputStream
outStream(file
);
100 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
105 ~TempFile() { if (!m_name
.IsEmpty()) wxRemoveFile(m_name
); }
109 // test with an ordinary file
111 void FileKindTestCase::File()
113 TempFile tmp
; // put first
115 tmp
.m_name
= wxFileName::CreateTempFileName(wxT("wxft"), &file
);
119 wxFFile
ffile(tmp
.m_name
);
120 TestFILE(ffile
, true);
125 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
126 void FileKindTestCase::Pipe()
133 rc
= _pipe(afd
, 256, O_BINARY
);
135 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc
);
137 wxFile
file0(afd
[0]);
138 wxFile
file1(afd
[1]);
139 TestFd(file0
, false);
142 wxFFile
ffile(fdopen(afd
[0], "r"));
143 TestFILE(ffile
, false);
147 // test with a socket
150 void FileKindTestCase::Socket()
152 int s
= socket(PF_INET
, SOCK_STREAM
, 0);
158 wxFFile
ffile(fdopen(s
, "r"));
159 TestFILE(ffile
, false);
163 // Socket streams should be non-seekable
166 void FileKindTestCase::SocketStream()
168 wxSocketClient client
;
169 wxSocketInputStream
inStream(client
);
170 CPPUNIT_ASSERT(!inStream
.IsSeekable());
171 wxSocketOutputStream
outStream(client
);
172 CPPUNIT_ASSERT(!outStream
.IsSeekable());
174 wxBufferedInputStream
nonSeekableBufferedInput(inStream
);
175 CPPUNIT_ASSERT(!nonSeekableBufferedInput
.IsSeekable());
176 wxBufferedOutputStream
nonSeekableBufferedOutput(outStream
);
177 CPPUNIT_ASSERT(!nonSeekableBufferedOutput
.IsSeekable());
181 // Memory streams should be seekable
183 void FileKindTestCase::MemoryStream()
186 wxMemoryInputStream
inStream(buf
, sizeof(buf
));
187 CPPUNIT_ASSERT(inStream
.IsSeekable());
188 wxMemoryOutputStream
outStream(buf
, sizeof(buf
));
189 CPPUNIT_ASSERT(outStream
.IsSeekable());
191 wxBufferedInputStream
seekableBufferedInput(inStream
);
192 CPPUNIT_ASSERT(seekableBufferedInput
.IsSeekable());
193 wxBufferedOutputStream
seekableBufferedOutput(outStream
);
194 CPPUNIT_ASSERT(seekableBufferedOutput
.IsSeekable());
197 // Stdin will usually be a terminal, if so then test it
199 void FileKindTestCase::Stdin()
202 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL
);
203 if (isatty(fileno(stdin
)))
204 CPPUNIT_ASSERT(wxGetFileKind(stdin
) == wxFILE_KIND_TERMINAL
);
207 // register in the unnamed registry so that these tests are run by default
208 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase
);
210 // also include in its own registry so that these tests can be run alone
211 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase
, "FileKindTestCase");
213 #endif // wxUSE_STREAMS