sstream.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/sstream.cpp
  3. // Purpose: Test wxStringInputStream/wxStringOutputStream
  4. // Author: Vadim Zeitlin
  5. // Copyright: (c) 2004 Vadim Zeitlin
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. // and "wx/cppunit.h"
  10. #include "testprec.h"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. // for all others, include the necessary headers
  15. #ifndef WX_PRECOMP
  16. #endif
  17. #include "wx/sstream.h"
  18. #include "bstream.h"
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // The test case
  21. //
  22. // Try to fully test wxStringInputStream and wxStringOutputStream
  23. class strStream :
  24. public BaseStreamTestCase<wxStringInputStream, wxStringOutputStream>
  25. {
  26. public:
  27. strStream();
  28. virtual ~strStream();
  29. CPPUNIT_TEST_SUITE(strStream);
  30. // Base class stream tests the strStream supports.
  31. CPPUNIT_TEST(Input_GetSize);
  32. CPPUNIT_TEST(Input_GetC);
  33. CPPUNIT_TEST(Input_Read);
  34. CPPUNIT_TEST(Input_Eof);
  35. CPPUNIT_TEST(Input_LastRead);
  36. CPPUNIT_TEST(Input_CanRead);
  37. CPPUNIT_TEST(Input_SeekI);
  38. CPPUNIT_TEST(Input_TellI);
  39. CPPUNIT_TEST(Input_Peek);
  40. CPPUNIT_TEST(Input_Ungetch);
  41. CPPUNIT_TEST(Output_PutC);
  42. CPPUNIT_TEST(Output_Write);
  43. CPPUNIT_TEST(Output_LastWrite);
  44. // seeking currently not supported by output string stream
  45. //CPPUNIT_TEST(Output_SeekO);
  46. //CPPUNIT_TEST(Output_TellO);
  47. // Other test specific for String stream test case.
  48. CPPUNIT_TEST(Output_Check);
  49. CPPUNIT_TEST_SUITE_END();
  50. protected:
  51. void Output_Check();
  52. private:
  53. // Implement base class functions.
  54. virtual wxStringInputStream *DoCreateInStream();
  55. virtual wxStringOutputStream *DoCreateOutStream();
  56. // output the given string to wxStringOutputStream and check that its
  57. // contents is exactly the same string
  58. void CheckString(const wxString& text);
  59. wxString m_str;
  60. };
  61. strStream::strStream()
  62. {
  63. static const size_t LEN = 256;
  64. m_str.reserve(LEN);
  65. for ( size_t n = 0; n < LEN; n++ )
  66. {
  67. m_str += wxChar(wxT('A') + n % (wxT('Z') - wxT('A') + 1));
  68. }
  69. }
  70. strStream::~strStream()
  71. {
  72. }
  73. wxStringInputStream *strStream::DoCreateInStream()
  74. {
  75. wxStringInputStream *pStrInStream = new wxStringInputStream(m_str);
  76. CPPUNIT_ASSERT(pStrInStream->IsOk());
  77. return pStrInStream;
  78. }
  79. wxStringOutputStream *strStream::DoCreateOutStream()
  80. {
  81. wxStringOutputStream *pStrOutStream = new wxStringOutputStream();
  82. CPPUNIT_ASSERT(pStrOutStream->IsOk());
  83. return pStrOutStream;
  84. }
  85. void strStream::CheckString(const wxString& text)
  86. {
  87. wxStringOutputStream sos;
  88. const wxCharBuffer buf(text.To8BitData());
  89. sos.Write(buf, buf.length());
  90. CPPUNIT_ASSERT_EQUAL( text, sos.GetString() );
  91. }
  92. void strStream::Output_Check()
  93. {
  94. CheckString("Hello world!");
  95. CheckString(wxString("hi\0dden", 8));
  96. }
  97. // Register the stream sub suite, by using some stream helper macro.
  98. STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(strStream)