performance_iterators.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_iterators.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <algorithm>
  8. #include <vector>
  9. #include <cstdlib> // for rand
  10. #include <functional>
  11. #include <sstream> // used to test stream iterators
  12. #include <boost/config.hpp>
  13. #ifdef BOOST_NO_STDC_NAMESPACE
  14. namespace std{
  15. using ::rand;
  16. }
  17. #endif
  18. #include <boost/detail/workaround.hpp>
  19. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  20. #include <boost/archive/dinkumware.hpp>
  21. #endif
  22. #ifndef BOOST_NO_CWCHAR
  23. #include <boost/archive/iterators/mb_from_wchar.hpp>
  24. #include <boost/archive/iterators/wchar_from_mb.hpp>
  25. #endif
  26. #include <boost/archive/iterators/xml_escape.hpp>
  27. #include <boost/archive/iterators/xml_unescape.hpp>
  28. #include <boost/archive/iterators/transform_width.hpp>
  29. #include <boost/archive/iterators/istream_iterator.hpp>
  30. #include <boost/archive/iterators/ostream_iterator.hpp>
  31. #include "../test/test_tools.hpp"
  32. #ifndef BOOST_NO_CWCHAR
  33. void test_wchar_from_mb(const wchar_t *la, const char * a, const unsigned int size){
  34. typedef boost::archive::iterators::wchar_from_mb<const char *> translator;
  35. BOOST_CHECK((
  36. std::equal(
  37. translator(BOOST_MAKE_PFTO_WRAPPER(a)),
  38. translator(BOOST_MAKE_PFTO_WRAPPER(a + size)),
  39. la
  40. )
  41. ));
  42. }
  43. void test_mb_from_wchar(const char * a, const wchar_t *la, const unsigned int size){
  44. typedef boost::archive::iterators::mb_from_wchar<const wchar_t *> translator;
  45. BOOST_CHECK(
  46. std::equal(
  47. translator(BOOST_MAKE_PFTO_WRAPPER(la)),
  48. translator(BOOST_MAKE_PFTO_WRAPPER(la + size)),
  49. a
  50. )
  51. );
  52. }
  53. #endif
  54. template<class CharType>
  55. void test_xml_escape(
  56. const CharType * xml_escaped,
  57. const CharType * xml,
  58. unsigned int size
  59. ){
  60. typedef boost::archive::iterators::xml_escape<const CharType *> translator;
  61. BOOST_CHECK(
  62. std::equal(
  63. translator(BOOST_MAKE_PFTO_WRAPPER(xml)),
  64. translator(BOOST_MAKE_PFTO_WRAPPER(xml + size)),
  65. xml_escaped
  66. )
  67. );
  68. }
  69. template<class CharType>
  70. void test_xml_unescape(
  71. const CharType * xml,
  72. const CharType * xml_escaped,
  73. unsigned int size
  74. ){
  75. // test xml_unescape
  76. typedef boost::archive::iterators::xml_unescape<const CharType *> translator;
  77. BOOST_CHECK(
  78. std::equal(
  79. translator(BOOST_MAKE_PFTO_WRAPPER(xml_escaped)),
  80. translator(BOOST_MAKE_PFTO_WRAPPER(xml_escaped + size)),
  81. xml
  82. )
  83. );
  84. }
  85. template<int BitsOut, int BitsIn>
  86. void test_transform_width(unsigned int size){
  87. // test transform_width
  88. char rawdata[8];
  89. char * rptr;
  90. for(rptr = rawdata + 6; rptr-- > rawdata;)
  91. *rptr = std::rand();
  92. // convert 8 to 6 bit characters
  93. typedef boost::archive::iterators::transform_width<
  94. char *, BitsOut, BitsIn
  95. > translator1;
  96. std::vector<char> v6;
  97. std::copy(
  98. translator1(BOOST_MAKE_PFTO_WRAPPER(static_cast<char *>(rawdata))),
  99. translator1(BOOST_MAKE_PFTO_WRAPPER(rawdata + size)),
  100. std::back_inserter(v6)
  101. );
  102. // check to see we got the expected # of characters out
  103. if(0 == size)
  104. BOOST_CHECK(v6.size() == 0);
  105. else
  106. BOOST_CHECK(v6.size() == (size * BitsIn - 1 ) / BitsOut + 1);
  107. typedef boost::archive::iterators::transform_width<
  108. std::vector<char>::iterator, BitsIn, BitsOut
  109. > translator2;
  110. BOOST_CHECK(
  111. std::equal(
  112. rawdata,
  113. rawdata + size,
  114. translator2(BOOST_MAKE_PFTO_WRAPPER(v6.begin()))
  115. )
  116. );
  117. }
  118. template<class CharType>
  119. void test_stream_iterators(
  120. const CharType * test_data,
  121. unsigned int size
  122. ){
  123. std::basic_stringstream<CharType> ss;
  124. boost::archive::iterators::ostream_iterator<CharType> osi =
  125. boost::archive::iterators::ostream_iterator<CharType>(ss);
  126. std::copy(test_data, test_data + size, osi);
  127. BOOST_CHECK(size == ss.str().size());
  128. boost::archive::iterators::istream_iterator<CharType> isi =
  129. boost::archive::iterators::istream_iterator<CharType>(ss);
  130. BOOST_CHECK(std::equal(test_data, test_data + size,isi));
  131. }
  132. int
  133. test_main(int /* argc */, char* /* argv */ [] )
  134. {
  135. const char xml[] = "<+>+&+\"+'";
  136. const char xml_escaped[] = "&lt;+&gt;+&amp;+&quot;+&apos;";
  137. test_xml_escape<const char>(
  138. xml_escaped,
  139. xml,
  140. sizeof(xml) / sizeof(char) - 1
  141. );
  142. test_xml_unescape<const char>(
  143. xml,
  144. xml_escaped,
  145. sizeof(xml_escaped) / sizeof(char) - 1
  146. );
  147. const char a[] = "abcdefghijklmnopqrstuvwxyz";
  148. #ifndef BOOST_NO_CWCHAR
  149. const wchar_t wxml[] = L"<+>+&+\"+'";
  150. const wchar_t wxml_escaped[] = L"&lt;+&gt;+&amp;+&quot;+&apos;";
  151. test_xml_escape<const wchar_t>(
  152. wxml_escaped,
  153. wxml,
  154. sizeof(wxml) / sizeof(wchar_t) - 1
  155. );
  156. test_xml_unescape<const wchar_t>(
  157. wxml,
  158. wxml_escaped,
  159. sizeof(wxml_escaped) / sizeof(wchar_t) - 1
  160. );
  161. const wchar_t la[] = L"abcdefghijklmnopqrstuvwxyz";
  162. test_wchar_from_mb(la, a, sizeof(a) / sizeof(char) - 1);
  163. test_mb_from_wchar(a, la, sizeof(la) / sizeof(wchar_t) - 1);
  164. test_stream_iterators<wchar_t>(la, sizeof(la)/sizeof(wchar_t) - 1);
  165. #endif
  166. test_stream_iterators<char>(a, sizeof(a) - 1);
  167. test_transform_width<6, 8>(0);
  168. test_transform_width<6, 8>(1);
  169. test_transform_width<6, 8>(2);
  170. test_transform_width<6, 8>(3);
  171. test_transform_width<6, 8>(4);
  172. test_transform_width<6, 8>(5);
  173. test_transform_width<6, 8>(6);
  174. test_transform_width<6, 8>(7);
  175. test_transform_width<6, 8>(8);
  176. return EXIT_SUCCESS;
  177. }