1 | // ---------------------------------------------------------------------------- |
2 | // Copyright (C) 2002-2006 Marcin Kalicinski |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. |
5 | // (See accompanying file LICENSE_1_0.txt or copy at |
6 | // http://www.boost.org/LICENSE_1_0.txt) |
7 | // |
8 | // For more information, see www.boost.org |
9 | // ---------------------------------------------------------------------------- |
10 | #include "test_utils.hpp" |
11 | #include <boost/any.hpp> |
12 | #include <boost/mp11.hpp> |
13 | #include <boost/range.hpp> |
14 | #include <list> |
15 | #include <cmath> |
16 | |
17 | // If using VC, disable some warnings that trip in boost::serialization bowels |
18 | #ifdef BOOST_MSVC |
19 | #pragma warning(disable:4267) // Narrowing conversion |
20 | #pragma warning(disable:4996) // Deprecated functions |
21 | #endif |
22 | |
23 | #include <boost/archive/text_iarchive.hpp> |
24 | #include <boost/archive/text_oarchive.hpp> |
25 | #include <boost/archive/binary_iarchive.hpp> |
26 | #include <boost/archive/binary_oarchive.hpp> |
27 | #include <boost/archive/xml_iarchive.hpp> |
28 | #include <boost/archive/xml_oarchive.hpp> |
29 | #include <boost/property_tree/ptree_serialization.hpp> |
30 | |
31 | // Predicate for sorting keys |
32 | template<class Ptree> |
33 | struct SortPred |
34 | { |
35 | bool operator()(const typename Ptree::value_type &v1, |
36 | const typename Ptree::value_type &v2) const |
37 | { |
38 | return v1.first < v2.first; |
39 | } |
40 | }; |
41 | |
42 | // Predicate for sorting keys in reverse |
43 | template<class Ptree> |
44 | struct SortPredRev |
45 | { |
46 | bool operator()(const typename Ptree::value_type &v1, |
47 | const typename Ptree::value_type &v2) const |
48 | { |
49 | return v1.first > v2.first; |
50 | } |
51 | }; |
52 | |
53 | // Custom translator that works with boost::any instead of std::string |
54 | template <typename E> |
55 | struct any_translator |
56 | { |
57 | typedef boost::any internal_type; |
58 | typedef E external_type; |
59 | |
60 | boost::optional<E> get_value(const internal_type &v) { |
61 | if(const E *p = boost::any_cast<E>(&v)) { |
62 | return *p; |
63 | } |
64 | return boost::optional<E>(); |
65 | } |
66 | boost::optional<internal_type> put_value(const E &v) { |
67 | return boost::any(v); |
68 | } |
69 | }; |
70 | |
71 | // Checks the validity of calling get_child with a default value type |
72 | template<class P, class C, class D> |
73 | using get_child_accepts_default_of_type = decltype( |
74 | std::declval<P>().get_child(std::declval<const C*>(), std::declval<D>())); |
75 | |
76 | namespace boost { namespace property_tree { |
77 | template <typename E> |
78 | struct translator_between<boost::any, E> |
79 | { |
80 | typedef any_translator<E> type; |
81 | }; |
82 | }} |
83 | |
84 | // Include char tests, case sensitive |
85 | #define CHTYPE char |
86 | #define T(s) s |
87 | #define PTREE boost::property_tree::ptree |
88 | #define NOCASE 0 |
89 | #define WIDECHAR 0 |
90 | # include "test_property_tree.hpp" |
91 | #undef CHTYPE |
92 | #undef T |
93 | #undef PTREE |
94 | #undef NOCASE |
95 | #undef WIDECHAR |
96 | |
97 | // Include wchar_t tests, case sensitive |
98 | #ifndef BOOST_NO_CWCHAR |
99 | # define CHTYPE wchar_t |
100 | # define T(s) L ## s |
101 | # define PTREE boost::property_tree::wptree |
102 | # define NOCASE 0 |
103 | # define WIDECHAR 1 |
104 | # include "test_property_tree.hpp" |
105 | # undef CHTYPE |
106 | # undef T |
107 | # undef PTREE |
108 | # undef NOCASE |
109 | # undef WIDECHAR |
110 | #endif |
111 | |
112 | // Include char tests, case insensitive |
113 | #define CHTYPE char |
114 | #define T(s) s |
115 | #define PTREE boost::property_tree::iptree |
116 | #define NOCASE 1 |
117 | # define WIDECHAR 0 |
118 | # include "test_property_tree.hpp" |
119 | #undef CHTYPE |
120 | #undef T |
121 | #undef PTREE |
122 | #undef NOCASE |
123 | #undef WIDECHAR |
124 | |
125 | // Include wchar_t tests, case insensitive |
126 | #ifndef BOOST_NO_CWCHAR |
127 | # define CHTYPE wchar_t |
128 | # define T(s) L ## s |
129 | # define PTREE boost::property_tree::wiptree |
130 | # define NOCASE 1 |
131 | # define WIDECHAR 1 |
132 | # include "test_property_tree.hpp" |
133 | # undef CHTYPE |
134 | # undef T |
135 | # undef PTREE |
136 | # undef NOCASE |
137 | # undef WIDECHAR |
138 | #endif |
139 | |
140 | template <typename Ptree> |
141 | void run_tests(Ptree* pt) |
142 | { |
143 | test_debug(pt); |
144 | test_constructor_destructor_assignment(pt); |
145 | test_insertion(pt); |
146 | test_erasing(pt); |
147 | test_clear(pt); |
148 | test_pushpop(pt); |
149 | test_container_iteration(pt); |
150 | test_swap(pt); |
151 | test_sort_reverse(pt); |
152 | test_case(pt); |
153 | test_comparison(pt); |
154 | test_front_back(pt); |
155 | test_get_put(pt); |
156 | test_get_child_put_child(pt); |
157 | test_equal_range(pt); |
158 | test_path_separator(pt); |
159 | test_path(pt); |
160 | test_precision(pt); |
161 | test_locale(pt); |
162 | test_custom_data_type(pt); |
163 | test_empty_size_max_size(pt); |
164 | test_ptree_bad_path(pt); |
165 | test_ptree_bad_data(pt); |
166 | test_serialization(pt); |
167 | test_bool(pt); |
168 | test_char(pt); |
169 | test_float(pt); |
170 | test_sort(pt); |
171 | test_leaks(pt); // must be a final test |
172 | } |
173 | |
174 | int main(int, char *[]) |
175 | { |
176 | |
177 | using namespace boost::property_tree; |
178 | |
179 | // char tests, case sensitive |
180 | { |
181 | ptree *pt = 0; |
182 | run_tests(pt); |
183 | } |
184 | |
185 | // wchar_t tests, case sensitive |
186 | #ifndef BOOST_NO_CWCHAR |
187 | { |
188 | wptree *pt = 0; |
189 | run_tests(pt); |
190 | } |
191 | #endif |
192 | |
193 | // char tests, case insensitive |
194 | { |
195 | iptree *pt = 0; |
196 | run_tests(pt); |
197 | } |
198 | |
199 | // wchar_t tests, case insensitive |
200 | #ifndef BOOST_NO_CWCHAR |
201 | { |
202 | wiptree *pt = 0; |
203 | run_tests(pt); |
204 | } |
205 | #endif |
206 | |
207 | return boost::report_errors(); |
208 | } |
209 | |