]> git.saurik.com Git - wxWidgets.git/blob - tests/filekind/filekind.cpp
Don't set cell value in wxDataViewEvent in one place only.
[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: wxWindows 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 #if wxUSE_STREAMS
22
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
35 #ifdef __VISUALC__
36 #define isatty _isatty
37 #define fdopen _fdopen
38 #define fileno _fileno
39 #endif
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // The test case
43
44 class FileKindTestCase : public CppUnit::TestCase
45 {
46 CPPUNIT_TEST_SUITE(FileKindTestCase);
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);
56 #if wxUSE_SOCKETS
57 CPPUNIT_TEST(SocketStream);
58 #endif
59 CPPUNIT_TEST_SUITE_END();
60
61 void File();
62 void Pipe();
63 void Socket();
64 void Stdin();
65 void MemoryStream();
66 #if wxUSE_SOCKETS
67 void SocketStream();
68 #endif
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 //
76 void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
77 {
78 CPPUNIT_ASSERT(file.IsOpened());
79 CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
80 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
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 //
91 void FileKindTestCase::TestFd(wxFile& file, bool expected)
92 {
93 CPPUNIT_ASSERT(file.IsOpened());
94 CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
95 CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
96
97 wxFileInputStream inStream(file);
98 CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
99
100 wxFileOutputStream outStream(file);
101 CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
102 }
103
104 struct TempFile
105 {
106 ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
107 wxString m_name;
108 };
109
110 // test with an ordinary file
111 //
112 void FileKindTestCase::File()
113 {
114 TempFile tmp; // put first
115 wxFile file;
116 tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file);
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__
127 void FileKindTestCase::Pipe()
128 {
129 int afd[2];
130 int rc;
131 #ifdef __UNIX__
132 rc = pipe(afd);
133 #else
134 rc = _pipe(afd, 256, O_BINARY);
135 #endif
136 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to create pipe", 0, rc);
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__
151 void FileKindTestCase::Socket()
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
167 void FileKindTestCase::SocketStream()
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 //
184 void FileKindTestCase::MemoryStream()
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 //
200 void FileKindTestCase::Stdin()
201 {
202 if (isatty(0))
203 CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
204 if (isatty(fileno(stdin)))
205 CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
206 }
207
208 // register in the unnamed registry so that these tests are run by default
209 CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
210
211 // also include in its own registry so that these tests can be run alone
212 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
213
214 #endif // wxUSE_STREAMS