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: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
16 // for all others, include the necessary headers
24 #include <sys/socket.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"
36 #define isatty _isatty
37 #define fdopen _fdopen
38 #define fileno _fileno
41 ///////////////////////////////////////////////////////////////////////////////
44 class FileKindTestCase
: public CppUnit::TestCase
46 CPPUNIT_TEST_SUITE(FileKindTestCase
);
48 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
55 CPPUNIT_TEST(MemoryStream
);
57 CPPUNIT_TEST(SocketStream
);
59 CPPUNIT_TEST_SUITE_END();
70 void TestFILE(wxFFile
& file
, bool expected
);
71 void TestFd(wxFile
& file
, bool expected
);
74 // test a wxFFile and wxFFileInput/OutputStreams of a known type
76 void FileKindTestCase::TestFILE(wxFFile
& file
, bool expected
)
78 CPPUNIT_ASSERT(file
.IsOpened());
79 CPPUNIT_ASSERT((wxGetFileKind(file
.fp()) == wxFILE_KIND_DISK
) == expected
);
80 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
82 wxFFileInputStream
inStream(file
);
83 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
85 wxFFileOutputStream
outStream(file
);
86 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
89 // test a wxFile and wxFileInput/OutputStreams of a known type
91 void FileKindTestCase::TestFd(wxFile
& file
, bool expected
)
93 CPPUNIT_ASSERT(file
.IsOpened());
94 CPPUNIT_ASSERT((wxGetFileKind(file
.fd()) == wxFILE_KIND_DISK
) == expected
);
95 CPPUNIT_ASSERT((file
.GetKind() == wxFILE_KIND_DISK
) == expected
);
97 wxFileInputStream
inStream(file
);
98 CPPUNIT_ASSERT(inStream
.IsSeekable() == expected
);
100 wxFileOutputStream
outStream(file
);
101 CPPUNIT_ASSERT(outStream
.IsSeekable() == expected
);
106 ~TempFile() { if (!m_name
.IsEmpty()) wxRemoveFile(m_name
); }
110 // test with an ordinary file
112 void FileKindTestCase::File()
114 TempFile tmp
; // put first
116 tmp
.m_name
= wxFileName::CreateTempFileName(wxT("wxft"), &file
);
120 wxFFile
ffile(tmp
.m_name
);
121 TestFILE(ffile
, true);
126 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
127 void FileKindTestCase::Pipe()
133 _pipe(afd
, 256, O_BINARY
);
136 wxFile
file0(afd
[0]);
137 wxFile
file1(afd
[1]);
138 TestFd(file0
, false);
141 wxFFile
ffile(fdopen(afd
[0], "r"));
142 TestFILE(ffile
, false);
146 // test with a socket
149 void FileKindTestCase::Socket()
151 int s
= socket(PF_INET
, SOCK_STREAM
, 0);
157 wxFFile
ffile(fdopen(s
, "r"));
158 TestFILE(ffile
, false);
162 // Socket streams should be non-seekable
165 void FileKindTestCase::SocketStream()
167 wxSocketClient client
;
168 wxSocketInputStream
inStream(client
);
169 CPPUNIT_ASSERT(!inStream
.IsSeekable());
170 wxSocketOutputStream
outStream(client
);
171 CPPUNIT_ASSERT(!outStream
.IsSeekable());
173 wxBufferedInputStream
nonSeekableBufferedInput(inStream
);
174 CPPUNIT_ASSERT(!nonSeekableBufferedInput
.IsSeekable());
175 wxBufferedOutputStream
nonSeekableBufferedOutput(outStream
);
176 CPPUNIT_ASSERT(!nonSeekableBufferedOutput
.IsSeekable());
180 // Memory streams should be seekable
182 void FileKindTestCase::MemoryStream()
185 wxMemoryInputStream
inStream(buf
, sizeof(buf
));
186 CPPUNIT_ASSERT(inStream
.IsSeekable());
187 wxMemoryOutputStream
outStream(buf
, sizeof(buf
));
188 CPPUNIT_ASSERT(outStream
.IsSeekable());
190 wxBufferedInputStream
seekableBufferedInput(inStream
);
191 CPPUNIT_ASSERT(seekableBufferedInput
.IsSeekable());
192 wxBufferedOutputStream
seekableBufferedOutput(outStream
);
193 CPPUNIT_ASSERT(seekableBufferedOutput
.IsSeekable());
196 // Stdin will usually be a terminal, if so then test it
198 void FileKindTestCase::Stdin()
201 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL
);
202 if (isatty(fileno(stdin
)))
203 CPPUNIT_ASSERT(wxGetFileKind(stdin
) == wxFILE_KIND_TERMINAL
);
206 // register in the unnamed registry so that these tests are run by default
207 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase
);
209 // also include in its own registry so that these tests can be run alone
210 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase
, "FileKindTestCase");
212 #endif // wxUSE_STREAMS