| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: tests/uris/uris.cpp |
| 3 | // Purpose: wxTextXXXStream unit test |
| 4 | // Author: Ryan Norton |
| 5 | // Created: 2004-08-14 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2004 Ryan Norton |
| 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 | // ---------------------------------------------------------------------------- |
| 32 | // test class |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | class TextStreamTestCase : public CppUnit::TestCase |
| 36 | { |
| 37 | public: |
| 38 | TextStreamTestCase(); |
| 39 | |
| 40 | private: |
| 41 | CPPUNIT_TEST_SUITE( TextStreamTestCase ); |
| 42 | CPPUNIT_TEST( Endline ); |
| 43 | #if wxUSE_LONGLONG |
| 44 | CPPUNIT_TEST( TestLongLong ); |
| 45 | CPPUNIT_TEST( TestLongLong ); |
| 46 | #endif |
| 47 | CPPUNIT_TEST_SUITE_END(); |
| 48 | |
| 49 | void Endline(); |
| 50 | #if wxUSE_LONGLONG |
| 51 | void TestLongLong(); |
| 52 | void TestULongLong(); |
| 53 | #endif // wxUSE_LONGLONG |
| 54 | |
| 55 | |
| 56 | DECLARE_NO_COPY_CLASS(TextStreamTestCase) |
| 57 | }; |
| 58 | |
| 59 | // register in the unnamed registry so that these tests are run by default |
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase ); |
| 61 | |
| 62 | // also include in it's own registry so that these tests can be run alone |
| 63 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" ); |
| 64 | |
| 65 | TextStreamTestCase::TextStreamTestCase() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | #if defined(__WXMSW__) || defined(__WXPM__) |
| 70 | # define NEWLINE "\r\n" |
| 71 | # define NEWLINELEN 2 |
| 72 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
| 73 | # define NEWLINE "\r" |
| 74 | # define NEWLINELEN 1 |
| 75 | #else |
| 76 | # define NEWLINE "\n" |
| 77 | # define NEWLINELEN 1 |
| 78 | #endif |
| 79 | |
| 80 | void TextStreamTestCase::Endline() |
| 81 | { |
| 82 | wxFileOutputStream* pOutFile = new wxFileOutputStream(_T("test.txt")); |
| 83 | wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); |
| 84 | *pOutText << _T("Test text") << endl |
| 85 | << _T("More Testing Text (There should be newline before this)"); |
| 86 | |
| 87 | delete pOutText; |
| 88 | delete pOutFile; |
| 89 | |
| 90 | wxFileInputStream* pInFile = new wxFileInputStream(_T("test.txt")); |
| 91 | |
| 92 | char szIn[9 + NEWLINELEN]; |
| 93 | |
| 94 | pInFile->Read(szIn, 9 + NEWLINELEN); |
| 95 | |
| 96 | CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 ); |
| 97 | |
| 98 | delete pInFile; |
| 99 | } |
| 100 | |
| 101 | #if wxUSE_LONGLONG |
| 102 | |
| 103 | template <typename T> |
| 104 | static void DoTestRoundTrip(const T *values, size_t numValues) |
| 105 | { |
| 106 | { |
| 107 | wxFileOutputStream fileOut(_T("test.txt")); |
| 108 | wxTextOutputStream textOut(fileOut); |
| 109 | |
| 110 | for ( size_t n = 0; n < numValues; n++ ) |
| 111 | { |
| 112 | textOut << values[n] << endl; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | wxFileInputStream fileIn(_T("test.txt")); |
| 118 | wxTextInputStream textIn(fileIn); |
| 119 | |
| 120 | T value; |
| 121 | for ( size_t n = 0; n < numValues; n++ ) |
| 122 | { |
| 123 | textIn >> value; |
| 124 | |
| 125 | CPPUNIT_ASSERT( value == values[n] ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void TextStreamTestCase::TestLongLong() |
| 131 | { |
| 132 | static const wxLongLong llvalues[] = |
| 133 | { |
| 134 | 0, |
| 135 | 1, |
| 136 | -1, |
| 137 | 0x12345678l, |
| 138 | -0x12345678l, |
| 139 | wxLL(0x123456789abcdef0), |
| 140 | wxLL(-0x123456789abcdef0), |
| 141 | }; |
| 142 | |
| 143 | DoTestRoundTrip(llvalues, WXSIZEOF(llvalues)); |
| 144 | } |
| 145 | |
| 146 | void TextStreamTestCase::TestULongLong() |
| 147 | { |
| 148 | static const wxULongLong ullvalues[] = |
| 149 | { |
| 150 | 0, |
| 151 | 1, |
| 152 | 0x12345678l, |
| 153 | wxULL(0x123456789abcdef0), |
| 154 | }; |
| 155 | |
| 156 | DoTestRoundTrip(ullvalues, WXSIZEOF(ullvalues)); |
| 157 | } |
| 158 | |
| 159 | #endif // wxUSE_LONGLONG |
| 160 | |