diff --git a/tests/Makefile.in b/tests/Makefile.in index 7c645ca69f..4466759637 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -73,6 +73,7 @@ TEST_OBJECTS = \ test_bstream.o \ test_datastreamtest.o \ test_ffilestream.o \ + test_fileback.o \ test_filestream.o \ test_largefile.o \ test_memstream.o \ @@ -370,6 +371,9 @@ test_datastreamtest.o: $(srcdir)/streams/datastreamtest.cpp $(TEST_ODEP) test_ffilestream.o: $(srcdir)/streams/ffilestream.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/ffilestream.cpp +test_fileback.o: $(srcdir)/streams/fileback.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/fileback.cpp + test_filestream.o: $(srcdir)/streams/filestream.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/streams/filestream.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 3ff71fdb31..a3f236e409 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -64,6 +64,7 @@ TEST_OBJECTS = \ $(OBJS)\test_bstream.obj \ $(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_ffilestream.obj \ + $(OBJS)\test_fileback.obj \ $(OBJS)\test_filestream.obj \ $(OBJS)\test_largefile.obj \ $(OBJS)\test_memstream.obj \ @@ -409,6 +410,9 @@ $(OBJS)\test_datastreamtest.obj: .\streams\datastreamtest.cpp $(OBJS)\test_ffilestream.obj: .\streams\ffilestream.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $** +$(OBJS)\test_fileback.obj: .\streams\fileback.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $** + $(OBJS)\test_filestream.obj: .\streams\filestream.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $** diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 5a5f27cbd1..d8b6ec7cc2 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -56,6 +56,7 @@ TEST_OBJECTS = \ $(OBJS)\test_bstream.o \ $(OBJS)\test_datastreamtest.o \ $(OBJS)\test_ffilestream.o \ + $(OBJS)\test_fileback.o \ $(OBJS)\test_filestream.o \ $(OBJS)\test_largefile.o \ $(OBJS)\test_memstream.o \ @@ -387,6 +388,9 @@ $(OBJS)\test_datastreamtest.o: ./streams/datastreamtest.cpp $(OBJS)\test_ffilestream.o: ./streams/ffilestream.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_fileback.o: ./streams/fileback.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_filestream.o: ./streams/filestream.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index fb110ed2ec..be7db5bdb1 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -56,6 +56,7 @@ TEST_OBJECTS = \ $(OBJS)\test_bstream.obj \ $(OBJS)\test_datastreamtest.obj \ $(OBJS)\test_ffilestream.obj \ + $(OBJS)\test_fileback.obj \ $(OBJS)\test_filestream.obj \ $(OBJS)\test_largefile.obj \ $(OBJS)\test_memstream.obj \ @@ -493,6 +494,9 @@ $(OBJS)\test_datastreamtest.obj: .\streams\datastreamtest.cpp $(OBJS)\test_ffilestream.obj: .\streams\ffilestream.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $** +$(OBJS)\test_fileback.obj: .\streams\fileback.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $** + $(OBJS)\test_filestream.obj: .\streams\filestream.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $** diff --git a/tests/makefile.wat b/tests/makefile.wat index edd6508ce2..fec8ef6c4c 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -261,6 +261,7 @@ TEST_OBJECTS = & $(OBJS)\test_bstream.obj & $(OBJS)\test_datastreamtest.obj & $(OBJS)\test_ffilestream.obj & + $(OBJS)\test_fileback.obj & $(OBJS)\test_filestream.obj & $(OBJS)\test_largefile.obj & $(OBJS)\test_memstream.obj & @@ -442,6 +443,9 @@ $(OBJS)\test_datastreamtest.obj : .AUTODEPEND .\streams\datastreamtest.cpp $(OBJS)\test_ffilestream.obj : .AUTODEPEND .\streams\ffilestream.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_fileback.obj : .AUTODEPEND .\streams\fileback.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_filestream.obj : .AUTODEPEND .\streams\filestream.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< diff --git a/tests/streams/fileback.cpp b/tests/streams/fileback.cpp new file mode 100644 index 0000000000..0fbce3ec1e --- /dev/null +++ b/tests/streams/fileback.cpp @@ -0,0 +1,202 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/streams/backfile.cpp +// Purpose: Test wxBackingFile +// Author: Mike Wetherell +// RCS-ID: $Id$ +// Copyright: (c) 2006 Mike Wetherell +// Licence: wxWidgets licence +/////////////////////////////////////////////////////////////////////////////// + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +// for all others, include the necessary headers +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif + +#include "wx/mstream.h" +#include "wx/fileback.h" +#include "bstream.h" + +#if wxUSE_STREAMS + +const size_t TESTSIZE = 256; +const size_t BUFSIZE = 100; + + +/////////////////////////////////////////////////////////////////////////////// +// Parent stream for testing + +class TestStream : public wxMemoryInputStream +{ +public: + TestStream(const void *buf, size_t size) + : wxMemoryInputStream(buf, size) { } + + wxFileOffset GetLength() const { return wxInvalidOffset; } +}; + + +/////////////////////////////////////////////////////////////////////////////// +// The test case + +class backStream : public CppUnit::TestCase +{ +public: + backStream(); + + CPPUNIT_TEST_SUITE(backStream); + CPPUNIT_TEST(ReadLenSeek); + CPPUNIT_TEST(LenSeekRead); + CPPUNIT_TEST(SeekReadLen); + CPPUNIT_TEST(ReadAll); + CPPUNIT_TEST(ReadTooMuch); + CPPUNIT_TEST(EmptyStream); + CPPUNIT_TEST_SUITE_END(); + + void ReadLenSeek(); + void LenSeekRead(); + void SeekReadLen(); + void ReadAll(); + void ReadTooMuch(); + void EmptyStream(); + +private: + void Read(wxInputStream& in, size_t size1, size_t size2, size_t size3); + void Len(wxInputStream& in); + void Seek(wxInputStream& in); + + char m_testdata[TESTSIZE]; +}; + +backStream::backStream() +{ + for (unsigned i = 0; i < TESTSIZE; i++) + m_testdata[i] = i; +} + +void backStream::ReadLenSeek() +{ + wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); + wxBackedInputStream in(bf); + + Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); + Len(in); + Seek(in); +} + +void backStream::LenSeekRead() +{ + wxBackingFile bf(new wxMemoryInputStream(m_testdata, TESTSIZE), BUFSIZE); + wxBackedInputStream in(bf); + + Len(in); + Seek(in); + Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); +} + +void backStream::SeekReadLen() +{ + wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); + wxBackedInputStream in(bf); + + Seek(in); + Read(in, BUFSIZE, BUFSIZE / 2, 2 * BUFSIZE / 3); + Len(in); +} + +void backStream::ReadAll() +{ + wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); + wxBackedInputStream in(bf); + + Read(in, TESTSIZE, 0, 0); +} + +void backStream::ReadTooMuch() +{ + wxBackingFile bf(new TestStream(m_testdata, TESTSIZE), BUFSIZE); + wxBackedInputStream in(bf); + + char buf[TESTSIZE * 2]; + + CPPUNIT_ASSERT_EQUAL(TESTSIZE, in.Read(buf, TESTSIZE * 2).LastRead()); + CPPUNIT_ASSERT(in.Eof()); + CPPUNIT_ASSERT(memcmp(buf, m_testdata, TESTSIZE) == 0); +} + +void backStream::EmptyStream() +{ + wxBackingFile bf(new TestStream(m_testdata, 0), BUFSIZE); + wxBackedInputStream in(bf); + + char buf[1]; + + CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, 1).LastRead()); + CPPUNIT_ASSERT(in.Eof()); +} + +void backStream::Read(wxInputStream& in, + size_t size1, + size_t size2, + size_t size3) +{ + const size_t remainder = TESTSIZE - size1 - size2 - size3; + char buf[TESTSIZE]; + char *testdata = m_testdata; + + in.SeekI(0); + + CPPUNIT_ASSERT_EQUAL(size1, in.Read(buf, size1).LastRead()); + CPPUNIT_ASSERT(in.IsOk()); + CPPUNIT_ASSERT(memcmp(buf, testdata, size1) == 0); + testdata += size1; + + CPPUNIT_ASSERT_EQUAL(size2, in.Read(buf, size2).LastRead()); + CPPUNIT_ASSERT(in.IsOk()); + CPPUNIT_ASSERT(memcmp(buf, testdata, size2) == 0); + testdata += size2; + + CPPUNIT_ASSERT_EQUAL(size3, in.Read(buf, size3).LastRead()); + CPPUNIT_ASSERT(in.IsOk()); + CPPUNIT_ASSERT(memcmp(buf, testdata, size3) == 0); + testdata += size3; + + CPPUNIT_ASSERT_EQUAL(remainder, in.Read(buf, TESTSIZE).LastRead()); + CPPUNIT_ASSERT(in.Eof()); + CPPUNIT_ASSERT(memcmp(buf, testdata, remainder) == 0); + + CPPUNIT_ASSERT_EQUAL(size_t(0), in.Read(buf, TESTSIZE).LastRead()); + CPPUNIT_ASSERT(in.Eof()); +} + +void backStream::Len(wxInputStream& in) +{ + CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.GetLength()); +} + +void backStream::Seek(wxInputStream& in) +{ + CPPUNIT_ASSERT_EQUAL(wxFileOffset(TESTSIZE), in.SeekI(TESTSIZE)); + in.GetC(); + CPPUNIT_ASSERT_EQUAL(size_t(0), in.LastRead()); + CPPUNIT_ASSERT(in.Eof()); + + for (wxFileOffset i = TESTSIZE - 1; i >= 0; i--) { + CPPUNIT_ASSERT_EQUAL(i, in.SeekI(i)); + CPPUNIT_ASSERT_EQUAL(i, in.TellI()); + CPPUNIT_ASSERT_EQUAL(char(i), in.GetC()); + CPPUNIT_ASSERT_EQUAL(size_t(1), in.LastRead()); + CPPUNIT_ASSERT(in.IsOk()); + } +} + +// Register the stream sub suite, by using some stream helper macro. +// Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite()) +STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(backStream) + +#endif // wxUSE_STREAMS diff --git a/tests/test.bkl b/tests/test.bkl index 67aeca9930..b5ee07080f 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -51,6 +51,7 @@ streams/bstream.cpp streams/datastreamtest.cpp streams/ffilestream.cpp + streams/fileback.cpp streams/filestream.cpp streams/largefile.cpp streams/memstream.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index 17f88cbbda..ba9224b9da 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -473,6 +473,10 @@ SOURCE=.\streams\ffilestream.cpp # End Source File # Begin Source File +SOURCE=.\streams\fileback.cpp +# End Source File +# Begin Source File + SOURCE=.\fileconf\fileconftest.cpp # End Source File # Begin Source File