1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxTextXXXStream unit test
4 // Author: Ryan Norton, Vince Harron
7 // Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/txtstrm.h"
25 #include "wx/wfstream.h"
28 #include "wx/longlong.h"
32 #include "wx/mstream.h"
33 #endif // wxUSE_UNICODE
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class TextStreamTestCase
: public CppUnit::TestCase
45 CPPUNIT_TEST_SUITE( TextStreamTestCase
);
46 CPPUNIT_TEST( Endline
);
49 CPPUNIT_TEST( TestLongLong
);
50 CPPUNIT_TEST( TestLongLong
);
51 #endif // wxUSE_LONGLONG
54 CPPUNIT_TEST( TestUTF8Input
);
55 CPPUNIT_TEST( TestEmbeddedZerosUTF16LEInput
);
56 CPPUNIT_TEST( TestEmbeddedZerosUTF16BEInput
);
57 CPPUNIT_TEST( TestEmbeddedZerosUTF32LEInput
);
58 CPPUNIT_TEST( TestEmbeddedZerosUTF32BEInput
);
59 #endif // wxUSE_UNICODE
60 CPPUNIT_TEST_SUITE_END();
67 #endif // wxUSE_LONGLONG
71 void TestEmbeddedZerosUTF16LEInput();
72 void TestEmbeddedZerosUTF16BEInput();
73 void TestEmbeddedZerosUTF32LEInput();
74 void TestEmbeddedZerosUTF32BEInput();
75 void TestInput(const wxMBConv
& conv
,
76 const void* encodedText
,
78 #endif // wxUSE_UNICODE
81 DECLARE_NO_COPY_CLASS(TextStreamTestCase
)
84 // register in the unnamed registry so that these tests are run by default
85 CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase
);
87 // also include in it's own registry so that these tests can be run alone
88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase
, "TextStreamTestCase" );
90 TextStreamTestCase::TextStreamTestCase()
94 #if defined(__WXMSW__) || defined(__WXPM__)
95 # define NEWLINE "\r\n"
97 #elif defined(__WXMAC__) && !defined(__DARWIN__)
101 # define NEWLINE "\n"
102 # define NEWLINELEN 1
105 void TextStreamTestCase::Endline()
107 wxFileOutputStream
* pOutFile
= new wxFileOutputStream(_T("test.txt"));
108 wxTextOutputStream
* pOutText
= new wxTextOutputStream(*pOutFile
);
109 *pOutText
<< _T("Test text") << endl
110 << _T("More Testing Text (There should be newline before this)");
115 wxFileInputStream
* pInFile
= new wxFileInputStream(_T("test.txt"));
117 char szIn
[9 + NEWLINELEN
];
119 pInFile
->Read(szIn
, 9 + NEWLINELEN
);
121 CPPUNIT_ASSERT( memcmp(&szIn
[9], NEWLINE
, NEWLINELEN
) == 0 );
128 template <typename T
>
129 static void DoTestRoundTrip(const T
*values
, size_t numValues
)
132 wxFileOutputStream
fileOut(_T("test.txt"));
133 wxTextOutputStream
textOut(fileOut
);
135 for ( size_t n
= 0; n
< numValues
; n
++ )
137 textOut
<< values
[n
] << endl
;
142 wxFileInputStream
fileIn(_T("test.txt"));
143 wxTextInputStream
textIn(fileIn
);
146 for ( size_t n
= 0; n
< numValues
; n
++ )
150 CPPUNIT_ASSERT( value
== values
[n
] );
155 void TextStreamTestCase::TestLongLong()
157 static const wxLongLong llvalues
[] =
164 wxLL(0x123456789abcdef0),
165 wxLL(-0x123456789abcdef0),
168 DoTestRoundTrip(llvalues
, WXSIZEOF(llvalues
));
171 void TextStreamTestCase::TestULongLong()
173 static const wxULongLong ullvalues
[] =
178 wxULL(0x123456789abcdef0),
181 DoTestRoundTrip(ullvalues
, WXSIZEOF(ullvalues
));
184 #endif // wxUSE_LONGLONG
188 const static wchar_t txtWchar
[4] =
190 0x0041, // LATIN CAPITAL LETTER A
191 0x0100, // A WITH BREVE, LATIN SMALL LETTER
192 0x0041, // LATIN CAPITAL LETTER A
193 0x0100, // A WITH BREVE, LATIN SMALL LETTER
196 const static unsigned char txtUtf8
[6] =
198 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
201 const static unsigned char txtUtf16le
[8] =
203 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
206 const static unsigned char txtUtf16be
[8] =
208 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
211 const static unsigned char txtUtf32le
[16] =
213 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
214 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
217 const static unsigned char txtUtf32be
[16] =
219 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
220 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
223 void TextStreamTestCase::TestUTF8Input()
225 TestInput(wxConvUTF8
, txtUtf8
, sizeof(txtUtf8
));
226 TestInput(wxCSConv(wxFONTENCODING_UTF8
), txtUtf8
, sizeof(txtUtf8
));
229 void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
231 TestInput(wxMBConvUTF16LE(), txtUtf16le
, sizeof(txtUtf16le
));
232 TestInput(wxCSConv(wxFONTENCODING_UTF16LE
), txtUtf16le
, sizeof(txtUtf16le
));
235 void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
237 TestInput(wxMBConvUTF16BE(), txtUtf16be
, sizeof(txtUtf16be
));
238 TestInput(wxCSConv(wxFONTENCODING_UTF16BE
), txtUtf16be
, sizeof(txtUtf16be
));
241 void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
243 TestInput(wxMBConvUTF32LE(), txtUtf32le
, sizeof(txtUtf32le
));
244 TestInput(wxCSConv(wxFONTENCODING_UTF32LE
), txtUtf32le
, sizeof(txtUtf32le
));
247 void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
249 TestInput(wxMBConvUTF32BE(), txtUtf32be
, sizeof(txtUtf32be
));
250 TestInput(wxCSConv(wxFONTENCODING_UTF32BE
), txtUtf32be
, sizeof(txtUtf32be
));
253 void TextStreamTestCase::TestInput(const wxMBConv
& conv
,
254 const void *encodedText
,
257 wxMemoryInputStream
byteIn(encodedText
, encodedSize
);
258 wxTextInputStream
textIn(byteIn
, wxT("\n"), conv
);
261 while ( wxChar c
= textIn
.GetChar() )
266 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar
), temp
.length() );
268 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar
, temp
.c_str(), sizeof(txtWchar
)) );
271 #endif // wxUSE_UNICODE