From 02e22828ebafb109d321370d75e2df95596b7a9a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 15 Jun 2008 17:34:50 +0000 Subject: [PATCH] added a unit test for input/output file streams and fixed the problem it exposed (#3335) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54246 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/wfstream.h | 4 +- interface/wfstream.h | 4 +- src/common/wfstream.cpp | 25 +++++++++--- tests/Makefile.in | 19 +++++++-- tests/makefile.bcc | 10 +++-- tests/makefile.gcc | 10 +++-- tests/makefile.vc | 10 +++-- tests/makefile.wat | 10 +++-- tests/streams/iostreams.cpp | 78 +++++++++++++++++++++++++++++++++++++ tests/test.bkl | 1 + tests/test_test.dsp | 4 ++ tests/test_vc7_test.vcproj | 2 + tests/test_vc8_test.vcproj | 3 ++ 13 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 tests/streams/iostreams.cpp diff --git a/include/wx/wfstream.h b/include/wx/wfstream.h index 378ef9fcda..8289252bc9 100644 --- a/include/wx/wfstream.h +++ b/include/wx/wfstream.h @@ -161,7 +161,7 @@ protected: class WXDLLIMPEXP_BASE wxFFileOutputStream : public wxOutputStream { public: - wxFFileOutputStream(const wxString& fileName, const wxString& mode = "w+b"); + wxFFileOutputStream(const wxString& fileName, const wxString& mode = "wb"); wxFFileOutputStream(wxFFile& file); wxFFileOutputStream(FILE *file); virtual ~wxFFileOutputStream(); @@ -192,7 +192,7 @@ class WXDLLIMPEXP_BASE wxFFileStream : public wxFFileInputStream, public wxFFileOutputStream { public: - wxFFileStream(const wxString& fileName); + wxFFileStream(const wxString& fileName, const wxString& mode = "w+b"); virtual bool IsOk() const; private: diff --git a/interface/wfstream.h b/interface/wfstream.h index 62c1399238..de501d0490 100644 --- a/interface/wfstream.h +++ b/interface/wfstream.h @@ -76,7 +76,7 @@ public: Initializes a file stream in write-only mode using the file descriptor @e fp. */ wxFFileOutputStream(const wxString& filename, - const wxString& mode = "w+b"); + const wxString& mode = "wb"); wxFFileOutputStream(wxFFile& file); wxFFileOutputStream(FILE* fp); //@} @@ -242,7 +242,7 @@ public: Initializes a new file stream in read-write mode using the specified @e iofilename name. */ - wxFFileStream(const wxString& iofileName); + wxFFileStream(const wxString& iofileName, const wxString& mode = "w+b"); }; diff --git a/src/common/wfstream.cpp b/src/common/wfstream.cpp index 65e8ceb266..de2af11173 100644 --- a/src/common/wfstream.cpp +++ b/src/common/wfstream.cpp @@ -220,9 +220,16 @@ size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size) // ---------------------------------------------------------------------------- wxFileStream::wxFileStream(const wxString& fileName) - : wxFileInputStream(fileName) + : wxFileInputStream(), + wxFileOutputStream() { - wxFileOutputStream::m_file = wxFileInputStream::m_file; + wxFileOutputStream::m_file = + wxFileInputStream::m_file = new wxFile(fileName, wxFile::read_write); + + // this is a bit ugly as streams are symmetric but we still have to delete + // the file we created above exactly once so we decide to (arbitrarily) do + // it in wxFileInputStream + wxFileInputStream::m_file_destroy = true; } bool wxFileStream::IsOk() const @@ -400,10 +407,18 @@ bool wxFFileOutputStream::IsOk() const // wxFFileStream // ---------------------------------------------------------------------------- -wxFFileStream::wxFFileStream(const wxString& fileName) - : wxFFileInputStream(fileName) +wxFFileStream::wxFFileStream(const wxString& fileName, const wxString& mode) + : wxFFileInputStream(), + wxFFileOutputStream() { - wxFFileOutputStream::m_file = wxFFileInputStream::m_file; + wxASSERT_MSG( mode.find_first_of('+') != wxString::npos, + "must be opened in read-write mode for this class to work" ); + + wxFFileOutputStream::m_file = + wxFFileInputStream::m_file = new wxFFile(fileName, mode); + + // see comment in wxFileStream ctor + wxFFileInputStream::m_file_destroy = true; } bool wxFFileStream::IsOk() const diff --git a/tests/Makefile.in b/tests/Makefile.in index a0dcb47e3f..4b41c6a3f6 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -88,6 +88,7 @@ TEST_OBJECTS = \ test_ffilestream.o \ test_fileback.o \ test_filestream.o \ + test_iostream.o \ test_largefile.o \ test_memstream.o \ test_sstream.o \ @@ -401,9 +402,6 @@ test_wxregextest.o: $(srcdir)/regex/wxregextest.cpp $(TEST_ODEP) test_scopeguardtest.o: $(srcdir)/scopeguard/scopeguardtest.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/scopeguard/scopeguardtest.cpp -test_iostream.o: $(srcdir)/strings/iostream.cpp $(TEST_ODEP) - $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/iostream.cpp - test_strings.o: $(srcdir)/strings/strings.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/strings.cpp @@ -488,6 +486,12 @@ test_xlocale.o: $(srcdir)/xlocale/xlocale.cpp $(TEST_ODEP) test_xmltest.o: $(srcdir)/xml/xmltest.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/xml/xmltest.cpp +test_iostream.o: $(srcdir)/strings/iostream.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/strings/iostream.cpp + +test_iostream.o: $(srcdir)/streams/iostream.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/iostream.cpp + test_gui_sample_rc.o: $(srcdir)/../samples/sample.rc $(TEST_GUI_ODEP) $(WINDRES) -i$< -o$@ --define __WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p_5) $(__EXCEPTIONS_DEFINE_p_5) $(__RTTI_DEFINE_p_5) $(__THREAD_DEFINE_p_5) --include-dir $(srcdir) $(__DLLFLAG_p_5) --include-dir $(srcdir)/../samples $(__RCDEFDIR_p_1) --include-dir $(top_srcdir)/include @@ -525,6 +529,15 @@ printfbench_printfbench.o: $(srcdir)/benchmarks/printfbench.cpp $(PRINTFBENCH_OD $(CXXC) -c -o $@ $(PRINTFBENCH_CXXFLAGS) $(srcdir)/benchmarks/printfbench.cpp +$(srcdir)/include/wx/stc/stc.h: \ +$(srcdir)/src/stc/scintilla/include/Scintilla.iface \ +$(srcdir)/src/stc/stc.cpp.in \ +$(srcdir)/src/stc/stc.h.in \ +$(srcdir)/src/stc/gen_iface.py + cd $(srcdir)/src/stc && ./gen_iface.py +monolib_stc.o monodll_stc.o stcdll_stc.o stclib_stc.o: \ +$(srcdir)/include/wx/stc/stc.h + # Include dependency info, if present: @IF_GNU_MAKE@-include .deps/*.d diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 5e211db3be..91d0a0db6a 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -74,6 +74,7 @@ TEST_OBJECTS = \ $(OBJS)\test_ffilestream.obj \ $(OBJS)\test_fileback.obj \ $(OBJS)\test_filestream.obj \ + $(OBJS)\test_iostream.obj \ $(OBJS)\test_largefile.obj \ $(OBJS)\test_memstream.obj \ $(OBJS)\test_sstream.obj \ @@ -430,9 +431,6 @@ $(OBJS)\test_wxregextest.obj: .\regex\wxregextest.cpp $(OBJS)\test_scopeguardtest.obj: .\scopeguard\scopeguardtest.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\scopeguard\scopeguardtest.cpp -$(OBJS)\test_iostream.obj: .\strings\iostream.cpp - $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\iostream.cpp - $(OBJS)\test_strings.obj: .\strings\strings.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\strings.cpp @@ -517,6 +515,12 @@ $(OBJS)\test_xlocale.obj: .\xlocale\xlocale.cpp $(OBJS)\test_xmltest.obj: .\xml\xmltest.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\xml\xmltest.cpp +$(OBJS)\test_iostream.obj: .\strings\iostream.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\strings\iostream.cpp + +$(OBJS)\test_iostream.obj: .\streams\iostream.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\streams\iostream.cpp + $(OBJS)\test_gui_sample.res: .\..\samples\sample.rc brcc32 -32 -r -fo$@ -i$(BCCDIR)\include -d__WXMSW__ $(__WXUNIV_DEFINE_p_3) $(__DEBUG_DEFINE_p_3) $(__EXCEPTIONS_DEFINE_p_3) $(__RTTI_DEFINE_p_3) $(__THREAD_DEFINE_p_3) $(__UNICODE_DEFINE_p_3) $(__MSLU_DEFINE_p_3) $(__GFXCTX_DEFINE_p_3) -i$(SETUPHDIR) -i.\..\include -i. $(__DLLFLAG_p_3) -i.\..\samples -dNOPCH .\..\samples\sample.rc diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 920221b1fd..0e9c9b6acc 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -66,6 +66,7 @@ TEST_OBJECTS = \ $(OBJS)\test_ffilestream.o \ $(OBJS)\test_fileback.o \ $(OBJS)\test_filestream.o \ + $(OBJS)\test_iostream.o \ $(OBJS)\test_largefile.o \ $(OBJS)\test_memstream.o \ $(OBJS)\test_sstream.o \ @@ -408,9 +409,6 @@ $(OBJS)\test_wxregextest.o: ./regex/wxregextest.cpp $(OBJS)\test_scopeguardtest.o: ./scopeguard/scopeguardtest.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< -$(OBJS)\test_iostream.o: ./strings/iostream.cpp - $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< - $(OBJS)\test_strings.o: ./strings/strings.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< @@ -495,6 +493,12 @@ $(OBJS)\test_xlocale.o: ./xlocale/xlocale.cpp $(OBJS)\test_xmltest.o: ./xml/xmltest.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_iostream.o: ./strings/iostream.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + +$(OBJS)\test_iostream.o: ./streams/iostream.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_sample_rc.o: ./../samples/sample.rc windres --use-temp-file -i$< -o$@ --define __WXMSW__ $(__WXUNIV_DEFINE_p_3) $(__DEBUG_DEFINE_p_3) $(__EXCEPTIONS_DEFINE_p_3) $(__RTTI_DEFINE_p_3) $(__THREAD_DEFINE_p_3) $(__UNICODE_DEFINE_p_3) $(__MSLU_DEFINE_p_3) $(__GFXCTX_DEFINE_p_3) --include-dir $(SETUPHDIR) --include-dir ./../include --include-dir . $(__DLLFLAG_p_3) --include-dir ./../samples --define NOPCH diff --git a/tests/makefile.vc b/tests/makefile.vc index 67b4f4e8a7..9c9688a305 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -67,6 +67,7 @@ TEST_OBJECTS = \ $(OBJS)\test_ffilestream.obj \ $(OBJS)\test_fileback.obj \ $(OBJS)\test_filestream.obj \ + $(OBJS)\test_iostream.obj \ $(OBJS)\test_largefile.obj \ $(OBJS)\test_memstream.obj \ $(OBJS)\test_sstream.obj \ @@ -515,9 +516,6 @@ $(OBJS)\test_wxregextest.obj: .\regex\wxregextest.cpp $(OBJS)\test_scopeguardtest.obj: .\scopeguard\scopeguardtest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\scopeguard\scopeguardtest.cpp -$(OBJS)\test_iostream.obj: .\strings\iostream.cpp - $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\iostream.cpp - $(OBJS)\test_strings.obj: .\strings\strings.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\strings.cpp @@ -602,6 +600,12 @@ $(OBJS)\test_xlocale.obj: .\xlocale\xlocale.cpp $(OBJS)\test_xmltest.obj: .\xml\xmltest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\xml\xmltest.cpp +$(OBJS)\test_iostream.obj: .\strings\iostream.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\strings\iostream.cpp + +$(OBJS)\test_iostream.obj: .\streams\iostream.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\streams\iostream.cpp + $(OBJS)\test_gui_dummy.obj: .\dummy.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) /Yctestprec.h .\dummy.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index 5093a6e9b2..ff54eb36cb 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -279,6 +279,7 @@ TEST_OBJECTS = & $(OBJS)\test_ffilestream.obj & $(OBJS)\test_fileback.obj & $(OBJS)\test_filestream.obj & + $(OBJS)\test_iostream.obj & $(OBJS)\test_largefile.obj & $(OBJS)\test_memstream.obj & $(OBJS)\test_sstream.obj & @@ -461,9 +462,6 @@ $(OBJS)\test_wxregextest.obj : .AUTODEPEND .\regex\wxregextest.cpp $(OBJS)\test_scopeguardtest.obj : .AUTODEPEND .\scopeguard\scopeguardtest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< -$(OBJS)\test_iostream.obj : .AUTODEPEND .\strings\iostream.cpp - $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< - $(OBJS)\test_strings.obj : .AUTODEPEND .\strings\strings.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< @@ -548,6 +546,12 @@ $(OBJS)\test_xlocale.obj : .AUTODEPEND .\xlocale\xlocale.cpp $(OBJS)\test_xmltest.obj : .AUTODEPEND .\xml\xmltest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_iostream.obj : .AUTODEPEND .\strings\iostream.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + +$(OBJS)\test_iostream.obj : .AUTODEPEND .\streams\iostream.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_gui_sample.res : .AUTODEPEND .\..\samples\sample.rc wrc -q -ad -bt=nt -r -fo=$^@ -d__WXMSW__ $(__WXUNIV_DEFINE_p) $(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) $(__UNICODE_DEFINE_p) $(__GFXCTX_DEFINE_p) -i=$(SETUPHDIR) -i=.\..\include -i=. $(__DLLFLAG_p) -i=.\..\samples -dNOPCH $< diff --git a/tests/streams/iostreams.cpp b/tests/streams/iostreams.cpp new file mode 100644 index 0000000000..92a640e790 --- /dev/null +++ b/tests/streams/iostreams.cpp @@ -0,0 +1,78 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/streams/iostreams.cpp +// Purpose: unit test for input/output streams +// Author: Vadim Zeitlin +// Created: 2008-06-15 +// RCS-ID: $Id$ +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#if wxUSE_STREAMS + +#include "wx/filename.h" +#include "wx/wfstream.h" + +// -------------------------------------------------------------------------- +// test class +// -------------------------------------------------------------------------- + +class IOStreamsTestCase : public CppUnit::TestCase +{ +public: + IOStreamsTestCase() { } + + virtual void tearDown() + { + if ( !m_fnTemp.empty() ) + { + wxRemoveFile(m_fnTemp); + m_fnTemp.clear(); + } + } + +private: + CPPUNIT_TEST_SUITE( IOStreamsTestCase ); + CPPUNIT_TEST( FStream ); + CPPUNIT_TEST( FFStream ); + CPPUNIT_TEST_SUITE_END(); + + void FStream() { wxFileStream s(GetTempFName()); DoTest(s); } + void FFStream() { wxFFileStream s(GetTempFName()); DoTest(s); } + + wxString GetTempFName() + { + m_fnTemp = wxFileName::CreateTempFileName("wxtest"); + return m_fnTemp; + } + + template + void DoTest(Stream& s) + { + s.PutC('x'); + WX_ASSERT_SIZET_EQUAL( 1, s.LastWrite() ); + + s.SeekI(0); + CPPUNIT_ASSERT_EQUAL( int('x'), s.GetC() ); + } + + wxString m_fnTemp; + + DECLARE_NO_COPY_CLASS(IOStreamsTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( IOStreamsTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IOStreamsTestCase, "IOStreamsTestCase" ); + +#endif // wxUSE_STREAMS diff --git a/tests/test.bkl b/tests/test.bkl index 8fa1ac432f..e5ad4455be 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -60,6 +60,7 @@ streams/ffilestream.cpp streams/fileback.cpp streams/filestream.cpp + streams/iostreams.cpp streams/largefile.cpp streams/memstream.cpp streams/sstream.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index 9726938675..6fa217af26 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -333,6 +333,10 @@ SOURCE=.\strings\iostream.cpp # End Source File # Begin Source File +SOURCE=.\streams\iostream.cpp +# End Source File +# Begin Source File + SOURCE=.\streams\largefile.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index 0332add381..dd9b752753 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -746,6 +746,8 @@ RelativePath=".\intl\intltest.cpp"/> + + -- 2.45.2