]> git.saurik.com Git - wxWidgets.git/blob - tests/filekind/filekind.cpp
[ 1602455 ] [docs] Fix docs for event used by pickers & collapsiblepane
[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: wxWidgets 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 #ifdef __UNIX__
22 #include <sys/socket.h>
23 #endif
24
25 #include "wx/file.h"
26 #include "wx/ffile.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"
32
33 #if wxUSE_STREAMS
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // The test case
37
38 class FileKindTestCase : public CppUnit::TestCase
39 {
40 CPPUNIT_TEST_SUITE(FileKindTestCase);
41 CPPUNIT_TEST(File);
42 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
43 CPPUNIT_TEST(Pipe);
44 #endif
45 #if defined __UNIX__
46 CPPUNIT_TEST(Socket);
47 #endif
48 CPPUNIT_TEST(Stdin);
49 CPPUNIT_TEST(MemoryStream);
50 #if wxUSE_SOCKETS
51 CPPUNIT_TEST(SocketStream);
52 #endif
53 CPPUNIT_TEST_SUITE_END();
54
55 void File();
56 void Pipe();
57 void Socket();
58 void Stdin();
59 void MemoryStream();
60 #if wxUSE_SOCKETS
61 void SocketStream();
62 #endif
63
64 void TestFILE(wxFFile& file, bool expected);
65 void TestFd(wxFile& file, bool expected);
66 };
67
68 // test a wxFFile and wxFFileInput/OutputStreams of a known type
69 //
70 void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
71 {
72 CPPUNIT_ASSERT(file.IsOpened());
73 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
74 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
75
76 wxFFileInputStream inStream(file);
77 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
78
79 wxFFileOutputStream outStream(file);
80 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
81 }
82
83 // test a wxFile and wxFileInput/OutputStreams of a known type
84 //
85 void FileKindTestCase::TestFd(wxFile& file, bool expected)
86 {
87 CPPUNIT_ASSERT(file.IsOpened());
88 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
89 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
90
91 wxFileInputStream inStream(file);
92 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
93
94 wxFileOutputStream outStream(file);
95 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
96 }
97
98 struct TempFile
99 {
100 ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
101 wxString m_name;
102 };
103
104 // test with an ordinary file
105 //
106 void FileKindTestCase::File()
107 {
108 TempFile tmp; // put first
109 wxFile file;
110 tmp.m_name = wxFileName::CreateTempFileName(_T("wxft"), &file);
111 TestFd(file, true);
112 file.Close();
113
114 wxFFile ffile(tmp.m_name);
115 TestFILE(ffile, true);
116 }
117
118 // test with a pipe
119 //
120 #if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
121 void FileKindTestCase::Pipe()
122 {
123 int afd[2];
124 #ifdef __UNIX__
125 pipe(afd);
126 #else
127 _pipe(afd, 256, O_BINARY);
128 #endif
129
130 wxFile file0(afd[0]);
131 wxFile file1(afd[1]);
132 TestFd(file0, false);
133 file0.Detach();
134
135 wxFFile ffile(fdopen(afd[0], "r"));
136 TestFILE(ffile, false);
137 }
138 #endif
139
140 // test with a socket
141 //
142 #if defined __UNIX__
143 void FileKindTestCase::Socket()
144 {
145 int s = socket(PF_INET, SOCK_STREAM, 0);
146
147 wxFile file(s);
148 TestFd(file, false);
149 file.Detach();
150
151 wxFFile ffile(fdopen(s, "r"));
152 TestFILE(ffile, false);
153 }
154 #endif
155
156 // Socket streams should be non-seekable
157 //
158 #if wxUSE_SOCKETS
159 void FileKindTestCase::SocketStream()
160 {
161 wxSocketClient client;
162 wxSocketInputStream inStream(client);
163 CPPUNIT_ASSERT(!inStream.IsSeekable());
164 wxSocketOutputStream outStream(client);
165 CPPUNIT_ASSERT(!outStream.IsSeekable());
166
167 wxBufferedInputStream nonSeekableBufferedInput(inStream);
168 CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable());
169 wxBufferedOutputStream nonSeekableBufferedOutput(outStream);
170 CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable());
171 }
172 #endif
173
174 // Memory streams should be seekable
175 //
176 void FileKindTestCase::MemoryStream()
177 {
178 char buf[20];
179 wxMemoryInputStream inStream(buf, sizeof(buf));
180 CPPUNIT_ASSERT(inStream.IsSeekable());
181 wxMemoryOutputStream outStream(buf, sizeof(buf));
182 CPPUNIT_ASSERT(outStream.IsSeekable());
183
184 wxBufferedInputStream seekableBufferedInput(inStream);
185 CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable());
186 wxBufferedOutputStream seekableBufferedOutput(outStream);
187 CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable());
188 }
189
190 // Stdin will usually be a terminal, if so then test it
191 //
192 void FileKindTestCase::Stdin()
193 {
194 if (isatty(0))
195 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
196 if (isatty(fileno(stdin)))
197 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
198 }
199
200 // register in the unnamed registry so that these tests are run by default
201 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
202
203 // also include in it's own registry so that these tests can be run alone
204 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
205
206 #endif // wxUSE_STREAMS