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