1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_iterators.cpp
3
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 #include <algorithm>
10 #include <list>
11
12 #if (defined _MSC_VER) && (_MSC_VER == 1200)
13 # pragma warning (disable : 4786) // too long name, harmless warning
14 #endif
15
16 #include <cstdlib>
17 #include <cstddef> // size_t
18
19 #include <boost/config.hpp>
20 #ifdef BOOST_NO_STDC_NAMESPACE
21 namespace std{
22 using ::rand;
23 using ::size_t;
24 }
25 #endif
26
27 #include <boost/archive/iterators/binary_from_base64.hpp>
28 #include <boost/archive/iterators/base64_from_binary.hpp>
29 #include <boost/archive/iterators/insert_linebreaks.hpp>
30 #include <boost/archive/iterators/remove_whitespace.hpp>
31 #include <boost/archive/iterators/transform_width.hpp>
32
33 #include "test_tools.hpp"
34
35 #include <iostream>
36
37 template<typename CharType>
test_base64(unsigned int size)38 void test_base64(unsigned int size){
39 CharType rawdata[150];
40 CharType * rptr;
41 for(rptr = rawdata + size; rptr-- > rawdata;)
42 *rptr = static_cast<CharType>(std::rand()& 0xff);
43
44 // convert to base64
45 typedef std::list<CharType> text_base64_type;
46 text_base64_type text_base64;
47
48 typedef
49 boost::archive::iterators::insert_linebreaks<
50 boost::archive::iterators::base64_from_binary<
51 boost::archive::iterators::transform_width<
52 CharType *
53 ,6
54 ,sizeof(CharType) * 8
55 >
56 >
57 ,76
58 >
59 translate_out;
60
61 std::copy(
62 translate_out(static_cast<CharType *>(rawdata)),
63 translate_out(rawdata + size),
64 std::back_inserter(text_base64)
65 );
66
67 // convert from base64 to binary and compare with the original
68 typedef
69 boost::archive::iterators::transform_width<
70 boost::archive::iterators::binary_from_base64<
71 boost::archive::iterators::remove_whitespace<
72 typename text_base64_type::iterator
73 >
74 >,
75 sizeof(CharType) * 8,
76 6
77 > translate_in;
78
79 BOOST_CHECK(
80 std::equal(
81 rawdata,
82 rawdata + size,
83 translate_in(text_base64.begin())
84 )
85 );
86
87 }
88
89 int
test_main(int,char * [])90 test_main( int /*argc*/, char* /*argv*/[] )
91 {
92 test_base64<char>(1);
93 test_base64<char>(2);
94 test_base64<char>(3);
95 test_base64<char>(4);
96 test_base64<char>(150);
97 #ifndef BOOST_NO_CWCHAR
98 test_base64<wchar_t>(1);
99 test_base64<wchar_t>(2);
100 test_base64<wchar_t>(3);
101 test_base64<wchar_t>(4);
102 test_base64<wchar_t>(150);
103 #endif
104 return EXIT_SUCCESS;
105 }
106