]> git.saurik.com Git - wxWidgets.git/commitdiff
added a unit test for input/output file streams and fixed the problem it exposed...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 15 Jun 2008 17:34:50 +0000 (17:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 15 Jun 2008 17:34:50 +0000 (17:34 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54246 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

13 files changed:
include/wx/wfstream.h
interface/wfstream.h
src/common/wfstream.cpp
tests/Makefile.in
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/streams/iostreams.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj

index 378ef9fcda90251d8e8db8dd5768f7eade7fc44a..8289252bc9d772387fd2223f7ca6391ed8ccde88 100644 (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:
index 62c13992383839ae2a034a5f12b97a3d0766aca2..de501d0490d6041b2cc723c23d4d1188cccdffd4 100644 (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");
 };
 
 
index 65e8ceb266e31b9dcf4d391b5fa55a06a9c357d4..de2af11173b63112f29ab52ba5bb787835a7b593 100644 (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
index a0dcb47e3fb828b6c76a761d67872bb4b0d170a5..4b41c6a3f633f9a2e27996ef6c597915cc9b6f74 100644 (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
 
index 5e211db3be546628f9655a4f29ce8ef6d3cdf356..91d0a0db6a2ddc959dce518494a62c81b09b95d3 100644 (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
 
index 920221b1fd53373d215679fb61312a1aa4a49f60..0e9c9b6acc0a9b2eca4eb932d1498418296adab5 100644 (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
 
index 67b4f4e8a7a6a88f2e20217e78feaf79c6247798..9c9688a3053697be125b04d36a2738c289922c8a 100644 (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
 
index 5093a6e9b23a3889564f8c6c213420827e5ef4e4..ff54eb36cb1aecc8b95dbc97ad465f8d8edce5fb 100644 (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 $<
 
diff --git a/tests/streams/iostreams.cpp b/tests/streams/iostreams.cpp
new file mode 100644 (file)
index 0000000..92a640e
--- /dev/null
@@ -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
index 8fa1ac432fce403dc005ba23f3451a5603398f13..e5ad4455bee02ce5f0a2b3bcf81d95358c2a783a 100644 (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
index 97269386757f076ca1bc3413c332e1727660b722..6fa217af26e9b3a0cb5ae202e6fce2cb8b04efd5 100644 (file)
@@ -333,6 +333,10 @@ SOURCE=.\strings\iostream.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\streams\iostream.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\streams\largefile.cpp\r
 # End Source File\r
 # Begin Source File\r
index 0332add3813f0ccb5d65b8c378b0f14eae73c73d..dd9b7527531a809b9180173e8468670833ada3f0 100644 (file)
                                RelativePath=".\intl\intltest.cpp"/>\r
                        <File\r
                                RelativePath=".\strings\iostream.cpp"/>\r
+                       <File\r
+                               RelativePath=".\streams\iostream.cpp"/>\r
                        <File\r
                                RelativePath=".\streams\largefile.cpp"/>\r
                        <File\r
index 0629254fdd96b2f6c80241c8969394de53d8b199..4098628499c0ad36c7b5e7c577c8c2805be92c1c 100644 (file)
                        <File\r
                                RelativePath=".\strings\iostream.cpp"\r
                        />\r
+                       <File\r
+                               RelativePath=".\streams\iostream.cpp"\r
+                       />\r
                        <File\r
                                RelativePath=".\streams\largefile.cpp"\r
                        />\r