test_property_tree.cpp 4.7 KB

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