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/range.hpp>
13 #include <list>
14 #include <cmath>
15 #include <iostream>
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 {
operator ()SortPred35 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 {
operator ()SortPredRev46 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
get_valueany_translator60 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 }
put_valueany_translator66 boost::optional<internal_type> put_value(const E &v) {
67 return boost::any(v);
68 }
69 };
70
71 namespace boost { namespace property_tree {
72 template <typename E>
73 struct translator_between<boost::any, E>
74 {
75 typedef any_translator<E> type;
76 };
77 }}
78
79 // Include char tests, case sensitive
80 #define CHTYPE char
81 #define T(s) s
82 #define PTREE boost::property_tree::ptree
83 #define NOCASE 0
84 #define WIDECHAR 0
85 # include "test_property_tree.hpp"
86 #undef CHTYPE
87 #undef T
88 #undef PTREE
89 #undef NOCASE
90 #undef WIDECHAR
91
92 // Include wchar_t tests, case sensitive
93 #ifndef BOOST_NO_CWCHAR
94 # define CHTYPE wchar_t
95 # define T(s) L ## s
96 # define PTREE boost::property_tree::wptree
97 # define NOCASE 0
98 # define WIDECHAR 1
99 # include "test_property_tree.hpp"
100 # undef CHTYPE
101 # undef T
102 # undef PTREE
103 # undef NOCASE
104 # undef WIDECHAR
105 #endif
106
107 // Include char tests, case insensitive
108 #define CHTYPE char
109 #define T(s) s
110 #define PTREE boost::property_tree::iptree
111 #define NOCASE 1
112 # define WIDECHAR 0
113 # include "test_property_tree.hpp"
114 #undef CHTYPE
115 #undef T
116 #undef PTREE
117 #undef NOCASE
118 #undef WIDECHAR
119
120 // Include wchar_t tests, case insensitive
121 #ifndef BOOST_NO_CWCHAR
122 # define CHTYPE wchar_t
123 # define T(s) L ## s
124 # define PTREE boost::property_tree::wiptree
125 # define NOCASE 1
126 # define WIDECHAR 1
127 # include "test_property_tree.hpp"
128 # undef CHTYPE
129 # undef T
130 # undef PTREE
131 # undef NOCASE
132 # undef WIDECHAR
133 #endif
134
135 template <typename Ptree>
run_tests(Ptree * pt)136 void run_tests(Ptree* pt)
137 {
138 test_debug(pt);
139 test_constructor_destructor_assignment(pt);
140 test_insertion(pt);
141 test_erasing(pt);
142 test_clear(pt);
143 test_pushpop(pt);
144 test_container_iteration(pt);
145 test_swap(pt);
146 test_sort_reverse(pt);
147 test_case(pt);
148 test_comparison(pt);
149 test_front_back(pt);
150 test_get_put(pt);
151 test_get_child_put_child(pt);
152 test_equal_range(pt);
153 test_path_separator(pt);
154 test_path(pt);
155 test_precision(pt);
156 test_locale(pt);
157 test_custom_data_type(pt);
158 test_empty_size_max_size(pt);
159 test_ptree_bad_path(pt);
160 test_ptree_bad_data(pt);
161 test_serialization(pt);
162 test_bool(pt);
163 test_char(pt);
164 test_float(pt);
165 test_sort(pt);
166 test_leaks(pt); // must be a final test
167 }
168
test_main(int,char * [])169 int test_main(int, char *[])
170 {
171
172 using namespace boost::property_tree;
173
174 // char tests, case sensitive
175 {
176 ptree *pt = 0;
177 run_tests(pt);
178 }
179
180 // wchar_t tests, case sensitive
181 #ifndef BOOST_NO_CWCHAR
182 {
183 wptree *pt = 0;
184 run_tests(pt);
185 }
186 #endif
187
188 // char tests, case insensitive
189 {
190 iptree *pt = 0;
191 run_tests(pt);
192 }
193
194 // wchar_t tests, case insensitive
195 #ifndef BOOST_NO_CWCHAR
196 {
197 wiptree *pt = 0;
198 run_tests(pt);
199 }
200 #endif
201
202 return 0;
203 }
204