1/////////////////////////////////////////////////////////////////////////////// 2// Name: tests/uris/uris.cpp 3// Purpose: wxTextXXXStream unit test 4// Author: Ryan Norton, Vince Harron 5// Created: 2004-08-14 6// RCS-ID: $Id: textstreamtest.cpp 43988 2006-12-16 15:09:32Z VZ $ 7// Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron 8/////////////////////////////////////////////////////////////////////////////// 9 10// ---------------------------------------------------------------------------- 11// headers 12// ---------------------------------------------------------------------------- 13 14#include "testprec.h" 15 16#ifdef __BORLANDC__ 17 #pragma hdrstop 18#endif 19 20#ifndef WX_PRECOMP 21 #include "wx/wx.h" 22#endif // WX_PRECOMP 23 24#include "wx/txtstrm.h" 25#include "wx/wfstream.h" 26 27#if wxUSE_LONGLONG 28 #include "wx/longlong.h" 29#endif 30 31#if wxUSE_UNICODE 32 #include "wx/mstream.h" 33#endif // wxUSE_UNICODE 34 35// ---------------------------------------------------------------------------- 36// test class 37// ---------------------------------------------------------------------------- 38 39class TextStreamTestCase : public CppUnit::TestCase 40{ 41public: 42 TextStreamTestCase(); 43 44private: 45 CPPUNIT_TEST_SUITE( TextStreamTestCase ); 46 CPPUNIT_TEST( Endline ); 47 48#if wxUSE_LONGLONG 49 CPPUNIT_TEST( TestLongLong ); 50 CPPUNIT_TEST( TestLongLong ); 51#endif // wxUSE_LONGLONG 52 53#if wxUSE_UNICODE 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(); 61 62 void Endline(); 63 64#if wxUSE_LONGLONG 65 void TestLongLong(); 66 void TestULongLong(); 67#endif // wxUSE_LONGLONG 68 69#if wxUSE_UNICODE 70 void TestUTF8Input(); 71 void TestEmbeddedZerosUTF16LEInput(); 72 void TestEmbeddedZerosUTF16BEInput(); 73 void TestEmbeddedZerosUTF32LEInput(); 74 void TestEmbeddedZerosUTF32BEInput(); 75 void TestInput(const wxMBConv& conv, 76 const void* encodedText, 77 size_t encodedSize ); 78#endif // wxUSE_UNICODE 79 80 81 DECLARE_NO_COPY_CLASS(TextStreamTestCase) 82}; 83 84// register in the unnamed registry so that these tests are run by default 85CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase ); 86 87// also include in it's own registry so that these tests can be run alone 88CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" ); 89 90TextStreamTestCase::TextStreamTestCase() 91{ 92} 93 94#if defined(__WXMSW__) || defined(__WXPM__) 95# define NEWLINE "\r\n" 96# define NEWLINELEN 2 97#elif defined(__WXMAC__) && !defined(__DARWIN__) 98# define NEWLINE "\r" 99# define NEWLINELEN 1 100#else 101# define NEWLINE "\n" 102# define NEWLINELEN 1 103#endif 104 105void TextStreamTestCase::Endline() 106{ 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)"); 111 112 delete pOutText; 113 delete pOutFile; 114 115 wxFileInputStream* pInFile = new wxFileInputStream(_T("test.txt")); 116 117 char szIn[9 + NEWLINELEN]; 118 119 pInFile->Read(szIn, 9 + NEWLINELEN); 120 121 CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 ); 122 123 delete pInFile; 124} 125 126#if wxUSE_LONGLONG 127 128template <typename T> 129static void DoTestRoundTrip(const T *values, size_t numValues) 130{ 131 { 132 wxFileOutputStream fileOut(_T("test.txt")); 133 wxTextOutputStream textOut(fileOut); 134 135 for ( size_t n = 0; n < numValues; n++ ) 136 { 137 textOut << values[n] << endl; 138 } 139 } 140 141 { 142 wxFileInputStream fileIn(_T("test.txt")); 143 wxTextInputStream textIn(fileIn); 144 145 T value; 146 for ( size_t n = 0; n < numValues; n++ ) 147 { 148 textIn >> value; 149 150 CPPUNIT_ASSERT( value == values[n] ); 151 } 152 } 153} 154 155void TextStreamTestCase::TestLongLong() 156{ 157 static const wxLongLong llvalues[] = 158 { 159 0, 160 1, 161 -1, 162 0x12345678l, 163 -0x12345678l, 164 wxLL(0x123456789abcdef0), 165 wxLL(-0x123456789abcdef0), 166 }; 167 168 DoTestRoundTrip(llvalues, WXSIZEOF(llvalues)); 169} 170 171void TextStreamTestCase::TestULongLong() 172{ 173 static const wxULongLong ullvalues[] = 174 { 175 0, 176 1, 177 0x12345678l, 178 wxULL(0x123456789abcdef0), 179 }; 180 181 DoTestRoundTrip(ullvalues, WXSIZEOF(ullvalues)); 182} 183 184#endif // wxUSE_LONGLONG 185 186#if wxUSE_UNICODE 187 188const static wchar_t txtWchar[4] = 189{ 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 194}; 195 196const static unsigned char txtUtf8[6] = 197{ 198 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80, 199}; 200 201const static unsigned char txtUtf16le[8] = 202{ 203 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01, 204}; 205 206const static unsigned char txtUtf16be[8] = 207{ 208 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00, 209}; 210 211const static unsigned char txtUtf32le[16] = 212{ 213 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 214 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 215}; 216 217const static unsigned char txtUtf32be[16] = 218{ 219 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00, 220 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00, 221}; 222 223void TextStreamTestCase::TestUTF8Input() 224{ 225 TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8)); 226 TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8)); 227} 228 229void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput() 230{ 231 TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le)); 232 TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le)); 233} 234 235void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput() 236{ 237 TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be)); 238 TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be)); 239} 240 241void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput() 242{ 243 TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le)); 244 TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le)); 245} 246 247void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput() 248{ 249 TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be)); 250 TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be)); 251} 252 253void TextStreamTestCase::TestInput(const wxMBConv& conv, 254 const void *encodedText, 255 size_t encodedSize) 256{ 257 wxMemoryInputStream byteIn(encodedText, encodedSize); 258 wxTextInputStream textIn(byteIn, wxT("\n"), conv); 259 260 wxString temp; 261 while ( wxChar c = textIn.GetChar() ) 262 { 263 temp.Append(c); 264 } 265 266 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar), temp.length() ); 267 268 CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.c_str(), sizeof(txtWchar)) ); 269} 270 271#endif // wxUSE_UNICODE 272