X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/00375592f92f68c4ca3f44d8e839bcfd47adc4e1..8ac8dba00c38e34f3b69d4199366c89bf19e2cd8:/tests/archive/archivetest.cpp diff --git a/tests/archive/archivetest.cpp b/tests/archive/archivetest.cpp index e836c3a16e..5395d6c2f8 100644 --- a/tests/archive/archivetest.cpp +++ b/tests/archive/archivetest.cpp @@ -7,7 +7,7 @@ // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// -#include "wx/wxprec.h" +#include "testprec.h" #ifdef __BORLANDC__ # pragma hdrstop @@ -21,15 +21,25 @@ #define WX_TEST_ARCHIVE_ITERATOR +// This sample uses some advanced typedef syntax that messes +// up MSVC 6 - turn off its warning about it +#if defined _MSC_VER +#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 #include +#include #include +using std::string; +using std::auto_ptr; + + // Check whether member templates can be used // #if defined __GNUC__ && \ @@ -104,7 +114,7 @@ class TestEntry { public: TestEntry(const wxDateTime& dt, int len, const char *data); - ~TestEntry() { delete [] m_data; } + ~TestEntry() { delete [] (char*) m_data; } wxDateTime GetDateTime() const { return m_dt; } wxFileOffset GetLength() const { return m_len; } @@ -150,7 +160,7 @@ public: ~TestOutputStream() { delete [] m_data; } int GetOptions() const { return m_options; } - size_t GetSize() const { return m_size; } + 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); @@ -201,7 +211,7 @@ wxFileOffset TestOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) } if (pos < 0 || pos > SEEK_LIMIT) return wxInvalidOffset; - m_pos = pos; + m_pos = (size_t)pos; return m_pos; } return wxInvalidOffset; @@ -209,7 +219,7 @@ wxFileOffset TestOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) wxFileOffset TestOutputStream::OnSysTell() const { - return (m_options & PipeOut) == 0 ? m_pos : wxInvalidOffset; + return (m_options & PipeOut) == 0 ? (wxFileOffset)m_pos : wxInvalidOffset; } size_t TestOutputStream::OnSysWrite(const void *buffer, size_t size) @@ -279,10 +289,10 @@ public: TestInputStream(TestOutputStream& out) : m_data(NULL) { SetData(out); } // this ctor 'dups' TestInputStream(const TestInputStream& in); - ~TestInputStream() { delete [] m_data; } + ~TestInputStream() { delete [] (char*) m_data; } void Rewind(); - size_t GetSize() const { return m_size; } + wxFileOffset GetLength() const { return m_size; } void SetData(TestOutputStream& out); private: @@ -323,7 +333,7 @@ void TestInputStream::Rewind() void TestInputStream::SetData(TestOutputStream& out) { - delete [] m_data; + delete [] (char*) m_data; m_options = out.GetOptions(); out.GetData(m_data, m_size); Rewind(); @@ -340,7 +350,7 @@ wxFileOffset TestInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) } if (pos < 0 || pos > TestOutputStream::SEEK_LIMIT) return wxInvalidOffset; - m_pos = pos; + m_pos = (size_t)pos; return m_pos; } return wxInvalidOffset; @@ -348,7 +358,7 @@ wxFileOffset TestInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) wxFileOffset TestInputStream::OnSysTell() const { - return (m_options & PipeIn) == 0 ? m_pos : wxInvalidOffset; + return (m_options & PipeIn) == 0 ? (wxFileOffset)m_pos : wxInvalidOffset; } size_t TestInputStream::OnSysRead(void *buffer, size_t size) @@ -527,9 +537,9 @@ template 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); @@ -592,7 +602,7 @@ protected: typedef std::map TestEntries; TestEntries m_testEntries; // test data - std::auto_ptr m_factory; // factory to make classes + auto_ptr 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 @@ -600,23 +610,34 @@ protected: 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 -ArchiveTestCase::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::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(factory)), +#else + m_factory(dynamic_cast(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 ArchiveTestCase::~ArchiveTestCase() { @@ -638,7 +659,7 @@ void ArchiveTestCase::runTest() CreateArchive(out, m_archiver); // check archive could be created - CPPUNIT_ASSERT(out.GetSize() > 0); + CPPUNIT_ASSERT(out.GetLength() > 0); TestInputStream in(out); @@ -722,7 +743,7 @@ TestEntry& ArchiveTestCase::Add(const char *name, template void ArchiveTestCase::CreateArchive(wxOutputStream& out) { - std::auto_ptr arc(m_factory->NewStream(out)); + auto_ptr arc(m_factory->NewStream(out)); TestEntries::iterator it; OnCreateArchive(*arc); @@ -745,12 +766,12 @@ void ArchiveTestCase::CreateArchive(wxOutputStream& out) // 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 entry(m_factory->NewEntry()); + auto_ptr entry(m_factory->NewEntry()); entry->SetName(name, wxPATH_UNIX); if (setIsDir) entry->SetIsDir(); @@ -861,9 +882,9 @@ template void ArchiveTestCase::ModifyArchive(wxInputStream& in, wxOutputStream& out) { - std::auto_ptr arcIn(m_factory->NewStream(in)); - std::auto_ptr arcOut(m_factory->NewStream(out)); - std::auto_ptr entry; + auto_ptr arcIn(m_factory->NewStream(in)); + auto_ptr arcOut(m_factory->NewStream(out)); + ; const wxString deleteName = _T("bin/bin1000"); const wxString renameFrom = _T("zero/zero1024"); @@ -873,14 +894,19 @@ void ArchiveTestCase::ModifyArchive(wxInputStream& in, arcOut->CopyArchiveMetaData(*arcIn); - while (entry.reset(arcIn->GetNextEntry()), entry.get() != NULL) { - OnSetNotifier(*entry); - wxString name = entry->GetName(wxPATH_UNIX); + auto_ptr* pEntry; + + for (pEntry = new auto_ptr(arcIn->GetNextEntry()) ; + pEntry->get() != NULL ; + delete pEntry, pEntry = new auto_ptr(arcIn->GetNextEntry())) + { + OnSetNotifier(**pEntry); + wxString name = (*pEntry)->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); @@ -893,7 +919,7 @@ void ArchiveTestCase::ModifyArchive(wxInputStream& in, } else { if (name == renameFrom) { - entry->SetName(renameTo); + (*pEntry)->SetName(renameTo); TestEntries::iterator it = m_testEntries.find(renameFrom); CPPUNIT_ASSERT_MESSAGE( "rename failed (already renamed?) for" + error_entry, @@ -904,10 +930,12 @@ void ArchiveTestCase::ModifyArchive(wxInputStream& in, } CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context, - arcOut->CopyEntry(entry.release(), *arcIn)); + arcOut->CopyEntry(pEntry->release(), *arcIn)); } } + delete pEntry; + // check that the deletion and rename were done CPPUNIT_ASSERT(m_testEntries.count(deleteName) == 0); CPPUNIT_ASSERT(m_testEntries.count(renameFrom) == 0); @@ -918,13 +946,13 @@ void ArchiveTestCase::ModifyArchive(wxInputStream& in, // 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 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 @@ -941,7 +969,7 @@ void ArchiveTestCase::ExtractArchive(wxInputStream& in) typedef std::list Entries; typedef typename Entries::iterator EntryIter; - std::auto_ptr arc(m_factory->NewStream(in)); + auto_ptr arc(m_factory->NewStream(in)); int expectedTotal = m_testEntries.size(); EntryPtr entry; Entries entries; @@ -954,8 +982,8 @@ void ArchiveTestCase::ExtractArchive(wxInputStream& in) // 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( @@ -975,8 +1003,8 @@ void ArchiveTestCase::ExtractArchive(wxInputStream& in) testEntry.GetLength() == entry->GetSize() || ((m_options & PipeIn) != 0 && entry->GetSize() == wxInvalidOffset)); CPPUNIT_ASSERT_MESSAGE( - "arc->GetSize() == entry->GetSize()" + error_context, - arc->GetSize() == (size_t)entry->GetSize()); + "arc->GetLength() == entry->GetSize()" + error_context, + arc->GetLength() == entry->GetSize()); if (name.Last() != _T('/')) { @@ -998,8 +1026,8 @@ void ArchiveTestCase::ExtractArchive(wxInputStream& in) CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context, testEntry.GetLength() == entry->GetSize()); CPPUNIT_ASSERT_MESSAGE( - "arc->GetSize() == entry->GetSize()" + error_context, - arc->GetSize() == (size_t)entry->GetSize()); + "arc->GetLength() == entry->GetSize()" + error_context, + arc->GetLength() == entry->GetSize()); if ((m_options & PipeIn) == 0) { OnEntryExtracted(*entry, testEntry, arc.get()); @@ -1092,8 +1120,8 @@ void ArchiveTestCase::VerifyDir(wxString& path, size_t rootlen /*=0*/) // 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( @@ -1102,7 +1130,6 @@ void ArchiveTestCase::VerifyDir(wxString& path, size_t rootlen /*=0*/) it != m_testEntries.end()); const TestEntry& testEntry = *it->second; - size_t size = 0; #ifndef __WXMSW__ CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context, @@ -1114,7 +1141,7 @@ void ArchiveTestCase::VerifyDir(wxString& path, size_t rootlen /*=0*/) CPPUNIT_ASSERT_MESSAGE( "entry not found in archive" + error_entry, in.Ok()); - size = in.GetSize(); + size_t size = in.GetLength(); wxCharBuffer buf(size); CPPUNIT_ASSERT_MESSAGE("Read" + error_context, in.Read(buf.data(), size).LastRead() == size); @@ -1142,7 +1169,7 @@ void ArchiveTestCase::TestIterator(wxInputStream& in) typedef std::list ArchiveCatalog; typedef typename ArchiveCatalog::iterator CatalogIter; - std::auto_ptr arc(m_factory->NewStream(in)); + auto_ptr arc(m_factory->NewStream(in)); size_t count = 0; #ifdef WXARC_MEMBER_TEMPLATES @@ -1154,7 +1181,7 @@ void ArchiveTestCase::TestIterator(wxInputStream& in) #endif for (CatalogIter it = cat.begin(); it != cat.end(); ++it) { - std::auto_ptr entry(*it); + auto_ptr entry(*it); count += m_testEntries.count(entry->GetName(wxPATH_UNIX)); } @@ -1171,7 +1198,7 @@ void ArchiveTestCase::TestPairIterator(wxInputStream& in) typedef std::map ArchiveCatalog; typedef typename ArchiveCatalog::iterator CatalogIter; - std::auto_ptr arc(m_factory->NewStream(in)); + auto_ptr arc(m_factory->NewStream(in)); size_t count = 0; #ifdef WXARC_MEMBER_TEMPLATES @@ -1179,11 +1206,11 @@ void ArchiveTestCase::TestPairIterator(wxInputStream& in) #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 entry(it->second); + auto_ptr entry(it->second); count += m_testEntries.count(entry->GetName(wxPATH_UNIX)); } @@ -1200,7 +1227,7 @@ void ArchiveTestCase::TestSmartIterator(wxInputStream& in) typedef typename ArchiveCatalog::iterator CatalogIter; typedef wxArchiveIterator > Iter; - std::auto_ptr arc(m_factory->NewStream(in)); + auto_ptr arc(m_factory->NewStream(in)); #ifdef WXARC_MEMBER_TEMPLATES ArchiveCatalog cat((Iter)*arc, Iter()); @@ -1226,14 +1253,14 @@ void ArchiveTestCase::TestSmartPairIterator(wxInputStream& in) typedef wxArchiveIterator > > PairIter; - std::auto_ptr arc(m_factory->NewStream(in)); + auto_ptr 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()); @@ -1253,8 +1280,8 @@ void ArchiveTestCase::ReadSimultaneous(TestInputStream& in) // create two archive input streams TestInputStream in2(in); - std::auto_ptr arc(m_factory->NewStream(in)); - std::auto_ptr arc2(m_factory->NewStream(in2)); + auto_ptr arc(m_factory->NewStream(in)); + auto_ptr arc2(m_factory->NewStream(in2)); // load the catalog #ifdef WXARC_MEMBER_TEMPLATES @@ -1262,7 +1289,7 @@ void ArchiveTestCase::ReadSimultaneous(TestInputStream& in) #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 @@ -1332,7 +1359,7 @@ void ArchiveTestCase::OnSetNotifier(EntryT& entry) class ZipTestCase : public ArchiveTestCase { public: - ZipTestCase(const wxString& name, + ZipTestCase(string name, int id, int options, const wxString& archiver = wxEmptyString, @@ -1410,8 +1437,9 @@ void ZipTestCase::OnEntryExtracted(wxZipEntry& entry, { // 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()); @@ -1458,8 +1486,8 @@ void ZipTestCase::OnSetNotifier(EntryT& entry) 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(); @@ -1482,7 +1510,7 @@ void ZipPipeTestCase::runTest() TestInputStream in(out); wxZipInputStream zip(in); - std::auto_ptr entry(zip.GetNextEntry()); + auto_ptr entry(zip.GetNextEntry()); CPPUNIT_ASSERT(entry.get() != NULL); if ((m_options & PipeIn) == 0) @@ -1518,11 +1546,11 @@ private: 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() @@ -1571,13 +1599,13 @@ ArchiveTestSuite *ArchiveTestSuite::makeSuite() // 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( @@ -1590,11 +1618,11 @@ ArchiveTestSuite *ArchiveTestSuite::makeSuite() 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++; } @@ -1605,11 +1633,11 @@ ArchiveTestSuite *ArchiveTestSuite::makeSuite() // 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(" "); @@ -1637,7 +1665,7 @@ wxString ArchiveTestSuite::Description(const wxString& type, descr << optstr; - return descr; + return (const char*)descr.mb_str(); } // register in the unnamed registry so that these tests are run by default