]> git.saurik.com Git - wxWidgets.git/blame - tests/filekind/filekind.cpp
Add wx/meta/removeref.h header defining wxRemoveRef<> helper.
[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
526954c5 7// Licence: wxWindows licence
3c70014d
MW
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
b4d47ca0
VZ
21#if wxUSE_STREAMS
22
3c70014d
MW
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
b4d47ca0
VZ
35#ifdef __VISUALC__
36 #define isatty _isatty
37 #define fdopen _fdopen
38 #define fileno _fileno
39#endif
3c70014d
MW
40
41///////////////////////////////////////////////////////////////////////////////
42// The test case
43
0912690b 44class FileKindTestCase : public CppUnit::TestCase
3c70014d 45{
0912690b 46 CPPUNIT_TEST_SUITE(FileKindTestCase);
3c70014d
MW
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);
f4dd8614 56#if wxUSE_SOCKETS
3c70014d 57 CPPUNIT_TEST(SocketStream);
f4dd8614 58#endif
3c70014d
MW
59 CPPUNIT_TEST_SUITE_END();
60
61 void File();
62 void Pipe();
63 void Socket();
64 void Stdin();
65 void MemoryStream();
f4dd8614 66#if wxUSE_SOCKETS
3c70014d 67 void SocketStream();
f4dd8614 68#endif
3c70014d
MW
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//
0912690b 76void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
3c70014d
MW
77{
78 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
79 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
80 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
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//
0912690b 91void FileKindTestCase::TestFd(wxFile& file, bool expected)
3c70014d
MW
92{
93 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
94 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
95 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
96
97 wxFileInputStream inStream(file);
98 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
99
100 wxFileOutputStream outStream(file);
101 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
102}
103
104struct TempFile
105{
106 ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
107 wxString m_name;
108};
109
110// test with an ordinary file
111//
0912690b 112void FileKindTestCase::File()
3c70014d
MW
113{
114 TempFile tmp; // put first
115 wxFile file;
9a83f860 116 tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file);
3c70014d
MW
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__
0912690b 127void FileKindTestCase::Pipe()
3c70014d
MW
128{
129 int afd[2];
a5f01356 130 int rc;
3c70014d 131#ifdef __UNIX__
a5f01356 132 rc = pipe(afd);
3c70014d 133#else
a5f01356 134 rc = _pipe(afd, 256, O_BINARY);
3c70014d 135#endif
a5f01356 136 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc);
3c70014d
MW
137
138 wxFile file0(afd[0]);
139 wxFile file1(afd[1]);
140 TestFd(file0, false);
141 file0.Detach();
142
143 wxFFile ffile(fdopen(afd[0], "r"));
144 TestFILE(ffile, false);
145}
146#endif
147
148// test with a socket
149//
150#if defined __UNIX__
0912690b 151void FileKindTestCase::Socket()
3c70014d
MW
152{
153 int s = socket(PF_INET, SOCK_STREAM, 0);
154
155 wxFile file(s);
156 TestFd(file, false);
157 file.Detach();
158
159 wxFFile ffile(fdopen(s, "r"));
160 TestFILE(ffile, false);
161}
162#endif
163
164// Socket streams should be non-seekable
165//
166#if wxUSE_SOCKETS
0912690b 167void FileKindTestCase::SocketStream()
3c70014d
MW
168{
169 wxSocketClient client;
170 wxSocketInputStream inStream(client);
171 CPPUNIT_ASSERT(!inStream.IsSeekable());
172 wxSocketOutputStream outStream(client);
173 CPPUNIT_ASSERT(!outStream.IsSeekable());
174
175 wxBufferedInputStream nonSeekableBufferedInput(inStream);
176 CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable());
177 wxBufferedOutputStream nonSeekableBufferedOutput(outStream);
178 CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable());
179}
180#endif
181
182// Memory streams should be seekable
183//
0912690b 184void FileKindTestCase::MemoryStream()
3c70014d
MW
185{
186 char buf[20];
187 wxMemoryInputStream inStream(buf, sizeof(buf));
188 CPPUNIT_ASSERT(inStream.IsSeekable());
189 wxMemoryOutputStream outStream(buf, sizeof(buf));
190 CPPUNIT_ASSERT(outStream.IsSeekable());
191
192 wxBufferedInputStream seekableBufferedInput(inStream);
193 CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable());
194 wxBufferedOutputStream seekableBufferedOutput(outStream);
195 CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable());
196}
197
198// Stdin will usually be a terminal, if so then test it
199//
0912690b 200void FileKindTestCase::Stdin()
3c70014d
MW
201{
202 if (isatty(0))
0912690b 203 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
3c70014d 204 if (isatty(fileno(stdin)))
0912690b 205 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
3c70014d
MW
206}
207
208// register in the unnamed registry so that these tests are run by default
0912690b 209CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
3c70014d 210
e3778b4d 211// also include in its own registry so that these tests can be run alone
0912690b 212CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
3c70014d
MW
213
214#endif // wxUSE_STREAMS