]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/memstream.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/memstream.cpp
3 // Purpose: Test wxMemoryInputStream/wxMemoryOutputStream
4 // Author: Hans Van Leemputten
5 // Copyright: (c) 2004 Hans Van Leemputten
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx/wx.h".
17 // for all others, include the necessary headers
22 #include "wx/mstream.h"
26 #define DATABUFFER_SIZE 256
28 ///////////////////////////////////////////////////////////////////////////////
31 // Try to fully test wxMemoryInputStream and wxMemoryOutputStream
33 class memStream
: public BaseStreamTestCase
<wxMemoryInputStream
, wxMemoryOutputStream
>
39 CPPUNIT_TEST_SUITE(memStream
);
40 // Base class stream tests the memStream supports.
41 CPPUNIT_TEST(Input_GetSize
);
42 CPPUNIT_TEST(Input_GetC
);
43 CPPUNIT_TEST(Input_Read
);
44 CPPUNIT_TEST(Input_Eof
);
45 CPPUNIT_TEST(Input_LastRead
);
46 CPPUNIT_TEST(Input_CanRead
);
47 CPPUNIT_TEST(Input_SeekI
);
48 CPPUNIT_TEST(Input_TellI
);
49 CPPUNIT_TEST(Input_Peek
);
50 CPPUNIT_TEST(Input_Ungetch
);
52 CPPUNIT_TEST(Output_PutC
);
53 CPPUNIT_TEST(Output_Write
);
54 CPPUNIT_TEST(Output_LastWrite
);
55 CPPUNIT_TEST(Output_SeekO
);
56 CPPUNIT_TEST(Output_TellO
);
58 // Other test specific for Memory stream test case.
59 CPPUNIT_TEST(Ctor_InFromIn
);
60 CPPUNIT_TEST(Ctor_InFromOut
);
61 CPPUNIT_TEST_SUITE_END();
66 void Ctor_InFromOut();
69 const char *GetDataBuffer();
72 // Implement base class functions.
73 virtual wxMemoryInputStream
*DoCreateInStream();
74 virtual wxMemoryOutputStream
*DoCreateOutStream();
77 char m_DataBuffer
[DATABUFFER_SIZE
];
80 memStream::memStream()
82 // Init the data buffer.
83 for (size_t i
= 0; i
< DATABUFFER_SIZE
; i
++)
84 m_DataBuffer
[i
] = (i
% 0xFF);
87 memStream::~memStream()
89 /* Nothing extra for now. */
92 const char *memStream::GetDataBuffer()
97 wxMemoryInputStream
*memStream::DoCreateInStream()
99 wxMemoryInputStream
*pMemInStream
= new wxMemoryInputStream(GetDataBuffer(), DATABUFFER_SIZE
);
100 CPPUNIT_ASSERT(pMemInStream
->IsOk());
103 wxMemoryOutputStream
*memStream::DoCreateOutStream()
105 wxMemoryOutputStream
*pMemOutStream
= new wxMemoryOutputStream();
106 CPPUNIT_ASSERT(pMemOutStream
->IsOk());
107 return pMemOutStream
;
110 void memStream::Ctor_InFromIn()
112 wxMemoryInputStream
*pMemInStream1
= DoCreateInStream();
113 wxMemoryInputStream
*pMemInStream2
= new wxMemoryInputStream(*pMemInStream1
);
114 CPPUNIT_ASSERT(pMemInStream2
->IsOk());
115 CPPUNIT_ASSERT_EQUAL(pMemInStream1
->GetLength(), pMemInStream2
->GetLength());
116 wxFileOffset len
= pMemInStream2
->GetLength();
117 char *dat
= new char[len
];
118 pMemInStream2
->Read(dat
, len
);
119 CPPUNIT_ASSERT_EQUAL(len
, (wxFileOffset
)pMemInStream2
->LastRead());
120 wxStreamBuffer
*buf
= pMemInStream1
->GetInputStreamBuffer();
121 void *pIn
= buf
->GetBufferStart();
122 CPPUNIT_ASSERT(memcmp(pIn
, dat
, len
) == 0);
123 delete pMemInStream2
;
125 wxFileOffset len2
= len
/ 2;
126 CPPUNIT_ASSERT(len2
);
127 CPPUNIT_ASSERT(pMemInStream1
->SeekI(-len2
, wxFromCurrent
) != wxInvalidOffset
);
128 pIn
= buf
->GetBufferPos();
129 pMemInStream2
= new wxMemoryInputStream(*pMemInStream1
, len2
);
130 CPPUNIT_ASSERT(pMemInStream2
->IsOk());
131 CPPUNIT_ASSERT_EQUAL((wxFileOffset
)len2
, pMemInStream2
->GetLength());
132 pMemInStream2
->Read(dat
, len2
);
133 CPPUNIT_ASSERT_EQUAL(len2
, (wxFileOffset
)pMemInStream2
->LastRead());
134 CPPUNIT_ASSERT(memcmp(pIn
, dat
, len2
) == 0);
137 delete pMemInStream2
;
138 delete pMemInStream1
;
141 void memStream::Ctor_InFromOut()
143 wxMemoryOutputStream
*pMemOutStream
= DoCreateOutStream();
144 pMemOutStream
->Write(GetDataBuffer(), DATABUFFER_SIZE
);
145 wxMemoryInputStream
*pMemInStream
= new wxMemoryInputStream(*pMemOutStream
);
146 CPPUNIT_ASSERT(pMemInStream
->IsOk());
147 CPPUNIT_ASSERT_EQUAL(pMemInStream
->GetLength(), pMemOutStream
->GetLength());
148 size_t len
= pMemInStream
->GetLength();
149 wxStreamBuffer
*in
= pMemInStream
->GetInputStreamBuffer();
150 wxStreamBuffer
*out
= pMemOutStream
->GetOutputStreamBuffer();
151 void *pIn
= in
->GetBufferStart();
152 void *pOut
= out
->GetBufferStart();
153 CPPUNIT_ASSERT(pIn
!= pOut
);
154 CPPUNIT_ASSERT(memcmp(pIn
, pOut
, len
) == 0);
156 delete pMemOutStream
;
159 // Register the stream sub suite, by using some stream helper macro.
160 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
161 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(memStream
)