X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ce4261521883ec655494a7f8ed053f7639b658d6..32753ae949d861ddaf7c66f4c483bc57493c6c6c:/tests/streams/zlibstream.cpp diff --git a/tests/streams/zlibstream.cpp b/tests/streams/zlibstream.cpp index f70ff1e0e2..af5fd16cdd 100644 --- a/tests/streams/zlibstream.cpp +++ b/tests/streams/zlibstream.cpp @@ -4,16 +4,12 @@ // Author: Hans Van Leemputten // RCS-ID: $Id$ // Copyright: (c) 2004 Hans Van Leemputten -// Licence: wxWidgets licence +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(__APPLE__) - #pragma implementation - #pragma interface -#endif - // For compilers that support precompilation, includes "wx/wx.h". -#include "wx/wxprec.h" +// and "wx/cppunit.h" +#include "testprec.h" #ifdef __BORLANDC__ #pragma hdrstop @@ -24,24 +20,22 @@ #include "wx/wx.h" #endif -#include "wx/cppunit.h" #include "wx/zstream.h" #include "wx/wfstream.h" #include "wx/mstream.h" - #include "wx/txtstrm.h" +#include "wx/buffer.h" #include "bstream.h" -using namespace std; -using namespace CppUnit; +using std::string; #define WXTEST_WITH_GZIP_CONDITION(testMethod) \ WXTEST_WITH_CONDITION( COMPOSE_TEST_NAME(zlibStream), wxZlibInputStream::CanHandleGZip() && wxZlibOutputStream::CanHandleGZip(), testMethod ) #define DATABUFFER_SIZE 1024 -static const wxString FILENAME_GZ = _T("zlibtest.gz"); +static const wxString FILENAME_GZ = wxT("zlibtest.gz"); /////////////////////////////////////////////////////////////////////////////// // The test case @@ -61,6 +55,7 @@ public: CPPUNIT_TEST(Input_Read); CPPUNIT_TEST(Input_Eof); CPPUNIT_TEST(Input_LastRead); + CPPUNIT_TEST(Input_CanRead); CPPUNIT_TEST_FAIL(Input_SeekI); CPPUNIT_TEST(Input_TellI); CPPUNIT_TEST(Input_Peek); @@ -77,6 +72,7 @@ public: CPPUNIT_TEST(TestStream_NoHeader_NoComp); CPPUNIT_TEST(TestStream_NoHeader_SpeedComp); CPPUNIT_TEST(TestStream_NoHeader_BestComp); + CPPUNIT_TEST(TestStream_NoHeader_Dictionary); CPPUNIT_TEST(TestStream_ZLib_Default); CPPUNIT_TEST(TestStream_ZLib_NoComp); CPPUNIT_TEST(TestStream_ZLib_SpeedComp); @@ -85,11 +81,9 @@ public: WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_NoComp); WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_SpeedComp); WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_BestComp); + WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Dictionary); WXTEST_WITH_GZIP_CONDITION(TestStream_ZLibGZip); CPPUNIT_TEST(Decompress_BadData); -#if WXWIN_COMPATIBILITY_2_4 - CPPUNIT_TEST(Decompress_wx24Data); -#endif CPPUNIT_TEST(Decompress_wx251_zlib114_Data_NoHeader); CPPUNIT_TEST(Decompress_wx251_zlib114_Data_ZLib); WXTEST_WITH_GZIP_CONDITION(Decompress_gzip135Data); @@ -101,6 +95,7 @@ protected: void TestStream_NoHeader_NoComp(); void TestStream_NoHeader_SpeedComp(); void TestStream_NoHeader_BestComp(); + void TestStream_NoHeader_Dictionary(); void TestStream_ZLib_Default(); void TestStream_ZLib_NoComp(); void TestStream_ZLib_SpeedComp(); @@ -109,33 +104,31 @@ protected: void TestStream_GZip_NoComp(); void TestStream_GZip_SpeedComp(); void TestStream_GZip_BestComp(); + void TestStream_GZip_Dictionary(); void TestStream_ZLibGZip(); // Try to decompress bad data. void Decompress_BadData(); - // Decompress data that was compress by an external app. + // Decompress data that was compress by an external app. // (like test wx 2.4.2, 2.5.1 and gzip data) // Note: This test is limited in testing range! -#if WXWIN_COMPATIBILITY_2_4 - void Decompress_wx24Data(); -#endif void Decompress_wx251_zlib114_Data_NoHeader(); void Decompress_wx251_zlib114_Data_ZLib(); - void Decompress_gzip135Data(); + void Decompress_gzip135Data(); -private: +private: const char *GetDataBuffer(); const unsigned char *GetCompressedData(); - void doTestStreamData(int input_flag, int output_flag, int compress_level); + void doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf = NULL); void doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag = wxZLIB_AUTO); private: // Implement base class functions. - virtual wxZlibInputStream *DoCreateInStream(); + virtual wxZlibInputStream *DoCreateInStream(); virtual wxZlibOutputStream *DoCreateOutStream(); virtual void DoDeleteInStream(); virtual void DoDeleteOutStream(); - // Helper that can be used to create new wx compatibility tests... + // Helper that can be used to create new wx compatibility tests... // Otherwise not used by the tests. void genExtTestData(wxTextOutputStream &out, const char *buf, int flag); @@ -143,6 +136,7 @@ private: char m_DataBuffer[DATABUFFER_SIZE]; size_t m_SizeCompressedData; unsigned char *m_pCompressedData; + wxMemoryBuffer m_Dictionary; // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream. wxMemoryInputStream *m_pTmpMemInStream; @@ -159,13 +153,15 @@ zlibStream::zlibStream() for (size_t i = 0; i < DATABUFFER_SIZE; i++) m_DataBuffer[i] = (i % 0xFF); + m_Dictionary.AppendData(m_DataBuffer, sizeof(m_DataBuffer) / 2); + // Set extra base config settings. m_bSimpleTellITest = true; m_bSimpleTellOTest = true; /* Example code on how to produce test data... { - wxFFileOutputStream fstream_out(_T("gentest.cpp")); + wxFFileOutputStream fstream_out(wxT("gentest.cpp")); wxTextOutputStream out( fstream_out ); genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4", wxZLIB_NO_HEADER); @@ -176,7 +172,7 @@ zlibStream::zlibStream() zlibStream::~zlibStream() { - delete m_pCompressedData; + delete[] m_pCompressedData; delete m_pTmpMemInStream; delete m_pTmpMemOutStream; @@ -198,6 +194,10 @@ void zlibStream::TestStream_NoHeader_BestComp() { doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_COMPRESSION); } +void zlibStream::TestStream_NoHeader_Dictionary() +{ + doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION, &m_Dictionary); +} void zlibStream::TestStream_ZLib_Default() { @@ -232,10 +232,14 @@ void zlibStream::TestStream_GZip_BestComp() { doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_COMPRESSION); } +void zlibStream::TestStream_GZip_Dictionary() +{ + doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION, &m_Dictionary); +} void zlibStream::TestStream_ZLibGZip() { - // Only use default compression level, as this test is + // Only use default compression level, as this test is // for testing if the streams can determine the stream type info them self... doTestStreamData(wxZLIB_AUTO, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION); doTestStreamData(wxZLIB_AUTO, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION); @@ -247,7 +251,7 @@ void zlibStream::Decompress_BadData() wxMemoryInputStream memstream_in(GetDataBuffer(), DATABUFFER_SIZE); CPPUNIT_ASSERT(memstream_in.IsOk()); wxZlibInputStream zstream_in(memstream_in); - CPPUNIT_ASSERT(zstream_in.IsOk()); // We did not yet read from the stream + CPPUNIT_ASSERT(zstream_in.IsOk()); // We did not yet read from the stream // so it should still be OK. // Try to force the stream to go to bad status. CPPUNIT_ASSERT(!zstream_in.Eof()); @@ -259,22 +263,6 @@ void zlibStream::Decompress_BadData() CPPUNIT_ASSERT(!zstream_in.IsOk()); } -#if WXWIN_COMPATIBILITY_2_4 -void zlibStream::Decompress_wx24Data() -{ - // The wx24_value was used in a wxWidgets 2.4(.2) - // application to produce wx24_data, using wxZlibOutputStream. - const unsigned char wx24_data[] = {120,156,242,72,205,201,201,87,40,207,47,202,73,97,0,0,0,0,255,255,0}; - const char *wx24_value = "Hello world"; - // Size of the value and date items. - const size_t data_size = sizeof(wx24_data); - const size_t value_size = strlen(wx24_value) + 1; // +1 because the wx24 app also did this. - - // Perform a generic data test on the data. - doDecompress_ExternalData(wx24_data, wx24_value, data_size, value_size, wxZLIB_24COMPATIBLE); -} -#endif - void zlibStream::Decompress_wx251_zlib114_Data_NoHeader() { const unsigned char data[] = {171,202,201,76,82,72,73,44,73,84,72,46,74,77,44,73,77,81,40,207,44,201,80,40,175,8,207,76,73,79,45,41,86,48,210,51,213,171,80,136,246,77,44,74,206,80,48,50,143,213,1,202,69,249,120,58,197,251,249,199,123,184,58,186,184,6,233,40,84,129,12,49,212,51,212,51,1,0,32}; @@ -324,17 +312,17 @@ const unsigned char *zlibStream::GetCompressedData() zstream_out.Write(buf, strlen(buf)); } - // Copy the to the + // Copy the to the m_SizeCompressedData = memstream_out.GetSize(); m_pCompressedData = new unsigned char[m_SizeCompressedData]; memstream_out.CopyTo(m_pCompressedData, m_SizeCompressedData); } - CPPUNIT_ASSERT(m_pCompressedData != NULL); + CPPUNIT_ASSERT(m_pCompressedData != NULL); return m_pCompressedData; } -void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level) +void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf) { size_t fail_pos; char last_value = 0; @@ -347,6 +335,9 @@ void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_ wxZlibOutputStream zstream_out(fstream_out, compress_level, output_flag); CPPUNIT_ASSERT_MESSAGE("Could not create the output stream", zstream_out.IsOk()); + if (buf) + zstream_out.SetDictionary(*buf); + // Next: Compress some data so the file is containing something. zstream_out.Write(GetDataBuffer(), DATABUFFER_SIZE); } @@ -356,19 +347,22 @@ void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_ fstream_out.PutC(' '); } - { // Part two: Verify that the compressed data when uncompressed + { // Part two: Verify that the compressed data when uncompressed // matches the original data. wxFileInputStream fstream_in(FILENAME_GZ); CPPUNIT_ASSERT(fstream_in.IsOk()); wxZlibInputStream zstream_in(fstream_in, input_flag); CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in.IsOk()); + if (buf) + zstream_in.SetDictionary(*buf); + // Next: Check char per char if the returned data is valid. const char *pbuf = GetDataBuffer(); for (fail_pos = 0; !zstream_in.Eof(); fail_pos++) { last_value = zstream_in.GetC(); - if (zstream_in.LastRead() != 1 || + if (zstream_in.LastRead() != 1 || last_value != pbuf[fail_pos]) break; } @@ -382,8 +376,11 @@ void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_ // Check state of the verify action. if (fail_pos != DATABUFFER_SIZE || !bWasEOF) { - wxString msg(wxString::Format(_T("Wrong data item at pos %d (Org_val %d != Zlib_val %d), with compression level %d"), - fail_pos, GetDataBuffer()[fail_pos], last_value, compress_level)); + wxString msg; + msg << wxT("Wrong data item at pos ") << fail_pos + << wxT(" (Org_val ") << GetDataBuffer()[fail_pos] + << wxT(" != Zlib_val ") << last_value + << wxT("), with compression level ") << compress_level; CPPUNIT_FAIL(string(msg.mb_str())); } } @@ -401,23 +398,26 @@ void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char { case wxZLIB_NO_HEADER: break; -#if WXWIN_COMPATIBILITY_2_4 - case wxZLIB_24COMPATIBLE: -#endif case wxZLIB_ZLIB: if (!(data_size >= 1 && data[0] == 0x78)) - wxLogError(_T("zlib data seems to not be zlib data!")); + { + wxLogError(wxT("zlib data seems to not be zlib data!")); + } break; case wxZLIB_GZIP: if (!(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B)) - wxLogError(_T("gzip data seems to not be gzip data!")); + { + wxLogError(wxT("gzip data seems to not be gzip data!")); + } break; case wxZLIB_AUTO: - if (!(data_size >= 1 && data[0] == 0x78) || + if (!(data_size >= 1 && data[0] == 0x78) || !(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B)) - wxLogError(_T("Data seems to not be zlib or gzip data!")); + { + wxLogError(wxT("Data seems to not be zlib or gzip data!")); + } default: - wxLogError(_T("Unknown flag, skipping quick test.")); + wxLogError(wxT("Unknown flag, skipping quick test.")); }; // Creat the needed streams. @@ -460,13 +460,13 @@ void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char // Move one item along in the stream. (void)zstream_in.GetC(); i++; - + // Check if we are in an infinite loop by multiplying value_size // by 5 to have a *much* bigger range then the real range. - // Note: Incase you ask your self, why 5, the answer is no reason... - // it is not to big and not to small a size, nothing more + // Note: In case you ask yourself, why 5, the answer is no reason... + // it is not too big and not to small a size, nothing more // nothing less to it. - if (i > (value_size*5)) + if (i > (value_size*5)) { // Note: Please make sure Input_Eof test passed. CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop"); @@ -476,12 +476,12 @@ void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char } } - CPPUNIT_ASSERT_MESSAGE("Could not decompress the compressed data, original and restored value did not match.", + CPPUNIT_ASSERT_MESSAGE("Could not decompress the compressed data, original and restored value did not match.", i == value_size && bValueEq); } -wxZlibInputStream *zlibStream::DoCreateInStream() -{ +wxZlibInputStream *zlibStream::DoCreateInStream() +{ const unsigned char *buf = GetCompressedData(); m_pTmpMemInStream = new wxMemoryInputStream(buf, m_SizeCompressedData); CPPUNIT_ASSERT(m_pTmpMemInStream->IsOk()); @@ -490,20 +490,20 @@ wxZlibInputStream *zlibStream::DoCreateInStream() return pzstream_in; } wxZlibOutputStream *zlibStream::DoCreateOutStream() -{ +{ m_pTmpMemOutStream = new wxMemoryOutputStream(); CPPUNIT_ASSERT(m_pTmpMemOutStream->IsOk()); wxZlibOutputStream *pzstream_out = new wxZlibOutputStream(*m_pTmpMemOutStream); CPPUNIT_ASSERT(pzstream_out->IsOk()); return pzstream_out; } -void zlibStream::DoDeleteInStream() -{ +void zlibStream::DoDeleteInStream() +{ delete m_pTmpMemInStream; m_pTmpMemInStream = NULL; } -void zlibStream::DoDeleteOutStream() -{ +void zlibStream::DoDeleteOutStream() +{ delete m_pTmpMemOutStream; m_pTmpMemOutStream = NULL; } @@ -513,7 +513,7 @@ void zlibStream::genExtTestData(wxTextOutputStream &out, const char *buf, int fl { unsigned char *data; size_t size; - + { // Gen data wxMemoryOutputStream memstream_out; { @@ -522,31 +522,31 @@ void zlibStream::genExtTestData(wxTextOutputStream &out, const char *buf, int fl } if (flag == wxZLIB_NO_HEADER) memstream_out.PutC(' '); - + size = memstream_out.GetSize(); data = new unsigned char[size]; memstream_out.CopyTo(data, size); } - out << _T("void zlibStream::Decompress_wxXXXData()") << _T("\n"); - out << _T("{") << _T("\n") << _T(" const unsigned char data[] = {"); - + out << wxT("void zlibStream::Decompress_wxXXXData()") << wxT("\n"); + out << wxT("{") << wxT("\n") << wxT(" const unsigned char data[] = {"); + size_t i; for (i = 0; i < size; i++) { if (i+1 != size) - out << wxString::Format(_T("%d,"), data[i]); + out << wxString::Format(wxT("%d,"), data[i]); else - out << wxString::Format(_T("%d"), data[i]); - } + out << wxString::Format(wxT("%d"), data[i]); + } delete [] data; - out << _T("};") << _T("\n"); - out << _T(" const char *value = \"") << wxString(buf, wxConvUTF8) << _T("\";") << _T("\n"); - out << _T(" const size_t data_size = sizeof(data);") << _T("\n"); - out << _T(" const size_t value_size = strlen(value);") << _T("\n"); - out << _T(" doDecompress_ExternalData(data, value, data_size, value_size);") << _T("\n"); - out << _T("}") << _T("\n"); + out << wxT("};") << wxT("\n"); + out << wxT(" const char *value = \"") << wxString(buf, wxConvUTF8) << wxT("\";") << wxT("\n"); + out << wxT(" const size_t data_size = sizeof(data);") << wxT("\n"); + out << wxT(" const size_t value_size = strlen(value);") << wxT("\n"); + out << wxT(" doDecompress_ExternalData(data, value, data_size, value_size);") << wxT("\n"); + out << wxT("}") << wxT("\n"); }