X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9386cb75e34acb49cd9fac945e09cf002f22db2d..3e21fc053d9017f79ad71675db8d2e1497049159:/tests/archive/archivetest.cpp diff --git a/tests/archive/archivetest.cpp b/tests/archive/archivetest.cpp index 476513e4f4..b6b20c7aca 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 +// 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 #include +#include #include +using std::string; +using std::auto_ptr; + + // Check whether member templates can be used // #if defined __GNUC__ && \ @@ -119,7 +129,7 @@ public: private: wxDateTime m_dt; size_t m_len; - const char *m_data; + char *m_data; wxString m_comment; bool m_isText; }; @@ -129,9 +139,8 @@ TestEntry::TestEntry(const wxDateTime& dt, int len, const char *data) 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; @@ -153,7 +162,7 @@ public: 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 }; @@ -201,7 +210,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; @@ -246,7 +255,7 @@ size_t TestOutputStream::OnSysWrite(const void *buffer, size_t size) 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; @@ -293,7 +302,7 @@ private: int m_options; size_t m_pos; size_t m_size; - const char *m_data; + char *m_data; }; TestInputStream::TestInputStream(const TestInputStream& in) @@ -301,9 +310,8 @@ TestInputStream::TestInputStream(const TestInputStream& in) 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() @@ -340,7 +348,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; @@ -527,9 +535,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 +600,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 +608,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() { @@ -713,7 +732,7 @@ TestEntry& ArchiveTestCase::Add(const char *name, { 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); } @@ -722,7 +741,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 +764,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 +880,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)); + EntryT *pEntry; const wxString deleteName = _T("bin/bin1000"); const wxString renameFrom = _T("zero/zero1024"); @@ -873,14 +892,15 @@ void ArchiveTestCase::ModifyArchive(wxInputStream& in, arcOut->CopyArchiveMetaData(*arcIn); - while (entry.reset(arcIn->GetNextEntry()), entry.get() != NULL) { + while ((pEntry = arcIn->GetNextEntry()) != NULL) { + auto_ptr 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); @@ -918,13 +938,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 +961,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 +974,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( @@ -1092,8 +1112,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( @@ -1113,7 +1133,7 @@ void ArchiveTestCase::VerifyDir(wxString& path, size_t rootlen /*=0*/) 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); @@ -1141,7 +1161,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 @@ -1153,7 +1173,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)); } @@ -1170,7 +1190,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 @@ -1178,11 +1198,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)); } @@ -1199,7 +1219,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()); @@ -1220,25 +1240,31 @@ void ArchiveTestCase::TestSmartIterator(wxInputStream& in) template void ArchiveTestCase::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, so this iterator can't be made to work. + (void)in; +#else typedef std::map > ArchiveCatalog; typedef typename ArchiveCatalog::iterator CatalogIter; 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()); 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 @@ -1252,8 +1278,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 @@ -1261,7 +1287,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 @@ -1331,7 +1357,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, @@ -1409,8 +1435,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()); @@ -1457,8 +1484,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(); @@ -1481,7 +1508,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) @@ -1517,11 +1544,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() @@ -1570,13 +1597,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( @@ -1589,11 +1616,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++; } @@ -1604,11 +1631,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(" "); @@ -1636,7 +1663,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