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