test_iterators_base64.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/archive/iterators/binary_from_base64.hpp>
  22. #include <boost/archive/iterators/base64_from_binary.hpp>
  23. #include <boost/archive/iterators/insert_linebreaks.hpp>
  24. #include <boost/archive/iterators/remove_whitespace.hpp>
  25. #include <boost/archive/iterators/transform_width.hpp>
  26. #include "test_tools.hpp"
  27. #include <iostream>
  28. template<typename CharType>
  29. void test_base64(unsigned int size){
  30. CharType rawdata[150];
  31. CharType * rptr;
  32. for(rptr = rawdata + size; rptr-- > rawdata;)
  33. *rptr = static_cast<CharType>(std::rand()& 0xff);
  34. // convert to base64
  35. typedef std::list<CharType> text_base64_type;
  36. text_base64_type text_base64;
  37. typedef
  38. boost::archive::iterators::insert_linebreaks<
  39. boost::archive::iterators::base64_from_binary<
  40. boost::archive::iterators::transform_width<
  41. CharType *
  42. ,6
  43. ,sizeof(CharType) * 8
  44. >
  45. >
  46. ,76
  47. >
  48. translate_out;
  49. std::copy(
  50. translate_out(static_cast<CharType *>(rawdata)),
  51. translate_out(rawdata + size),
  52. std::back_inserter(text_base64)
  53. );
  54. // convert from base64 to binary and compare with the original
  55. typedef
  56. boost::archive::iterators::transform_width<
  57. boost::archive::iterators::binary_from_base64<
  58. boost::archive::iterators::remove_whitespace<
  59. typename text_base64_type::iterator
  60. >
  61. >,
  62. sizeof(CharType) * 8,
  63. 6
  64. > translate_in;
  65. BOOST_CHECK(
  66. std::equal(
  67. rawdata,
  68. rawdata + size,
  69. translate_in(text_base64.begin())
  70. )
  71. );
  72. }
  73. int
  74. test_main( int /*argc*/, char* /*argv*/[] )
  75. {
  76. test_base64<char>(1);
  77. test_base64<char>(2);
  78. test_base64<char>(3);
  79. test_base64<char>(4);
  80. test_base64<char>(150);
  81. #ifndef BOOST_NO_CWCHAR
  82. test_base64<wchar_t>(1);
  83. test_base64<wchar_t>(2);
  84. test_base64<wchar_t>(3);
  85. test_base64<wchar_t>(4);
  86. test_base64<wchar_t>(150);
  87. #endif
  88. return EXIT_SUCCESS;
  89. }