summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/serialization/test/test_iterators_base64.cpp
blob: 1f772c6d75ab0423cab271d2f44031e74960e325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_iterators.cpp

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <algorithm>
#include <list>

#if (defined _MSC_VER) && (_MSC_VER == 1200)
#  pragma warning (disable : 4786) // too long name, harmless warning
#endif

#include <cstdlib>
#include <cstddef> // size_t

#include <boost/config.hpp>
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std{
    using ::rand;
    using ::size_t;
}
#endif

#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <boost/archive/iterators/transform_width.hpp>

#include "test_tools.hpp"

#include <iostream>

template<typename CharType>
void test_base64(unsigned int size){
    CharType rawdata[150];
    CharType * rptr;
    for(rptr = rawdata + size; rptr-- > rawdata;)
        *rptr = static_cast<CharType>(std::rand()& 0xff);

    // convert to base64
    typedef std::list<CharType> text_base64_type;
    text_base64_type text_base64;

    typedef
        boost::archive::iterators::insert_linebreaks<
            boost::archive::iterators::base64_from_binary<
                boost::archive::iterators::transform_width<
                    CharType *
                    ,6
                    ,sizeof(CharType) * 8
                >
            >
            ,76
        >
        translate_out;

    std::copy(
        translate_out(static_cast<CharType *>(rawdata)),
        translate_out(rawdata + size),
        std::back_inserter(text_base64)
    );

    // convert from base64 to binary and compare with the original
    typedef
        boost::archive::iterators::transform_width<
            boost::archive::iterators::binary_from_base64<
                boost::archive::iterators::remove_whitespace<
                    typename text_base64_type::iterator
                >
            >,
            sizeof(CharType) * 8,
            6
        > translate_in;

    BOOST_CHECK(
        std::equal(
            rawdata,
            rawdata + size,
            translate_in(text_base64.begin())
        )
    );

}

int
test_main( int /*argc*/, char* /*argv*/[] )
{
    test_base64<char>(1);
    test_base64<char>(2);
    test_base64<char>(3);
    test_base64<char>(4);
    test_base64<char>(150);
    #ifndef BOOST_NO_CWCHAR
    test_base64<wchar_t>(1);
    test_base64<wchar_t>(2);
    test_base64<wchar_t>(3);
    test_base64<wchar_t>(4);
    test_base64<wchar_t>(150);
    #endif
    return EXIT_SUCCESS;
}