summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/serialization/performance/performance_codecvt_null.cpp
blob: 70dd573ee75d9573694f70b9708a7055aacf99d8 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_codecvt_null.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)

// should pass compilation and execution.  Note: compilation with compilers
// which use wchar_t as 2 byte objects will emit warnings.  These should be
// ignored.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <locale>
#include <vector>
#include <cstddef>

#include <cstdio> // remove
#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{ 
    using ::remove;
}
#endif

#include "test_tools.hpp"

#include <boost/archive/add_facet.hpp>
#include <boost/archive/codecvt_null.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <boost/archive/iterators/istream_iterator.hpp>

template<std::size_t S>
struct test_data
{
    static wchar_t wchar_encoding[];
};

template<>
wchar_t test_data<2>::wchar_encoding[] = {
    0x0001,
    0x007f,
    0x0080,
    0x07ff,
    0x0800,
    0x7fff
};

template<>
wchar_t test_data<4>::wchar_encoding[] = {
    0x00000001,
    0x0000007f,
    0x00000080,
    0x000007ff,
    0x00000800,
    0x0000ffff,
    0x00010000,
    0x0010ffff,
    0x001fffff,
    0x00200000,
    0x03ffffff,
    0x04000000,
    0x7fffffff
};

#include <iostream>

int test_main( int /* argc */, char* /* argv */[] ) {
    const char * testfile = boost::archive::tmpnam(NULL);
    BOOST_REQUIRE(NULL != testfile);

    std::locale old_loc;
    std::locale * null_locale = 
        boost::archive::add_facet(old_loc, new boost::archive::codecvt_null<wchar_t>);

    typedef test_data<sizeof(wchar_t)> td;
    {
        std::wofstream ofs;
        ofs.imbue(*null_locale);
        ofs.open(testfile, std::ios::binary);
        std::copy(
            td::wchar_encoding,
            #if ! defined(__BORLANDC__)
                // borland 5.60 complains about this
                td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
            #else
                // so use this instead
                td::wchar_encoding + 6,
            #endif
            boost::archive::iterators::ostream_iterator<wchar_t>(ofs)
        );
    }
    bool ok = false;
    {
        std::wifstream ifs;
        ifs.imbue(*null_locale);
        ifs.open(testfile, std::ios::binary);
        ok = std::equal(
            td::wchar_encoding,
            #if ! defined(__BORLANDC__)
                // borland 5.60 complains about this
                td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
            #else
                // so use this instead
                td::wchar_encoding + 6,
            #endif
            boost::archive::iterators::istream_iterator<wchar_t>(ifs)
        );
    }

    BOOST_CHECK(ok);
    {
        std::wofstream ofs("testfile2");
        ofs.imbue(*null_locale);
        int i = 10;
        ofs << i;
        ofs.close();
        
        std::wifstream ifs("testfile2");
        ifs.imbue(*null_locale);
        int i2;
        ifs >> i2;
        std::cout << "i=" << i << std::endl;
        std::cout << "i2=" << i2 << std::endl;
        BOOST_CHECK(i == i2);
        ifs.close();
    }
 
    delete null_locale;
    std::remove(testfile);
    return EXIT_SUCCESS;
}