// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
-#include "wx/wxprec.h"
+#include "testprec.h"
#ifdef __BORLANDC__
# pragma hdrstop
#define WX_TEST_ARCHIVE_ITERATOR
+// VC++ 6 warns that the list iterator's '->' operator will not work whenever
+// std::list is used with a non-pointer, so switch it off.
+#if defined _MSC_VER && _MSC_VER < 1300
+#pragma warning (disable:4284)
+#endif
+
#include "wx/zipstrm.h"
#include "wx/mstream.h"
#include "wx/wfstream.h"
#include "wx/dir.h"
-#include "wx/cppunit.h"
#include <string>
#include <list>
+#include <map>
#include <sys/stat.h>
+using std::string;
+using std::auto_ptr;
+
+
// Check whether member templates can be used
//
#if defined __GNUC__ && \
private:
wxDateTime m_dt;
size_t m_len;
- const char *m_data;
+ char *m_data;
wxString m_comment;
bool m_isText;
};
m_len(len),
m_isText(len > 0)
{
- char *d = new char[len];
- memcpy(d, data, len);
- m_data = d;
+ m_data = new char[len];
+ memcpy(m_data, data, len);
for (int i = 0; i < len && m_isText; i++)
m_isText = (signed char)m_data[i] > 0;
wxFileOffset GetLength() const { return m_size; }
// gives away the data, this stream is then empty, and can be reused
- void GetData(const char*& data, size_t& size);
+ void GetData(char*& data, size_t& size);
enum { STUB_SIZE = 2048, INITIAL_SIZE = 0x18000, SEEK_LIMIT = 0x100000 };
}
if (pos < 0 || pos > SEEK_LIMIT)
return wxInvalidOffset;
- m_pos = pos;
+ m_pos = (size_t)pos;
return m_pos;
}
return wxInvalidOffset;
wxCHECK(newsize > m_pos, 0);
if (m_capacity < newsize) {
- size_t capacity = m_capacity ? m_capacity : INITIAL_SIZE;
+ size_t capacity = m_capacity ? m_capacity : (size_t)INITIAL_SIZE;
while (capacity < newsize) {
capacity <<= 1;
return size;
}
-void TestOutputStream::GetData(const char*& data, size_t& size)
+void TestOutputStream::GetData(char*& data, size_t& size)
{
data = m_data;
size = m_size;
int m_options;
size_t m_pos;
size_t m_size;
- const char *m_data;
+ char *m_data;
};
TestInputStream::TestInputStream(const TestInputStream& in)
- : m_options(in.m_options),
+ : wxInputStream(),
+ m_options(in.m_options),
m_pos(in.m_pos),
m_size(in.m_size)
{
- char *p = new char[m_size];
- memcpy(p, in.m_data, m_size);
- m_data = p;
+ m_data = new char[m_size];
+ memcpy(m_data, in.m_data, m_size);
}
void TestInputStream::Rewind()
}
if (pos < 0 || pos > TestOutputStream::SEEK_LIMIT)
return wxInvalidOffset;
- m_pos = pos;
+ m_pos = (size_t)pos;
return m_pos;
}
return wxInvalidOffset;
class ArchiveTestCase : public CppUnit::TestCase
{
public:
- ArchiveTestCase(const wxString& name,
+ ArchiveTestCase(string name,
int id,
- typename Classes::ClassFactoryT *factory,
+ wxArchiveClassFactory *factory,
int options,
const wxString& archiver = wxEmptyString,
const wxString& unarchiver = wxEmptyString);
typedef std::map<wxString, TestEntry*> TestEntries;
TestEntries m_testEntries; // test data
- std::auto_ptr<ClassFactoryT> m_factory; // factory to make classes
+ auto_ptr<ClassFactoryT> m_factory; // factory to make classes
int m_options; // test options
wxDateTime m_timeStamp; // timestamp to give test entries
int m_id; // select between the possibilites
wxString m_unarchiver; // external unarchiver
};
+// Constructor
+// The only way I could get this to compile on VC++ 5.0 was to pass 'factory'
+// as a wxArchiveFactory* then cast it, even then only with some ifdefing.
+//
template <class Classes>
-ArchiveTestCase<Classes>::ArchiveTestCase(const wxString& name,
- int id,
- ClassFactoryT *factory,
- int options,
- const wxString& archiver,
- const wxString& unarchiver)
- : CppUnit::TestCase(std::string(name.mb_str())),
- m_factory(factory),
+ArchiveTestCase<Classes>::ArchiveTestCase(
+ string name,
+ int id,
+ wxArchiveClassFactory *factory,
+ int options,
+ const wxString& archiver,
+ const wxString& unarchiver)
+ :
+ CppUnit::TestCase(name),
+#if defined _MSC_VER && _MSC_VER < 1300
+ m_factory(dynamic_cast<Classes::ClassFactoryT*>(factory)),
+#else
+ m_factory(dynamic_cast<typename Classes::ClassFactoryT*>(factory)),
+#endif
m_options(options),
m_timeStamp(1, wxDateTime::Mar, 2005, 12, 0),
m_id(id),
m_archiver(archiver),
m_unarchiver(unarchiver)
{
+ wxASSERT(m_factory.get() != NULL);
}
template <class Classes>
ExtractArchive(in);
else
ExtractArchive(in, m_unarchiver);
-
+
// check that all the test entries were found in the archive
CPPUNIT_ASSERT(m_testEntries.empty());
}
{
wxCharBuffer buf(len);
for (int i = 0; i < len; i++)
- buf.data()[i] = value == EOF ? rand() : value;
+ buf.data()[i] = (char)(value == EOF ? rand() : value);
return Add(name, buf, len);
}
template <class Classes>
void ArchiveTestCase<Classes>::CreateArchive(wxOutputStream& out)
{
- std::auto_ptr<OutputStreamT> arc(m_factory->NewStream(out));
+ auto_ptr<OutputStreamT> arc(m_factory->NewStream(out));
TestEntries::iterator it;
OnCreateArchive(*arc);
// provide some context for the error message so that we know which
// iteration of the loop we were on
- std::string error_entry((_T(" '") + name + _T("'")).mb_str());
- std::string error_context(" failed for entry" + error_entry);
+ string error_entry((_T(" '") + name + _T("'")).mb_str());
+ string error_context(" failed for entry" + error_entry);
if ((choices & 2) || testEntry.IsText()) {
// try PutNextEntry(EntryT *pEntry)
- std::auto_ptr<EntryT> entry(m_factory->NewEntry());
+ auto_ptr<EntryT> entry(m_factory->NewEntry());
entry->SetName(name, wxPATH_UNIX);
if (setIsDir)
entry->SetIsDir();
void ArchiveTestCase<Classes>::ModifyArchive(wxInputStream& in,
wxOutputStream& out)
{
- std::auto_ptr<InputStreamT> arcIn(m_factory->NewStream(in));
- std::auto_ptr<OutputStreamT> arcOut(m_factory->NewStream(out));
- std::auto_ptr<EntryT> entry;
+ auto_ptr<InputStreamT> arcIn(m_factory->NewStream(in));
+ auto_ptr<OutputStreamT> arcOut(m_factory->NewStream(out));
+ EntryT *pEntry;
const wxString deleteName = _T("bin/bin1000");
const wxString renameFrom = _T("zero/zero1024");
arcOut->CopyArchiveMetaData(*arcIn);
- while (entry.reset(arcIn->GetNextEntry()), entry.get() != NULL) {
+ while ((pEntry = arcIn->GetNextEntry()) != NULL) {
+ auto_ptr<EntryT> entry(pEntry);
OnSetNotifier(*entry);
wxString name = entry->GetName(wxPATH_UNIX);
// provide some context for the error message so that we know which
// iteration of the loop we were on
- std::string error_entry((_T(" '") + name + _T("'")).mb_str());
- std::string error_context(" failed for entry" + error_entry);
+ string error_entry((_T(" '") + name + _T("'")).mb_str());
+ string error_context(" failed for entry" + error_entry);
if (name == deleteName) {
TestEntries::iterator it = m_testEntries.find(name);
// try adding a new entry
TestEntry& testEntry = Add(newName.mb_str(), newData);
- entry.reset(m_factory->NewEntry());
- entry->SetName(newName);
- entry->SetDateTime(testEntry.GetDateTime());
- entry->SetSize(testEntry.GetLength());
- OnCreateEntry(*arcOut, testEntry, entry.get());
- OnSetNotifier(*entry);
- CPPUNIT_ASSERT(arcOut->PutNextEntry(entry.release()));
+ auto_ptr<EntryT> newentry(m_factory->NewEntry());
+ newentry->SetName(newName);
+ newentry->SetDateTime(testEntry.GetDateTime());
+ newentry->SetSize(testEntry.GetLength());
+ OnCreateEntry(*arcOut, testEntry, newentry.get());
+ OnSetNotifier(*newentry);
+ CPPUNIT_ASSERT(arcOut->PutNextEntry(newentry.release()));
CPPUNIT_ASSERT(arcOut->Write(newData, strlen(newData)).IsOk());
// should work with or without explicit Close
typedef std::list<EntryPtr> Entries;
typedef typename Entries::iterator EntryIter;
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
int expectedTotal = m_testEntries.size();
EntryPtr entry;
Entries entries;
// provide some context for the error message so that we know which
// iteration of the loop we were on
- std::string error_entry((_T(" '") + name + _T("'")).mb_str());
- std::string error_context(" failed for entry" + error_entry);
+ string error_entry((_T(" '") + name + _T("'")).mb_str());
+ string error_context(" failed for entry" + error_entry);
TestEntries::iterator it = m_testEntries.find(name);
CPPUNIT_ASSERT_MESSAGE(
wxFileName fn(tmpdir.GetName());
fn.SetExt(_T("arc"));
wxString tmparc = fn.GetFullPath();
-
+
if (m_options & Stub)
in.SeekI(TestOutputStream::STUB_SIZE * 2);
// provide some context for the error message so that we know which
// iteration of the loop we were on
- std::string error_entry((_T(" '") + name + _T("'")).mb_str());
- std::string error_context(" failed for entry" + error_entry);
+ string error_entry((_T(" '") + name + _T("'")).mb_str());
+ string error_context(" failed for entry" + error_entry);
TestEntries::iterator it = m_testEntries.find(name);
CPPUNIT_ASSERT_MESSAGE(
CPPUNIT_ASSERT_MESSAGE(
"entry not found in archive" + error_entry, in.Ok());
- size_t size = in.GetLength();
+ size_t size = (size_t)in.GetLength();
wxCharBuffer buf(size);
CPPUNIT_ASSERT_MESSAGE("Read" + error_context,
in.Read(buf.data(), size).LastRead() == size);
typedef std::list<EntryT*> ArchiveCatalog;
typedef typename ArchiveCatalog::iterator CatalogIter;
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
size_t count = 0;
#ifdef WXARC_MEMBER_TEMPLATES
#endif
for (CatalogIter it = cat.begin(); it != cat.end(); ++it) {
- std::auto_ptr<EntryT> entry(*it);
+ auto_ptr<EntryT> entry(*it);
count += m_testEntries.count(entry->GetName(wxPATH_UNIX));
}
typedef std::map<wxString, EntryT*> ArchiveCatalog;
typedef typename ArchiveCatalog::iterator CatalogIter;
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
size_t count = 0;
#ifdef WXARC_MEMBER_TEMPLATES
#else
ArchiveCatalog cat;
for (PairIterT i(*arc); i != PairIterT(); ++i)
- cat.push_back(*i);
+ cat.insert(*i);
#endif
for (CatalogIter it = cat.begin(); it != cat.end(); ++it) {
- std::auto_ptr<EntryT> entry(it->second);
+ auto_ptr<EntryT> entry(it->second);
count += m_testEntries.count(entry->GetName(wxPATH_UNIX));
}
typedef typename ArchiveCatalog::iterator CatalogIter;
typedef wxArchiveIterator<InputStreamT, Ptr<EntryT> > Iter;
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
#ifdef WXARC_MEMBER_TEMPLATES
ArchiveCatalog cat((Iter)*arc, Iter());
template <class Classes>
void ArchiveTestCase<Classes>::TestSmartPairIterator(wxInputStream& in)
{
+#if defined _MSC_VER && defined _MSC_VER < 1200
+ // With VC++ 5.0 the '=' operator of std::pair breaks when the second
+ // type is Ptr<EntryT>, so this iterator can't be made to work.
+ (void)in;
+#else
typedef std::map<wxString, Ptr<EntryT> > ArchiveCatalog;
typedef typename ArchiveCatalog::iterator CatalogIter;
typedef wxArchiveIterator<InputStreamT,
std::pair<wxString, Ptr<EntryT> > > PairIter;
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
#ifdef WXARC_MEMBER_TEMPLATES
ArchiveCatalog cat((PairIter)*arc, PairIter());
#else
ArchiveCatalog cat;
for (PairIter i(*arc); i != PairIter(); ++i)
- cat.push_back(*i);
+ cat.insert(*i);
#endif
CPPUNIT_ASSERT(m_testEntries.size() == cat.size());
for (CatalogIter it = cat.begin(); it != cat.end(); ++it)
CPPUNIT_ASSERT(m_testEntries.count(it->second->GetName(wxPATH_UNIX)));
+#endif
}
// try reading two entries at the same time
// create two archive input streams
TestInputStream in2(in);
- std::auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
- std::auto_ptr<InputStreamT> arc2(m_factory->NewStream(in2));
+ auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
+ auto_ptr<InputStreamT> arc2(m_factory->NewStream(in2));
// load the catalog
#ifdef WXARC_MEMBER_TEMPLATES
#else
ArchiveCatalog cat;
for (PairIter i(*arc); i != PairIter(); ++i)
- cat.push_back(*i);
+ cat.insert(*i);
#endif
// the names of two entries to read
class ZipTestCase : public ArchiveTestCase<ZipClasses>
{
public:
- ZipTestCase(const wxString& name,
+ ZipTestCase(string name,
int id,
int options,
const wxString& archiver = wxEmptyString,
protected:
void OnCreateArchive(wxZipOutputStream& zip);
-
+
void OnArchiveExtracted(wxZipInputStream& zip, int expectedTotal);
-
+
void OnCreateEntry(wxZipOutputStream& zip,
TestEntry& testEntry,
wxZipEntry *entry);
-
+
void OnEntryExtracted(wxZipEntry& entry,
const TestEntry& testEntry,
wxZipInputStream *arc);
{
// provide some context for the error message so that we know which
// iteration of the loop we were on
- std::string error_entry((_T(" '") + entry.GetName() + _T("'")).mb_str());
- std::string error_context(" failed for entry" + error_entry);
+ wxString name = _T(" '") + entry.GetName() + _T("'");
+ string error_entry(name.mb_str());
+ string error_context(" failed for entry" + error_entry);
CPPUNIT_ASSERT_MESSAGE("GetComment" + error_context,
entry.GetComment() == testEntry.GetComment());
class ZipPipeTestCase : public CppUnit::TestCase
{
public:
- ZipPipeTestCase(const wxString& name, int options) :
- CppUnit::TestCase(std::string(name.mb_str())), m_options(options) { }
+ ZipPipeTestCase(string name, int options) :
+ CppUnit::TestCase(name), m_options(options) { }
protected:
void runTest();
TestInputStream in(out);
wxZipInputStream zip(in);
- std::auto_ptr<wxZipEntry> entry(zip.GetNextEntry());
+ auto_ptr<wxZipEntry> entry(zip.GetNextEntry());
CPPUNIT_ASSERT(entry.get() != NULL);
if ((m_options & PipeIn) == 0)
void AddCmd(wxArrayString& cmdlist, const wxString& cmd);
bool IsInPath(const wxString& cmd);
- wxString Description(const wxString& type,
- int options,
- bool genericInterface = false,
- const wxString& archiver = wxEmptyString,
- const wxString& unarchiver = wxEmptyString);
+ string Description(const wxString& type,
+ int options,
+ bool genericInterface = false,
+ const wxString& archiver = wxEmptyString,
+ const wxString& unarchiver = wxEmptyString);
};
ArchiveTestSuite::ArchiveTestSuite()
// unzip doesn't support piping in the zip
if ((options & PipeIn) && !i->empty())
continue;
-#ifdef WXARC_NO_POPEN
+#ifdef WXARC_NO_POPEN
// if no popen then can use piped output of zip
if ((options & PipeOut) && !j->empty())
continue;
#endif
- wxString name = Description(_T("wxZip"), options,
- genInterface != 0, *j, *i);
+ string name = Description(_T("wxZip"), options,
+ genInterface != 0, *j, *i);
if (genInterface)
addTest(new ArchiveTestCase<ArchiveClasses>(
m_id++;
}
-#ifndef WXARC_NO_POPEN
+#ifndef WXARC_NO_POPEN
// if have popen then can check the piped output of 'zip - -'
if (IsInPath(_T("zip")))
for (int options = 0; options <= PipeIn; options += PipeIn) {
- wxString name = Description(_T("ZipPipeTestCase"), options);
+ string name = Description(_T("ZipPipeTestCase"), options);
addTest(new ZipPipeTestCase(name, options));
m_id++;
}
// make a display string for the option bits
//
-wxString ArchiveTestSuite::Description(const wxString& type,
- int options,
- bool genericInterface,
- const wxString& archiver,
- const wxString& unarchiver)
+string ArchiveTestSuite::Description(const wxString& type,
+ int options,
+ bool genericInterface,
+ const wxString& archiver,
+ const wxString& unarchiver)
{
wxString descr;
descr << m_id << _T(" ");
-
+
if (genericInterface)
descr << _T("wxArchive (") << type << _T(")");
else
descr << _T(" ") << archiver.BeforeFirst(_T(' '));
if (!unarchiver.empty())
descr << _T(" ") << unarchiver.BeforeFirst(_T(' '));
-
+
wxString optstr;
if ((options & PipeIn) != 0)
descr << optstr;
- return descr;
+ return (const char*)descr.mb_str();
}
// register in the unnamed registry so that these tests are run by default