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