Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / serialization / test / test_codecvt_null.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_codecvt_null.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 // should pass compilation and execution.  Note: compilation with compilers
10 // which use wchar_t as 2 byte objects will emit warnings.  These should be
11 // ignored.
12
13 #include <algorithm> // std::copy
14 #include <fstream>
15 #include <iostream>
16 #include <iterator>
17 #include <locale>
18 #include <vector>
19 #include <cstdio> // remove
20 #include <cstddef> // NULL, size_t
21
22 #include <boost/config.hpp>
23 #if defined(BOOST_NO_STDC_NAMESPACE)
24 namespace std{ 
25     using ::remove;
26 }
27 #endif
28
29 #include "test_tools.hpp"
30
31 #include <boost/archive/add_facet.hpp>
32 #include <boost/archive/codecvt_null.hpp>
33 #include <boost/archive/iterators/ostream_iterator.hpp>
34 #include <boost/archive/iterators/istream_iterator.hpp>
35
36 template<std::size_t S>
37 struct test_data
38 {
39     static wchar_t wchar_encoding[];
40 };
41
42 template<>
43 wchar_t test_data<2>::wchar_encoding[] = {
44     0x0001,
45     0x007f,
46     0x0080,
47     0x07ff,
48     0x0800,
49     0x7fff
50 };
51
52 template<>
53 wchar_t test_data<4>::wchar_encoding[] = {
54     0x00000001,
55     0x0000007f,
56     0x00000080,
57     0x000007ff,
58     0x00000800,
59     0x0000ffff,
60     0x00010000,
61     0x0010ffff,
62     0x001fffff,
63     0x00200000,
64     0x03ffffff,
65     0x04000000,
66     0x7fffffff
67 };
68
69 #include <iostream>
70
71 int test_main( int /* argc */, char* /* argv */[] ) {
72     const char * testfile = boost::archive::tmpnam(NULL);
73     BOOST_REQUIRE(NULL != testfile);
74
75     std::locale old_loc;
76     std::locale * null_locale = 
77         boost::archive::add_facet(old_loc, new boost::archive::codecvt_null<wchar_t>);
78
79     typedef test_data<sizeof(wchar_t)> td;
80     {
81         std::wofstream ofs;
82         ofs.imbue(*null_locale);
83         ofs.open(testfile, std::ios::binary);
84         std::copy(
85             td::wchar_encoding,
86             #if ! defined(__BORLANDC__)
87                 // borland 5.60 complains about this
88                 td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
89             #else
90                 // so use this instead
91                 td::wchar_encoding + 6,
92             #endif
93             boost::archive::iterators::ostream_iterator<wchar_t>(ofs)
94         );
95     }
96     bool ok = false;
97     {
98         std::wifstream ifs;
99         ifs.imbue(*null_locale);
100         ifs.open(testfile, std::ios::binary);
101         ok = std::equal(
102             td::wchar_encoding,
103             #if ! defined(__BORLANDC__)
104                 // borland 5.60 complains about this
105                 td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
106             #else
107                 // so use this instead
108                 td::wchar_encoding + 6,
109             #endif
110             boost::archive::iterators::istream_iterator<wchar_t>(ifs)
111         );
112     }
113
114     BOOST_CHECK(ok);
115     {
116         std::wofstream ofs("testfile2");
117         ofs.imbue(*null_locale);
118         int i = 10;
119         ofs << i;
120         ofs.close();
121         
122         std::wifstream ifs("testfile2");
123         ifs.imbue(*null_locale);
124         int i2;
125         ifs >> i2;
126         std::cout << "i=" << i << std::endl;
127         std::cout << "i2=" << i2 << std::endl;
128         BOOST_CHECK(i == i2);
129         ifs.close();
130     }
131  
132     delete null_locale;
133     std::remove(testfile);
134     return EXIT_SUCCESS;
135 }
136