]>
Commit | Line | Data |
---|---|---|
20effc67 | 1 | // Copyright (c) 2009-2020 Vladimir Batov. |
7c673cae FG |
2 | // Use, modification and distribution are subject to the Boost Software License, |
3 | // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. | |
4 | ||
5 | #ifndef BOOST_CONVERT_TEST_HPP | |
6 | #define BOOST_CONVERT_TEST_HPP | |
7 | ||
b32b8144 | 8 | #include <boost/convert/detail/config.hpp> |
7c673cae | 9 | #include <boost/make_default.hpp> |
20effc67 | 10 | #include <boost/detail/lightweight_test.hpp> |
7c673cae FG |
11 | #include <string> |
12 | #include <istream> | |
20effc67 TL |
13 | #include <cstdio> |
14 | #include <cstdlib> | |
15 | #include <cstring> // For strlen, strcmp, memcpy | |
16 | #include <climits> | |
17 | #include <ctime> | |
7c673cae FG |
18 | |
19 | #if defined(_MSC_VER) | |
20 | # pragma warning(disable: 4189) // local variable is initialized but not referenced. | |
21 | # pragma warning(disable: 4127) // conditional expression is constant. | |
22 | # pragma warning(disable: 4100) // unreferenced formal parameter. | |
23 | # pragma warning(disable: 4714) // marked as __forceinline not #endif | |
24 | # pragma warning(disable: 4706) | |
25 | # pragma warning(disable: 4005) | |
b32b8144 FG |
26 | # pragma warning(disable: 4459) // declaration hides global declaration |
27 | # pragma warning(disable: 4456) // declaration hides previous local declaration | |
7c673cae FG |
28 | #endif |
29 | ||
30 | //[change_declaration | |
31 | struct change | |
32 | { | |
33 | enum value_type { no, up, dn }; | |
34 | ||
35 | change(value_type v =no) : value_(v) {} | |
36 | bool operator==(change v) const { return value_ == v.value_; } | |
37 | value_type value() const { return value_; } | |
38 | ||
39 | private: value_type value_; | |
40 | }; | |
41 | //] | |
42 | //[change_stream_operators | |
43 | std::istream& operator>>(std::istream& stream, change& chg) | |
44 | { | |
45 | std::string str; stream >> str; | |
46 | ||
47 | /**/ if (str == "up") chg = change::up; | |
48 | else if (str == "dn") chg = change::dn; | |
49 | else if (str == "no") chg = change::no; | |
50 | else stream.setstate(std::ios_base::failbit); | |
51 | ||
52 | return stream; | |
53 | } | |
54 | ||
55 | std::ostream& operator<<(std::ostream& stream, change const& chg) | |
56 | { | |
57 | return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no"); | |
58 | } | |
59 | //] | |
60 | //[change_convert_operators | |
61 | inline void operator>>(change chg, boost::optional<std::string>& str) | |
62 | { | |
63 | str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no"; | |
64 | } | |
65 | ||
66 | inline void operator>>(std::string const& str, boost::optional<change>& chg) | |
67 | { | |
68 | /**/ if (str == "up") chg = change::up; | |
69 | else if (str == "dn") chg = change::dn; | |
70 | else if (str == "no") chg = change::no; | |
71 | } | |
72 | //] | |
73 | //[direction_declaration | |
74 | struct direction | |
75 | { | |
76 | // Note: the class does NOT have the default constructor. | |
77 | ||
78 | enum value_type { up, dn }; | |
79 | ||
80 | direction(value_type value) : value_(value) {} | |
81 | bool operator==(direction that) const { return value_ == that.value_; } | |
82 | value_type value() const { return value_; } | |
83 | ||
84 | private: value_type value_; | |
85 | }; | |
86 | //] | |
87 | //[direction_stream_operators | |
88 | std::istream& operator>>(std::istream& stream, direction& dir) | |
89 | { | |
90 | std::string str; stream >> str; | |
91 | ||
92 | /**/ if (str == "up") dir = direction::up; | |
93 | else if (str == "dn") dir = direction::dn; | |
94 | else stream.setstate(std::ios_base::failbit); | |
95 | ||
96 | return stream; | |
97 | } | |
98 | std::ostream& operator<<(std::ostream& stream, direction const& dir) | |
99 | { | |
100 | return stream << (dir.value() == direction::up ? "up" : "dn"); | |
101 | } | |
102 | //] | |
103 | //[direction_declaration_make_default | |
104 | namespace boost | |
105 | { | |
106 | template<> inline direction make_default<direction>() | |
107 | { | |
108 | return direction(direction::up); | |
109 | } | |
110 | } | |
111 | //] | |
112 | // Quick and dirty small-string implementation for performance tests. | |
113 | //[my_string_declaration | |
114 | struct my_string | |
115 | { | |
20effc67 TL |
116 | using this_type = my_string; |
117 | using value_type = char; | |
118 | using iterator = value_type*; | |
119 | using const_iterator = value_type const*; | |
7c673cae FG |
120 | |
121 | my_string (); | |
122 | my_string (const_iterator, const_iterator =0); | |
123 | ||
124 | char const* c_str () const { return storage_; } | |
125 | const_iterator begin () const { return storage_; } | |
126 | const_iterator end () const { return storage_ + strlen(storage_); } | |
127 | this_type& operator= (char const*); | |
128 | ||
129 | private: | |
130 | ||
131 | static size_t const size_ = 12; | |
132 | char storage_[size_]; | |
133 | }; | |
134 | //] | |
135 | inline | |
136 | my_string::my_string() | |
137 | { | |
138 | storage_[0] = 0; | |
139 | } | |
140 | ||
141 | inline | |
142 | my_string::my_string(const_iterator beg, const_iterator end) | |
143 | { | |
144 | std::size_t const sz = end ? (end - beg) : strlen(beg); | |
145 | ||
146 | BOOST_ASSERT(sz < size_); | |
147 | memcpy(storage_, beg, sz); storage_[sz] = 0; | |
148 | } | |
149 | ||
150 | inline | |
151 | my_string& | |
152 | my_string::operator=(char const* str) | |
153 | { | |
154 | BOOST_ASSERT(strlen(str) < size_); | |
155 | strcpy(storage_, str); | |
156 | return *this; | |
157 | } | |
158 | ||
159 | inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; } | |
160 | inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; } | |
161 | ||
162 | namespace test | |
163 | { | |
164 | struct cnv | |
165 | { | |
166 | #if defined(__QNXNTO__) | |
167 | static bool const is_qnx = true; | |
168 | #else | |
169 | static bool const is_qnx = false; | |
170 | #endif | |
171 | ||
172 | #if defined(_MSC_VER) && _MSC_VER < 1900 | |
173 | static bool const is_msc = true; | |
174 | static bool const is_old_msc = true; | |
175 | #elif defined(_MSC_VER) | |
176 | static bool const is_msc = true; | |
177 | static bool const is_old_msc = false; | |
178 | #elif defined(__MINGW32__) | |
179 | static bool const is_msc = true; | |
180 | static bool const is_old_msc = true; | |
181 | #else | |
182 | static bool const is_msc = false; | |
183 | static bool const is_old_msc = false; | |
184 | #endif | |
185 | }; | |
186 | } | |
187 | ||
188 | #endif // BOOST_CONVERT_TEST_HPP |