]> git.saurik.com Git - wxWidgets.git/blame - tests/filekind/filekind.cpp
Store HTML "id" parameter value in wxHtmlCell.
[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 4// Author: Mike Wetherell
3c70014d 5// Copyright: (c) 2005 Mike Wetherell
526954c5 6// Licence: wxWindows licence
3c70014d
MW
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
b4d47ca0
VZ
20#if wxUSE_STREAMS
21
3c70014d
MW
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
b4d47ca0
VZ
34#ifdef __VISUALC__
35 #define isatty _isatty
36 #define fdopen _fdopen
37 #define fileno _fileno
38#endif
3c70014d
MW
39
40///////////////////////////////////////////////////////////////////////////////
41// The test case
42
0912690b 43class FileKindTestCase : public CppUnit::TestCase
3c70014d 44{
0912690b 45 CPPUNIT_TEST_SUITE(FileKindTestCase);
3c70014d
MW
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);
f4dd8614 55#if wxUSE_SOCKETS
3c70014d 56 CPPUNIT_TEST(SocketStream);
f4dd8614 57#endif
3c70014d
MW
58 CPPUNIT_TEST_SUITE_END();
59
60 void File();
61 void Pipe();
62 void Socket();
63 void Stdin();
64 void MemoryStream();
f4dd8614 65#if wxUSE_SOCKETS
3c70014d 66 void SocketStream();
f4dd8614 67#endif
3c70014d
MW
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//
0912690b 75void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
3c70014d
MW
76{
77 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
78 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
79 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
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//
0912690b 90void FileKindTestCase::TestFd(wxFile& file, bool expected)
3c70014d
MW
91{
92 CPPUNIT_ASSERT(file.IsOpened());
0912690b
MW
93 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
94 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
3c70014d
MW
95
96 wxFileInputStream inStream(file);
97 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
98
99 wxFileOutputStream outStream(file);
100 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
101}
102
103struct TempFile
104{
105 ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
106 wxString m_name;
107};
108
109// test with an ordinary file
110//
0912690b 111void FileKindTestCase::File()
3c70014d
MW
112{
113 TempFile tmp; // put first
114 wxFile file;
9a83f860 115 tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file);
3c70014d
MW
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__
0912690b 126void FileKindTestCase::Pipe()
3c70014d
MW
127{
128 int afd[2];
a5f01356 129 int rc;
3c70014d 130#ifdef __UNIX__
a5f01356 131 rc = pipe(afd);
3c70014d 132#else
a5f01356 133 rc = _pipe(afd, 256, O_BINARY);
3c70014d 134#endif
a5f01356 135 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc);
3c70014d
MW
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__
0912690b 150void FileKindTestCase::Socket()
3c70014d
MW
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
0912690b 166void FileKindTestCase::SocketStream()
3c70014d
MW
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//
0912690b 183void FileKindTestCase::MemoryStream()
3c70014d
MW
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//
0912690b 199void FileKindTestCase::Stdin()
3c70014d
MW
200{
201 if (isatty(0))
0912690b 202 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
3c70014d 203 if (isatty(fileno(stdin)))
0912690b 204 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
3c70014d
MW
205}
206
207// register in the unnamed registry so that these tests are run by default
0912690b 208CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
3c70014d 209
e3778b4d 210// also include in its own registry so that these tests can be run alone
0912690b 211CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
3c70014d
MW
212
213#endif // wxUSE_STREAMS