]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // Copyright (c) 2012 Artyom Beilis (Tonkikh) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See | |
20effc67 | 5 | // accompanying file LICENSE or copy at |
f67539c2 TL |
6 | // http://www.boost.org/LICENSE_1_0.txt) |
7 | // | |
8 | ||
9 | #include <boost/nowide/convert.hpp> | |
10 | #include <iostream> | |
11 | ||
12 | #include "test.hpp" | |
13 | #include "test_sets.hpp" | |
14 | ||
15 | #if defined(BOOST_MSVC) && BOOST_MSVC < 1700 | |
16 | #pragma warning(disable : 4428) // universal-character-name encountered in source | |
17 | #endif | |
18 | ||
19 | std::wstring widen_buf_ptr(const std::string& s) | |
20 | { | |
21 | wchar_t buf[50]; | |
22 | TEST(boost::nowide::widen(buf, 50, s.c_str()) == buf); | |
23 | return buf; | |
24 | } | |
25 | ||
26 | std::string narrow_buf_ptr(const std::wstring& s) | |
27 | { | |
28 | char buf[50]; | |
29 | TEST(boost::nowide::narrow(buf, 50, s.c_str()) == buf); | |
30 | return buf; | |
31 | } | |
32 | ||
33 | std::wstring widen_buf_range(const std::string& s) | |
34 | { | |
35 | wchar_t buf[50]; | |
36 | TEST(boost::nowide::widen(buf, 50, s.c_str(), s.c_str() + s.size()) == buf); | |
37 | return buf; | |
38 | } | |
39 | ||
40 | std::string narrow_buf_range(const std::wstring& s) | |
41 | { | |
42 | char buf[50]; | |
43 | TEST(boost::nowide::narrow(buf, 50, s.c_str(), s.c_str() + s.size()) == buf); | |
44 | return buf; | |
45 | } | |
46 | ||
47 | std::wstring widen_raw_string(const std::string& s) | |
48 | { | |
49 | return boost::nowide::widen(s.c_str()); | |
50 | } | |
51 | ||
52 | std::string narrow_raw_string(const std::wstring& s) | |
53 | { | |
54 | return boost::nowide::narrow(s.c_str()); | |
55 | } | |
56 | ||
57 | std::wstring widen_raw_string_and_size(const std::string& s) | |
58 | { | |
59 | // Remove NULL | |
60 | const std::string s2 = s + "DummyData"; | |
61 | return boost::nowide::widen(s2.c_str(), s.size()); | |
62 | } | |
63 | ||
64 | std::string narrow_raw_string_and_size(const std::wstring& s) | |
65 | { | |
66 | // Remove NULL | |
67 | const std::wstring s2 = s + L"DummyData"; | |
68 | return boost::nowide::narrow(s2.c_str(), s.size()); | |
69 | } | |
70 | ||
71 | void test_main(int, char**, char**) | |
72 | { | |
73 | std::string hello = "\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d"; | |
74 | std::wstring whello = L"\u05e9\u05dc\u05d5\u05dd"; | |
75 | std::wstring whello_3e = L"\u05e9\u05dc\u05d5\ufffd"; | |
76 | std::wstring whello_3 = L"\u05e9\u05dc\u05d5"; | |
77 | ||
78 | std::cout << "- boost::nowide::widen" << std::endl; | |
79 | { | |
80 | const char* b = hello.c_str(); | |
81 | const char* e = b + hello.size(); | |
82 | wchar_t buf[6] = {0, 0, 0, 0, 0, 1}; | |
83 | TEST(boost::nowide::widen(buf, 5, b, e) == buf); | |
84 | TEST(buf == whello); | |
85 | TEST(buf[5] == 1); | |
86 | TEST(boost::nowide::widen(buf, 4, b, e) == 0); | |
87 | TEST(boost::nowide::widen(buf, 5, b, e - 1) == buf); | |
88 | TEST(buf == whello_3e); | |
89 | TEST(boost::nowide::widen(buf, 5, b, e - 2) == buf); | |
90 | TEST(buf == whello_3); | |
91 | TEST(boost::nowide::widen(buf, 5, b, b) == buf && buf[0] == 0); | |
92 | TEST(boost::nowide::widen(buf, 5, b, b + 2) == buf && buf[1] == 0 && buf[0] == whello[0]); | |
93 | } | |
94 | std::cout << "- boost::nowide::narrow" << std::endl; | |
95 | { | |
96 | const wchar_t* b = whello.c_str(); | |
97 | const wchar_t* e = b + whello.size(); //-V594 | |
98 | char buf[10] = {0}; | |
99 | buf[9] = 1; | |
100 | TEST(boost::nowide::narrow(buf, 9, b, e) == buf); | |
101 | TEST(buf == hello); | |
102 | TEST(buf[9] == 1); | |
103 | TEST(boost::nowide::narrow(buf, 8, b, e) == 0); | |
104 | TEST(boost::nowide::narrow(buf, 7, b, e - 1) == buf); | |
105 | TEST(buf == hello.substr(0, 6)); | |
106 | } | |
107 | ||
108 | std::cout << "- (output_buffer, buffer_size, input_raw_string)" << std::endl; | |
109 | run_all(widen_buf_ptr, narrow_buf_ptr); | |
110 | std::cout << "- (output_buffer, buffer_size, input_raw_string, string_len)" << std::endl; | |
111 | run_all(widen_buf_range, narrow_buf_range); | |
112 | std::cout << "- (input_raw_string)" << std::endl; | |
113 | run_all(widen_raw_string, narrow_raw_string); | |
114 | std::cout << "- (input_raw_string, size)" << std::endl; | |
115 | run_all(widen_raw_string_and_size, narrow_raw_string_and_size); | |
116 | std::cout << "- (const std::string&)" << std::endl; | |
117 | run_all(boost::nowide::widen, boost::nowide::narrow); | |
118 | } |