PageRenderTime 157ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/labo/2009.08.17/poco-1.3.5-mingw/Foundation/testsuite/src/TextIteratorTest.cpp

http://mingw-lib.googlecode.com/
C++ | 244 lines | 160 code | 48 blank | 36 comment | 54 complexity | 4a78a4e2373b2e09261536bcc10a0678 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. //
  2. // TextIteratorTest.cpp
  3. //
  4. // $Id: //poco/1.3/Foundation/testsuite/src/TextIteratorTest.cpp#2 $
  5. //
  6. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // Permission is hereby granted, free of charge, to any person or organization
  10. // obtaining a copy of the software and accompanying documentation covered by
  11. // this license (the "Software") to use, reproduce, display, distribute,
  12. // execute, and transmit the Software, and to prepare derivative works of the
  13. // Software, and to permit third-parties to whom the Software is furnished to
  14. // do so, all subject to the following:
  15. //
  16. // The copyright notices in the Software and this entire statement, including
  17. // the above license grant, this restriction and the following disclaimer,
  18. // must be included in all copies of the Software, in whole or in part, and
  19. // all derivative works of the Software, unless such copies or derivative
  20. // works are solely in the form of machine-executable object code generated by
  21. // a source language processor.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //
  31. #include "TextIteratorTest.h"
  32. #include "CppUnit/TestCaller.h"
  33. #include "CppUnit/TestSuite.h"
  34. #include "Poco/TextIterator.h"
  35. #include "Poco/Latin1Encoding.h"
  36. #include "Poco/UTF8Encoding.h"
  37. using Poco::TextIterator;
  38. using Poco::Latin1Encoding;
  39. using Poco::UTF8Encoding;
  40. TextIteratorTest::TextIteratorTest(const std::string& name): CppUnit::TestCase(name)
  41. {
  42. }
  43. TextIteratorTest::~TextIteratorTest()
  44. {
  45. }
  46. void TextIteratorTest::testEmptyLatin1()
  47. {
  48. Latin1Encoding encoding;
  49. std::string text;
  50. TextIterator it(text, encoding);
  51. TextIterator end(text);
  52. assert (it == end);
  53. }
  54. void TextIteratorTest::testOneLatin1()
  55. {
  56. Latin1Encoding encoding;
  57. std::string text("x");
  58. TextIterator it(text, encoding);
  59. TextIterator end(text);
  60. assert (it != end);
  61. assert (*it == 'x');
  62. ++it;
  63. assert (it == end);
  64. }
  65. void TextIteratorTest::testLatin1()
  66. {
  67. Latin1Encoding encoding;
  68. std::string text("Latin1");
  69. TextIterator it(text, encoding);
  70. TextIterator end(text);
  71. assert (it != end);
  72. assert (*it++ == 'L');
  73. assert (it != end);
  74. assert (*it++ == 'a');
  75. assert (it != end);
  76. assert (*it++ == 't');
  77. assert (it != end);
  78. assert (*it++ == 'i');
  79. assert (it != end);
  80. assert (*it++ == 'n');
  81. assert (it != end);
  82. assert (*it++ == '1');
  83. assert (it == end);
  84. std::string empty;
  85. it = TextIterator(empty, encoding);
  86. end = TextIterator(empty);
  87. assert (it == end);
  88. }
  89. void TextIteratorTest::testEmptyUTF8()
  90. {
  91. UTF8Encoding encoding;
  92. std::string text;
  93. TextIterator it(text, encoding);
  94. TextIterator end(text);
  95. assert (it == end);
  96. }
  97. void TextIteratorTest::testOneUTF8()
  98. {
  99. UTF8Encoding encoding;
  100. // 1 byte sequence
  101. std::string text("x");
  102. TextIterator it(text, encoding);
  103. TextIterator end(text);
  104. assert (it != end);
  105. assert (*it == 'x');
  106. ++it;
  107. assert (it == end);
  108. unsigned char data[Poco::TextEncoding::MAX_SEQUENCE_LENGTH];
  109. // 2 byte sequence
  110. int n = encoding.convert(0xab, data, sizeof(data));
  111. assert (n == 2);
  112. text.assign((char*) data, n);
  113. it = TextIterator(text, encoding);
  114. end = TextIterator(text);
  115. assert (it != end);
  116. assert (*it++ == 0xab);
  117. assert (it == end);
  118. // 3 byte sequence
  119. n = encoding.convert(0xabcd, data, sizeof(data));
  120. assert (n == 3);
  121. text.assign((char*) data, n);
  122. it = TextIterator(text, encoding);
  123. end = TextIterator(text);
  124. assert (it != end);
  125. assert (*it++ == 0xabcd);
  126. assert (it == end);
  127. // 4 byte sequence
  128. n = encoding.convert(0xabcde, data, sizeof(data));
  129. assert (n == 4);
  130. text.assign((char*) data, n);
  131. it = TextIterator(text, encoding);
  132. end = TextIterator(text);
  133. assert (it != end);
  134. assert (*it++ == 0xabcde);
  135. assert (it == end);
  136. // 5 byte sequence - not supported
  137. n = encoding.convert(0xabcdef, data, sizeof(data));
  138. assert (n == 0);
  139. // 6 byte sequence - not supported
  140. n = encoding.convert(0xfabcdef, data, sizeof(data));
  141. assert (n == 0);
  142. }
  143. void TextIteratorTest::testUTF8()
  144. {
  145. UTF8Encoding encoding;
  146. const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x00};
  147. std::string text((const char*) greek);
  148. TextIterator it(text, encoding);
  149. TextIterator end(text);
  150. assert (it != end);
  151. assert (*it++ == 0x0020);
  152. assert (it != end);
  153. assert (*it++ == 0x03ba);
  154. assert (it != end);
  155. assert (*it++ == 0x1f79);
  156. assert (it != end);
  157. assert (*it++ == 0x03c3);
  158. assert (it != end);
  159. assert (*it++ == 0x03bc);
  160. assert (it != end);
  161. assert (*it++ == 0x03b5);
  162. assert (it != end);
  163. assert (*it++ == 0x0020);
  164. assert (it == end);
  165. }
  166. void TextIteratorTest::testSwap()
  167. {
  168. Latin1Encoding encoding;
  169. std::string text("x");
  170. TextIterator it1(text, encoding);
  171. TextIterator it2(text, encoding);
  172. TextIterator end(text);
  173. assert (it1 == it2);
  174. it2.swap(end);
  175. assert (it1 != it2);
  176. it2.swap(end);
  177. assert (it1 == it2);
  178. }
  179. void TextIteratorTest::setUp()
  180. {
  181. }
  182. void TextIteratorTest::tearDown()
  183. {
  184. }
  185. CppUnit::Test* TextIteratorTest::suite()
  186. {
  187. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextIteratorTest");
  188. CppUnit_addTest(pSuite, TextIteratorTest, testEmptyLatin1);
  189. CppUnit_addTest(pSuite, TextIteratorTest, testOneLatin1);
  190. CppUnit_addTest(pSuite, TextIteratorTest, testLatin1);
  191. CppUnit_addTest(pSuite, TextIteratorTest, testEmptyUTF8);
  192. CppUnit_addTest(pSuite, TextIteratorTest, testOneUTF8);
  193. CppUnit_addTest(pSuite, TextIteratorTest, testUTF8);
  194. CppUnit_addTest(pSuite, TextIteratorTest, testSwap);
  195. return pSuite;
  196. }