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

source code of boost/libs/convert/test/test.hpp