]> git.saurik.com Git - wxWidgets.git/blame - tests/filekind/filekind.cpp
final revision
[wxWidgets.git] / tests / filekind / filekind.cpp
CommitLineData
3c70014d
MW
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/filetype/filetype.cpp
0912690b 3// Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
3c70014d
MW
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
0912690b 38class FileKindTestCase : public CppUnit::TestCase
3c70014d 39{
0912690b 40 CPPUNIT_TEST_SUITE(FileKindTestCase);
3c70014d
MW
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);
f4dd8614 50#if wxUSE_SOCKETS
3c70014d 51 CPPUNIT_TEST(SocketStream);
f4dd8614 52#endif
3c70014d
MW
53 CPPUNIT_TEST_SUITE_END();
54
55 void File();
56 void Pipe();
57 void Socket();
58 void Stdin();
59 void MemoryStream();
f4dd8614 60#if wxUSE_SOCKETS
3c70014d 61 void SocketStream();
f4dd8614 62#endif
3c70014d
MW
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//
0912690b 70void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
3c70014d
MW
71{
72 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
73 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
74 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
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//
0912690b 85void FileKindTestCase::TestFd(wxFile& file, bool expected)
3c70014d
MW
86{
87 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
88 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
89 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
90
91 wxFileInputStream inStream(file);
92 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
93
94 wxFileOutputStream outStream(file);
95 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
96}
97
98struct TempFile
99{
100 ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
101 wxString m_name;
102};
103
104// test with an ordinary file
105//
0912690b 106void FileKindTestCase::File()
3c70014d
MW
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__
0912690b 121void FileKindTestCase::Pipe()
3c70014d
MW
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__
0912690b 143void FileKindTestCase::Socket()
3c70014d
MW
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
0912690b 159void FileKindTestCase::SocketStream()
3c70014d
MW
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//
0912690b 176void FileKindTestCase::MemoryStream()
3c70014d
MW
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//
0912690b 192void FileKindTestCase::Stdin()
3c70014d
MW
193{
194 if (isatty(0))
0912690b 195 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
3c70014d 196 if (isatty(fileno(stdin)))
0912690b 197 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
3c70014d
MW
198}
199
200// register in the unnamed registry so that these tests are run by default
0912690b 201CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
3c70014d
MW
202
203// also include in it's own registry so that these tests can be run alone
0912690b 204CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
3c70014d
MW
205
206#endif // wxUSE_STREAMS