2005-02-11 12:39:03 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/filetype/filetype.cpp
|
2005-02-12 21:53:51 +00:00
|
|
|
// Purpose: Test wxGetFileKind and wxStreamBase::IsSeekable
|
2005-02-11 12:39:03 +00:00
|
|
|
// Author: Mike Wetherell
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2005 Mike Wetherell
|
|
|
|
// Licence: wxWidgets licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __UNIX__
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/file.h"
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/wfstream.h"
|
|
|
|
#include "wx/filename.h"
|
|
|
|
#include "wx/socket.h"
|
|
|
|
#include "wx/sckstrm.h"
|
|
|
|
#include "wx/mstream.h"
|
|
|
|
|
|
|
|
#if wxUSE_STREAMS
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The test case
|
|
|
|
|
2005-02-12 21:53:51 +00:00
|
|
|
class FileKindTestCase : public CppUnit::TestCase
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_TEST_SUITE(FileKindTestCase);
|
2005-02-11 12:39:03 +00:00
|
|
|
CPPUNIT_TEST(File);
|
|
|
|
#if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
|
|
|
|
CPPUNIT_TEST(Pipe);
|
|
|
|
#endif
|
|
|
|
#if defined __UNIX__
|
|
|
|
CPPUNIT_TEST(Socket);
|
|
|
|
#endif
|
|
|
|
CPPUNIT_TEST(Stdin);
|
|
|
|
CPPUNIT_TEST(MemoryStream);
|
2005-04-25 10:32:55 +00:00
|
|
|
#if wxUSE_SOCKETS
|
2005-02-11 12:39:03 +00:00
|
|
|
CPPUNIT_TEST(SocketStream);
|
2005-04-25 10:32:55 +00:00
|
|
|
#endif
|
2005-02-11 12:39:03 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void File();
|
|
|
|
void Pipe();
|
|
|
|
void Socket();
|
|
|
|
void Stdin();
|
|
|
|
void MemoryStream();
|
2005-04-25 10:32:55 +00:00
|
|
|
#if wxUSE_SOCKETS
|
2005-02-11 12:39:03 +00:00
|
|
|
void SocketStream();
|
2005-04-25 10:32:55 +00:00
|
|
|
#endif
|
2005-02-11 12:39:03 +00:00
|
|
|
|
|
|
|
void TestFILE(wxFFile& file, bool expected);
|
|
|
|
void TestFd(wxFile& file, bool expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
// test a wxFFile and wxFFileInput/OutputStreams of a known type
|
|
|
|
//
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::TestFILE(wxFFile& file, bool expected)
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_ASSERT((wxGetFileKind(file.fp()) == wxFILE_KIND_DISK) == expected);
|
|
|
|
CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
|
2005-02-11 12:39:03 +00:00
|
|
|
|
|
|
|
wxFFileInputStream inStream(file);
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
|
|
|
|
|
|
|
|
wxFFileOutputStream outStream(file);
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test a wxFile and wxFileInput/OutputStreams of a known type
|
|
|
|
//
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::TestFd(wxFile& file, bool expected)
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_ASSERT((wxGetFileKind(file.fd()) == wxFILE_KIND_DISK) == expected);
|
|
|
|
CPPUNIT_ASSERT((file.GetKind() == wxFILE_KIND_DISK) == expected);
|
2005-02-11 12:39:03 +00:00
|
|
|
|
|
|
|
wxFileInputStream inStream(file);
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable() == expected);
|
|
|
|
|
|
|
|
wxFileOutputStream outStream(file);
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable() == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TempFile
|
|
|
|
{
|
|
|
|
~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); }
|
|
|
|
wxString m_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
// test with an ordinary file
|
|
|
|
//
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::File()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
TempFile tmp; // put first
|
|
|
|
wxFile file;
|
|
|
|
tmp.m_name = wxFileName::CreateTempFileName(_T("wxft"), &file);
|
|
|
|
TestFd(file, true);
|
|
|
|
file.Close();
|
|
|
|
|
|
|
|
wxFFile ffile(tmp.m_name);
|
|
|
|
TestFILE(ffile, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with a pipe
|
|
|
|
//
|
|
|
|
#if defined __UNIX__ || defined _MSC_VER || defined __MINGW32__
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::Pipe()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
int afd[2];
|
|
|
|
#ifdef __UNIX__
|
|
|
|
pipe(afd);
|
|
|
|
#else
|
|
|
|
_pipe(afd, 256, O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wxFile file0(afd[0]);
|
|
|
|
wxFile file1(afd[1]);
|
|
|
|
TestFd(file0, false);
|
|
|
|
file0.Detach();
|
|
|
|
|
|
|
|
wxFFile ffile(fdopen(afd[0], "r"));
|
|
|
|
TestFILE(ffile, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// test with a socket
|
|
|
|
//
|
|
|
|
#if defined __UNIX__
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::Socket()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
int s = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
wxFile file(s);
|
|
|
|
TestFd(file, false);
|
|
|
|
file.Detach();
|
|
|
|
|
|
|
|
wxFFile ffile(fdopen(s, "r"));
|
|
|
|
TestFILE(ffile, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Socket streams should be non-seekable
|
|
|
|
//
|
|
|
|
#if wxUSE_SOCKETS
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::SocketStream()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
wxSocketClient client;
|
|
|
|
wxSocketInputStream inStream(client);
|
|
|
|
CPPUNIT_ASSERT(!inStream.IsSeekable());
|
|
|
|
wxSocketOutputStream outStream(client);
|
|
|
|
CPPUNIT_ASSERT(!outStream.IsSeekable());
|
|
|
|
|
|
|
|
wxBufferedInputStream nonSeekableBufferedInput(inStream);
|
|
|
|
CPPUNIT_ASSERT(!nonSeekableBufferedInput.IsSeekable());
|
|
|
|
wxBufferedOutputStream nonSeekableBufferedOutput(outStream);
|
|
|
|
CPPUNIT_ASSERT(!nonSeekableBufferedOutput.IsSeekable());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Memory streams should be seekable
|
|
|
|
//
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::MemoryStream()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
wxMemoryInputStream inStream(buf, sizeof(buf));
|
|
|
|
CPPUNIT_ASSERT(inStream.IsSeekable());
|
|
|
|
wxMemoryOutputStream outStream(buf, sizeof(buf));
|
|
|
|
CPPUNIT_ASSERT(outStream.IsSeekable());
|
|
|
|
|
|
|
|
wxBufferedInputStream seekableBufferedInput(inStream);
|
|
|
|
CPPUNIT_ASSERT(seekableBufferedInput.IsSeekable());
|
|
|
|
wxBufferedOutputStream seekableBufferedOutput(outStream);
|
|
|
|
CPPUNIT_ASSERT(seekableBufferedOutput.IsSeekable());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stdin will usually be a terminal, if so then test it
|
|
|
|
//
|
2005-02-12 21:53:51 +00:00
|
|
|
void FileKindTestCase::Stdin()
|
2005-02-11 12:39:03 +00:00
|
|
|
{
|
|
|
|
if (isatty(0))
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_ASSERT(wxGetFileKind(0) == wxFILE_KIND_TERMINAL);
|
2005-02-11 12:39:03 +00:00
|
|
|
if (isatty(fileno(stdin)))
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_ASSERT(wxGetFileKind(stdin) == wxFILE_KIND_TERMINAL);
|
2005-02-11 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(FileKindTestCase);
|
2005-02-11 12:39:03 +00:00
|
|
|
|
|
|
|
// also include in it's own registry so that these tests can be run alone
|
2005-02-12 21:53:51 +00:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileKindTestCase, "FileKindTestCase");
|
2005-02-11 12:39:03 +00:00
|
|
|
|
|
|
|
#endif // wxUSE_STREAMS
|