]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/memstream.cpp
a9e8cd6ac144ecc5d611964d36d996d8a4f51dc3
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/memstream.cpp
3 // Purpose: Test wxMemoryInputStream/wxMemoryOutputStream
4 // Author: Hans Van Leemputten
6 // Copyright: (c) 2004 Hans Van Leemputten
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
18 // for all others, include the necessary headers
23 #include "wx/mstream.h"
27 #define DATABUFFER_SIZE 256
29 ///////////////////////////////////////////////////////////////////////////////
32 // Try to fully test wxMemoryInputStream and wxMemoryOutputStream
34 class memStream
: public BaseStreamTestCase
<wxMemoryInputStream
, wxMemoryOutputStream
>
40 CPPUNIT_TEST_SUITE(memStream
);
41 // Base class stream tests the memStream supports.
42 CPPUNIT_TEST(Input_GetSize
);
43 CPPUNIT_TEST(Input_GetC
);
44 CPPUNIT_TEST(Input_Read
);
45 CPPUNIT_TEST(Input_Eof
);
46 CPPUNIT_TEST(Input_LastRead
);
47 CPPUNIT_TEST(Input_CanRead
);
48 CPPUNIT_TEST(Input_SeekI
);
49 CPPUNIT_TEST(Input_TellI
);
50 CPPUNIT_TEST(Input_Peek
);
51 CPPUNIT_TEST(Input_Ungetch
);
53 CPPUNIT_TEST(Output_PutC
);
54 CPPUNIT_TEST(Output_Write
);
55 CPPUNIT_TEST(Output_LastWrite
);
56 CPPUNIT_TEST(Output_SeekO
);
57 CPPUNIT_TEST(Output_TellO
);
59 // Other test specific for Memory stream test case.
60 CPPUNIT_TEST(Ctor_InFromIn
);
61 CPPUNIT_TEST(Ctor_InFromOut
);
62 CPPUNIT_TEST_SUITE_END();
67 void Ctor_InFromOut();
70 const char *GetDataBuffer();
73 // Implement base class functions.
74 virtual wxMemoryInputStream
*DoCreateInStream();
75 virtual wxMemoryOutputStream
*DoCreateOutStream();
78 char m_DataBuffer
[DATABUFFER_SIZE
];
81 memStream::memStream()
83 // Init the data buffer.
84 for (size_t i
= 0; i
< DATABUFFER_SIZE
; i
++)
85 m_DataBuffer
[i
] = (i
% 0xFF);
88 memStream::~memStream()
90 /* Nothing extra for now. */
93 const char *memStream::GetDataBuffer()
98 wxMemoryInputStream
*memStream::DoCreateInStream()
100 wxMemoryInputStream
*pMemInStream
= new wxMemoryInputStream(GetDataBuffer(), DATABUFFER_SIZE
);
101 CPPUNIT_ASSERT(pMemInStream
->IsOk());
104 wxMemoryOutputStream
*memStream::DoCreateOutStream()
106 wxMemoryOutputStream
*pMemOutStream
= new wxMemoryOutputStream();
107 CPPUNIT_ASSERT(pMemOutStream
->IsOk());
108 return pMemOutStream
;
111 void memStream::Ctor_InFromIn()
113 wxMemoryInputStream
*pMemInStream1
= DoCreateInStream();
114 wxMemoryInputStream
*pMemInStream2
= new wxMemoryInputStream(*pMemInStream1
);
115 CPPUNIT_ASSERT(pMemInStream2
->IsOk());
116 CPPUNIT_ASSERT_EQUAL(pMemInStream1
->GetLength(), pMemInStream2
->GetLength());
117 wxFileOffset len
= pMemInStream2
->GetLength();
118 char *dat
= new char[len
];
119 pMemInStream2
->Read(dat
, len
);
120 CPPUNIT_ASSERT_EQUAL(len
, (wxFileOffset
)pMemInStream2
->LastRead());
121 wxStreamBuffer
*buf
= pMemInStream1
->GetInputStreamBuffer();
122 void *pIn
= buf
->GetBufferStart();
123 CPPUNIT_ASSERT(memcmp(pIn
, dat
, len
) == 0);
124 delete pMemInStream2
;
126 wxFileOffset len2
= len
/ 2;
127 CPPUNIT_ASSERT(len2
);
128 CPPUNIT_ASSERT(pMemInStream1
->SeekI(-len2
, wxFromCurrent
) != wxInvalidOffset
);
129 pIn
= buf
->GetBufferPos();
130 pMemInStream2
= new wxMemoryInputStream(*pMemInStream1
, len2
);
131 CPPUNIT_ASSERT(pMemInStream2
->IsOk());
132 CPPUNIT_ASSERT_EQUAL((wxFileOffset
)len2
, pMemInStream2
->GetLength());
133 pMemInStream2
->Read(dat
, len2
);
134 CPPUNIT_ASSERT_EQUAL(len2
, (wxFileOffset
)pMemInStream2
->LastRead());
135 CPPUNIT_ASSERT(memcmp(pIn
, dat
, len2
) == 0);
138 delete pMemInStream2
;
139 delete pMemInStream1
;
142 void memStream::Ctor_InFromOut()
144 wxMemoryOutputStream
*pMemOutStream
= DoCreateOutStream();
145 pMemOutStream
->Write(GetDataBuffer(), DATABUFFER_SIZE
);
146 wxMemoryInputStream
*pMemInStream
= new wxMemoryInputStream(*pMemOutStream
);
147 CPPUNIT_ASSERT(pMemInStream
->IsOk());
148 CPPUNIT_ASSERT_EQUAL(pMemInStream
->GetLength(), pMemOutStream
->GetLength());
149 size_t len
= pMemInStream
->GetLength();
150 wxStreamBuffer
*in
= pMemInStream
->GetInputStreamBuffer();
151 wxStreamBuffer
*out
= pMemOutStream
->GetOutputStreamBuffer();
152 void *pIn
= in
->GetBufferStart();
153 void *pOut
= out
->GetBufferStart();
154 CPPUNIT_ASSERT(pIn
!= pOut
);
155 CPPUNIT_ASSERT(memcmp(pIn
, pOut
, len
) == 0);
157 delete pMemOutStream
;
160 // Register the stream sub suite, by using some stream helper macro.
161 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
162 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(memStream
)