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
This commit is contained in:
Vadim Zeitlin 2008-06-15 17:34:50 +00:00
parent d1b021ffb9
commit 02e22828eb
13 changed files with 156 additions and 24 deletions

View File

@ -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:

View File

@ -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");
};

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 $<

View File

@ -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 <class Stream>
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

View File

@ -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

View File

@ -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

View File

@ -746,6 +746,8 @@
RelativePath=".\intl\intltest.cpp"/>
<File
RelativePath=".\strings\iostream.cpp"/>
<File
RelativePath=".\streams\iostream.cpp"/>
<File
RelativePath=".\streams\largefile.cpp"/>
<File

View File

@ -944,6 +944,9 @@
<File
RelativePath=".\strings\iostream.cpp"
/>
<File
RelativePath=".\streams\iostream.cpp"
/>
<File
RelativePath=".\streams\largefile.cpp"
/>