]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/memstream.cpp
OSX adaptions
[wxWidgets.git] / 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 ///////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx/wx.h".
10 // and "wx/cppunit.h"
11 #include "testprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 // for all others, include the necessary headers
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #include "wx/mstream.h"
23
24 #include "bstream.h"
25
26 #define DATABUFFER_SIZE 256
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // The test case
30 //
31 // Try to fully test wxMemoryInputStream and wxMemoryOutputStream
32
33 class memStream : public BaseStreamTestCase<wxMemoryInputStream, wxMemoryOutputStream>
34 {
35 public:
36 memStream();
37 virtual ~memStream();
38
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);
51
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);
57
58 // Other test specific for Memory stream test case.
59 CPPUNIT_TEST(Ctor_InFromIn);
60 CPPUNIT_TEST(Ctor_InFromOut);
61 CPPUNIT_TEST_SUITE_END();
62
63 protected:
64 // Add own test here.
65 void Ctor_InFromIn();
66 void Ctor_InFromOut();
67
68 private:
69 const char *GetDataBuffer();
70
71 private:
72 // Implement base class functions.
73 virtual wxMemoryInputStream *DoCreateInStream();
74 virtual wxMemoryOutputStream *DoCreateOutStream();
75
76 private:
77 char m_DataBuffer[DATABUFFER_SIZE];
78 };
79
80 memStream::memStream()
81 {
82 // Init the data buffer.
83 for (size_t i = 0; i < DATABUFFER_SIZE; i++)
84 m_DataBuffer[i] = (i % 0xFF);
85 }
86
87 memStream::~memStream()
88 {
89 /* Nothing extra for now. */
90 }
91
92 const char *memStream::GetDataBuffer()
93 {
94 return m_DataBuffer;
95 }
96
97 wxMemoryInputStream *memStream::DoCreateInStream()
98 {
99 wxMemoryInputStream *pMemInStream = new wxMemoryInputStream(GetDataBuffer(), DATABUFFER_SIZE);
100 CPPUNIT_ASSERT(pMemInStream->IsOk());
101 return pMemInStream;
102 }
103 wxMemoryOutputStream *memStream::DoCreateOutStream()
104 {
105 wxMemoryOutputStream *pMemOutStream = new wxMemoryOutputStream();
106 CPPUNIT_ASSERT(pMemOutStream->IsOk());
107 return pMemOutStream;
108 }
109
110 void memStream::Ctor_InFromIn()
111 {
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;
124
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);
135
136 delete[] dat;
137 delete pMemInStream2;
138 delete pMemInStream1;
139 }
140
141 void memStream::Ctor_InFromOut()
142 {
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);
155 delete pMemInStream;
156 delete pMemOutStream;
157 }
158
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)