]> git.saurik.com Git - wxWidgets.git/blob - tests/filekind/filekind.cpp
Add wxEVT_AUI_PANE_ACTIVATED event.
[wxWidgets.git] / tests / filekind / filekind.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filetype/filetype.cpp
3 // Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2005 Mike Wetherell
7 // Licence: wxWindows 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
21 #if wxUSE_STREAMS
22
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
35 #ifdef __VISUALC__
36 #define isatty _isatty
37 #define fdopen _fdopen
38 #define fileno _fileno
39 #endif
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // The test case
43
44 class FileKindTestCase : public CppUnit::TestCase
45 {
46 CPPUNIT_TEST_SUITE(FileKindTestCase);
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);
56 #if wxUSE_SOCKETS
57 CPPUNIT_TEST(SocketStream);
58 #endif
59 CPPUNIT_TEST_SUITE_END();
60
61 void File();
62 void Pipe();
63 void Socket();
64 void Stdin();
65 void MemoryStream();
66 #if wxUSE_SOCKETS
67 void SocketStream();
68 #endif
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 //
76 void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
77 {
78 CPPUNIT_ASSERT(file.IsOpened());
79 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
80 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
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 //
91 void FileKindTestCase::TestFd(wxFile& file, bool expected)
92 {
93 CPPUNIT_ASSERT(file.IsOpened());
94 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
95 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
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 //
112 void FileKindTestCase::File()
113 {
114 TempFile tmp; // put first
115 wxFile file;
116 tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file);
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__
127 void FileKindTestCase::Pipe()
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__
149 void FileKindTestCase::Socket()
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
165 void FileKindTestCase::SocketStream()
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 //
182 void FileKindTestCase::MemoryStream()
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 //
198 void FileKindTestCase::Stdin()
199 {
200 if (isatty(0))
201 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
202 if (isatty(fileno(stdin)))
203 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
204 }
205
206 // register in the unnamed registry so that these tests are run by default
207 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
208
209 // also include in its own registry so that these tests can be run alone
210 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
211
212 #endif // wxUSE_STREAMS