performance_iterators_base64.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <list>
  9. #if (defined _MSC_VER) && (_MSC_VER == 1200)
  10. # pragma warning (disable : 4786) // too long name, harmless warning
  11. #endif
  12. #include <cstdlib>
  13. #include <cstddef> // size_t
  14. #include <boost/config.hpp>
  15. #ifdef BOOST_NO_STDC_NAMESPACE
  16. namespace std{
  17. using ::rand;
  18. using ::size_t;
  19. }
  20. #endif
  21. #include <boost/serialization/pfto.hpp>
  22. #include <boost/archive/iterators/binary_from_base64.hpp>
  23. #include <boost/archive/iterators/base64_from_binary.hpp>
  24. #include <boost/archive/iterators/insert_linebreaks.hpp>
  25. #include <boost/archive/iterators/remove_whitespace.hpp>
  26. #include <boost/archive/iterators/transform_width.hpp>
  27. #include "../test/test_tools.hpp"
  28. #include <iostream>
  29. template<typename CharType>
  30. void test_base64(){
  31. CharType rawdata[150];
  32. std::size_t size = sizeof(rawdata) / sizeof(CharType);
  33. CharType * rptr;
  34. for(rptr = rawdata + size; rptr-- > rawdata;)
  35. *rptr = std::rand();
  36. // convert to base64
  37. typedef std::list<CharType> text_base64_type;
  38. text_base64_type text_base64;
  39. typedef
  40. boost::archive::iterators::insert_linebreaks<
  41. boost::archive::iterators::base64_from_binary<
  42. boost::archive::iterators::transform_width<
  43. CharType *
  44. ,6
  45. ,sizeof(CharType) * 8
  46. >
  47. >
  48. ,72
  49. >
  50. translate_out;
  51. std::copy(
  52. translate_out(BOOST_MAKE_PFTO_WRAPPER(static_cast<CharType *>(rawdata))),
  53. translate_out(BOOST_MAKE_PFTO_WRAPPER(rawdata + size)),
  54. std::back_inserter(text_base64)
  55. );
  56. // convert from base64 to binary and compare with the original
  57. typedef
  58. boost::archive::iterators::transform_width<
  59. boost::archive::iterators::binary_from_base64<
  60. boost::archive::iterators::remove_whitespace<
  61. BOOST_DEDUCED_TYPENAME text_base64_type::iterator
  62. >
  63. >,
  64. sizeof(CharType) * 8,
  65. 6
  66. > translate_in;
  67. BOOST_CHECK(
  68. std::equal(
  69. rawdata,
  70. rawdata + size,
  71. translate_in(BOOST_MAKE_PFTO_WRAPPER(text_base64.begin()))
  72. )
  73. );
  74. }
  75. int
  76. test_main( int argc, char* argv[] )
  77. {
  78. test_base64<char>();
  79. #ifndef BOOST_NO_CWCHAR
  80. test_base64<wchar_t>();
  81. #endif
  82. return EXIT_SUCCESS;
  83. }