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