• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/codecvt_null.hpp>
32 #include <boost/archive/iterators/ostream_iterator.hpp>
33 #include <boost/archive/iterators/istream_iterator.hpp>
34 
35 template<std::size_t S>
36 struct test_data
37 {
38     static wchar_t wchar_encoding[];
39 };
40 
41 template<>
42 wchar_t test_data<2>::wchar_encoding[] = {
43     (wchar_t) 0x0001,
44     (wchar_t) 0x007f,
45     (wchar_t) 0x0080,
46     (wchar_t) 0x07ff,
47     (wchar_t) 0x0800,
48     (wchar_t) 0x7fff
49 };
50 
51 template<>
52 wchar_t test_data<4>::wchar_encoding[] = {
53     (wchar_t) 0x00000001,
54     (wchar_t) 0x0000007f,
55     (wchar_t) 0x00000080,
56     (wchar_t) 0x000007ff,
57     (wchar_t) 0x00000800,
58     (wchar_t) 0x0000ffff,
59     (wchar_t) 0x00010000,
60     (wchar_t) 0x0010ffff,
61     (wchar_t) 0x001fffff,
62     (wchar_t) 0x00200000,
63     (wchar_t) 0x03ffffff,
64     (wchar_t) 0x04000000,
65     (wchar_t) 0x7fffffff
66 };
67 
68 #include <iostream>
69 
test_main(int,char * [])70 int test_main( int /* argc */, char* /* argv */[] ) {
71     const char * testfile = boost::archive::tmpnam(NULL);
72     BOOST_REQUIRE(NULL != testfile);
73 
74     std::locale old_loc;
75     std::locale null_locale = std::locale(
76         old_loc,
77         new boost::archive::codecvt_null<wchar_t>
78     );
79 
80     typedef test_data<sizeof(wchar_t)> td;
81     {
82         std::wofstream ofs;
83         ofs.imbue(null_locale);
84         ofs.open(testfile, std::ios::binary);
85         std::copy(
86             td::wchar_encoding,
87             td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
88             boost::archive::iterators::ostream_iterator<wchar_t>(ofs)
89         );
90     }
91     bool ok = false;
92     {
93         std::wifstream ifs;
94         ifs.imbue(null_locale);
95         ifs.open(testfile, std::ios::binary);
96         ok = std::equal(
97             td::wchar_encoding,
98             td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
99             boost::archive::iterators::istream_iterator<wchar_t>(ifs)
100         );
101     }
102 
103     BOOST_CHECK(ok);
104     {
105         std::wofstream ofs("testfile2");
106         ofs.imbue(null_locale);
107         int i = 10;
108         ofs << i << '\n';
109         ofs.close();
110 
111         std::wifstream ifs("testfile2");
112         ifs.imbue(null_locale);
113         int i2;
114         ifs >> i2;
115         std::cout << "i=" << i << std::endl;
116         std::cout << "i2=" << i2 << std::endl;
117         BOOST_CHECK(i == i2);
118         ifs.close();
119     }
120 
121     std::remove(testfile);
122     return EXIT_SUCCESS;
123 }
124 
125