1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/textstreamtest.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
);
47 CPPUNIT_TEST( MiscTests
);
50 CPPUNIT_TEST( TestLongLong
);
51 CPPUNIT_TEST( TestLongLong
);
52 #endif // wxUSE_LONGLONG
55 CPPUNIT_TEST( TestUTF8Input
);
56 CPPUNIT_TEST( TestEmbeddedZerosUTF16LEInput
);
57 CPPUNIT_TEST( TestEmbeddedZerosUTF16BEInput
);
58 CPPUNIT_TEST( TestEmbeddedZerosUTF32LEInput
);
59 CPPUNIT_TEST( TestEmbeddedZerosUTF32BEInput
);
60 #endif // wxUSE_UNICODE
61 CPPUNIT_TEST_SUITE_END();
69 #endif // wxUSE_LONGLONG
73 void TestEmbeddedZerosUTF16LEInput();
74 void TestEmbeddedZerosUTF16BEInput();
75 void TestEmbeddedZerosUTF32LEInput();
76 void TestEmbeddedZerosUTF32BEInput();
77 void TestInput(const wxMBConv
& conv
,
78 const void* encodedText
,
80 #endif // wxUSE_UNICODE
83 DECLARE_NO_COPY_CLASS(TextStreamTestCase
)
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase
);
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase
, "TextStreamTestCase" );
92 TextStreamTestCase::TextStreamTestCase()
96 #if defined(__WXMSW__) || defined(__WXPM__)
97 # define NEWLINE "\r\n"
99 #elif defined(__WXMAC__) && !defined(__DARWIN__)
100 # define NEWLINE "\r"
101 # define NEWLINELEN 1
103 # define NEWLINE "\n"
104 # define NEWLINELEN 1
107 void TextStreamTestCase::Endline()
109 wxFileOutputStream
* pOutFile
= new wxFileOutputStream(wxT("test.txt"));
110 wxTextOutputStream
* pOutText
= new wxTextOutputStream(*pOutFile
);
111 *pOutText
<< wxT("Test text") << endl
112 << wxT("More Testing Text (There should be newline before this)");
117 wxFileInputStream
* pInFile
= new wxFileInputStream(wxT("test.txt"));
119 char szIn
[9 + NEWLINELEN
];
121 pInFile
->Read(szIn
, 9 + NEWLINELEN
);
123 CPPUNIT_ASSERT( memcmp(&szIn
[9], NEWLINE
, NEWLINELEN
) == 0 );
128 void TextStreamTestCase::MiscTests()
130 wxString filename
= wxT("testdata.fc");
131 wxFileInputStream
fsIn(filename
);
137 wxTextInputStream
tis(fsIn
);
138 CPPUNIT_ASSERT_EQUAL("# this is the test data file for wxFileConfig tests", tis
.ReadLine());
139 CPPUNIT_ASSERT_EQUAL("value1=one", tis
.ReadLine());
140 CPPUNIT_ASSERT_EQUAL("# a comment here", tis
.ReadLine());
141 CPPUNIT_ASSERT_EQUAL("value2=two", tis
.ReadLine());
142 CPPUNIT_ASSERT_EQUAL("value\\ with\\ spaces\\ inside\\ it=nothing special", tis
.ReadLine());
143 CPPUNIT_ASSERT_EQUAL("path=$PATH", tis
.ReadLine());
148 template <typename T
>
149 static void DoTestRoundTrip(const T
*values
, size_t numValues
)
152 wxFileOutputStream
fileOut(wxT("test.txt"));
153 wxTextOutputStream
textOut(fileOut
);
155 for ( size_t n
= 0; n
< numValues
; n
++ )
157 textOut
<< values
[n
] << endl
;
162 wxFileInputStream
fileIn(wxT("test.txt"));
163 wxTextInputStream
textIn(fileIn
);
166 for ( size_t n
= 0; n
< numValues
; n
++ )
170 CPPUNIT_ASSERT( value
== values
[n
] );
175 void TextStreamTestCase::TestLongLong()
177 static const wxLongLong llvalues
[] =
184 wxLL(0x123456789abcdef0),
185 wxLL(-0x123456789abcdef0),
188 DoTestRoundTrip(llvalues
, WXSIZEOF(llvalues
));
191 void TextStreamTestCase::TestULongLong()
193 static const wxULongLong ullvalues
[] =
198 wxULL(0x123456789abcdef0),
201 DoTestRoundTrip(ullvalues
, WXSIZEOF(ullvalues
));
204 #endif // wxUSE_LONGLONG
208 static const wchar_t txtWchar
[4] =
210 0x0041, // LATIN CAPITAL LETTER A
211 0x0100, // A WITH BREVE, LATIN SMALL LETTER
212 0x0041, // LATIN CAPITAL LETTER A
213 0x0100, // A WITH BREVE, LATIN SMALL LETTER
216 static const unsigned char txtUtf8
[6] =
218 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
221 static const unsigned char txtUtf16le
[8] =
223 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
226 static const unsigned char txtUtf16be
[8] =
228 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
231 static const unsigned char txtUtf32le
[16] =
233 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
234 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
237 static const unsigned char txtUtf32be
[16] =
239 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
240 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
243 void TextStreamTestCase::TestUTF8Input()
245 TestInput(wxConvUTF8
, txtUtf8
, sizeof(txtUtf8
));
246 TestInput(wxCSConv(wxFONTENCODING_UTF8
), txtUtf8
, sizeof(txtUtf8
));
249 void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
251 TestInput(wxMBConvUTF16LE(), txtUtf16le
, sizeof(txtUtf16le
));
252 TestInput(wxCSConv(wxFONTENCODING_UTF16LE
), txtUtf16le
, sizeof(txtUtf16le
));
255 void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
257 TestInput(wxMBConvUTF16BE(), txtUtf16be
, sizeof(txtUtf16be
));
258 TestInput(wxCSConv(wxFONTENCODING_UTF16BE
), txtUtf16be
, sizeof(txtUtf16be
));
261 void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
263 TestInput(wxMBConvUTF32LE(), txtUtf32le
, sizeof(txtUtf32le
));
264 TestInput(wxCSConv(wxFONTENCODING_UTF32LE
), txtUtf32le
, sizeof(txtUtf32le
));
267 void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
269 TestInput(wxMBConvUTF32BE(), txtUtf32be
, sizeof(txtUtf32be
));
270 TestInput(wxCSConv(wxFONTENCODING_UTF32BE
), txtUtf32be
, sizeof(txtUtf32be
));
273 void TextStreamTestCase::TestInput(const wxMBConv
& conv
,
274 const void *encodedText
,
277 wxMemoryInputStream
byteIn(encodedText
, encodedSize
);
278 wxTextInputStream
textIn(byteIn
, wxT("\n"), conv
);
281 while ( wxChar c
= textIn
.GetChar() )
286 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar
), temp
.length() );
288 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar
, temp
.wc_str(), sizeof(txtWchar
)) );
291 #endif // wxUSE_UNICODE