From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/boost/libs/property_tree/breaking_changes.txt | 51 + src/boost/libs/property_tree/examples/Jamfile.v2 | 12 + .../property_tree/examples/custom_data_type.cpp | 88 ++ .../libs/property_tree/examples/debug_settings.cpp | 95 ++ .../libs/property_tree/examples/debug_settings.xml | 15 + .../property_tree/examples/empty_ptree_trick.cpp | 71 + .../property_tree/examples/info_grammar_spirit.cpp | 152 +++ .../examples/settings_fully-existent.info | 6 + .../examples/settings_non-existent.info | 6 + .../examples/settings_partially-existent.info | 6 + .../libs/property_tree/examples/speed_test.cpp | 130 ++ src/boost/libs/property_tree/index.html | 14 + src/boost/libs/property_tree/meta/libraries.json | 16 + src/boost/libs/property_tree/test/Jamfile.v2 | 27 + .../property_tree/test/prefixing_callbacks.hpp | 79 ++ src/boost/libs/property_tree/test/sandbox.cpp | 22 + .../libs/property_tree/test/test_info_parser.cpp | 249 ++++ .../libs/property_tree/test/test_ini_parser.cpp | 226 ++++ .../libs/property_tree/test/test_json_parser.cpp | 443 +++++++ .../libs/property_tree/test/test_json_parser2.cpp | 935 ++++++++++++++ .../libs/property_tree/test/test_multi_module1.cpp | 21 + .../libs/property_tree/test/test_multi_module2.cpp | 18 + .../libs/property_tree/test/test_property_tree.cpp | 203 +++ .../libs/property_tree/test/test_property_tree.hpp | 1355 ++++++++++++++++++++ src/boost/libs/property_tree/test/test_utils.hpp | 252 ++++ .../property_tree/test/test_xml_parser_common.hpp | 150 +++ .../test/test_xml_parser_rapidxml.cpp | 34 + .../property_tree/test/xml_parser_test_data.hpp | 777 +++++++++++ 28 files changed, 5453 insertions(+) create mode 100644 src/boost/libs/property_tree/breaking_changes.txt create mode 100644 src/boost/libs/property_tree/examples/Jamfile.v2 create mode 100644 src/boost/libs/property_tree/examples/custom_data_type.cpp create mode 100644 src/boost/libs/property_tree/examples/debug_settings.cpp create mode 100644 src/boost/libs/property_tree/examples/debug_settings.xml create mode 100644 src/boost/libs/property_tree/examples/empty_ptree_trick.cpp create mode 100644 src/boost/libs/property_tree/examples/info_grammar_spirit.cpp create mode 100644 src/boost/libs/property_tree/examples/settings_fully-existent.info create mode 100644 src/boost/libs/property_tree/examples/settings_non-existent.info create mode 100644 src/boost/libs/property_tree/examples/settings_partially-existent.info create mode 100644 src/boost/libs/property_tree/examples/speed_test.cpp create mode 100644 src/boost/libs/property_tree/index.html create mode 100644 src/boost/libs/property_tree/meta/libraries.json create mode 100644 src/boost/libs/property_tree/test/Jamfile.v2 create mode 100644 src/boost/libs/property_tree/test/prefixing_callbacks.hpp create mode 100644 src/boost/libs/property_tree/test/sandbox.cpp create mode 100644 src/boost/libs/property_tree/test/test_info_parser.cpp create mode 100644 src/boost/libs/property_tree/test/test_ini_parser.cpp create mode 100644 src/boost/libs/property_tree/test/test_json_parser.cpp create mode 100644 src/boost/libs/property_tree/test/test_json_parser2.cpp create mode 100644 src/boost/libs/property_tree/test/test_multi_module1.cpp create mode 100644 src/boost/libs/property_tree/test/test_multi_module2.cpp create mode 100644 src/boost/libs/property_tree/test/test_property_tree.cpp create mode 100644 src/boost/libs/property_tree/test/test_property_tree.hpp create mode 100644 src/boost/libs/property_tree/test/test_utils.hpp create mode 100644 src/boost/libs/property_tree/test/test_xml_parser_common.hpp create mode 100644 src/boost/libs/property_tree/test/test_xml_parser_rapidxml.cpp create mode 100644 src/boost/libs/property_tree/test/xml_parser_test_data.hpp (limited to 'src/boost/libs/property_tree') diff --git a/src/boost/libs/property_tree/breaking_changes.txt b/src/boost/libs/property_tree/breaking_changes.txt new file mode 100644 index 000000000..f9044e483 --- /dev/null +++ b/src/boost/libs/property_tree/breaking_changes.txt @@ -0,0 +1,51 @@ +Copyright (C) 2009 Sebastian Redl +Distributed under the Boost Software License, Version 1.0. +See http://www.boost.org/LICENSE_1_0.txt + + +List all breaking changes done to the interface during the update here. + +- Template parameters +Template parameters have been thoroughly changed. +Impact: If you were using a custom instantiation of basic_ptree, you have to + change your code. +Rationale: The old order made no sense. It ordered the key comparison predicate + before the key, although it could easily be defaulted based on it, + and the path before the data type, when the path is something you + will very rarely want to change (and it could default, too). + +- put* +The put and put_child functions of basic_ptree had add and add_child split from +them, by separating along the lines of the do_not_replace parameter. +Impact: If you were using the third parameter of these functions, you have to + change your code. +Rationale: I'm not fond of using boolean parameters to change function behavior. + They're hard to remember and hard to read in code. When projects + adopt the convention of passing /*boolean=*/true, + /*parameters=*/false, /*like=*/false this, something's wrong. + It's even more wrong when the parameter has a negative name, as + do_not_replace had. + +- Custom paths +Custom paths have been thoroughly changed. +Impact: If you were using custom paths, you have to change your code. If you + referred to the basic_path template by name, you have to change your + code. +Rationale: The old interface required a huge amount of repeated code for + custom paths. The new interface is a lot easier to implement. + +- Translators +Translators have been thoroughly changed. +Impact: If you were using translators at all, you probably have to change your + code. +Rationale: The new interface makes it easier to pass custom translators to + specific get/put operations. It also keeps the translator out of + the tree's type. + +- find +find() returns an assoc_iterator. +Impact: If you use find, you may have to change your code. Most importantly, + you need to compare against not_found() instead of end(). +Rationale: equal_range() also returns assoc_iterators. equal_range() cannot + return normal iterators, since the conversion would not preserve + the equal range or even the range property. diff --git a/src/boost/libs/property_tree/examples/Jamfile.v2 b/src/boost/libs/property_tree/examples/Jamfile.v2 new file mode 100644 index 000000000..07c382aed --- /dev/null +++ b/src/boost/libs/property_tree/examples/Jamfile.v2 @@ -0,0 +1,12 @@ +# Boost PropertyTree Library Example Jamfile +# Copyright (c) 2013 Sebastian Redl +# Distributed under the Boost Software License, Version 1.0. +# See http://www.boost.org/LICENSE_1_0.txt + +project + : requirements + ../../../ + : + ; + +exe debug_settings : debug_settings.cpp ; diff --git a/src/boost/libs/property_tree/examples/custom_data_type.cpp b/src/boost/libs/property_tree/examples/custom_data_type.cpp new file mode 100644 index 000000000..dbc905fe0 --- /dev/null +++ b/src/boost/libs/property_tree/examples/custom_data_type.cpp @@ -0,0 +1,88 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +// This example shows what need to be done to customize data_type of ptree. +// +// It creates my_ptree type, which is a basic_ptree having boost::any as its data +// container (instead of std::string that standard ptree has). + +#include +#include +#include +#include +#include + +// Custom translator that works with boost::any instead of std::string +template +struct variant_translator +{ + typedef Ext external_type; + typedef Int internal_type; + + external_type + get_value(const internal_type &value) const + { + return boost::any_cast(value); + } + internal_type + put_value(const external_type &value) const + { + return value; + } +}; + +int main() +{ + + using namespace boost::property_tree; + + // Property_tree with boost::any as data type + // Key type: std::string + // Data type: boost::any + // Key comparison: default (std::less) + typedef basic_ptree my_ptree; + my_ptree pt; + + // Put/get int value + typedef variant_translator int_tran; + pt.put("int value", 3, int_tran()); + int int_value = pt.get("int value", int_tran()); + std::cout << "Int value: " << int_value << "\n"; + + // Put/get string value + typedef variant_translator string_tran; + pt.put("string value", "foo bar", string_tran()); + std::string string_value = pt.get( + "string value" + , string_tran() + ); + std::cout << "String value: " << string_value << "\n"; + + // Put/get list value + typedef std::list intlist; + typedef variant_translator intlist_tran; + int list_data[] = { 1, 2, 3, 4, 5 }; + pt.put( + "list value" + , intlist( + list_data + , list_data + sizeof(list_data) / sizeof(*list_data) + ) + , intlist_tran() + ); + intlist list_value = pt.get( + "list value" + , intlist_tran() + ); + std::cout << "List value: "; + for (intlist::iterator it = list_value.begin(); it != list_value.end(); ++it) + std::cout << *it << ' '; + std::cout << '\n'; +} diff --git a/src/boost/libs/property_tree/examples/debug_settings.cpp b/src/boost/libs/property_tree/examples/debug_settings.cpp new file mode 100644 index 000000000..5e214290b --- /dev/null +++ b/src/boost/libs/property_tree/examples/debug_settings.cpp @@ -0,0 +1,95 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +//[debug_settings_includes +#include +#include +#include +#include +#include +#include +#include +namespace pt = boost::property_tree; +//] +//[debug_settings_data +struct debug_settings +{ + std::string m_file; // log filename + int m_level; // debug level + std::set m_modules; // modules where logging is enabled + void load(const std::string &filename); + void save(const std::string &filename); +}; +//] +//[debug_settings_load +void debug_settings::load(const std::string &filename) +{ + // Create empty property tree object + pt::ptree tree; + + // Parse the XML into the property tree. + pt::read_xml(filename, tree); + + // Use the throwing version of get to find the debug filename. + // If the path cannot be resolved, an exception is thrown. + m_file = tree.get("debug.filename"); + + // Use the default-value version of get to find the debug level. + // Note that the default value is used to deduce the target type. + m_level = tree.get("debug.level", 0); + + // Use get_child to find the node containing the modules, and iterate over + // its children. If the path cannot be resolved, get_child throws. + // A C++11 for-range loop would also work. + BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")) { + // The data function is used to access the data stored in a node. + m_modules.insert(v.second.data()); + } + +} +//] +//[debug_settings_save +void debug_settings::save(const std::string &filename) +{ + // Create an empty property tree object. + pt::ptree tree; + + // Put the simple values into the tree. The integer is automatically + // converted to a string. Note that the "debug" node is automatically + // created if it doesn't exist. + tree.put("debug.filename", m_file); + tree.put("debug.level", m_level); + + // Add all the modules. Unlike put, which overwrites existing nodes, add + // adds a new node at the lowest level, so the "modules" node will have + // multiple "module" children. + BOOST_FOREACH(const std::string &name, m_modules) + tree.add("debug.modules.module", name); + + // Write property tree to XML file + pt::write_xml(filename, tree); +} +//] + +int main() +{ + try + { + debug_settings ds; + ds.load("debug_settings.xml"); + ds.save("debug_settings_out.xml"); + std::cout << "Success\n"; + } + catch (std::exception &e) + { + std::cout << "Error: " << e.what() << "\n"; + } + return 0; +} diff --git a/src/boost/libs/property_tree/examples/debug_settings.xml b/src/boost/libs/property_tree/examples/debug_settings.xml new file mode 100644 index 000000000..2ac170ec0 --- /dev/null +++ b/src/boost/libs/property_tree/examples/debug_settings.xml @@ -0,0 +1,15 @@ + + + + debug.log + + Finance + Admin + HR + + 2 + diff --git a/src/boost/libs/property_tree/examples/empty_ptree_trick.cpp b/src/boost/libs/property_tree/examples/empty_ptree_trick.cpp new file mode 100644 index 000000000..37285b24a --- /dev/null +++ b/src/boost/libs/property_tree/examples/empty_ptree_trick.cpp @@ -0,0 +1,71 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#include +#include +#include +#include +#include + +using namespace boost::property_tree; + +// Process settings using empty ptree trick. Note that it is considerably simpler +// than version which does not use the "trick" +void process_settings(const std::string &filename) +{ + ptree pt; + read_info(filename, pt); + const ptree &settings = pt.get_child("settings", empty_ptree()); + std::cout << "\n Processing " << filename << std::endl; + std::cout << " Setting 1 is " << settings.get("setting1", 0) << std::endl; + std::cout << " Setting 2 is " << settings.get("setting2", 0.0) << std::endl; + std::cout << " Setting 3 is " << settings.get("setting3", "default") << std::endl; +} + +// Process settings not using empty ptree trick. This one must duplicate much of the code. +void process_settings_without_trick(const std::string &filename) +{ + ptree pt; + read_info(filename, pt); + if (boost::optional settings = pt.get_child_optional("settings")) + { + std::cout << "\n Processing " << filename << std::endl; + std::cout << " Setting 1 is " << settings.get().get("setting1", 0) << std::endl; + std::cout << " Setting 2 is " << settings.get().get("setting2", 0.0) << std::endl; + std::cout << " Setting 3 is " << settings.get().get("setting3", "default") << std::endl; + } + else + { + std::cout << "\n Processing " << filename << std::endl; + std::cout << " Setting 1 is " << 0 << std::endl; + std::cout << " Setting 2 is " << 0.0 << std::endl; + std::cout << " Setting 3 is " << "default" << std::endl; + } +} + +int main() +{ + try + { + std::cout << "Processing settings with empty-ptree-trick:\n"; + process_settings("settings_fully-existent.info"); + process_settings("settings_partially-existent.info"); + process_settings("settings_non-existent.info"); + std::cout << "\nProcessing settings without empty-ptree-trick:\n"; + process_settings_without_trick("settings_fully-existent.info"); + process_settings_without_trick("settings_partially-existent.info"); + process_settings_without_trick("settings_non-existent.info"); + } + catch (std::exception &e) + { + std::cout << "Error: " << e.what() << "\n"; + } + return 0; +} diff --git a/src/boost/libs/property_tree/examples/info_grammar_spirit.cpp b/src/boost/libs/property_tree/examples/info_grammar_spirit.cpp new file mode 100644 index 000000000..07025341b --- /dev/null +++ b/src/boost/libs/property_tree/examples/info_grammar_spirit.cpp @@ -0,0 +1,152 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +/* This is grammar of INFO file format written in form of boost::spirit rules. + For simplicity, it does not parse #include directive. Note that INFO parser + included in property_tree library does not use Spirit. +*/ + +//#define BOOST_SPIRIT_DEBUG // uncomment to enable debug output +#include + +struct info_grammar: public boost::spirit::grammar +{ + + template + struct definition + { + + boost::spirit::rule::type> chr, qchr, escape_seq; + boost::spirit::rule string, qstring, cstring, key, value, entry, info; + + definition(const info_grammar &self) + { + + using namespace boost::spirit; + + escape_seq = chset_p("0abfnrtv\"\'\\"); + chr = (anychar_p - space_p - '\\' - '{' - '}' - '#' - '"') | ('\\' >> escape_seq); + qchr = (anychar_p - '"' - '\n' - '\\') | ('\\' >> escape_seq); + string = lexeme_d[+chr]; + qstring = lexeme_d['"' >> *qchr >> '"']; + cstring = lexeme_d['"' >> *qchr >> '"' >> '\\']; + key = string | qstring; + value = string | qstring | (+cstring >> qstring) | eps_p; + entry = key >> value >> !('{' >> *entry >> '}'); + info = *entry >> end_p; + + // Debug nodes + BOOST_SPIRIT_DEBUG_NODE(escape_seq); + BOOST_SPIRIT_DEBUG_NODE(chr); + BOOST_SPIRIT_DEBUG_NODE(qchr); + BOOST_SPIRIT_DEBUG_NODE(string); + BOOST_SPIRIT_DEBUG_NODE(qstring); + BOOST_SPIRIT_DEBUG_NODE(key); + BOOST_SPIRIT_DEBUG_NODE(value); + BOOST_SPIRIT_DEBUG_NODE(entry); + BOOST_SPIRIT_DEBUG_NODE(info); + + } + + const boost::spirit::rule &start() const + { + return info; + } + + }; +}; + +void info_parse(const char *s) +{ + + using namespace boost::spirit; + + // Parse and display result + info_grammar g; + parse_info pi = parse(s, g, space_p | comment_p(";")); + std::cout << "Parse result: " << (pi.hit ? "Success" : "Failure") << "\n"; + +} + +int main() +{ + + // Sample data 1 + const char *data1 = + "\n" + "key1 data1\n" + "{\n" + "\tkey data\n" + "}\n" + "key2 \"data2 \" {\n" + "\tkey data\n" + "}\n" + "key3 \"data\"\n" + "\t \"3\" {\n" + "\tkey data\n" + "}\n" + "\n" + "\"key4\" data4\n" + "{\n" + "\tkey data\n" + "}\n" + "\"key.5\" \"data.5\" { \n" + "\tkey data \n" + "}\n" + "\"key6\" \"data\"\n" + "\t \"6\" {\n" + "\tkey data\n" + "}\n" + " \n" + "key1 data1\n" + "{\n" + "\tkey data\n" + "}\n" + "key2 \"data2 \" {\n" + "\tkey data\n" + "}\n" + "key3 \"data\"\n" + "\t \"3\" {\n" + "\tkey data\n" + "}\n" + "\n" + "\"key4\" data4\n" + "{\n" + "\tkey data\n" + "}\n" + "\"key.5\" \"data.5\" {\n" + "\tkey data\n" + "}\n" + "\"key6\" \"data\"\n" + "\t \"6\" {\n" + "\tkey data\n" + "}\n" + "\\\\key\\t7 data7\\n\\\"data7\\\"\n" + "{\n" + "\tkey data\n" + "}\n" + "\"\\\\key\\t8\" \"data8\\n\\\"data8\\\"\"\n" + "{\n" + "\tkey data\n" + "}\n" + "\n"; + + // Sample data 2 + const char *data2 = + "key1\n" + "key2\n" + "key3\n" + "key4\n"; + + // Parse sample data + info_parse(data1); + info_parse(data2); + +} diff --git a/src/boost/libs/property_tree/examples/settings_fully-existent.info b/src/boost/libs/property_tree/examples/settings_fully-existent.info new file mode 100644 index 000000000..bec7a64b3 --- /dev/null +++ b/src/boost/libs/property_tree/examples/settings_fully-existent.info @@ -0,0 +1,6 @@ +settings +{ + setting1 15 + setting2 9.876 + setting3 Alice in Wonderland +} diff --git a/src/boost/libs/property_tree/examples/settings_non-existent.info b/src/boost/libs/property_tree/examples/settings_non-existent.info new file mode 100644 index 000000000..4f76b2a45 --- /dev/null +++ b/src/boost/libs/property_tree/examples/settings_non-existent.info @@ -0,0 +1,6 @@ +;settings // non-existent +;{ +; setting1 15 +; setting2 9.876 +; setting3 Alice in Wonderland +;} diff --git a/src/boost/libs/property_tree/examples/settings_partially-existent.info b/src/boost/libs/property_tree/examples/settings_partially-existent.info new file mode 100644 index 000000000..ed5e8c225 --- /dev/null +++ b/src/boost/libs/property_tree/examples/settings_partially-existent.info @@ -0,0 +1,6 @@ +settings +{ + setting1 15 + ;setting2 9.876 // non-existent + ;setting3 Alice in Wonderland // non-existent +} diff --git a/src/boost/libs/property_tree/examples/speed_test.cpp b/src/boost/libs/property_tree/examples/speed_test.cpp new file mode 100644 index 000000000..4d5efd8c3 --- /dev/null +++ b/src/boost/libs/property_tree/examples/speed_test.cpp @@ -0,0 +1,130 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#define _HAS_ITERATOR_DEBUGGING 0 + +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace boost; +using namespace boost::property_tree; + +string dummy; +vector keys; +vector shuffled_keys; + +void prepare_keys(int size) +{ + // Prepare keys + keys.clear(); + for (int i = 0; i < size; ++i) + keys.push_back((format("%d") % i).str()); + shuffled_keys = keys; + srand(0); + random_shuffle(shuffled_keys.begin(), shuffled_keys.end()); +} + +void clock_push_back(int size) +{ + prepare_keys(size); + int max_repeats = 1000000 / size; + shared_array pt_array(new ptree[max_repeats]); + + int n = 0; + clock_t t1 = clock(), t2; + do + { + if (n >= max_repeats) + break; + ptree &pt = pt_array[n]; + for (int i = 0; i < size; ++i) + pt.push_back(ptree::value_type(shuffled_keys[i], ptree())); + t2 = clock(); + ++n; + } while (t2 - t1 < CLOCKS_PER_SEC); + + cout << " push_back (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n"; + +} + +void clock_find(int size) +{ + prepare_keys(size); + + ptree pt; + for (int i = 0; i < size; ++i) + pt.push_back(ptree::value_type(keys[i], ptree("data"))); + + int n = 0; + clock_t t1 = clock(), t2; + do + { + for (int i = 0; i < size; ++i) + pt.find(shuffled_keys[i]); + t2 = clock(); + ++n; + } while (t2 - t1 < CLOCKS_PER_SEC); + + cout << " find (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n"; + +} + +void clock_erase(int size) +{ + prepare_keys(size); + + int max_repeats = 100000 / size; + shared_array pt_array(new ptree[max_repeats]); + + ptree pt; + for (int n = 0; n < max_repeats; ++n) + for (int i = 0; i < size; ++i) + pt_array[n].push_back(ptree::value_type(keys[i], ptree("data"))); + + int n = 0; + clock_t t1 = clock(), t2; + do + { + if (n >= max_repeats) + break; + ptree &pt = pt_array[n]; + for (int i = 0; i < size; ++i) + pt.erase(shuffled_keys[i]); + t2 = clock(); + ++n; + } while (t2 - t1 < CLOCKS_PER_SEC); + + cout << " erase (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n"; +} + +int main() +{ + + // push_back + clock_push_back(10); + clock_push_back(100); + clock_push_back(1000); + + // erase + clock_erase(10); + clock_erase(100); + clock_erase(1000); + + // find + clock_find(10); + clock_find(100); + clock_find(1000); + +} diff --git a/src/boost/libs/property_tree/index.html b/src/boost/libs/property_tree/index.html new file mode 100644 index 000000000..5d800309c --- /dev/null +++ b/src/boost/libs/property_tree/index.html @@ -0,0 +1,14 @@ + + + + + +Automatic redirection failed, please go to the +Boost.PropertyTree documentation. +

Copyright Sebastian Redl 2009

+

Distributed under the Boost Software License, Version 1.0. (See accompanying file +LICENSE_1_0.txt or copy at +www.boost.org/LICENSE_1_0.txt). +

+ + diff --git a/src/boost/libs/property_tree/meta/libraries.json b/src/boost/libs/property_tree/meta/libraries.json new file mode 100644 index 000000000..025c04397 --- /dev/null +++ b/src/boost/libs/property_tree/meta/libraries.json @@ -0,0 +1,16 @@ +{ + "key": "property_tree", + "name": "Property Tree", + "authors": [ + "Marcin Kalicinski", + "Sebastian Redl" + ], + "description": "A tree data structure especially suited to storing configuration data.", + "category": [ + "Containers", + "Data" + ], + "maintainers": [ + "Sebastian Redl " + ] +} diff --git a/src/boost/libs/property_tree/test/Jamfile.v2 b/src/boost/libs/property_tree/test/Jamfile.v2 new file mode 100644 index 000000000..af25731d7 --- /dev/null +++ b/src/boost/libs/property_tree/test/Jamfile.v2 @@ -0,0 +1,27 @@ +# Boost.PropertyTree +# +# Copyright (c) 2009 Sebastian Redl +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +# bring in rules for testing +import testing ; + +project + : requirements + static + msvc:_SCL_SECURE_NO_WARNINGS=1 + ; + +test-suite "property_tree" + : [ run test_property_tree.cpp /boost/serialization//boost_serialization ] + [ run test_info_parser.cpp ] + [ run test_json_parser.cpp ] + [ run test_json_parser2.cpp /boost/test//boost_unit_test_framework ] + [ run test_ini_parser.cpp ] + [ run test_xml_parser_rapidxml.cpp ] + + [ run test_multi_module1.cpp test_multi_module2.cpp ] +; diff --git a/src/boost/libs/property_tree/test/prefixing_callbacks.hpp b/src/boost/libs/property_tree/test/prefixing_callbacks.hpp new file mode 100644 index 000000000..d993fb592 --- /dev/null +++ b/src/boost/libs/property_tree/test/prefixing_callbacks.hpp @@ -0,0 +1,79 @@ +#ifndef JSON_PARSER_PREFIXING_CALLBACKS_HPP +#define JSON_PARSER_PREFIXING_CALLBACKS_HPP + +#include + +namespace constants +{ + template const Ch* null_prefix(); + template <> inline const char* null_prefix() { return "_:"; } + template <> inline const wchar_t* null_prefix() { return L"_:"; } + + template const Ch* boolean_prefix(); + template <> inline const char* boolean_prefix() { return "b:"; } + template <> inline const wchar_t* boolean_prefix() { return L"b:"; } + + template const Ch* number_prefix(); + template <> inline const char* number_prefix() { return "n:"; } + template <> inline const wchar_t* number_prefix() { return L"n:"; } + + template const Ch* string_prefix(); + template <> inline const char* string_prefix() { return "s:"; } + template <> inline const wchar_t* string_prefix() { return L"s:"; } + + template const Ch* array_prefix(); + template <> inline const char* array_prefix() { return "a:"; } + template <> inline const wchar_t* array_prefix() { return L"a:"; } + + template const Ch* object_prefix(); + template <> inline const char* object_prefix() { return "o:"; } + template <> inline const wchar_t* object_prefix() { return L"o:"; } +} + +template +struct prefixing_callbacks + : boost::property_tree::json_parser::detail::standard_callbacks { + typedef boost::property_tree::json_parser::detail::standard_callbacks + base; + typedef typename base::string string; + typedef typename base::char_type char_type; + + void on_null() { + base::on_null(); + this->current_value().insert(0, constants::null_prefix()); + } + + void on_boolean(bool b) { + base::on_boolean(b); + this->current_value().insert(0, constants::boolean_prefix()); + } + + template + void on_number(Range code_units) { + base::on_number(code_units); + this->current_value().insert(0, constants::number_prefix()); + } + void on_begin_number() { + base::on_begin_number(); + this->current_value() = constants::number_prefix(); + } + + void on_begin_string() { + base::on_begin_string(); + if (!this->is_key()) { + this->current_value() = constants::string_prefix(); + } + } + + void on_begin_array() { + base::on_begin_array(); + this->current_value() = constants::array_prefix(); + } + + void on_begin_object() { + base::on_begin_object(); + this->current_value() = constants::object_prefix(); + } +}; + +#endif diff --git a/src/boost/libs/property_tree/test/sandbox.cpp b/src/boost/libs/property_tree/test/sandbox.cpp new file mode 100644 index 000000000..9eb5908de --- /dev/null +++ b/src/boost/libs/property_tree/test/sandbox.cpp @@ -0,0 +1,22 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2009 Sebastian Redl +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#define _CRT_SECURE_NO_DEPRECATE +#include +#include +#include + +int main() +{ + using namespace boost::property_tree; + ptree pt; + read_xml("simple_all.xml", pt); + write_info(std::cout, pt); +} diff --git a/src/boost/libs/property_tree/test/test_info_parser.cpp b/src/boost/libs/property_tree/test/test_info_parser.cpp new file mode 100644 index 000000000..798f09e44 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_info_parser.cpp @@ -0,0 +1,249 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#include "test_utils.hpp" +#include + +/////////////////////////////////////////////////////////////////////////////// +// Test data + +const char *ok_data_1 = + ";Test file for info_parser\n" + "\n" + "key1 data1\n" + "{\n" + "\tkey data\n" + "}\n" + "#include \"testok1_inc.info\"\n" + "key2 \"data2 \" {\n" + "\tkey data\n" + "}\n" + "#\tinclude \"testok1_inc.info\"\n" + "key3 \"data\"\n" + "\t \"3\" {\n" + "\tkey data\n" + "}\n" + "\t#include \"testok1_inc.info\"\n" + "\n" + "\"key4\" data4\n" + "{\n" + "\tkey data\n" + "}\n" + "#include \"testok1_inc.info\"\n" + "\"key.5\" \"data.5\" { \n" + "\tkey data \n" + "}\n" + "#\tinclude \"testok1_inc.info\"\n" + "\"key6\" \"data\"\n" + "\t \"6\" {\n" + "\tkey data\n" + "}\n" + "\t#include \"testok1_inc.info\"\n" + " \n" + "key1 data1; comment\n" + "{; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "#include \"testok1_inc.info\"\n" + "key2 \"data2 \" {; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "#\tinclude \"testok1_inc.info\"\n" + "key3 \"data\"; comment\n" + "\t \"3\" {; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "\t#include \"testok1_inc.info\"\n" + "\n" + "\"key4\" data4; comment\n" + "{; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "#include \"testok1_inc.info\"\n" + "\"key.5\" \"data.5\" {; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "#\tinclude \"testok1_inc.info\"\n" + "\"key6\" \"data\"; comment\n" + "\t \"6\" {; comment\n" + "\tkey data; comment\n" + "}; comment\n" + "\t#include \"testok1_inc.info\"\n" + "\\\\key\\t7 data7\\n\\\"data7\\\"\n" + "{\n" + "\tkey data\n" + "}\n" + "\"\\\\key\\t8\" \"data8\\n\\\"data8\\\"\"\n" + "{\n" + "\tkey data\n" + "}\n" + "\n"; + +const char *ok_data_1_inc = + ";Test file for info_parser\n" + "\n" + "inc_key inc_data ;;; comment\\"; + +const char *ok_data_2 = + ""; + +const char *ok_data_3 = + "key1 \"\"\n" + "key2 \"\"\n" + "key3 \"\"\n" + "key4 \"\"\n"; + +const char *ok_data_4 = + "key1 data key2 data"; + +const char *ok_data_5 = + "key { key \"\" key \"\" }\n"; + +const char *ok_data_6 = + "\"key with spaces\" \"data with spaces\"\n" + "\"key with spaces\" \"multiline data\"\\\n" + "\"cont\"\\\n" + "\"cont\""; + +const char *error_data_1 = + ";Test file for info_parser\n" + "#include \"bogus_file\"\n"; // Nonexistent include file + +const char *error_data_2 = + ";Test file for info_parser\n" + "key \"data with bad escape: \\q\"\n"; // Bad escape + +const char *error_data_3 = + ";Test file for info_parser\n" + "{\n"; // Opening brace without key + +const char *error_data_4 = + ";Test file for info_parser\n" + "}\n"; // Closing brace without opening brace + +const char *error_data_5 = + ";Test file for info_parser\n" + "key data\n" + "{\n" + ""; // No closing brace + +struct ReadFunc +{ + template + void operator()(const std::string &filename, Ptree &pt) const + { + boost::property_tree::read_info(filename, pt); + } +}; + +struct WriteFunc +{ + template + void operator()(const std::string &filename, const Ptree &pt) const + { + boost::property_tree::write_info(filename, pt); + } +}; + +template +void test_info_parser() +{ + + using namespace boost::property_tree; + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_1, ok_data_1_inc, + "testok1.info", "testok1_inc.info", "testok1out.info", 45, 240, 192 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_2, NULL, + "testok2.info", NULL, "testok2out.info", 1, 0, 0 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_3, NULL, + "testok3.info", NULL, "testok3out.info", 5, 0, 16 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_4, NULL, + "testok4.info", NULL, "testok4out.info", 3, 8, 8 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_5, NULL, + "testok5.info", NULL, "testok5out.info", 4, 0, 9 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_6, NULL, + "testok6.info", NULL, "testok6out.info", 3, 38, 30 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_1, NULL, + "testerr1.info", NULL, "testerr1out.info", 2 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_2, NULL, + "testerr2.info", NULL, "testerr2out.info", 2 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_3, NULL, + "testerr3.info", NULL, "testerr3out.info", 2 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_4, NULL, + "testerr4.info", NULL, "testerr4out.info", 2 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_5, NULL, + "testerr5.info", NULL, "testerr5out.info", 4 + ); + + // Test read with default ptree + { + Ptree pt, default_pt; + pt.put_value(1); + default_pt.put_value(2); + BOOST_CHECK(pt != default_pt); + read_info("nonexisting file.nonexisting file", pt, default_pt); + BOOST_CHECK(pt == default_pt); + } + +} + +int test_main(int argc, char *argv[]) +{ + using namespace boost::property_tree; + test_info_parser(); + test_info_parser(); +#ifndef BOOST_NO_CWCHAR + test_info_parser(); + test_info_parser(); +#endif + return 0; +} diff --git a/src/boost/libs/property_tree/test/test_ini_parser.cpp b/src/boost/libs/property_tree/test/test_ini_parser.cpp new file mode 100644 index 000000000..2edb48cd4 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_ini_parser.cpp @@ -0,0 +1,226 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#include "test_utils.hpp" +#include +#include + +using namespace boost::property_tree; + +/////////////////////////////////////////////////////////////////////////////// +// Test data + +// Correct data +const char *ok_data_1 = + "\n" + "; Comment\n" + "[Section1]\n" + "\t \t; Comment\n" + " Key1=Data1\n" + " \n" + " Key2 = Data2\n" + "Key 3 = Data 3 \n" + "Key4=Data4\n" + "[Section2] ;Comment\n" + "\t \tKey1=Data4\n"; + +// Correct data +const char *ok_data_2 = + "[Section1]\n" + "Key1=Data1"; // No eol + +// Correct data +const char *ok_data_3 = + ""; + +// Correct data +const char *ok_data_4 = + ";Comment"; + +// Correct data +const char *ok_data_5 = + "Key1=Data1\n" // No section + "Key2=Data2\n"; + +// Treat # as comment. +const char *ok_data_6 = + "# Comment\n" + "[Section1]\n" + "Key1=Data1\n"; + +// Erroneous data +const char *error_data_1 = + "[Section1]\n" + "Key1\n" // No equals sign + "Key2=Data2"; + +// Erroneous data +const char *error_data_2 = + "[Section1]\n" + "Key1=Data1\n" + "=Data2\n"; // No key + +struct ReadFunc +{ + template + void operator()(const std::string &filename, Ptree &pt) const + { + read_ini(filename, pt); + } +}; + +struct WriteFunc +{ + template + void operator()(const std::string &filename, const Ptree &pt) const + { + write_ini(filename, pt); + } +}; + +void test_erroneous_write(const boost::property_tree::ptree &pt) +{ + std::stringstream stream; + try + { + write_ini(stream, pt); + BOOST_ERROR("No required exception thrown"); + } + catch (ini_parser_error &e) + { + (void)e; + } + catch (...) + { + BOOST_ERROR("Wrong exception type thrown"); + } +} + +template +void test_ini_parser() +{ + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_1, NULL, + "testok1.ini", NULL, "testok1out.ini", 8, 26, 37 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_2, NULL, + "testok2.ini", NULL, "testok2out.ini", 3, 5, 12 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_3, NULL, + "testok3.ini", NULL, "testok3out.ini", 1, 0, 0 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_4, NULL, + "testok4.ini", NULL, "testok4out.ini", 1, 0, 0 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_5, NULL, + "testok5.ini", NULL, "testok5out.ini", 3, 10, 8 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(), ok_data_6, NULL, + "testok6.ini", NULL, "testok6out.ini", 3, 5, 12 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_1, NULL, + "testerr1.ini", NULL, "testerr1out.ini", 2 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(), error_data_2, NULL, + "testerr2.ini", NULL, "testerr2out.ini", 3 + ); +} + +void test_unmappable_trees() +{ + // Test too deep ptrees + { + ptree pt; + pt.put_child("section.key.bogus", ptree()); + test_erroneous_write(pt); + } + + // Test duplicate sections + { + ptree pt; + pt.push_back(std::make_pair("section", ptree())); + pt.push_back(std::make_pair("section", ptree())); + test_erroneous_write(pt); + } + + // Test duplicate keys + { + ptree pt; + ptree &child = pt.put_child("section", ptree()); + child.push_back(std::make_pair("key", ptree())); + child.push_back(std::make_pair("key", ptree())); + test_erroneous_write(pt); + } + + // Test mixed data and children. + { + ptree pt; + ptree &child = pt.put_child("section", ptree("value")); + child.push_back(std::make_pair("key", ptree())); + child.push_back(std::make_pair("key", ptree())); + test_erroneous_write(pt); + } +} + +void test_other_trees() +{ + // Top-level keys must be written before any section. + { + ptree pt; + pt.put("section.innerkey", "v1"); + pt.put("nosection", "v2"); + std::stringstream s; + write_ini(s, pt); + s.clear(); + s.seekg(0, std::ios_base::beg); + ptree result; + read_ini(s, result); + BOOST_CHECK(result.get("section.innerkey", "bad") == "v1"); + BOOST_CHECK(result.get("nosection", "bad") == "v2"); + } +} + +int test_main(int argc, char *argv[]) +{ + test_ini_parser(); + test_ini_parser(); +#ifndef BOOST_NO_CWCHAR + test_ini_parser(); + test_ini_parser(); +#endif + + test_unmappable_trees(); + test_other_trees(); + + return 0; + +} diff --git a/src/boost/libs/property_tree/test/test_json_parser.cpp b/src/boost/libs/property_tree/test/test_json_parser.cpp new file mode 100644 index 000000000..3c14d9b33 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_json_parser.cpp @@ -0,0 +1,443 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#include "test_utils.hpp" +#include + +/////////////////////////////////////////////////////////////////////////////// +// Test data + +const char *ok_data_1 = + "{}"; + +const char *ok_data_2 = + " \t {\n" + " \t \"name 0\" : \"value\", \t \n" + " \t \"name 1\" : \"\", \n" + " \t \"name 2\" : true, \n" + " \t \"name 3\" : false, \n" + " \t \"name 4\" : null, \n" + " \t \"name 5\" : 0, \n" + " \t \"name 6\" : -5, \n" + " \t \"name 7\" : 1.1, \n" + " \t \"name 8\" : -956.45e+4, \n" + " \t \"name 8\" : 5956.45E-11, \n" + " \t \"name 9\" : [1,2,3,4], \n" + " \t \"name 10\" : {\"a\":\"b\"} \n" + " \t } \n"; + +const char *ok_data_3 = + "{\"a\":{\"b\":\"c\"}}"; + +const char *ok_data_4 = + "{\"a\":[{\"b\":\"c\"},{\"d\":\"e\"},1,2,3,4],\"f\":null}"; + +const char *ok_data_6 = + "{\"menu\": {\n" + " \"header\": \"SVG Viewer\",\n" + " \"items\": [\n" + " {\"id\": \"Open\"},\n" + " {\"id\": \"OpenNew\", \"label\": \"Open New\"},\n" + " null,\n" + " {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\n" + " {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\n" + " {\"id\": \"OriginalView\", \"label\": \"Original View\"},\n" + " null,\n" + " {\"id\": \"Quality\"},\n" + " {\"id\": \"Pause\"},\n" + " {\"id\": \"Mute\"},\n" + " null,\n" + " {\"id\": \"Find\", \"label\": \"Find...\"},\n" + " {\"id\": \"FindAgain\", \"label\": \"Find Again\"},\n" + " {\"id\": \"Copy\"},\n" + " {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},\n" + " {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},\n" + " {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},\n" + " {\"id\": \"ViewSource\", \"label\": \"View Source\"},\n" + " {\"id\": \"SaveAs\", \"label\": \"Save As\"},\n" + " null,\n" + " {\"id\": \"Help\"},\n" + " {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\n" + " ]\n" + "}}\n"; + +const char *ok_data_7 = + "{\"web-app\": {\n" + " \"servlet\": [ \n" + " {\n" + " \"servlet-name\": \"cofaxCDS\",\n" + " \"servlet-class\": \"org.cofax.cds.CDSServlet\",\n" + " \"init-param\": {\n" + " \"configGlossary:installationAt\": \"Philadelphia, PA\",\n" + " \"configGlossary:adminEmail\": \"ksm@pobox.com\",\n" + " \"configGlossary:poweredBy\": \"Cofax\",\n" + " \"configGlossary:poweredByIcon\": \"/images/cofax.gif\",\n" + " \"configGlossary:staticPath\": \"/content/static\",\n" + " \"templateProcessorClass\": \"org.cofax.WysiwygTemplate\",\n" + " \"templateLoaderClass\": \"org.cofax.FilesTemplateLoader\",\n" + " \"templatePath\": \"templates\",\n" + " \"templateOverridePath\": \"\",\n" + " \"defaultListTemplate\": \"listTemplate.htm\",\n" + " \"defaultFileTemplate\": \"articleTemplate.htm\",\n" + " \"useJSP\": false,\n" + " \"jspListTemplate\": \"listTemplate.jsp\",\n" + " \"jspFileTemplate\": \"articleTemplate.jsp\",\n" + " \"cachePackageTagsTrack\": 200,\n" + " \"cachePackageTagsStore\": 200,\n" + " \"cachePackageTagsRefresh\": 60,\n" + " \"cacheTemplatesTrack\": 100,\n" + " \"cacheTemplatesStore\": 50,\n" + " \"cacheTemplatesRefresh\": 15,\n" + " \"cachePagesTrack\": 200,\n" + " \"cachePagesStore\": 100,\n" + " \"cachePagesRefresh\": 10,\n" + " \"cachePagesDirtyRead\": 10,\n" + " \"searchEngineListTemplate\": \"forSearchEnginesList.htm\",\n" + " \"searchEngineFileTemplate\": \"forSearchEngines.htm\",\n" + " \"searchEngineRobotsDb\": \"WEB-INF/robots.db\",\n" + " \"useDataStore\": true,\n" + " \"dataStoreClass\": \"org.cofax.SqlDataStore\",\n" + " \"redirectionClass\": \"org.cofax.SqlRedirection\",\n" + " \"dataStoreName\": \"cofax\",\n" + " \"dataStoreDriver\": \"com.microsoft.jdbc.sqlserver.SQLServerDriver\",\n" + " \"dataStoreUrl\": \"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\",\n" + " \"dataStoreUser\": \"sa\",\n" + " \"dataStorePassword\": \"dataStoreTestQuery\",\n" + " \"dataStoreTestQuery\": \"SET NOCOUNT ON;select test='test';\",\n" + " \"dataStoreLogFile\": \"/usr/local/tomcat/logs/datastore.log\",\n" + " \"dataStoreInitConns\": 10,\n" + " \"dataStoreMaxConns\": 100,\n" + " \"dataStoreConnUsageLimit\": 100,\n" + " \"dataStoreLogLevel\": \"debug\",\n" + " \"maxUrlLength\": 500}},\n" + " {\n" + " \"servlet-name\": \"cofaxEmail\",\n" + "\n \"servlet-class\": \"org.cofax.cds.EmailServlet\",\n" + " \"init-param\": {\n" + " \"mailHost\": \"mail1\",\n" + " \"mailHostOverride\": \"mail2\"}},\n" + " {\n" + " \"servlet-name\": \"cofaxAdmin\",\n" + " \"servlet-class\": \"org.cofax.cds.AdminServlet\"},\n" + " \n" + " {\n" + " \"servlet-name\": \"fileServlet\",\n" + " \"servlet-class\": \"org.cofax.cds.FileServlet\"},\n" + " {\n" + " \"servlet-name\": \"cofaxTools\",\n" + " \"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\",\n" + " \"init-param\": {\n" + " \"templatePath\": \"toolstemplates/\",\n" + " \"log\": 1,\n" + " \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\",\n" + " \"logMaxSize\": \"\",\n" + " \"dataLog\": 1,\n" + " \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\",\n" + " \"dataLogMaxSize\": \"\",\n" + " \"removePageCache\": \"/content/admin/remove?cache=pages&id=\",\n" + " \"removeTemplateCache\": \"/content/admin/remove?cache=templates&id=\",\n" + " \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\",\n" + " \"lookInContext\": 1,\n" + " \"adminGroupID\": 4,\n" + " \"betaServer\": true}}],\n" + " \"servlet-mapping\": {\n" + " \"cofaxCDS\": \"/\",\n" + " \"cofaxEmail\": \"/cofaxutil/aemail/*\",\n" + " \"cofaxAdmin\": \"/admin/*\",\n" + " \"fileServlet\": \"/static/*\",\n" + " \"cofaxTools\": \"/tools/*\"},\n" + " \n" + " \"taglib\": {\n" + " \"taglib-uri\": \"cofax.tld\",\n" + " \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}\n"; + +const char *ok_data_8 = + "{\"widget\": {\n" + " \"debug\": \"on\",\n" + " \"window\": {\n" + " \"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500\n" + " }, \"image\": { \n" + " \"src\": \"Images/Sun.png\",\n" + " \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\"\n" + " }, \"text\": {\n" + " \"data\": \"Click Here\",\n" + " \"size\": 36,\n" + " \"style\": \"bold\", \"name\": \"text1\", \"hOffset\": 250, \"vOffset\": 100, \"alignment\": \"center\",\n" + " \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n" + " }\n" + "}} \n"; + +const char *ok_data_9 = + "{\"menu\": {\n" + " \"id\": \"file\",\n" + " \"value\": \"File:\",\n" + " \"popup\": {\n" + " \"menuitem\": [\n" + " {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n" + " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" + "\n {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" + " ]\n" + " }\n" + "}}\n"; + +const char *ok_data_10 = + "{\n" + " \"glossary\": {\n" + " \"title\": \"example glossary\",\n" + " \"GlossDiv\": {\n" + " \"title\": \"S\",\n" + " \"GlossList\": [{\n" + " \"ID\": \"SGML\",\n" + " \"SortAs\": \"SGML\",\n" + " \"GlossTerm\": \"Standard Generalized Markup Language\",\n" + " \"Acronym\": \"SGML\",\n" + " \"Abbrev\": \"ISO 8879:1986\",\n" + " \"GlossDef\": \n" + "\"A meta-markup language, used to create markup languages such as DocBook.\",\n" + " \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"]\n" + " }]\n" + " }\n" + " }\n" + "}\n"; + +const char *ok_data_11 = + "{\n" + " \"data\" : [\n" + " {\n" + " \"First Name\" : \"Bob\",\n" + " \"Last Name\" : \"Smith\",\n" + " \"Email\" : \"bsmith@someurl.com\",\n" + " \"Phone\" : \"(555) 555-1212\"\n" + " },\n" + " {\n" + " \"First Name\" : \"Jan\",\n" + " \"Last Name\" : \"Smith\",\n" + " \"Email\" : \"jsmith@someurl.com\",\n" + " \"Phone\" : \"(555) 555-3434\"\n" + " },\n" + " {\n" + " \"First Name\" : \"Sue\",\n" + " \"Last Name\" : \"Smith\",\n" + " \"Email\" : \"ssmith@someurl.com\",\n" + " \"Phone\" : \"(555) 555-5656\"\n" + " }\n" + " ]\n" + "}\n"; + +const char *ok_data_12 = + "{\" \\\" \\\\ \\b \\f \\n \\r \\t \" : \"\"}"; + +const char *error_data_1 = + ""; // No root object + +const char *error_data_2 = + "{\n\"a\":1\n"; // Unclosed object brace + +const char *error_data_3 = + "{\n\"a\":\n[1,2,3,4\n}"; // Unclosed array brace + +const char *error_data_4 = + "{\n\"a\"\n}"; // No object + +const char *bug_data_pr4387 = + "[1, 2, 3]"; // Root array + +const char *bug_data_pr7180_1 = + "{\n" + " \"a\": [\n" + " \"1\",\n" + " \"2\"\n" + " ]\n" + "}\n"; +const char *bug_data_pr7180_2 = + "{\"a\":[\"1\",\"2\"]}\n"; + +struct ReadFunc +{ + template + void operator()(const std::string &filename, Ptree &pt) const + { + boost::property_tree::read_json(filename, pt); + } + template + void operator()( + std::basic_istream& is, + Ptree& pt) const + { + boost::property_tree::read_json(is, pt); + } +}; + +struct WriteFunc +{ + WriteFunc(bool pretty = false) : pretty(pretty) {} + + template + void operator()(const std::string &filename, const Ptree &pt) const + { + boost::property_tree::write_json(filename, pt, std::locale(), pretty); + } + + template + void operator()( + std::basic_ostream& os, + Ptree& pt) const + { + boost::property_tree::write_json(os, pt, pretty); + } + + bool pretty; +}; + +template +void test_json_parser() +{ + + using namespace boost::property_tree; + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_1, NULL, + "testok1.json", NULL, "testok1out.json", 1, 0, 0 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_2, NULL, + "testok2.json", NULL, "testok2out.json", 18, 50, 74 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_3, NULL, + "testok3.json", NULL, "testok3out.json", 3, 1, 2 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_4, NULL, + "testok4.json", NULL, "testok4out.json", 11, 10, 4 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_6, NULL, + "testok6.json", NULL, "testok6out.json", 56, 265, 111 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_7, NULL, + "testok7.json", NULL, "testok7out.json", 87, 1046, 1216 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_8, NULL, + "testok8.json", NULL, "testok8out.json", 23, 149, 125 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_9, NULL, + "testok9.json", NULL, "testok9out.json", 15, 54, 60 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_10, NULL, + "testok10.json", NULL, "testok10out.json", 17, 162, 85 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_11, NULL, + "testok11.json", NULL, "testok11out.json", 17, 120, 91 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), ok_data_12, NULL, + "testok12.json", NULL, "testok12out.json", 2, 0, 17 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(false), error_data_1, NULL, + "testerr1.json", NULL, "testerr1out.json", 1 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(false), error_data_2, NULL, + "testerr2.json", NULL, "testerr2out.json", 3 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(false), error_data_3, NULL, + "testerr3.json", NULL, "testerr3out.json", 4 + ); + + generic_parser_test_error + ( + ReadFunc(), WriteFunc(false), error_data_4, NULL, + "testerr4.json", NULL, "testerr4out.json", 3 + ); + + generic_parser_test_ok + ( + ReadFunc(), WriteFunc(false), bug_data_pr4387, NULL, + "testpr4387.json", NULL, "testpr4387out.json", 4, 3, 0 + ); + + check_exact_roundtrip(ReadFunc(), WriteFunc(true), + bug_data_pr7180_1); + check_exact_roundtrip(ReadFunc(), WriteFunc(false), + bug_data_pr7180_2); + +} + +void test_escaping_utf8() +{ + // This is cyrillic text encoded as UTF-8 + std::string str = "\xD0\x9C\xD0\xB0\xD0\xBC\xD0\xB0 " + "\xD0\xBC\xD1\x8B\xD0\xBB\xD0\xB0 \xD1\x80\xD0\xB0\xD0\xBC\xD1\x83"; + // Should NOT escape UTF-8 + BOOST_CHECK(boost::property_tree::json_parser::create_escapes(str) == str); +} + +void test_escaping_wide() +{ + // Should NOT escape characters within ASCII range. + std::wstring str1 = L"I am wstring with ASCII"; + BOOST_CHECK(boost::property_tree::json_parser::create_escapes(str1) == str1); + // Should escape characters outside ASCII range - this is NOT utf-8 + // This is cyrillic text + std::wstring str2 = L"\u041C\u0430\u043C\u0430 " + L"\u043C\u044B\u043B\u0430 \u0440\u0430\u043C\u0443"; + BOOST_CHECK(boost::property_tree::json_parser::create_escapes(str2) == + L"\\u041C\\u0430\\u043C\\u0430 " + L"\\u043C\\u044B\\u043B\\u0430 \\u0440\\u0430\\u043C\\u0443"); +} + +int test_main(int argc, char *argv[]) +{ + using namespace boost::property_tree; + test_json_parser(); + test_json_parser(); + test_escaping_utf8(); +#ifndef BOOST_NO_CWCHAR + test_json_parser(); + test_json_parser(); + test_escaping_wide(); +#endif + return 0; +} diff --git a/src/boost/libs/property_tree/test/test_json_parser2.cpp b/src/boost/libs/property_tree/test/test_json_parser2.cpp new file mode 100644 index 000000000..248ba4692 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_json_parser2.cpp @@ -0,0 +1,935 @@ +#include +#include +#include +#include +#include "prefixing_callbacks.hpp" + +#define BOOST_TEST_NO_MAIN +#include +#include + +#include +#include + +#include +#include +#include + +using namespace boost::property_tree; + +template struct encoding; +template <> struct encoding + : json_parser::detail::utf8_utf8_encoding +{}; +template <> struct encoding + : json_parser::detail::wide_wide_encoding +{}; + +template +struct test_parser +{ + Callbacks callbacks; + ::encoding encoding; + typedef std::basic_string string; + typedef basic_ptree tree; + json_parser::detail::parser, + typename string::const_iterator, + typename string::const_iterator> + parser; + + test_parser() : parser(callbacks, encoding) {} + + bool parse_null(const string& input, string& output) { + parser.set_input("", input); + bool result = parser.parse_null(); + if (result) { + parser.finish(); + output = callbacks.output().data(); + } + return result; + } + + bool parse_boolean(const string& input, string& output) { + parser.set_input("", input); + bool result = parser.parse_boolean(); + if (result) { + parser.finish(); + output = callbacks.output().data(); + } + return result; + } + + bool parse_number(const string& input, string& output) { + parser.set_input("", input); + bool result = parser.parse_number(); + if (result) { + parser.finish(); + output = callbacks.output().data(); + } + return result; + } + + bool parse_string(const string& input, string& output) { + parser.set_input("", input); + bool result = parser.parse_string(); + if (result) { + parser.finish(); + output = callbacks.output().data(); + } + return result; + } + + bool parse_array(const string& input, tree& output) { + parser.set_input("", input); + bool result = parser.parse_array(); + if (result) { + parser.finish(); + output = callbacks.output(); + } + return result; + } + + bool parse_object(const string& input, tree& output) { + parser.set_input("", input); + bool result = parser.parse_object(); + if (result) { + parser.finish(); + output = callbacks.output(); + } + return result; + } + + void parse_value(const string& input, tree& output) { + parser.set_input("", input); + parser.parse_value(); + parser.finish(); + output = callbacks.output(); + } +}; + +template +struct standard_parser + : test_parser< + json_parser::detail::standard_callbacks< + basic_ptree, std::basic_string > >, + Ch> +{}; + +template +struct prefixing_parser + : test_parser< + prefixing_callbacks< + basic_ptree, std::basic_string > >, + Ch> +{}; + +#define BOM_N "\xef\xbb\xbf" +#define BOM_W L"\xfeff" + +namespace boost { namespace test_tools { namespace tt_detail { + template<> + struct print_log_value { + void operator()(std::ostream& os, const std::wstring& s) { + print_log_value()(os, s.c_str()); + } + }; +}}} +BOOST_TEST_DONT_PRINT_LOG_VALUE(ptree::iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(ptree::const_iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(wptree::iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(wptree::const_iterator) + +BOOST_AUTO_TEST_CASE(null_parse_result_is_input) { + std::string parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_null("null", parsed)); + BOOST_CHECK_EQUAL("null", parsed); +} + +BOOST_AUTO_TEST_CASE(uses_traits_from_null) +{ + std::string parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_null("null", parsed)); + BOOST_CHECK_EQUAL("_:null", parsed); +} + +BOOST_AUTO_TEST_CASE(null_parse_skips_bom) { + std::string parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_null(BOM_N "null", parsed)); + BOOST_CHECK_EQUAL("null", parsed); +} + +BOOST_AUTO_TEST_CASE(null_parse_result_is_input_w) { + std::wstring parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_null(L"null", parsed)); + BOOST_CHECK_EQUAL(L"null", parsed); +} + +BOOST_AUTO_TEST_CASE(uses_traits_from_null_w) +{ + std::wstring parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_null(L"null", parsed)); + BOOST_CHECK_EQUAL(L"_:null", parsed); +} + +BOOST_AUTO_TEST_CASE(null_parse_skips_bom_w) { + std::wstring parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_null(BOM_W L"null", parsed)); + BOOST_CHECK_EQUAL(L"null", parsed); +} + +void boolean_parse_result_is_input_n(const char* param) { + std::string parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_boolean(param, parsed)); + BOOST_CHECK_EQUAL(param, parsed); +} + +const char* const booleans_n[] = { "true", "false" }; + +BOOST_AUTO_TEST_CASE(uses_traits_from_boolean_n) +{ + std::string parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_boolean("true", parsed)); + BOOST_CHECK_EQUAL("b:true", parsed); +} + +void boolean_parse_result_is_input_w(const wchar_t* param) { + std::wstring parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_boolean(param, parsed)); + BOOST_CHECK_EQUAL(param, parsed); +} + +const wchar_t* const booleans_w[] = { L"true", L"false" }; + +BOOST_AUTO_TEST_CASE(uses_traits_from_boolean_w) +{ + std::wstring parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_boolean(L"true", parsed)); + BOOST_CHECK_EQUAL(L"b:true", parsed); +} + +void number_parse_result_is_input_n(const char* param) { + std::string parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_number(param, parsed)); + BOOST_CHECK_EQUAL(param, parsed); +} + +const char* const numbers_n[] = { + "0", + "-0", + "1824", + "-0.1", + "123.142", + "1e+0", + "1E-0", + "1.1e134" +}; + +BOOST_AUTO_TEST_CASE(uses_traits_from_number_n) +{ + std::string parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_number("12345", parsed)); + BOOST_CHECK_EQUAL("n:12345", parsed); +} + +void number_parse_result_is_input_w(const wchar_t* param) { + std::wstring parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_number(param, parsed)); + BOOST_CHECK_EQUAL(param, parsed); +} + +const wchar_t* const numbers_w[] = { + L"0", + L"-0", + L"1824", + L"-0.1", + L"123.142", + L"1e+0", + L"1E-0", + L"1.1e134" +}; + +BOOST_AUTO_TEST_CASE(uses_traits_from_number_w) +{ + std::wstring parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_number(L"12345", parsed)); + BOOST_CHECK_EQUAL(L"n:12345", parsed); +} + +struct string_input_n { + const char* encoded; + const char* expected; +}; + +void string_parsed_correctly_n(string_input_n param) { + std::string parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_string(param.encoded, parsed)); + BOOST_CHECK_EQUAL(param.expected, parsed); +} + +const string_input_n strings_n[] = { + {"\"\"", ""}, + {"\"abc\"", "abc"}, + {"\"a\\nb\"", "a\nb"}, + {"\"\\\"\"", "\""}, + {"\"\\\\\"", "\\"}, + {"\"\\/\"", "/"}, + {"\"\\b\"", "\b"}, + {"\"\\f\"", "\f"}, + {"\"\\r\"", "\r"}, + {"\"\\t\"", "\t"}, + {"\"\\u0001\\u00f2\\u28Ec\"", "\x01" "\xC3\xB2" "\xE2\xA3\xAC"}, + {"\"\\ud801\\udc37\"", "\xf0\x90\x90\xb7"}, // U+10437 + {"\xef\xbb\xbf\"\"", ""} // BOM +}; + +BOOST_AUTO_TEST_CASE(uses_string_callbacks) +{ + std::string parsed; + prefixing_parser p; + BOOST_REQUIRE(p.parse_string("\"a\"", parsed)); + BOOST_CHECK_EQUAL("s:a", parsed); +} + +struct string_input_w { + const wchar_t* encoded; + const wchar_t* expected; +}; + +void string_parsed_correctly_w(string_input_w param) { + std::wstring parsed; + standard_parser p; + BOOST_REQUIRE(p.parse_string(param.encoded, parsed)); + BOOST_CHECK_EQUAL(param.expected, parsed); +} + +const string_input_w strings_w[] = { + {L"\"\"", L""}, + {L"\"abc\"", L"abc"}, + {L"\"a\\nb\"", L"a\nb"}, + {L"\"\\\"\"", L"\""}, + {L"\"\\\\\"", L"\\"}, + {L"\"\\/\"", L"/"}, + {L"\"\\b\"", L"\b"}, + {L"\"\\f\"", L"\f"}, + {L"\"\\r\"", L"\r"}, + {L"\"\\t\"", L"\t"}, + {L"\"\\u0001\\u00f2\\u28Ec\"", L"\x0001" L"\x00F2" L"\x28EC"}, + {L"\xfeff\"\"", L""} // BOM +}; + +BOOST_AUTO_TEST_CASE(empty_array) { + ptree tree; + standard_parser p; + const char* input = " [ ]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_CHECK_EQUAL("", tree.data()); + BOOST_CHECK_EQUAL(0u, tree.size()); +} + +BOOST_AUTO_TEST_CASE(array_gets_tagged) { + wptree tree; + prefixing_parser p; + const wchar_t* input = L" [ ]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_CHECK_EQUAL(L"a:", tree.data()); + BOOST_CHECK_EQUAL(0u, tree.size()); +} + +BOOST_AUTO_TEST_CASE(array_with_values) { + wptree tree; + standard_parser p; + const wchar_t* input = L"[\n" +L" 123, \"abc\" ,true ,\n" +L" null\n" +L" ]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_REQUIRE_EQUAL(4u, tree.size()); + wptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL(L"", it->first); + BOOST_CHECK_EQUAL(L"123", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"", it->first); + BOOST_CHECK_EQUAL(L"abc", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"", it->first); + BOOST_CHECK_EQUAL(L"true", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"", it->first); + BOOST_CHECK_EQUAL(L"null", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(array_values_get_tagged) { + ptree tree; + prefixing_parser p; + const char* input = "[\n" +" 123, \"abc\" ,true ,\n" +" null\n" +" ]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_REQUIRE_EQUAL(4u, tree.size()); + BOOST_CHECK_EQUAL("a:", tree.data()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("n:123", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("s:abc", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("b:true", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("_:null", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(nested_array) { + ptree tree; + standard_parser p; + const char* input = "[[1,2],3,[4,5]]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_REQUIRE_EQUAL(3u, tree.size()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("1", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("2", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("3", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("4", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("5", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(empty_object) { + ptree tree; + standard_parser p; + const char* input = " { }"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_CHECK_EQUAL("", tree.data()); + BOOST_CHECK_EQUAL(0u, tree.size()); +} + +BOOST_AUTO_TEST_CASE(object_gets_tagged) { + wptree tree; + prefixing_parser p; + const wchar_t* input = L" { }"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_CHECK_EQUAL(L"o:", tree.data()); + BOOST_CHECK_EQUAL(0u, tree.size()); +} + +BOOST_AUTO_TEST_CASE(object_with_values) { + wptree tree; + standard_parser p; + const wchar_t* input = L"{\n" +L" \"1\":123, \"2\"\n" +L" :\"abc\" ,\"3\": true ,\n" +L" \"4\" : null\n" +L" }"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_REQUIRE_EQUAL(4u, tree.size()); + wptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL(L"1", it->first); + BOOST_CHECK_EQUAL(L"123", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"2", it->first); + BOOST_CHECK_EQUAL(L"abc", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"3", it->first); + BOOST_CHECK_EQUAL(L"true", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(L"4", it->first); + BOOST_CHECK_EQUAL(L"null", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(object_values_get_tagged) { + ptree tree; + prefixing_parser p; + const char* input = "{\n" + "\"1\": 123, \"2\": \"abc\" ,\"3\": true ,\n" + "\"4\": null\n" + "}"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_REQUIRE_EQUAL(4u, tree.size()); + BOOST_CHECK_EQUAL("o:", tree.data()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("1", it->first); + BOOST_CHECK_EQUAL("n:123", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("2", it->first); + BOOST_CHECK_EQUAL("s:abc", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("3", it->first); + BOOST_CHECK_EQUAL("b:true", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("4", it->first); + BOOST_CHECK_EQUAL("_:null", it->second.data()); + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(nested_object) { + ptree tree; + standard_parser p; + const char* input = "{\"a\":{\"b\":1,\"c\":2},\"d\":3,\"e\":{\"f\":4,\"g\":5}}"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_REQUIRE_EQUAL(3u, tree.size()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("a", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("b", iit->first); + BOOST_CHECK_EQUAL("1", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("c", iit->first); + BOOST_CHECK_EQUAL("2", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL("d", it->first); + BOOST_CHECK_EQUAL("3", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("e", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("f", iit->first); + BOOST_CHECK_EQUAL("4", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("g", iit->first); + BOOST_CHECK_EQUAL("5", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(array_in_object) { + ptree tree; + standard_parser p; + const char* input = "{\"a\":[1,2],\"b\":3,\"c\":[4,5]}"; + BOOST_REQUIRE(p.parse_object(input, tree)); + BOOST_REQUIRE_EQUAL(3u, tree.size()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("a", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("1", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("2", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL("b", it->first); + BOOST_CHECK_EQUAL("3", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("c", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("4", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("5", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(object_in_array) { + ptree tree; + standard_parser p; + const char* input = "[{\"a\":1,\"b\":2},3,{\"c\":4,\"d\":5}]"; + BOOST_REQUIRE(p.parse_array(input, tree)); + BOOST_REQUIRE_EQUAL(3u, tree.size()); + ptree::iterator it = tree.begin(); + BOOST_CHECK_EQUAL("", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("a", iit->first); + BOOST_CHECK_EQUAL("1", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("b", iit->first); + BOOST_CHECK_EQUAL("2", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL("", it->first); + BOOST_CHECK_EQUAL("3", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("", it->first); + { + ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(2u, sub.size()); + ptree::iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("c", iit->first); + BOOST_CHECK_EQUAL("4", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("d", iit->first); + BOOST_CHECK_EQUAL("5", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +BOOST_AUTO_TEST_CASE(parser_works_with_input_iterators) { + const char* input = " {\n" +" \"1\":123, \"2\"\n" +" :\"abc\" ,\"3\": true ,\n" +" \"4\" : null, \"5\" : [ 1, 23\n" +" , 456 ]\n" +" }"; + + std::istringstream is(input); + typedef std::istreambuf_iterator iterator; + json_parser::detail::standard_callbacks callbacks; + json_parser::detail::utf8_utf8_encoding encoding; + json_parser::detail::parser, + json_parser::detail::utf8_utf8_encoding, + iterator, iterator> + p(callbacks, encoding); + + p.set_input("", boost::make_iterator_range(iterator(is), iterator())); + p.parse_value(); + + const ptree& tree = callbacks.output(); + BOOST_REQUIRE_EQUAL(5u, tree.size()); + ptree::const_iterator it = tree.begin(); + BOOST_CHECK_EQUAL("1", it->first); + BOOST_CHECK_EQUAL("123", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("2", it->first); + BOOST_CHECK_EQUAL("abc", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("3", it->first); + BOOST_CHECK_EQUAL("true", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("4", it->first); + BOOST_CHECK_EQUAL("null", it->second.data()); + ++it; + BOOST_CHECK_EQUAL("5", it->first); + { + const ptree& sub = it->second; + BOOST_CHECK_EQUAL("", sub.data()); + BOOST_REQUIRE_EQUAL(3u, sub.size()); + ptree::const_iterator iit = sub.begin(); + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("1", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("23", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL("", iit->first); + BOOST_CHECK_EQUAL("456", iit->second.data()); + ++iit; + BOOST_CHECK_EQUAL(sub.end(), iit); + } + ++it; + BOOST_CHECK_EQUAL(tree.end(), it); +} + +struct bad_parse_n { + const char* json; + const char* message_substring; +}; + +void parse_error_thrown_with_message_n(bad_parse_n param) { + try { + standard_parser p; + ptree dummy; + p.parse_value(param.json, dummy); + BOOST_FAIL("expected exception"); + } catch (json_parser::json_parser_error& e) { + std::string message = e.message(); + BOOST_CHECK_MESSAGE(message.find(param.message_substring) != + std::string::npos, + "bad error message on input '" << param.json + << "', need: '" << param.message_substring + << "' but found '" << message << "'"); + } +} + +const bad_parse_n errors_n[] = { + {"", "expected value"}, + {"(", "expected value"}, + + {"n", "expected 'null'"}, + {"nu", "expected 'null'"}, + {"nul", "expected 'null'"}, + {"n ", "expected 'null'"}, + {"nu ", "expected 'null'"}, + {"nul ", "expected 'null'"}, + {"nx", "expected 'null'"}, + {"nux", "expected 'null'"}, + {"nulx", "expected 'null'"}, + + {"t", "expected 'true'"}, + {"tr", "expected 'true'"}, + {"tu", "expected 'true'"}, + {"t ", "expected 'true'"}, + {"tr ", "expected 'true'"}, + {"tru ", "expected 'true'"}, + {"tx", "expected 'true'"}, + {"trx", "expected 'true'"}, + {"trux", "expected 'true'"}, + + {"f", "expected 'false'"}, + {"fa", "expected 'false'"}, + {"fal", "expected 'false'"}, + {"fals", "expected 'false'"}, + {"f ", "expected 'false'"}, + {"fa ", "expected 'false'"}, + {"fal ", "expected 'false'"}, + {"fals ", "expected 'false'"}, + {"fx", "expected 'false'"}, + {"fax", "expected 'false'"}, + {"falx", "expected 'false'"}, + {"falsx", "expected 'false'"}, + + {"-", "expected digits"}, + {"01", "garbage after data"}, + {"0.", "need at least one digit after '.'"}, + {"0e", "need at least one digit in exponent"}, + {"0e-", "need at least one digit in exponent"}, + + {"\"", "unterminated string"}, + {"\"asd", "unterminated string"}, + {"\"\n\"", "invalid code sequence"}, // control character + {"\"\xff\"", "invalid code sequence"}, // bad lead byte + {"\"\x80\"", "invalid code sequence"}, // stray trail byte + {"\"\xc0", "invalid code sequence"}, // eos after lead byte + {"\"\xc0\"", "invalid code sequence"}, // bad trail byte + {"\"\xc0m\"", "invalid code sequence"}, // also bad trail byte + {"\"\\", "invalid escape sequence"}, + {"\"\\p\"", "invalid escape sequence"}, + {"\"\\u", "invalid escape sequence"}, + {"\"\\u\"", "invalid escape sequence"}, + {"\"\\ug\"", "invalid escape sequence"}, + {"\"\\u1\"", "invalid escape sequence"}, + {"\"\\u1g\"", "invalid escape sequence"}, + {"\"\\u11\"", "invalid escape sequence"}, + {"\"\\u11g\"", "invalid escape sequence"}, + {"\"\\u111\"", "invalid escape sequence"}, + {"\"\\u111g\"", "invalid escape sequence"}, + {"\"\\ude00\"", "stray low surrogate"}, + {"\"\\ud900", "stray high surrogate"}, + {"\"\\ud900foo\"", "stray high surrogate"}, + {"\"\\ud900\\", "expected codepoint reference"}, + {"\"\\ud900\\n\"", "expected codepoint reference"}, + {"\"\\ud900\\u1000\"", "expected low surrogate"}, + + {"[", "expected value"}, + {"[1", "expected ']' or ','"}, + {"[1,", "expected value"}, + {"[1,]", "expected value"}, + {"[1}", "expected ']' or ','"}, + + {"{", "expected key string"}, + {"{1:2}", "expected key string"}, + {"{\"\"", "expected ':'"}, + {"{\"\"}", "expected ':'"}, + {"{\"\":", "expected value"}, + {"{\"\":}", "expected value"}, + {"{\"\":0", "expected '}' or ','"}, + {"{\"\":0]", "expected '}' or ','"}, + {"{\"\":0,", "expected key string"}, + {"{\"\":0,}", "expected key string"}, +}; + +struct bad_parse_w { + const wchar_t* json; + const char* message_substring; +}; + +void parse_error_thrown_with_message_w(bad_parse_w param) { + try { + standard_parser p; + wptree dummy; + p.parse_value(param.json, dummy); + BOOST_FAIL("expected exception"); + } catch (json_parser::json_parser_error& e) { + std::string message = e.message(); + BOOST_CHECK_MESSAGE(message.find(param.message_substring) != + std::string::npos, + "bad error message on input '" << param.json + << "', need: '" << param.message_substring + << "' but found '" << message << "'"); + } +} + +const bad_parse_w errors_w[] = { + {L"", "expected value"}, + {L"(", "expected value"}, + + {L"n", "expected 'null'"}, + {L"nu", "expected 'null'"}, + {L"nul", "expected 'null'"}, + {L"n ", "expected 'null'"}, + {L"nu ", "expected 'null'"}, + {L"nul ", "expected 'null'"}, + {L"nx", "expected 'null'"}, + {L"nux", "expected 'null'"}, + {L"nulx", "expected 'null'"}, + + {L"t", "expected 'true'"}, + {L"tr", "expected 'true'"}, + {L"tu", "expected 'true'"}, + {L"t ", "expected 'true'"}, + {L"tr ", "expected 'true'"}, + {L"tru ", "expected 'true'"}, + {L"tx", "expected 'true'"}, + {L"trx", "expected 'true'"}, + {L"trux", "expected 'true'"}, + + {L"f", "expected 'false'"}, + {L"fa", "expected 'false'"}, + {L"fal", "expected 'false'"}, + {L"fals", "expected 'false'"}, + {L"f ", "expected 'false'"}, + {L"fa ", "expected 'false'"}, + {L"fal ", "expected 'false'"}, + {L"fals ", "expected 'false'"}, + {L"fx", "expected 'false'"}, + {L"fax", "expected 'false'"}, + {L"falx", "expected 'false'"}, + {L"falsx", "expected 'false'"}, + + {L"-", "expected digits"}, + {L"01", "garbage after data"}, + {L"0.", "need at least one digit after '.'"}, + {L"0e", "need at least one digit in exponent"}, + {L"0e-", "need at least one digit in exponent"}, + + {L"\"", "unterminated string"}, + {L"\"asd", "unterminated string"}, + {L"\"\n\"", "invalid code sequence"}, // control character + // Encoding not known, so no UTF-16 encoding error tests. + {L"\"\\", "invalid escape sequence"}, + {L"\"\\p\"", "invalid escape sequence"}, + {L"\"\\u", "invalid escape sequence"}, + {L"\"\\u\"", "invalid escape sequence"}, + {L"\"\\ug\"", "invalid escape sequence"}, + {L"\"\\u1\"", "invalid escape sequence"}, + {L"\"\\u1g\"", "invalid escape sequence"}, + {L"\"\\u11\"", "invalid escape sequence"}, + {L"\"\\u11g\"", "invalid escape sequence"}, + {L"\"\\u111\"", "invalid escape sequence"}, + {L"\"\\u111g\"", "invalid escape sequence"}, + {L"\"\\ude00\"", "stray low surrogate"}, + {L"\"\\ud900", "stray high surrogate"}, + {L"\"\\ud900foo\"", "stray high surrogate"}, + {L"\"\\ud900\\", "expected codepoint reference"}, + {L"\"\\ud900\\n\"", "expected codepoint reference"}, + {L"\"\\ud900\\u1000\"", "expected low surrogate"}, + + {L"[", "expected value"}, + {L"[1", "expected ']' or ','"}, + {L"[1,", "expected value"}, + {L"[1,]", "expected value"}, + {L"[1}", "expected ']' or ','"}, + + {L"{", "expected key string"}, + {L"{1:2}", "expected key string"}, + {L"{\"\"", "expected ':'"}, + {L"{\"\"}", "expected ':'"}, + {L"{\"\":", "expected value"}, + {L"{\"\":}", "expected value"}, + {L"{\"\":0", "expected '}' or ','"}, + {L"{\"\":0]", "expected '}' or ','"}, + {L"{\"\":0,", "expected key string"}, + {L"{\"\":0,}", "expected key string"}, +}; + +template +std::size_t arraysize(T(&)[N]) { return N; } + +#define ARRAY_TEST_CASE(fn, a) \ + BOOST_PARAM_TEST_CASE(fn, (a), (a) + arraysize((a))) + +using namespace boost::unit_test; + +test_suite* init_unit_test_suite(int, char*[]) +{ + master_test_suite_t& ts = boost::unit_test::framework::master_test_suite(); + ts.add(ARRAY_TEST_CASE(boolean_parse_result_is_input_n, booleans_n)); + ts.add(ARRAY_TEST_CASE(boolean_parse_result_is_input_w, booleans_w)); + ts.add(ARRAY_TEST_CASE(number_parse_result_is_input_n, numbers_n)); + ts.add(ARRAY_TEST_CASE(number_parse_result_is_input_w, numbers_w)); + ts.add(ARRAY_TEST_CASE(string_parsed_correctly_n, strings_n)); + ts.add(ARRAY_TEST_CASE(string_parsed_correctly_w, strings_w)); + ts.add(ARRAY_TEST_CASE(parse_error_thrown_with_message_n, errors_n)); + ts.add(ARRAY_TEST_CASE(parse_error_thrown_with_message_w, errors_w)); + + return 0; +} diff --git a/src/boost/libs/property_tree/test/test_multi_module1.cpp b/src/boost/libs/property_tree/test/test_multi_module1.cpp new file mode 100644 index 000000000..3bb3bb455 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_multi_module1.cpp @@ -0,0 +1,21 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include + +void f(); + +int main() +{ + f(); +} diff --git a/src/boost/libs/property_tree/test/test_multi_module2.cpp b/src/boost/libs/property_tree/test/test_multi_module2.cpp new file mode 100644 index 000000000..6d35253f6 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_multi_module2.cpp @@ -0,0 +1,18 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include + +void f() +{ +} diff --git a/src/boost/libs/property_tree/test/test_property_tree.cpp b/src/boost/libs/property_tree/test/test_property_tree.cpp new file mode 100644 index 000000000..55ff5a830 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_property_tree.cpp @@ -0,0 +1,203 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- +#include "test_utils.hpp" +#include +#include +#include +#include +#include + +// If using VC, disable some warnings that trip in boost::serialization bowels +#ifdef BOOST_MSVC + #pragma warning(disable:4267) // Narrowing conversion + #pragma warning(disable:4996) // Deprecated functions +#endif + +#include +#include +#include +#include +#include +#include +#include + +// Predicate for sorting keys +template +struct SortPred +{ + bool operator()(const typename Ptree::value_type &v1, + const typename Ptree::value_type &v2) const + { + return v1.first < v2.first; + } +}; + +// Predicate for sorting keys in reverse +template +struct SortPredRev +{ + bool operator()(const typename Ptree::value_type &v1, + const typename Ptree::value_type &v2) const + { + return v1.first > v2.first; + } +}; + +// Custom translator that works with boost::any instead of std::string +template +struct any_translator +{ + typedef boost::any internal_type; + typedef E external_type; + + boost::optional get_value(const internal_type &v) { + if(const E *p = boost::any_cast(&v)) { + return *p; + } + return boost::optional(); + } + boost::optional put_value(const E &v) { + return boost::any(v); + } +}; + +namespace boost { namespace property_tree { + template + struct translator_between + { + typedef any_translator type; + }; +}} + +// Include char tests, case sensitive +#define CHTYPE char +#define T(s) s +#define PTREE boost::property_tree::ptree +#define NOCASE 0 +#define WIDECHAR 0 +# include "test_property_tree.hpp" +#undef CHTYPE +#undef T +#undef PTREE +#undef NOCASE +#undef WIDECHAR + +// Include wchar_t tests, case sensitive +#ifndef BOOST_NO_CWCHAR +# define CHTYPE wchar_t +# define T(s) L ## s +# define PTREE boost::property_tree::wptree +# define NOCASE 0 +# define WIDECHAR 1 +# include "test_property_tree.hpp" +# undef CHTYPE +# undef T +# undef PTREE +# undef NOCASE +# undef WIDECHAR +#endif + +// Include char tests, case insensitive +#define CHTYPE char +#define T(s) s +#define PTREE boost::property_tree::iptree +#define NOCASE 1 +# define WIDECHAR 0 +# include "test_property_tree.hpp" +#undef CHTYPE +#undef T +#undef PTREE +#undef NOCASE +#undef WIDECHAR + +// Include wchar_t tests, case insensitive +#ifndef BOOST_NO_CWCHAR +# define CHTYPE wchar_t +# define T(s) L ## s +# define PTREE boost::property_tree::wiptree +# define NOCASE 1 +# define WIDECHAR 1 +# include "test_property_tree.hpp" +# undef CHTYPE +# undef T +# undef PTREE +# undef NOCASE +# undef WIDECHAR +#endif + +template +void run_tests(Ptree* pt) +{ + test_debug(pt); + test_constructor_destructor_assignment(pt); + test_insertion(pt); + test_erasing(pt); + test_clear(pt); + test_pushpop(pt); + test_container_iteration(pt); + test_swap(pt); + test_sort_reverse(pt); + test_case(pt); + test_comparison(pt); + test_front_back(pt); + test_get_put(pt); + test_get_child_put_child(pt); + test_equal_range(pt); + test_path_separator(pt); + test_path(pt); + test_precision(pt); + test_locale(pt); + test_custom_data_type(pt); + test_empty_size_max_size(pt); + test_ptree_bad_path(pt); + test_ptree_bad_data(pt); + test_serialization(pt); + test_bool(pt); + test_char(pt); + test_float(pt); + test_sort(pt); + test_leaks(pt); // must be a final test +} + +int test_main(int, char *[]) +{ + + using namespace boost::property_tree; + + // char tests, case sensitive + { + ptree *pt = 0; + run_tests(pt); + } + + // wchar_t tests, case sensitive +#ifndef BOOST_NO_CWCHAR + { + wptree *pt = 0; + run_tests(pt); + } +#endif + + // char tests, case insensitive + { + iptree *pt = 0; + run_tests(pt); + } + + // wchar_t tests, case insensitive +#ifndef BOOST_NO_CWCHAR + { + wiptree *pt = 0; + run_tests(pt); + } +#endif + + return 0; +} diff --git a/src/boost/libs/property_tree/test/test_property_tree.hpp b/src/boost/libs/property_tree/test/test_property_tree.hpp new file mode 100644 index 000000000..4216945a1 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_property_tree.hpp @@ -0,0 +1,1355 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +// Intentionally no include guards (to be included more than once) + +#if !defined(CHTYPE) || !defined(T) || !defined(PTREE) || !defined(NOCASE) || !defined(WIDECHAR) +# error No character type specified +#endif + +void test_debug(PTREE *) +{ +#if 0 + // Check count + BOOST_CHECK(PTREE::debug_get_instances_count() == 0); + + { + + // Create ptrees + PTREE pt1, pt2; + BOOST_CHECK(PTREE::debug_get_instances_count() == 2); + + // Create PTREE + PTREE *pt3 = new PTREE; + BOOST_CHECK(PTREE::debug_get_instances_count() == 3); + + // Insert + pt1.push_back(std::make_pair(T("key"), *pt3)); + BOOST_CHECK(PTREE::debug_get_instances_count() == 4); + + // Insert + pt2.push_back(std::make_pair(T("key"), *pt3)); + BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + + // Clear + pt1.clear(); + BOOST_CHECK(PTREE::debug_get_instances_count() == 4); + + // Clear + pt2.clear(); + BOOST_CHECK(PTREE::debug_get_instances_count() == 3); + + // Delete + delete pt3; + BOOST_CHECK(PTREE::debug_get_instances_count() == 2); + + } + + // Check count + BOOST_CHECK(PTREE::debug_get_instances_count() == 0); +#endif +} + +void test_constructor_destructor_assignment(PTREE *) +{ + + { + + // Test constructor from string + PTREE pt1(T("data")); + BOOST_CHECK(pt1.data() == T("data")); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 1); + + // Do insertions + PTREE &tmp1 = pt1.put(T("key1"), T("data1")); + PTREE &tmp2 = pt1.put(T("key2"), T("data2")); + tmp1.put(T("key3"), T("data3")); + tmp2.put(T("key4"), T("data4")); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + + // Make a copy using copy constructor + PTREE *pt2 = new PTREE(pt1); + BOOST_CHECK(*pt2 == pt1); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 10); + + // Make a copy using = operator + PTREE *pt3 = new PTREE; + *pt3 = *pt2; + BOOST_CHECK(*pt3 == *pt2); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 15); + + // Test self assignment + pt1 = pt1; + BOOST_CHECK(pt1 == *pt2); + BOOST_CHECK(pt1 == *pt3); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 15); + + // Destroy + delete pt2; + //BOOST_CHECK(PTREE::debug_get_instances_count() == 10); + + // Destroy + delete pt3; + //BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + + } + + // Check count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 0); + +} + +void test_insertion(PTREE *) +{ + + // Do insertions + PTREE pt; + PTREE tmp1(T("data1")); + PTREE tmp2(T("data2")); + PTREE tmp3(T("data3")); + PTREE tmp4(T("data4")); + PTREE::iterator it1 = pt.insert(pt.end(), std::make_pair(T("key1"), tmp1)); + PTREE::iterator it2 = pt.insert(it1, std::make_pair(T("key2"), tmp2)); + PTREE::iterator it3 = it1->second.push_back(std::make_pair(T("key3"), tmp3)); + PTREE::iterator it4 = it1->second.push_front(std::make_pair(T("key4"), tmp4)); + it2->second.insert(it2->second.end(), it1->second.begin(), it1->second.end()); + + // Check instance count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 11); + + // Check contents + BOOST_CHECK(pt.get(T("key1"), T("")) == T("data1")); + BOOST_CHECK(pt.get(T("key2"), T("")) == T("data2")); + BOOST_CHECK(pt.get(T("key1.key3"), T("")) == T("data3")); + BOOST_CHECK(pt.get(T("key1.key4"), T("")) == T("data4")); + BOOST_CHECK(pt.get(T("key2.key3"), T("")) == T("data3")); + BOOST_CHECK(pt.get(T("key2.key4"), T("")) == T("data4")); + + // Check sequence + PTREE::iterator it = it2; + ++it; BOOST_CHECK(it == it1); + ++it; BOOST_CHECK(it == pt.end()); + it = it4; + ++it; BOOST_CHECK(it == it3); + ++it; BOOST_CHECK(it == it1->second.end()); + +} + +void test_erasing(PTREE *) +{ + + // Do insertions + PTREE pt; + PTREE tmp1(T("data1")); + PTREE tmp2(T("data2")); + PTREE tmp3(T("data3")); + PTREE tmp4(T("data4")); + PTREE::iterator it1 = pt.insert(pt.end(), std::make_pair(T("key1"), tmp1)); + PTREE::iterator it2 = pt.insert(it1, std::make_pair(T("key2"), tmp2)); + it1->second.push_back(std::make_pair(T("key"), tmp3)); + it1->second.push_front(std::make_pair(T("key"), tmp4)); + it2->second.insert(it2->second.end(), it1->second.begin(), it1->second.end()); + + // Check instance count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 11); + + // Test range erase + PTREE::iterator it = it1->second.erase(it1->second.begin(), it1->second.end()); + BOOST_CHECK(it == it1->second.end()); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 9); + + // Test single erase + PTREE::size_type n = pt.erase(T("key1")); + BOOST_CHECK(n == 1); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 8); + + // Test multiple erase + n = it2->second.erase(T("key")); + BOOST_CHECK(n == 2); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + + // Test one more erase + n = pt.erase(T("key2")); + BOOST_CHECK(n == 1); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + +} + +void test_clear(PTREE *) +{ + + // Do insertions + PTREE pt(T("data")); + pt.push_back(std::make_pair(T("key"), PTREE(T("data")))); + + // Check instance count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 2); + + // Test clear + pt.clear(); + BOOST_CHECK(pt.empty()); + BOOST_CHECK(pt.data().empty()); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 1); + +} + +void test_pushpop(PTREE *) +{ + + // Do insertions + PTREE pt; + PTREE tmp1(T("data1")); + PTREE tmp2(T("data2")); + PTREE tmp3(T("data3")); + PTREE tmp4(T("data4")); + pt.push_back(std::make_pair(T("key3"), tmp3)); + pt.push_front(std::make_pair(T("key2"), tmp2)); + pt.push_back(std::make_pair(T("key4"), tmp4)); + pt.push_front(std::make_pair(T("key1"), tmp1)); + + // Check instance count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 9); + + // Check sequence + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it == pt.end()); + + // Test pops + pt.pop_back(); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 8); + BOOST_CHECK(pt.front().second.data() == T("data1")); + BOOST_CHECK(pt.back().second.data() == T("data3")); + pt.pop_front(); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 7); + BOOST_CHECK(pt.front().second.data() == T("data2")); + BOOST_CHECK(pt.back().second.data() == T("data3")); + pt.pop_back(); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + BOOST_CHECK(pt.front().second.data() == T("data2")); + BOOST_CHECK(pt.back().second.data() == T("data2")); + pt.pop_front(); + //BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + BOOST_CHECK(pt.empty()); + +} + +void test_container_iteration(PTREE *) +{ + + // Do insertions + PTREE pt; + pt.put(T("key3"), T("")); + pt.put(T("key1"), T("")); + pt.put(T("key4"), T("")); + pt.put(T("key2"), T("")); + + // iterator + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it == pt.end()); + } + + // const_iterator + { + PTREE::const_iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it == pt.end()); + } + + // reverse_iterator + { + PTREE::reverse_iterator it = pt.rbegin(); + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it == pt.rend()); + } + + // const_reverse_iterator + { + PTREE::const_reverse_iterator it = pt.rbegin(); + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it == PTREE::const_reverse_iterator(pt.rend())); + } + +} + +void test_swap(PTREE *) +{ + + PTREE pt1, pt2; + + // Do insertions + pt1.put(T("key1"), T("")); + pt1.put(T("key2"), T("")); + pt1.put(T("key1.key3"), T("")); + pt1.put(T("key1.key4"), T("")); + + // Check counts + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + BOOST_CHECK(pt1.size() == 2); + BOOST_CHECK(pt1.get_child(T("key1")).size() == 2); + BOOST_CHECK(pt2.size() == 0); + + // Swap using member function + pt1.swap(pt2); + + // Check counts + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + BOOST_CHECK(pt2.size() == 2); + BOOST_CHECK(pt2.get_child(T("key1")).size() == 2); + BOOST_CHECK(pt1.size() == 0); + + // Swap using free function + swap(pt1, pt2); + + // Check counts + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + BOOST_CHECK(pt1.size() == 2); + BOOST_CHECK(pt1.get_child(T("key1")).size() == 2); + BOOST_CHECK(pt2.size() == 0); + + // Swap using std algorithm + std::swap(pt1, pt2); + + // Check counts + //BOOST_CHECK(PTREE::debug_get_instances_count() == 6); + BOOST_CHECK(pt2.size() == 2); + BOOST_CHECK(pt2.get_child(T("key1")).size() == 2); + BOOST_CHECK(pt1.size() == 0); + +} + +void test_sort_reverse(PTREE *) +{ + + PTREE pt; + + // Do insertions + pt.put(T("key2"), T("data2")); + pt.put(T("key1"), T("data1")); + pt.put(T("key4"), T("data4")); + pt.put(T("key3"), T("data3")); + pt.put(T("key3.key1"), T("")); + pt.put(T("key4.key2"), T("")); + + // Reverse + pt.reverse(); + + // Check sequence + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it == pt.end()); + } + // Check sequence using find to check if index is ok + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key3")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key4")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key1")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key2")))); ++it; + BOOST_CHECK(it == pt.end()); + } + + // Sort + pt.sort(SortPred()); + + // Check sequence + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it == pt.end()); + } + // Check sequence (using find to check if index is ok) + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key1")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key2")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key3")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key4")))); ++it; + BOOST_CHECK(it == pt.end()); + } + + // Sort reverse + pt.sort(SortPredRev()); + + // Check sequence + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it->first == T("key4")); ++it; + BOOST_CHECK(it->first == T("key3")); ++it; + BOOST_CHECK(it->first == T("key2")); ++it; + BOOST_CHECK(it->first == T("key1")); ++it; + BOOST_CHECK(it == pt.end()); + } + // Check sequence (using find to check if index is ok) + { + PTREE::iterator it = pt.begin(); + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key4")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key3")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key2")))); ++it; + BOOST_CHECK(it == pt.to_iterator(pt.find(T("key1")))); ++it; + BOOST_CHECK(it == pt.end()); + } + +} + +void test_case(PTREE *) +{ + + // Do insertions + PTREE pt; + pt.put(T("key1"), T("data1")); + pt.put(T("KEY2"), T("data2")); + pt.put(T("kEy1.keY3"), T("data3")); + pt.put(T("KEY1.key4"), T("data4")); + + // Check findings depending on traits type +#if (NOCASE == 0) + //BOOST_CHECK(PTREE::debug_get_instances_count() == 7); + BOOST_CHECK(pt.get(T("key1"), T("")) == T("data1")); + BOOST_CHECK(pt.get(T("key2"), T("")) == T("")); + BOOST_CHECK(pt.get(T("key1.key3"), T("")) == T("")); + BOOST_CHECK(pt.get(T("KEY1.key4"), T("")) == T("data4")); +#else + //BOOST_CHECK(PTREE::debug_get_instances_count() == 5); + BOOST_CHECK(pt.get(T("key1"), T("1")) == pt.get(T("KEY1"), T("2"))); + BOOST_CHECK(pt.get(T("key2"), T("1")) == pt.get(T("KEY2"), T("2"))); + BOOST_CHECK(pt.get(T("key1.key3"), T("1")) == pt.get(T("KEY1.KEY3"), T("2"))); + BOOST_CHECK(pt.get(T("key1.key4"), T("1")) == pt.get(T("KEY1.KEY4"), T("2"))); +#endif + + // Do more insertions + pt.push_back(PTREE::value_type(T("key1"), PTREE())); + pt.push_back(PTREE::value_type(T("key1"), PTREE())); + + // Test counts +#if (NOCASE == 0) + BOOST_CHECK(pt.count(T("key1")) == 3); + BOOST_CHECK(pt.count(T("KEY1")) == 1); + BOOST_CHECK(pt.count(T("key2")) == 0); + BOOST_CHECK(pt.count(T("KEY2")) == 1); + BOOST_CHECK(pt.count(T("key3")) == 0); + BOOST_CHECK(pt.count(T("KEY3")) == 0); +#else + BOOST_CHECK(pt.count(T("key1")) == 3); + BOOST_CHECK(pt.count(T("KEY1")) == 3); + BOOST_CHECK(pt.count(T("key2")) == 1); + BOOST_CHECK(pt.count(T("KEY2")) == 1); + BOOST_CHECK(pt.count(T("key3")) == 0); + BOOST_CHECK(pt.count(T("KEY3")) == 0); +#endif + +} + +void test_comparison(PTREE *) +{ + + // Prepare original + PTREE pt_orig(T("data")); + pt_orig.put(T("key1"), T("data1")); + pt_orig.put(T("key1.key3"), T("data2")); + pt_orig.put(T("key1.key4"), T("data3")); + pt_orig.put(T("key2"), T("data4")); + + // Test originals + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + BOOST_CHECK(pt1 == pt2); + BOOST_CHECK(pt2 == pt1); + BOOST_CHECK(!(pt1 != pt2)); + BOOST_CHECK(!(pt2 != pt1)); + } + + // Test originals with modified case +#if (NOCASE != 0) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.pop_back(); + pt1.put(T("KEY2"), T("data4")); + BOOST_CHECK(pt1 == pt2); + BOOST_CHECK(pt2 == pt1); + BOOST_CHECK(!(pt1 != pt2)); + BOOST_CHECK(!(pt2 != pt1)); + } +#endif + + // Test modified copies (both modified the same way) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.put(T("key1.key5"), T(".")); + pt2.put(T("key1.key5"), T(".")); + BOOST_CHECK(pt1 == pt2); + BOOST_CHECK(pt2 == pt1); + BOOST_CHECK(!(pt1 != pt2)); + BOOST_CHECK(!(pt2 != pt1)); + } + + // Test modified copies (modified root data) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.data() = T("a"); + pt2.data() = T("b"); + BOOST_CHECK(!(pt1 == pt2)); + BOOST_CHECK(!(pt2 == pt1)); + BOOST_CHECK(pt1 != pt2); + BOOST_CHECK(pt2 != pt1); + } + + // Test modified copies (added subkeys with different data) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.put(T("key1.key5"), T("a")); + pt2.put(T("key1.key5"), T("b")); + BOOST_CHECK(!(pt1 == pt2)); + BOOST_CHECK(!(pt2 == pt1)); + BOOST_CHECK(pt1 != pt2); + BOOST_CHECK(pt2 != pt1); + } + + // Test modified copies (added subkeys with different keys) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.put(T("key1.key5"), T("")); + pt2.put(T("key1.key6"), T("")); + BOOST_CHECK(!(pt1 == pt2)); + BOOST_CHECK(!(pt2 == pt1)); + BOOST_CHECK(pt1 != pt2); + BOOST_CHECK(pt2 != pt1); + } + + // Test modified copies (added subkey to only one copy) + { + PTREE pt1(pt_orig); + PTREE pt2(pt_orig); + pt1.put(T("key1.key5"), T("")); + BOOST_CHECK(!(pt1 == pt2)); + BOOST_CHECK(!(pt2 == pt1)); + BOOST_CHECK(pt1 != pt2); + BOOST_CHECK(pt2 != pt1); + } + +} + +void test_front_back(PTREE *) +{ + + // Do insertions + PTREE pt; + pt.put(T("key1"), T("")); + pt.put(T("key2"), T("")); + + // Check front and back + BOOST_CHECK(pt.front().first == T("key1")); + BOOST_CHECK(pt.back().first == T("key2")); + +} + +void test_get_put(PTREE *) +{ + + typedef std::basic_string str_t; + + // Temporary storage + str_t tmp_string; + boost::optional opt_int; + boost::optional opt_long; + boost::optional opt_double; + boost::optional opt_float; + boost::optional opt_string; + boost::optional opt_char; + boost::optional opt_bool; + + // Do insertions via put + PTREE pt; + PTREE &pt1 = pt.put(T("k1"), 1); + PTREE &pt2 = pt.put(T("k2.k"), 2.5); + PTREE &pt3 = pt.put(T("k3.k.k"), T("ala ma kota")); + PTREE &pt4 = pt.put(T("k4.k.k.k"), CHTYPE('c')); + PTREE &pt5 = pt.put(T("k5.k.k.k.f"), false); + PTREE &pt6 = pt.put(T("k5.k.k.k.t"), true); + + // Check instances count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 17); + + // Check if const char * version returns std::string + BOOST_CHECK(typeid(pt.get_value(T(""))) == typeid(str_t)); + + // Do extractions via get (throwing version) + BOOST_CHECK(pt.get(T("k1")) == 1); + BOOST_CHECK(pt.get(T("k1")) == 1); + BOOST_CHECK(pt.get(T("k2.k")) == 2.5); + BOOST_CHECK(pt.get(T("k2.k")) == 2.5f); + BOOST_CHECK(pt.get(T("k3.k.k")) == str_t(T("ala ma kota"))); + BOOST_CHECK(pt.get(T("k4.k.k.k")) == CHTYPE('c')); + BOOST_CHECK(pt.get(T("k5.k.k.k.f")) == false); + BOOST_CHECK(pt.get(T("k5.k.k.k.t")) == true); + + // Do extractions via get (default value version) + BOOST_CHECK(pt.get(T("k1"), 0) == 1); + BOOST_CHECK(pt.get(T("k1"), 0L) == 1); + BOOST_CHECK(pt.get(T("k2.k"), 0.0) == 2.5); + BOOST_CHECK(pt.get(T("k2.k"), 0.0f) == 2.5f); + BOOST_CHECK(pt.get(T("k3.k.k"), str_t()) == str_t(T("ala ma kota"))); + BOOST_CHECK(pt.get(T("k3.k.k"), T("")) == T("ala ma kota")); + BOOST_CHECK(pt.get(T("k4.k.k.k"), CHTYPE('\0')) == CHTYPE('c')); + BOOST_CHECK(pt.get(T("k5.k.k.k.f"), true) == false); + BOOST_CHECK(pt.get(T("k5.k.k.k.t"), false) == true); + + // Do extractions via get (optional version) + opt_int = pt.get_optional(T("k1")); + BOOST_CHECK(opt_int && *opt_int == 1); + opt_long = pt.get_optional(T("k1")); + BOOST_CHECK(opt_long && *opt_long == 1); + opt_double = pt.get_optional(T("k2.k")); + BOOST_CHECK(opt_double && *opt_double == 2.5); + opt_float = pt.get_optional(T("k2.k")); + BOOST_CHECK(opt_float && *opt_float == 2.5f); + opt_string = pt.get_optional(T("k3.k.k")); + BOOST_CHECK(opt_string && *opt_string == str_t(T("ala ma kota"))); + opt_char = pt.get_optional(T("k4.k.k.k")); + BOOST_CHECK(opt_char && *opt_char == CHTYPE('c')); + opt_bool = pt.get_optional(T("k5.k.k.k.f")); + BOOST_CHECK(opt_bool && *opt_bool == false); + opt_bool = pt.get_optional(T("k5.k.k.k.t")); + BOOST_CHECK(opt_bool && *opt_bool == true); + + // Do insertions via put_value + pt1.put_value(short(1)); + pt2.put_value(2.5f); + pt3.put_value(str_t(T("ala ma kota"))); + pt4.put_value(CHTYPE('c')); + pt5.put_value(false); + pt6.put_value(true); + + // Do extractions via get_value (throwing version) + BOOST_CHECK(pt1.get_value() == 1); + BOOST_CHECK(pt1.get_value() == 1); + BOOST_CHECK(pt2.get_value() == 2.5); + BOOST_CHECK(pt2.get_value() == 2.5f); + BOOST_CHECK(pt3.get_value() == str_t(T("ala ma kota"))); + BOOST_CHECK(pt4.get_value() == CHTYPE('c')); + BOOST_CHECK(pt5.get_value() == false); + BOOST_CHECK(pt6.get_value() == true); + + // Do extractions via get_value (default value version) + BOOST_CHECK(pt1.get_value(0) == 1); + BOOST_CHECK(pt1.get_value(0L) == 1); + BOOST_CHECK(pt2.get_value(0.0) == 2.5); + BOOST_CHECK(pt2.get_value(0.0f) == 2.5f); + BOOST_CHECK(pt3.get_value(str_t()) == str_t(T("ala ma kota"))); + BOOST_CHECK(pt3.get_value(T("")) == T("ala ma kota")); + BOOST_CHECK(pt4.get_value(CHTYPE('\0')) == CHTYPE('c')); + BOOST_CHECK(pt5.get_value(true) == false); + BOOST_CHECK(pt6.get_value(false) == true); + + // Do extractions via get_value (optional version) + opt_int = pt1.get_value_optional(); + BOOST_CHECK(opt_int && *opt_int == 1); + opt_long = pt1.get_value_optional(); + BOOST_CHECK(opt_long && *opt_long == 1); + opt_double = pt2.get_value_optional(); + BOOST_CHECK(opt_double && *opt_double == 2.5); + opt_float = pt2.get_value_optional(); + BOOST_CHECK(opt_float && *opt_float == 2.5f); + opt_string = pt3.get_value_optional(); + BOOST_CHECK(opt_string && *opt_string == str_t(T("ala ma kota"))); + opt_char = pt4.get_value_optional(); + BOOST_CHECK(opt_char && *opt_char == CHTYPE('c')); + opt_bool = pt5.get_value_optional(); + BOOST_CHECK(opt_bool && *opt_bool == false); + opt_bool = pt6.get_value_optional(); + BOOST_CHECK(opt_bool && *opt_bool == true); + + // Do incorrect extractions (throwing version) + try + { + pt.get(T("k2.k.bogus.path")); + BOOST_ERROR("No required exception thrown"); + } + catch (boost::property_tree::ptree_bad_path &) { } + catch (...) + { + BOOST_ERROR("Wrong exception type thrown"); + } + try + { + pt.get(T("k2.k")); + BOOST_ERROR("No required exception thrown"); + } + catch (boost::property_tree::ptree_bad_data &) { } + catch (...) + { + BOOST_ERROR("Wrong exception type thrown"); + } + + // Do incorrect extractions (default value version) + BOOST_CHECK(pt.get(T("k2.k"), -7) == -7); + BOOST_CHECK(pt.get(T("k3.k.k"), -7) == -7); + BOOST_CHECK(pt.get(T("k4.k.k.k"), -7) == -7); + + // Do incorrect extractions (optional version) + BOOST_CHECK(!pt.get_optional(T("k2.k"))); + BOOST_CHECK(!pt.get_optional(T("k3.k.k"))); + BOOST_CHECK(!pt.get_optional(T("k4.k.k.k"))); + + // Test multiple puts with the same key + { + PTREE pt; + pt.put(T("key"), 1); + BOOST_CHECK(pt.get(T("key")) == 1); + BOOST_CHECK(pt.size() == 1); + pt.put(T("key"), 2); + BOOST_CHECK(pt.get(T("key")) == 2); + BOOST_CHECK(pt.size() == 1); + pt.put(T("key.key.key"), 1); + PTREE &child = pt.get_child(T("key.key")); + BOOST_CHECK(pt.get(T("key.key.key")) == 1); + BOOST_CHECK(child.size() == 1); + BOOST_CHECK(child.count(T("key")) == 1); + pt.put(T("key.key.key"), 2); + BOOST_CHECK(pt.get(T("key.key.key")) == 2); + BOOST_CHECK(child.size() == 1); + BOOST_CHECK(child.count(T("key")) == 1); + } + + // Test multiple puts with the same key + { + PTREE pt; + pt.put(T("key"), 1); + BOOST_CHECK(pt.get(T("key")) == 1); + BOOST_CHECK(pt.size() == 1); + pt.put(T("key"), 2); + BOOST_CHECK(pt.get(T("key")) == 2); + BOOST_CHECK(pt.size() == 1); + pt.put(T("key.key.key"), 1); + PTREE &child = pt.get_child(T("key.key")); + BOOST_CHECK(child.size() == 1); + BOOST_CHECK(child.count(T("key")) == 1); + pt.add(T("key.key.key"), 2); + BOOST_CHECK(child.size() == 2); + BOOST_CHECK(child.count(T("key")) == 2); + } + + // Test that put does not destroy children + { + PTREE pt; + pt.put(T("key1"), 1); + pt.put(T("key1.key2"), 2); + BOOST_CHECK(pt.get(T("key1"), 0) == 1); + BOOST_CHECK(pt.get(T("key1.key2"), 0) == 2); + pt.put(T("key1"), 2); + BOOST_CHECK(pt.get(T("key1"), 0) == 2); + BOOST_CHECK(pt.get(T("key1.key2"), 0) == 2); + } + + // Test that get of single character that is whitespace works + { + PTREE pt; + pt.put_value(T(' ')); + CHTYPE ch = pt.get_value(); + BOOST_CHECK(ch == T(' ')); + } + + // Test that get of non-char value with trailing and leading whitespace works + { + PTREE pt; + pt.put_value(T(" \t\n99 \t\n")); + BOOST_CHECK(pt.get_value(0) == 99); + } + +} + +void test_get_child_put_child(PTREE *) +{ + PTREE pt(T("ala ma kota")); + + // Do insertions via put_child + PTREE pt1, pt2, pt3; + pt1.put_child(T("k1"), PTREE()); + pt1.put_child(T("k2.k"), PTREE()); + pt2.put_child(T("k1"), pt); + pt2.put_child(T("k2.k"), pt); + + // Const references to test const versions of methods + const PTREE &cpt1 = pt1, &cpt2 = pt2; + + // Do correct extractions via get_child (throwing version) + BOOST_CHECK(pt1.get_child(T("k1")).empty()); + BOOST_CHECK(pt1.get_child(T("k2.k")).empty()); + BOOST_CHECK(pt2.get_child(T("k1")) == pt); + BOOST_CHECK(pt2.get_child(T("k2.k")) == pt); + BOOST_CHECK(cpt1.get_child(T("k1")).empty()); + BOOST_CHECK(cpt1.get_child(T("k2.k")).empty()); + BOOST_CHECK(cpt2.get_child(T("k1")) == pt); + BOOST_CHECK(cpt2.get_child(T("k2.k")) == pt); + + // Do correct extractions via get_child (default value version) + BOOST_CHECK(pt1.get_child(T("k1"), PTREE(T("def"))) != PTREE(T("def"))); + BOOST_CHECK(pt1.get_child(T("k2.k"), PTREE(T("def"))) != PTREE(T("def"))); + BOOST_CHECK(pt2.get_child(T("k1"), PTREE(T("def"))) == pt); + BOOST_CHECK(pt2.get_child(T("k2.k"), PTREE(T("def"))) == pt); + BOOST_CHECK(cpt1.get_child(T("k1"), PTREE(T("def"))) != PTREE(T("def"))); + BOOST_CHECK(cpt1.get_child(T("k2.k"), PTREE(T("def"))) != PTREE(T("def"))); + BOOST_CHECK(cpt2.get_child(T("k1"), PTREE(T("def"))) == pt); + BOOST_CHECK(cpt2.get_child(T("k2.k"), PTREE(T("def"))) == pt); + + // Do correct extractions via get_child (optional version) + boost::optional opt; + boost::optional copt; + opt = pt1.get_child_optional(T("k1")); + BOOST_CHECK(opt); + opt = pt1.get_child_optional(T("k2.k")); + BOOST_CHECK(opt); + opt = pt2.get_child_optional(T("k1")); + BOOST_CHECK(opt && *opt == pt); + opt = pt2.get_child_optional(T("k2.k")); + BOOST_CHECK(opt && *opt == pt); + copt = cpt1.get_child_optional(T("k1")); + BOOST_CHECK(copt); + copt = cpt1.get_child_optional(T("k2.k")); + BOOST_CHECK(copt); + copt = cpt2.get_child_optional(T("k1")); + BOOST_CHECK(copt && *copt == pt); + copt = cpt2.get_child_optional(T("k2.k")); + BOOST_CHECK(copt && *copt == pt); + + // Do incorrect extractions via get_child (throwing version) + try + { + pt.get_child(T("k2.k.bogus.path")); + BOOST_ERROR("No required exception thrown"); + } + catch (boost::property_tree::ptree_bad_path &) { } + catch (...) + { + BOOST_ERROR("Wrong exception type thrown"); + } + + // Do incorrect extractions via get_child (default value version) + BOOST_CHECK(&pt.get_child(T("k2.k.bogus.path"), pt3) == &pt3); + + // Do incorrect extractions via get_child (optional version) + BOOST_CHECK(!pt.get_child_optional(T("k2.k.bogus.path"))); + + // Test multiple puts with the same key + { + PTREE pt, tmp1(T("data1")), tmp2(T("data2")); + pt.put_child(T("key"), tmp1); + BOOST_CHECK(pt.get_child(T("key")) == tmp1); + BOOST_CHECK(pt.size() == 1); + pt.put_child(T("key"), tmp2); + BOOST_CHECK(pt.get_child(T("key")) == tmp2); + BOOST_CHECK(pt.size() == 1); + pt.put_child(T("key.key.key"), tmp1); + PTREE &child = pt.get_child(T("key.key")); + BOOST_CHECK(child.size() == 1); + pt.put_child(T("key.key.key"), tmp2); + BOOST_CHECK(child.size() == 1); + } + + // Test multiple adds with the same key + { + PTREE pt, tmp1(T("data1")), tmp2(T("data2")); + pt.add_child(T("key"), tmp1); + BOOST_CHECK(pt.size() == 1); + pt.add_child(T("key"), tmp2); + BOOST_CHECK(pt.size() == 2); + BOOST_CHECK(pt.count(T("key")) == 2); + pt.add_child(T("key.key.key"), tmp1); + PTREE &child = pt.get_child(T("key.key")); + BOOST_CHECK(child.size() == 1); + BOOST_CHECK(child.count(T("key")) == 1); + pt.add_child(T("key.key.key"), tmp2); + BOOST_CHECK(child.size() == 2); + BOOST_CHECK(child.count(T("key")) == 2); + } + + // Test assigning child to tree + { + PTREE pt; + pt.put(T("foo.bar"), T("baz")); + pt = pt.get_child(T("foo")); + BOOST_CHECK(pt.size() == 1); + BOOST_CHECK(pt.get< std::basic_string >(T("bar")) == T("baz")); + } + +} + +void test_equal_range(PTREE *) +{ + PTREE pt; + pt.add_child(T("k1"), PTREE()); + pt.add_child(T("k2"), PTREE()); + pt.add_child(T("k1"), PTREE()); + pt.add_child(T("k3"), PTREE()); + pt.add_child(T("k1"), PTREE()); + pt.add_child(T("k2"), PTREE()); + + BOOST_CHECK(boost::distance(pt.equal_range(T("k1"))) == 3); + BOOST_CHECK(boost::distance(pt.equal_range(T("k2"))) == 2); + BOOST_CHECK(boost::distance(pt.equal_range(T("k3"))) == 1); +} + +void test_path_separator(PTREE *) +{ + + typedef PTREE::path_type path; + + // Check instances count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 0); + + // Do insertions + PTREE pt; + pt.put(T("key1"), T("1")); + pt.put(T("key2.key"), T("2")); + pt.put(T("key3.key.key"), T("3")); + pt.put(path(T("key4"), CHTYPE('/')), T("4")); + pt.put(path(T("key5/key"), CHTYPE('/')), T("5")); + pt.put(path(T("key6/key/key"), CHTYPE('/')), T("6")); + + // Check instances count + //BOOST_CHECK(PTREE::debug_get_instances_count() == 13); + + // Do correct extractions + BOOST_CHECK(pt.get(T("key1"), 0) == 1); + BOOST_CHECK(pt.get(T("key2.key"), 0) == 2); + BOOST_CHECK(pt.get(T("key3.key.key"), 0) == 3); + BOOST_CHECK(pt.get(path(T("key4"), CHTYPE('/')), 0) == 4); + BOOST_CHECK(pt.get(path(T("key5/key"), CHTYPE('/')), 0) == 5); + BOOST_CHECK(pt.get(path(T("key6/key/key"), CHTYPE('/')), 0) == 6); + + // Do incorrect extractions + BOOST_CHECK(pt.get(T("key2/key"), 0) == 0); + BOOST_CHECK(pt.get(T("key3/key/key"), 0) == 0); + BOOST_CHECK(pt.get(path(T("key5.key"), CHTYPE('/')), 0) == 0); + BOOST_CHECK(pt.get(path(T("key6.key.key"), CHTYPE('/')), 0) == 0); + +} + +void test_path(PTREE *) +{ + + typedef PTREE::path_type path; + + // Insert + PTREE pt; + pt.put(T("key1.key2.key3"), 1); + + // Test operator /= + { + path p; + p /= T("key1"); p /= T("key2"); p /= T("key3"); + BOOST_CHECK(pt.get(p, 0) == 1); + } + + // Test operator /= + { + path p(T("key1")); + p /= T("key2.key3"); + BOOST_CHECK(pt.get(p, 0) == 1); + } + + // Test operator /= + { + path p; + path p1(T("key1.key2")); + path p2(T("key3")); + p /= p1; + p /= p2; + BOOST_CHECK(pt.get(p, 0) == 1); + } + + // Test operator / + { + path p = path(T("key1")) / T("key2.key3"); + BOOST_CHECK(pt.get(p, 0) == 1); + } + + // Test operator / + { + path p = T("key1.key2") / path(T("key3")); + BOOST_CHECK(pt.get(p, 0) == 1); + } + +} + +void test_precision(PTREE *) +{ + + typedef double real; + + // Quite precise PI value + real pi = real(3.1415926535897932384626433832795028841971); + + // Put and get + PTREE pt; + pt.put_value(pi); + real pi2 = pt.get_value(); + + // Test if precision is "good enough", i.e. if stream precision increase worked + using namespace std; + real error = abs(pi - pi2) * + pow(real(numeric_limits::radix), + real(numeric_limits::digits)); + BOOST_CHECK(error < 100); + +} + +void test_locale(PTREE *) +{ + typedef boost::property_tree::translator_between< + std::basic_string, double>::type translator; + try + { + + // Write strings in english and french locales + PTREE pt; +#ifdef BOOST_WINDOWS + std::locale loc_english("english"); + std::locale loc_french("french"); +#else + std::locale loc_english("en_GB"); + std::locale loc_french("fr_FR"); +#endif + pt.put(T("english"), 1.234, translator(loc_english)); + pt.put(T("french"), 1.234, translator(loc_french)); + + // Test contents + BOOST_CHECK(pt.get(T("english")) == T("1.234")); + BOOST_CHECK(pt.get(T("french")) == T("1,234")); + + } + catch (boost::property_tree::ptree_error &) + { + throw; + } + catch (std::runtime_error &e) + { + std::cerr << "Required locale not supported by the platform. " + "Skipping locale tests (caught std::runtime_error with message " << + e.what() << ").\n"; + } + +} + +void test_custom_data_type(PTREE *) +{ + + typedef std::basic_string Str; + typedef PTREE::key_compare Comp; + + // Property_tree with boost::any as data type + typedef boost::property_tree::basic_ptree my_ptree; + my_ptree pt; + + // Put/get int value + pt.put(T("int value"), 3); + int int_value = pt.get(T("int value")); + BOOST_CHECK(int_value == 3); + + // Put/get string value + pt.put >(T("string value"), T("foo bar")); + std::basic_string string_value = pt.get >(T("string value")); + BOOST_CHECK(string_value == T("foo bar")); + + // Put/get list value + int list_data[] = { 1, 2, 3, 4, 5 }; + pt.put >(T("list value"), std::list(list_data, list_data + sizeof(list_data) / sizeof(*list_data))); + std::list list_value = pt.get >(T("list value")); + BOOST_CHECK(list_value.size() == 5); + BOOST_CHECK(list_value.front() == 1); + BOOST_CHECK(list_value.back() == 5); + +} + +void test_empty_size_max_size(PTREE *) +{ + + PTREE pt; + BOOST_CHECK(pt.max_size()); + BOOST_CHECK(pt.empty()); + BOOST_CHECK(pt.size() == 0); + + pt.put(T("test1"), 1); + BOOST_CHECK(pt.max_size()); + BOOST_CHECK(!pt.empty()); + BOOST_CHECK(pt.size() == 1); + + pt.put(T("test2"), 2); + BOOST_CHECK(pt.max_size()); + BOOST_CHECK(!pt.empty()); + BOOST_CHECK(pt.size() == 2); + +} + +void test_ptree_bad_path(PTREE *) +{ + + PTREE pt; + + try + { + pt.get(T("non.existent.path")); + } + catch (boost::property_tree::ptree_bad_path &e) + { + PTREE::path_type path = e.path(); + std::string what = e.what(); + BOOST_CHECK(what.find("non.existent.path") != std::string::npos); + return; + } + + BOOST_ERROR("No required exception thrown"); + +} + +void test_ptree_bad_data(PTREE *) +{ + + PTREE pt; + pt.put_value("non convertible to int"); + + try + { + pt.get_value(); + } + catch (boost::property_tree::ptree_bad_data &e) + { + PTREE::data_type data = e.data(); + std::string what = e.what(); + // FIXME: Bring back what translation or make it more clear that it + // doesn't work. + //BOOST_CHECK(what.find("non convertible to int") != std::string::npos); + return; + } + + BOOST_ERROR("No required exception thrown"); +} + +void test_serialization(PTREE *) +{ + + // Prepare test tree + PTREE pt; + pt.put_value(1); + pt.put(T("key1"), 3); + pt.put(T("key1.key11)"), 3.3); + pt.put(T("key1.key12"), T("foo")); + pt.put(T("key2"), true); + pt.put(T("key2.key21.key211.key2111.key21111"), T("super deep!")); + pt.put_child(T("empty"), PTREE()); + pt.put(T("loooooong"), PTREE::data_type(10000, CHTYPE('a'))); + + // Endforce const for input + const PTREE &pt1 = pt; + + // Test text archives + { + std::stringstream stream; + boost::archive::text_oarchive oa(stream); + oa & pt1; + boost::archive::text_iarchive ia(stream); + PTREE pt2; + ia & pt2; + BOOST_CHECK(pt1 == pt2); + } + + // Test binary archives + { + std::stringstream stream; + boost::archive::binary_oarchive oa(stream); + oa & pt1; + boost::archive::binary_iarchive ia(stream); + PTREE pt2; + ia & pt2; + BOOST_CHECK(pt1 == pt2); + } + + // Test XML archives + { + std::stringstream stream; + boost::archive::xml_oarchive oa(stream); + oa & boost::serialization::make_nvp("pt", pt1); + boost::archive::xml_iarchive ia(stream); + PTREE pt2; + ia & boost::serialization::make_nvp("pt", pt2); + BOOST_CHECK(pt1 == pt2); + } + +} + +void test_bool(PTREE *) +{ + + // Prepare test tree + PTREE pt; + pt.put(T("bool.false.1"), false); + pt.put(T("bool.false.2"), T("0")); + pt.put(T("bool.true.1"), true); + pt.put(T("bool.true.2"), 1); + pt.put(T("bool.invalid.1"), T("")); + pt.put(T("bool.invalid.2"), T("tt")); + pt.put(T("bool.invalid.3"), T("ff")); + pt.put(T("bool.invalid.4"), T("2")); + pt.put(T("bool.invalid.5"), T("-1")); + + // Test false + for (PTREE::iterator it = pt.get_child(T("bool.false")).begin(); it != pt.get_child(T("bool.false")).end(); ++it) + BOOST_CHECK(it->second.get_value() == false); + + // Test true + for (PTREE::iterator it = pt.get_child(T("bool.true")).begin(); it != pt.get_child(T("bool.true")).end(); ++it) + BOOST_CHECK(it->second.get_value() == true); + + // Test invalid + for (PTREE::iterator it = pt.get_child(T("bool.invalid")).begin(); it != pt.get_child(T("bool.invalid")).end(); ++it) + { + BOOST_CHECK(it->second.get_value(false) == false); + BOOST_CHECK(it->second.get_value(true) == true); + } + +} + +void test_char(PTREE *) +{ + typedef signed char schar; + typedef unsigned char uchar; + + // Prepare test tree + PTREE pt; +#if WIDECHAR == 0 + pt.put(T("char"), char('A')); +#endif + pt.put(T("signed char"), static_cast('A')); + pt.put(T("unsigned char"), static_cast('A')); + pt.put(T("signed char min"), (std::numeric_limits::min)()); + pt.put(T("signed char max"), (std::numeric_limits::max)()); + pt.put(T("signed char underflow"), + static_cast((std::numeric_limits::min)()) - 1); + pt.put(T("signed char overflow"), + static_cast((std::numeric_limits::max)()) + 1); + pt.put(T("unsigned char min"), (std::numeric_limits::min)()); + pt.put(T("unsigned char max"), (std::numeric_limits::max)()); + pt.put(T("unsigned char overflow"), + static_cast((std::numeric_limits::max)()) + 1); + + // Verify normal conversions +#if WIDECHAR == 0 + BOOST_CHECK(pt.get(T("char")) == 'A'); +#endif + BOOST_CHECK(pt.get(T("signed char")) == static_cast('A')); + BOOST_CHECK(pt.get(T("unsigned char")) == static_cast('A')); + + // Verify that numbers are saved for signed and unsigned char + BOOST_CHECK(pt.get(T("signed char")) == int('A')); + BOOST_CHECK(pt.get(T("unsigned char")) == int('A')); + + // Verify ranges + BOOST_CHECK(pt.get(T("signed char min")) == + (std::numeric_limits::min)()); + BOOST_CHECK(pt.get(T("signed char max")) == + (std::numeric_limits::max)()); + BOOST_CHECK(pt.get(T("unsigned char min")) == + (std::numeric_limits::min)()); + BOOST_CHECK(pt.get(T("unsigned char max")) == + (std::numeric_limits::max)()); + + // Check that out-of-range throws. + try { + pt.get(T("signed char underflow")); + BOOST_ERROR("expected ptree_bad_data, but nothing was thrown"); + } catch (boost::property_tree::ptree_bad_data&) { + } catch (...) { + BOOST_ERROR("expected ptree_bad_data, but something else was thrown"); + } + try { + pt.get(T("signed char overflow")); + BOOST_ERROR("expected ptree_bad_data, but nothing was thrown"); + } catch (boost::property_tree::ptree_bad_data&) { + } catch (...) { + BOOST_ERROR("expected ptree_bad_data, but something else was thrown"); + } + try { + pt.get(T("unsigned char overflow")); + BOOST_ERROR("expected ptree_bad_data, but nothing was thrown"); + } catch (boost::property_tree::ptree_bad_data&) { + } catch (...) { + BOOST_ERROR("expected ptree_bad_data, but something else was thrown"); + } +} + +void test_float(PTREE*) +{ + const double difficult = -183.12345000098765e-10; + PTREE pt; + pt.put(T("num"), difficult); + double result = pt.get(T("num")); + + BOOST_CHECK(!(result < difficult || result > difficult)); +} + +void test_sort(PTREE *) +{ + PTREE pt; + pt.put(T("one"), T("v1")); + pt.put(T("two"), T("v2")); + pt.put(T("three"), T("v3")); + pt.put(T("four"), T("v4")); + + pt.sort(); + + PTREE::iterator it = pt.begin(); + BOOST_CHECK(std::distance(it, pt.end()) == 4); + BOOST_CHECK(it->first == T("four")); + BOOST_CHECK(it->second.data() == T("v4")); + ++it; + BOOST_CHECK(it->first == T("one")); + BOOST_CHECK(it->second.data() == T("v1")); + ++it; + BOOST_CHECK(it->first == T("three")); + BOOST_CHECK(it->second.data() == T("v3")); + ++it; + BOOST_CHECK(it->first == T("two")); + BOOST_CHECK(it->second.data() == T("v2")); +} + +void test_leaks(PTREE *) +{ + //BOOST_CHECK(PTREE::debug_get_instances_count() == 0); +} diff --git a/src/boost/libs/property_tree/test/test_utils.hpp b/src/boost/libs/property_tree/test/test_utils.hpp new file mode 100644 index 000000000..809f91f57 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_utils.hpp @@ -0,0 +1,252 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2005 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- +#ifndef BOOST_PROPERTY_TREE_TEST_UTILS_INCLUDED +#define BOOST_PROPERTY_TREE_TEST_UTILS_INCLUDED + +#define BOOST_PROPERTY_TREE_DEBUG // Enable ptree debugging +#include + +// Do not deprecate insecure CRT calls on VC8 +#if (defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) && !defined(_CRT_SECURE_NO_DEPRECATE)) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include +#include +#include +#include +#include + +template +typename Ptree::size_type total_size(const Ptree &pt) +{ + typename Ptree::size_type size = 1; + for (typename Ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) + size += total_size(it->second); + return size; +} + +template +typename Ptree::size_type total_keys_size(const Ptree &pt) +{ + typename Ptree::size_type size = 0; + for (typename Ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) + { + size += it->first.size(); + size += total_keys_size(it->second); + } + return size; +} + +template +typename Ptree::size_type total_data_size(const Ptree &pt) +{ + typename Ptree::size_type size = pt.data().size(); + for (typename Ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) + size += total_data_size(it->second); + return size; +} + +class test_file +{ +public: + test_file(const char *test_data, const char *filename) + { + if (test_data && filename) + { + name = filename; + std::ofstream stream(name.c_str()); + using namespace std; + stream.write(test_data, strlen(test_data)); + BOOST_CHECK(stream.good()); + } + } + ~test_file() + { + if (!name.empty()) + remove(name.c_str()); + } +private: + std::string name; +}; + +template +Ptree get_test_ptree() +{ + using namespace boost::property_tree; + typedef typename Ptree::key_type Str; + Ptree pt; + pt.put_value(detail::widen("data0")); + pt.put(detail::widen("key1"), detail::widen("data1")); + pt.put(detail::widen("key1.key"), detail::widen("data2")); + pt.put(detail::widen("key2"), detail::widen("data3")); + pt.put(detail::widen("key2.key"), detail::widen("data4")); + return pt; +} + +// Generic test for file parser +template +void generic_parser_test(Ptree &pt, + ReadFunc rf, + WriteFunc wf, + const char *test_data_1, + const char *test_data_2, + const char *filename_1, + const char *filename_2, + const char *filename_out) +{ + + using namespace boost::property_tree; + + // Create test files + test_file file_1(test_data_1, filename_1); + test_file file_2(test_data_2, filename_2); + test_file file_out("", filename_out); + + rf(filename_1, pt); // Read file + wf(filename_out, pt); // Write file + Ptree pt2; + rf(filename_out, pt2); // Read file again + + // Compare original with read + BOOST_CHECK(pt == pt2); + +} + +// Generic test for file parser with expected success +template +void generic_parser_test_ok(ReadFunc rf, + WriteFunc wf, + const char *test_data_1, + const char *test_data_2, + const char *filename_1, + const char *filename_2, + const char *filename_out, + unsigned int total_size, + unsigned int total_data_size, + unsigned int total_keys_size) +{ + + using namespace boost::property_tree; + + std::cerr << "(progress) Starting generic parser test with test file \"" << filename_1 << "\"\n"; + + // Make sure no instances exist + //BOOST_CHECK(Ptree::debug_get_instances_count() == 0); + + try + { + + // Read file + Ptree pt; + generic_parser_test(pt, rf, wf, + test_data_1, test_data_2, + filename_1, filename_2, filename_out); + + // Determine total sizes + typename Ptree::size_type actual_total_size = ::total_size(pt); + typename Ptree::size_type actual_data_size = ::total_data_size(pt); + typename Ptree::size_type actual_keys_size = ::total_keys_size(pt); + if (actual_total_size != total_size || + actual_data_size != total_data_size || + actual_keys_size != total_keys_size) + std::cerr << "Sizes: " << (unsigned)::total_size(pt) << ", " << (unsigned)::total_data_size(pt) << ", " << (unsigned)::total_keys_size(pt) << "\n"; + + // Check total sizes + BOOST_CHECK(actual_total_size == total_size); + BOOST_CHECK(actual_data_size == total_data_size); + BOOST_CHECK(actual_keys_size == total_keys_size); + + } + catch (std::runtime_error &e) + { + BOOST_ERROR(e.what()); + } + + // Test for leaks + //BOOST_CHECK(Ptree::debug_get_instances_count() == 0); + +} + +// Generic test for file parser with expected error +template +void generic_parser_test_error(ReadFunc rf, + WriteFunc wf, + const char *test_data_1, + const char *test_data_2, + const char *filename_1, + const char *filename_2, + const char *filename_out, + unsigned long expected_error_line) +{ + + std::cerr << "(progress) Starting generic parser test with test file \"" << filename_1 << "\"\n"; + + // Make sure no instances exist + //BOOST_CHECK(Ptree::debug_get_instances_count() == 0); + + { + + // Create ptree as a copy of test ptree (to test if read failure does not damage ptree) + Ptree pt(get_test_ptree()); + try + { + generic_parser_test(pt, rf, wf, + test_data_1, test_data_2, + filename_1, filename_2, filename_out); + BOOST_ERROR("No required exception thrown"); + } + catch (Error &e) + { + BOOST_CHECK(e.line() == expected_error_line); // Test line number + BOOST_CHECK(pt == get_test_ptree()); // Test if error damaged contents + } + catch (...) + { + BOOST_ERROR("Invalid exception type thrown"); + throw; + } + + } + + // Test for leaks + //BOOST_CHECK(Ptree::debug_get_instances_count() == 0); + +} + +template std::basic_ostream& errstream(); +template <> inline +std::basic_ostream& errstream() { return std::cerr; } +#ifndef BOOST_NO_CWCHAR +template <> inline +std::basic_ostream& errstream() { return std::wcerr; } +#endif + +template +void check_exact_roundtrip(ReadFunc rf, WriteFunc wf, const char *test_data) { + std::cerr << "(progress) Starting exact roundtrip test with test data:\n" + << test_data << "\n-----\n"; + using namespace boost::property_tree; + typedef typename Ptree::key_type::value_type Ch; + typedef typename Ptree::key_type Str; + Str native_test_data = detail::widen(test_data); + + std::basic_istringstream in_stream(native_test_data); + std::basic_ostringstream out_stream; + Ptree tree; + rf(in_stream, tree); + wf(out_stream, tree); + std::cerr << "(progress) Roundtripped data:\n"; + errstream() << out_stream.str(); + std::cerr << "\n-----\n"; + BOOST_CHECK(native_test_data == out_stream.str()); +} + +#endif diff --git a/src/boost/libs/property_tree/test/test_xml_parser_common.hpp b/src/boost/libs/property_tree/test/test_xml_parser_common.hpp new file mode 100644 index 000000000..019773bb6 --- /dev/null +++ b/src/boost/libs/property_tree/test/test_xml_parser_common.hpp @@ -0,0 +1,150 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- +#ifndef TEST_XML_PARSER_COMMON_HPP_INCLUDED +#define TEST_XML_PARSER_COMMON_HPP_INCLUDED + +#include "test_utils.hpp" +#include +#include "xml_parser_test_data.hpp" + +struct ReadFuncWS +{ + template + void operator()(const std::string &filename, Ptree &pt) const + { + boost::property_tree::read_xml(filename, pt, + boost::property_tree::xml_parser::no_concat_text); + } +}; + +struct WriteFuncWS +{ + template + void operator()(const std::string &filename, const Ptree &pt) const + { + boost::property_tree::write_xml(filename, pt); + } +}; + +struct ReadFuncNS +{ + template + void operator()(const std::string &filename, Ptree &pt) const + { + boost::property_tree::read_xml(filename, pt, + boost::property_tree::xml_parser::trim_whitespace); + } +}; + +struct WriteFuncNS +{ + template + void operator()(const std::string &filename, const Ptree &pt) const + { + boost::property_tree::write_xml(filename, pt, std::locale(), + boost::property_tree::xml_writer_make_settings(' ', 4)); + } +}; + +template int umlautsize(); +template <> inline int umlautsize() { return 2; } +template <> inline int umlautsize() { return 1; } + +template +void test_xml_parser() +{ + + using namespace boost::property_tree; + typedef typename Ptree::data_type::value_type char_type; + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), ok_data_1, NULL, + "testok1.xml", NULL, "testok1out.xml", 2, 0, 5 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), ok_data_2, NULL, + "testok2a.xml", NULL, "testok2aout.xml", 15, 23, 89 + ); + + generic_parser_test_ok + ( + ReadFuncNS(), WriteFuncNS(), ok_data_2, NULL, + "testok2b.xml", NULL, "testok2bout.xml", 6, 15, 8 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), ok_data_3, NULL, + "testok3a.xml", NULL, "testok3aout.xml", 1662, 35377, 11706 + ); + + generic_parser_test_ok + ( + ReadFuncNS(), WriteFuncNS(), ok_data_3, NULL, + "testok3b.xml", NULL, "testok3bout.xml", 787, 31376, 3831 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), ok_data_4, NULL, + "testok4.xml", NULL, "testok4out.xml", 11, 7, 74 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), ok_data_5, NULL, + "testok5.xml", NULL, "testok5out.xml", + 3, umlautsize(), 12 + ); + + generic_parser_test_error + ( + ReadFuncWS(), WriteFuncWS(), error_data_1, NULL, + "testerr1.xml", NULL, "testerr1out.xml", 1 + ); + + generic_parser_test_error + ( + ReadFuncWS(), WriteFuncWS(), error_data_2, NULL, + "testerr2.xml", NULL, "testerr2out.xml", 2 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), bug_data_pr2855, NULL, + "testpr2855.xml", NULL, "testpr2855out.xml", 3, 7, 14 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), bug_data_pr1678, NULL, + "testpr1678.xml", NULL, "testpr1678out.xml", 2, 0, 4 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), bug_data_pr5203, NULL, + "testpr5203.xml", NULL, "testpr5203out.xml", + 3, 4 * umlautsize(), 13 + ); + + generic_parser_test_ok + ( + ReadFuncWS(), WriteFuncWS(), bug_data_pr4840, NULL, + "testpr4840.xml", NULL, "testpr4840out.xml", + 4, 13, 15 + ); + +} + +#endif diff --git a/src/boost/libs/property_tree/test/test_xml_parser_rapidxml.cpp b/src/boost/libs/property_tree/test/test_xml_parser_rapidxml.cpp new file mode 100644 index 000000000..ed24e5fcb --- /dev/null +++ b/src/boost/libs/property_tree/test/test_xml_parser_rapidxml.cpp @@ -0,0 +1,34 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// Copyright (C) 2009-2010 Sebastian Redl +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#include "test_xml_parser_common.hpp" +#include +#define BOOST_UTF8_BEGIN_NAMESPACE namespace boost { namespace property_tree { +#define BOOST_UTF8_END_NAMESPACE }} +#define BOOST_UTF8_DECL +#include +#include + +int test_main(int argc, char *argv[]) +{ + using namespace boost::property_tree; + test_xml_parser(); + test_xml_parser(); +#ifndef BOOST_NO_CWCHAR + using std::locale; + // We need a UTF-8-aware global locale now. + locale loc(locale(), new utf8_codecvt_facet); + locale::global(loc); + test_xml_parser(); + test_xml_parser(); +#endif + return 0; +} diff --git a/src/boost/libs/property_tree/test/xml_parser_test_data.hpp b/src/boost/libs/property_tree/test/xml_parser_test_data.hpp new file mode 100644 index 000000000..d0b4c071c --- /dev/null +++ b/src/boost/libs/property_tree/test/xml_parser_test_data.hpp @@ -0,0 +1,777 @@ +// ---------------------------------------------------------------------------- +// Copyright (C) 2002-2006 Marcin Kalicinski +// Copyright (C) 2009 Sebastian Redl +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// For more information, see www.boost.org +// ---------------------------------------------------------------------------- + +#ifndef XML_PARSER_TEST_DATA_HPP_INCLUDED +#define XML_PARSER_TEST_DATA_HPP_INCLUDED + +/////////////////////////////////////////////////////////////////////////////// +// Test data + +// Correct +const char *ok_data_1 = + "\n" + ""; + +// Correct +const char *ok_data_2 = + "\n" + "\n" + "<>&\n" + "1<2>3&4\n" + " < > & \n" + " \n" + "\n"; + +// Correct +const char *ok_data_3 = + "\n" + "\n" + "\n" + "]>\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "
\n" + "\t<![CDATA[XML Linking Language (XLink)]]>\n" + "\tVersion 1.0\n" + "\tWD-xlink-19990527\n" + "\tWorld Wide Web Consortium Working Draft\n" + "\t29May1999\n" + "\t\n" + "\t

This draft is for public discussion.

\n" + "\t
\n" + "\thttp://www.w3.org/XML/Group/1999/05/WD-xlink-current\n" + "\t\n" + "\t\n" + "\thttp://www.w3.org/XML/Group/1999/05/WD-xlink-19990527\n" + "\thttp://www.w3.org/XML/Group/1999/05/WD-xlink-19990505\n" + "\thttp://www.w3.org/TR/1998/WD-xlink-19980303\n" + "\thttp://www.w3.org/TR/WD-xml-link-970630\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " Steve DeRose\n" + " Inso Corp. and Brown University\n" + " Steven_DeRose@Brown.edu\n" + " \n" + " \n" + " David Orchard\n" + " IBM Corp.\n" + " dorchard@ca.ibm.com\n" + " \n" + " \n" + " Ben Trafford\n" + " Invited Expert\n" + " bent@exemplary.net\n" + " \n" + " \n" + " \n" + "\n" + " \n" + "

This is a W3C Working Draft for review by W3C members and other interested parties. It is a draft document and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use W3C Working Drafts as reference material or to cite them as other than \"work in progress\". A list of current W3C working drafts can be found at http://www.w3.org/TR.

\n" + "

Note: Since working drafts are subject to frequent change, you are advised to reference the above URI, rather than the URIs for working drafts themselves. Some of the work remaining is described in .

\n" + "

This work is part of the W3C XML Activity (for current status, see http://www.w3.org/XML/Activity ). For information about the XPointer language which is expected to be used with XLink, see http://www.w3.org/TR/WD-xptr.\n" + "

\n" + "

See http://www.w3.org/TR/NOTE-xlink-principles for additional background on the design principles informing XLink.

\n" + "

Also see http://www.w3.org/TR/NOTE-xlink-req/ for the XLink requirements that this document attempts to satisfy.

\n" + "
\n" + "\n" + " \n" + " \n" + "

This specification defines constructs that may be inserted into XML DTDs, schemas and document instances to describe links between objects. It uses XML syntax to create structures that can describe the simple unidirectional hyperlinks of today's HTML as well as more sophisticated links.

\n" + "
\n" + "\n" + " \n" + "

Burlington, Seekonk, et al.: World-Wide Web Consortium, XML Working Group, 1998.

\n" + "
\n" + "\n" + " \n" + "

Created in electronic form.

\n" + "
\n" + "\n" + " \n" + " English\n" + " Extended Backus-Naur Form (formal grammar)\n" + " \n" + "\n" + " \n" + " \n" + " 1997-01-15 : Skeleton draft by TB\n" + " 1997-01-24 : Fleshed out by sjd\n" + " 1997-04-08 : Substantive draft\n" + " 1997-06-30 : Public draft\n" + " 1997-08-01 : Public draft\n" + " 1997-08-05 : Prose/organization work by sjd\n" + " 1997-10-14: Conformance and design principles; a bit of cleanup by elm\n" + " 1997-11-07: Update for editorial issues per issues doc, by sjd.\n" + " 1997-12-01: Update for editorial issues per issues doc in preparation for F2F meeting, by sjd.\n" + " 1998-01-13: Editorial cleanup, addition of new design principles, by elm.\n" + " 1998-02-27: Splitting out of XLink and XPointer, by elm.\n" + " 1998-03-03: Moved most of the XPointer locator stuff here. elm\n" + " 1999-04-24: Editorial rewrites to represent new ideas on XLink, especially the inclusion of arcs. bent\n" + " 1999-05-05: Prose/organization work by dorchard. Moved much of the semantics section around, from: locators, link semantics, remote resource semantics, local resource semantics; to: resource semantics, locators, behavior semantics, link semantics, arc semantics\n" + " 1999-05-12: Prose/organization work. Re-organized some of the sections, removed XML constructs from the document, added descriptive prose, edited document text for clarity. Rewrote the link recognition section. bent\n" + " 1999-05-17: Further prose work. Added non-normative examples. Clarified arcs. bent\n" + " 1999-05-23: Edited for grammar and clarity. bent\n" + " 1999-05-27: Final once-over before sending to group. Fixed sjd's email address. bent\n" + " \n" + " \n" + "
\n" + "\n" + "\n" + " \n" + " Introduction\n" + "

This specification defines constructs that may be inserted into XML DTDs, schemas, and document instances to describe links between objects. A link, as the term is used here, is an explicit relationship between two or more data objects or portions of data objects. This specification is concerned with the syntax used to assert link existence and describe link characteristics. Implicit (unasserted) relationships, for example that of one word to the next or that of a word in a text to its entry in an on-line dictionary are obviously important, but outside its scope.

\n" + "

Links are asserted by elements contained in XML document instances. The simplest case is very like an HTML A link, and has these characteristics:\n" + " \n" + "

The link is expressed at one of its ends (similar to the A element in some document)

\n" + "

Users can only initiate travel from that end to the other

\n" + "

The link's effect on windows, frames, go-back lists, stylesheets in use, and so on is mainly determined by browsers, not by the link itself. For example, traveral of A links normally replaces the current view, perhaps with a user option to open a new window.

\n" + "

The link goes to only one destination (although a server may have great freedom in finding or dynamically creating that destination).

\n" + " \n" + "

\n" + "

While this set of characteristics is already very powerful and obviously has proven itself highly useful and effective, each of these assumptions also limits the range of hypertext functionality. The linking model defined here provides ways to create links that go beyond each of these specific characteristics, thus providing features previously available mostly in dedicated hypermedia systems.\n" + "

\n" + "\n" + "\n" + " Origin and Goals\n" + "

Following is a summary of the design principles governing XLink:\n" + " \n" + "

XLink must be straightforwardly usable over the Internet.

\n" + "

XLink must be usable by a wide variety of link usage domains and classes of linking application software.

\n" + "

XLink must support HTML 4.0 linking constructs.

\n" + "

The XLink expression language must be XML.

\n" + "

The XLink design must be formal, concise, and illustrative.

\n" + "

XLinks must be human-readable and human-writable.

\n" + "

XLinks may reside within or outside the documents in which the\n" + " participating resources reside.

\n" + "

XLink must represent the abstract structure and significance of links.

\n" + "

XLink must be feasible to implement.

\n" + "

XLink must be informed by knowledge of established hypermedia systems and standards.

\n" + " \n" + "

\n" + "
\n" + "\n" + "\n" + "\n" + " Relationship to Existing Standards\n" + "

Three standards have been especially influential:\n" + " \n" + "

HTML: Defines several SGML element types that represent links.

\n" + "

HyTime: Defines inline and out-of-line link structures and some semantic features, including traversal control and presentation of objects. \n" + "

\n" + "

Text Encoding Initiative Guidelines (TEI P3): Provides structures for creating links, aggregate objects, and link collections out of them.

\n" + " \n" + "

\n" + "

Many other linking systems have also informed this design, especially Dexter, FRESS, MicroCosm, and InterMedia.

\n" + "
\n" + "\n" + "\n" + " Terminology\n" + "

The following basic terms apply in this document. \n" + " \n" + " \n" + " \n" + "

A symbolic representation of traversal behavior in links, especially the direction, context and timing of traversal.

\n" + " \n" + " \n" + " \n" + "

A representation of the relevant structure specified by the tags and attributes in an XML document, based on \"groves\" as defined in the ISO DSSSL standard.

\n" + "
\n" + " \n" + " \n" + "

Abstractly, a link which serves as one of its own resources. Concretely, a link where the content of the linking element serves as a participating resource.\n" + " HTML A, HyTime clink, and TEI XREF\n" + " are all inline links.

\n" + "
\n" + " \n" + " \n" + "

An explicit relationship between two or more data objects or portions of data objects.

\n" + "
\n" + " \n" + " \n" + "

An element that asserts the existence and describes the characteristics of a link.

\n" + "
\n" + " \n" + " \n" + "

The content of an inlinelinking element. Note that the content of the linking element could be explicitly pointed to by means of a regular locator in the same linking element, in which case the resource is considered remote, not local.

\n" + "
\n" + " \n" + " \n" + "

Data, provided as part of a link, which identifies a\n" + " resource.

\n" + "
\n" + " \n" + " \n" + "

A link whose traversal can be initiated from more than one of its participating resources. Note that being able to \"go back\" after following a one-directional link does not make the link multidirectional.

\n" + "
\n" + " \n" + " \n" + "

A link whose content does not serve as one of the link's participating resources . Such links presuppose a notion like extended link groups, which instruct application software where to look for links. Out-of-line links are generally required for supporting multidirectional traversal and for allowing read-only resources to have outgoing links.

\n" + "
\n" + " \n" + "

In the context of link behavior, a parsed link is any link whose content is transcluded into the document where the link originated. The use of the term \"parsed\" directly refers to the concept in XML of a\n" + " parsed entity.

\n" + "
\n" + " \n" + " \n" + "

A resource that belongs to a link. All resources are potential contributors to a link; participating resources are the actual contributors to a particular link.

\n" + "
\n" + " \n" + " \n" + "

Any participating resource of a link that is pointed to with a locator.

\n" + "
\n" + " \n" + " \n" + "

In the abstract sense, an addressable unit of information or service that is participating in a link. Examples include files, images, documents, programs, and query results. Concretely, anything reachable by the use of a locator in some linking element. Note that this term and its definition are taken from the basic specifications governing the World Wide Web. \n" + "

\n" + "
\n" + " \n" + " \n" + "

A portion of a resource, pointed to as the precise destination of a link. As one example, a link might specify that an entire document be retrieved and displayed, but that some specific part(s) of it is the specific linked data, to be treated in an application-appropriate manner such as indication by highlighting, scrolling, etc.

\n" + "
\n" + " \n" + " \n" + "

The action of using a link; that is, of accessing a resource. Traversal may be initiated by a user action (for example, clicking on the displayed content of a linking element) or occur under program control.

\n" + "
\n" + " \n" + "

\n" + "
\n" + "\n" + "\n" + " Notation\n" + "

The formal grammar for locators is given using a simple Extended Backus-Naur Form (EBNF) location, as described in the XML specification.

\n" + " \n" + "
\n" + "
\n" + "\n" + "\n" + " Locator Syntax\n" + "

The locator for a resource is typically provided by means of a Uniform Resource Identifier, or URI. XPointers can be used in conjunction with the URI structure, as fragment identifiers, to specify a more precise sub-resource.

\n" + " \n" + "

A locator generally contains a URI, as described in IETF RFCs and . As these RFCs state, the URI may include a trailing query (marked by a leading \"?\"), and be followed by a \"#\" and a fragment identifier, with the query interpreted by the host providing the indicated resource, and the interpretation of the fragment identifier dependent on the data type of the indicated resource.

\n" + " \n" + "

In order to locate XML documents and portions of documents, a locator value may contain either a URI or a fragment identifier, or both. Any fragment identifier for pointing into XML must be an XPointer.

\n" + "

Special syntax may be used to request the use of particular processing models in accessing the locator's resource. This is designed to reflect the realities of network operation, where it may or may not be desirable to exercise fine control over the distribution of work between local and remote processors. \n" + " \n" + " Locator\n" + " \n" + " Locator\n" + " URI\n" + " | Connector (XPointer | Name)\n" + " | URI Connector (XPointer | Name)\n" + " \n" + " \n" + " Connector'#' | '|'\n" + " \n" + " \n" + " URIURIchar*\n" + " \n" + " \n" + "

\n" + "

In this discussion, the term designated resource refers to the resource which an entire locator serves to locate. The following rules apply:\n" + " \n" + " \n" + "

The URI, if provided, locates a resource called the containing resource.

\n" + " \n" + " \n" + "

If the URI is not provided, the containing resource is considered to be the document in which the linking element is contained. \n" + "

\n" + " \n" + "

If an XPointer is provided, the designated resource is a sub-resource\n" + " of the containing resource; otherwise the designated resource is the\n" + " containing resource.

\n" + "
\n" + " \n" + " \n" + "

If the Connector is followed directly by a Name, the Name is shorthand for the XPointer\"id(Name)\"; that is, the sub-resource is the element in the containing resource that has an XML ID attribute whose value matches the Name. This shorthand is to encourage use of the robust id addressing mode.

\n" + "
\n" + " \n" + " \n" + "

If the connector is \"#\", this signals an intent that the containing resource is to be fetched as a whole from the host that provides it, and that the XPointer processing to extract the sub-resource\n" + " is to be performed on the client, that is to say on the same system where the linking element is recognized and processed.

\n" + "
\n" + " \n" + "

If the connector is \"|\", no intent is signaled as to what processing model is to be used to go about accessing the designated resource.

\n" + "
\n" + " \n" + "

\n" + "

Note that the definition of a URI includes an optional query component.

\n" + "

In the case where the URI contains a query (to be interpreted by the server), information providers and authors of server software are urged to use queries as follows: \n" + " \n" + " Query\n" + " \n" + " Query'XML-XPTR=' ( XPointer | Name)\n" + " \n" + " \n" + "

\n" + " \n" + "
\n" + "\n" + "\n" + " Link Recognition\n" + "

The existence of a link is asserted by a linking element. Linking elements must be recognized reliably by application software in order to provide appropriate display and behavior. There are several ways link recognition could be accomplished: for example, reserving element type names, reserving attributes names, leaving the matter of recognition entirely up to stylesheets and application software, or using the XLink namespace to specify element names and attribute names that would be recognized by namespace and XLink-aware processors. Using element and attribute names within the XLink namespace provides a balance between giving users control of their own markup language design and keeping the identification of linking elements simple and unambiguous.

\n" + "

The two approaches to identifying linking elements are relatively simple to implement. For example, here's how the HTML A element would be declared using attributes within the XLink namespace, and then how an element within the XLink namespace might do the same:\n" + " <A xlink:type=\"simple\" xlink:href=\"http://www.w3.org/TR/wd-xlink/\"\n" + "xlink:title=\"The Xlink Working Draft\">The XLink Working Draft.</A>\n" + " <xlink:simple href=\"http://www.w3.org/TR/wd-xlink/\"\n" + "title=\"The XLink Working Draft\">The XLink Working Draft</xlink:simple>\n" + " Any arbitrary element can be made into an XLink by using the xlink:type attribute. And, of course, the explicit XLink elements may be used, as well. This document will go on to describe the linking attributes that are associated with linking elements. It may be assumed by the reader that these attributes would require the xlink namespace prefix if they existed within an arbitrary element, or that they may be used directly if they exist within an explicit Xlink element.

\n" + " \n" + "
\n" + "\n" + "\n" + "\n" + " Linking Attributes\n" + "

XLink has several attributes associated with the variety of links it may represent. These attributes define four main concepts: locators, arcs, behaviors, and semantics. Locators define where the actual resource is located. Arcs define the traversal of links. Where does the link come from? Where does it go to? All this information can be stored in the arc attributes. Behaviors define how the link is activated, and what the application should do with the resource being linked to. Semantics define useful information that the application may use, and enables the link for such specalized targets as constricted devices and accessibility software.

\n" + " \n" + " \n" + " Locator Attributes\n" + "

The only locator attribute at this time is href. This attribute must contain either a string in the form of a URI that defines the remote resource being linked to, a string containing a fragment identifier that links to a local resource, or a string containing a URI with a fragment identifier concacenated onto it.

\n" + "
\n" + "\n" + " \n" + " Arc Attributes\n" + "

Arcs contain two attributes, from and to. The from attribute may contain a string containing the content of a role attribute from the resource being linked from. The purpose of the from attribute is to define where this link is being actuated from.

\n" + "

The to attribute may contain a string containing the content of a role attribute from the resource being linked to. The purpose of the to attribute is to define where this link traverses to.

\n" + "

The application may use this information in a number of ways, especially in a complex hypertext system, but it is mainly useful in providing context for application behavior.

\n" + " \n" + "
\n" + "\n" + " \n" + " Behavior Attributes\n" + "

There are two attributes associated with behavior: show and actuate. The show attribute defines how the remote resource is to be revealed to the user. It has three options: new, parsed, and replace. The new option indicates that the remote resource should be shown in a new window (or other device context) without replacing the previous content. The parsed option, relating directly to the XML concept of a parsed entity, indicates that the content should be integrated into the document from which the link was actuated. The replace option is the one most commonly seen on the World Wide Web, where the document being linked from is entirely replaced by the object being linked to.

\n" + "

The actuate attribute defines how the link is initiated. It has two options: user and auto. The user option indicates that the link must be initiated by some sort of human-initiated selection, such as clicking on an HTML anchor. The auto option indicates that the link is automatically initiated when the application deems that the user has reached the link. It then follows the behavior set out in the show option.

\n" + " \n" + "
\n" + "\n" + " \n" + " Semantic Attributes\n" + "

There are two attributes associated with semantics, role and title. The role attribute is a generic string used to describe the function of the link's content. For example, a poem might have a link with a role=\"stanza\". The role is also used as an identifier for the from and to attributes of arcs.

\n" + "

The title attribute is designed to provide human-readable text describing the link. It is very useful for those who have text-based applications, whether that be due to a constricted device that cannot display the link's content, or if it's being read by an application to a visually-impaired user, or if it's being used to create a table of links. The title attribute contains a simple, descriptive string.

\n" + "
\n" + "
\n" + "\n" + "\n" + " Linking Elements\n" + "

There are several kinds of linking elements in XLink: simple links, locators, arcs, and extended links. These elements may be instantiated via element declarations from the XLink namespace, or they may be instantiated via attribute declarations from the XLink namespace. Both kinds of instantiation are described in the definition of each linking element.

\n" + "

The simple link is used to declare a link that approximates the functionality of the HTML A element. It has, however, a few added features to increase its value, including the potential declaration of semantics and behavior. The locator elements are used to define the resource being linked to. Some links may contain multiple locators, representing a choice of potential links to be traversed. The arcs are used to define the traversal semantics of the link. Finally, an extended linking element differs from a simple link in that it can connect any number of resources, not just one local resource (optionally) and one remote resource, and in that extended links are more often out-of-line than simple links.

\n" + "\n" + "\n" + " Simple Links\n" + "

Simple links can be used for purposes that approximate the functionality of a basic HTML A link, but they can also support a limited amount of additional functionality. Simple links have only one locator and thus, for convenience, combine the functions of a linking element and a locator into a single element. As a result of this combination, the simple linking element offers both a locator attribute and all the behavior and semantic attributes.

\n" + "

The following are two examples of linking elements, each showing all the possible attributes that can be associated with a simple link. Here is the explicit XLink simple linking element.\n" + " <!ELEMENT xlink:simple ANY>\n" + "<!ATTLIST xlink:slink\n" + " href CDATA #REQUIRED\n" + " role CDATA #IMPLIED\n" + " title CDATA #IMPLIED\n" + " show (new|parsed|replace) \"replace\"\n" + " actuate (user|auto) \"user\"\n" + ">\n" + " And here is how to make an arbitrary element into a simple link.\n" + " <!ELEMENT xlink:simple ANY>\n" + "<!ATTLIST foo\n" + " xlink:type (simple|extended|locator|arc) #FIXED \"simple\" \n" + " xlink:href CDATA #REQUIRED\n" + " xlink:role CDATA #IMPLIED\n" + " xlink:title CDATA #IMPLIED\n" + " xlink:show (new|parsed|replace) \"replace\"\n" + " xlink:actuate (user|auto) \"user\"\n" + ">\n" + " Here is how the first example might look in a document:\n" + "<xlink:simple href=\"http://www.w3.org/TR/wd-xlink\" role=\"working draft\" \n" + " title=\"The XLink Working Draft\" show=\"replace\" actuate=\"user\">\n" + "The XLink Working Draft.</xlink:simple>\n" + "<foo xlink:href=\"http://www.w3.org/TR/wd-xlink\" xlink:role=\"working draft\"\n" + " xlink:title=\"The XLink Working Draft\" xlink:show=\"new\" xlink:actuate=\"user\">\n" + "The XLink Working Draft.</foo>\n" + " Alternately, a simple link could be as terse as this:\n" + "<foo xlink:href=\"#stanza1\">The First Stanza.</foo>\n" + "

\n" + "

\n" + " There are no constraints on the contents of a simple linking element. In\n" + " the sample declaration above, it is given a content model of ANY\n" + " to illustrate that any content model or declared content is acceptable. In\n" + " a valid document, every element that is significant to XLink must still conform\n" + " to the constraints expressed in its governing DTD.

\n" + "

Note that it is meaningful to have an out-of-line simple link, although\n" + " such links are uncommon. They are called \"one-ended\" and are typically used\n" + " to associate discrete semantic properties with locations. The properties might\n" + " be expressed by attributes on the link, the link's element type name, or in\n" + " some other way, and are not considered full-fledged resources of the link.\n" + " Most out-of-line links are extended links, as these have a far wider range\n" + " of uses.

\n" + "
\n" + "\n" + "\n" + "Extended Links\n" + "

An extended link differs from a simple link in that it can connect any number of resources, not just one local resource (optionally) and one remote resource, and in that extended links are more often out-of-line than simple links.

\n" + "

These additional capabilities of extended links are required for: \n" + " \n" + " \n" + "

Enabling outgoing links in documents that cannot be modified to add an inline link

\n" + " \n" + " \n" + "

Creating links to and from resources in formats with no native support for embedded links (such as most multimedia formats)

\n" + "
\n" + " \n" + "

Applying and filtering sets of relevant links on demand

\n" + "
\n" + "

Enabling other advanced hypermedia capabilities

\n" + " \n" + "

\n" + "

Application software might be expected to provide traversal among all of a link's participating resources (subject to semantic constraints outside the scope of this specification) and to signal the fact that a given resource or sub-resource participates in one or more links when it is displayed (even though there is no markup at exactly that point to signal it).

\n" + "

A linking element for an extended link contains a series of child elements that serve as locators and arcs. Because an extended link can have more than one remote resource, it separates out linking itself from the mechanisms used to locate each resource (whereas a simple link combines the two).

\n" + "

The xlink:type attribute value for an extended link must be extended, if the link is being instantiated on an arbitrary element. Note that extended links introduce variants of the show and actuate behavior attributes. These attributes, the showdefault and actuatedefault define the same behavior as their counterparts. However, in this case, they are considered to define the default behavior for all the linking elements that they contain.

\n" + "

However, when a linking element within an extended link has a show or actuate attribute of its own, that attribute overrides the defaults set on the extended linking element.

\n" + "

The extended linking element itself retains those attributes relevant to the link as a whole, and to its local resource if any. Following are two sample declaration for an extended link. The first is an example of the explicit XLink extended link:\n" + " \n" + "<!ELEMENT xlink:extended ((xlink:arc | xlink:locator)*)>\n" + "<!ATTLIST xlink:extended\n" + " role CDATA #IMPLIED\n" + " title CDATA #IMPLIED\n" + " showdefault (new|parsed|replace) #IMPLIED \n" + " actuatedefault (user|auto) #IMPLIED >\n" + "\n" + " The second is an example of an arbitrary element being used an extended link:\n" + "\n" + "<!ELEMENT foo ((xlink:arc | xlink:locator)*)>\n" + "<!ATTLIST foo\n" + " xlink:type (simple|extended|locator|arc) #FIXED \"extended\"\n" + " xlink:role CDATA #IMPLIED\n" + " xlink:title CDATA #IMPLIED\n" + " xlink:showdefault (new|parsed|replace) #IMPLIED \n" + " xlink:actuatedefault (user|auto) #IMPLIED >\n" + "\n" + " The following two examples demonstrate how each of the above might appear within a document instance. Note that the content of these examples would be other elements. For brevity's sake, they've been left blank. The first example shows how the link might appear, using an explicit XLink extended link:\n" + "\n" + "<xlink:extended role=\"address book\" title=\"Ben's Address Book\" showdefault=\"replace\" actuatedefault=\"user\"> ... </xlink:extended>\n" + "\n" + " And the second shows how the link might appear, using an arbitrary element:\n" + "\n" + "<foo xlink:type=\"extended\" xlink:role=\"address book\" xlink:title=\"Ben's Address Book\" xlink:showdefault=\"replace\" xlink:actuatedefault=\"user\"> ... </foo>\n" + "

\n" + "\n" + "
\n" + "\n" + "\n" + " Arc Elements\n" + "

An arc is contained within an extended link for the purpose of defining traversal behavior. More than one arc may be associated with a link. Otherwise, arc elements function exactly as the arc attributes might lead on to expect.

\n" + " \n" + "
\n" + "\n" + "
\n" + "\n" + "Conformance\n" + "

An element conforms to XLink if: \n" + "

The element has an xml:link attribute whose value is\n" + "one of the attribute values prescribed by this specification, and

\n" + "

the element and all of its attributes and content adhere to the\n" + "syntactic\n" + "requirements imposed by the chosen xml:link attribute value,\n" + "as prescribed in this specification.

\n" + "

\n" + "

Note that conformance is assessed at the level of individual elements,\n" + "rather than whole XML documents, because XLink and non-XLink linking mechanisms\n" + "may be used side by side in any one document.

\n" + "

An application conforms to XLink if it interprets XLink-conforming elements\n" + "according to all required semantics prescribed by this specification and,\n" + "for any optional semantics it chooses to support, supports them in the way\n" + "prescribed.

\n" + "
\n" + "\n" + "\n" + "Unfinished Work\n" + "\n" + "Structured Titles\n" + "

The simple title mechanism described in this draft is insufficient to cope\n" + "with internationalization or the use of multimedia in link titles. A future\n" + "version will provide a mechanism for the use of structured link titles.

\n" + "
\n" + "
\n" + "\n" + "References\n" + "\n" + "Eve Maler and Steve DeRose, editors. \n" + "XML Pointer Language (XPointer) V1.0. ArborText, Inso, and Brown\n" + "University. Burlington, Seekonk, et al.: World Wide Web Consortium, 1998.\n" + "(See http://www.w3.org/TR/WD-xptr\n" + " .)\n" + "ISO (International Organization for\n" + "Standardization). ISO/IEC 10744-1992 (E). Information technology\n" + "- Hypermedia/Time-based Structuring Language (HyTime). [Geneva]:\n" + "International Organization for Standardization, 1992. Extended\n" + "Facilities\n" + "Annex. [Geneva]: International Organization for Standardization,\n" + "1996. (See http://www.ornl.go\n" + "v/sgml/wg8/hytime/html/is10744r.html ).\n" + "IETF (Internet Engineering Task\n" + "Force). \n" + "RFC 1738: Uniform Resource Locators. 1991. (See \n" + "http://www.w3.org/Addressing/rfc1738.txt).\n" + "IETF (Internet Engineering Task\n" + "Force). \n" + "RFC 1808: Relative Uniform Resource Locators. 1995. (See http://www.w3.org/Addressing/rfc\n" + "1808.txt ).\n" + "C. M. Sperberg-McQueen and Lou Burnard, editors.\n" + "\n" + "Guidelines for Electronic Text Encoding and Interchange. Association\n" + "for Computers and the Humanities (ACH), Association for Computational\n" + "Linguistics\n" + "(ACL), and Association for Literary and Linguistic Computing (ALLC). Chicago,\n" + "Oxford: Text Encoding Initiative, 1994. \n" + "]Steven J. DeRose and David G. Durand. 1995. \"The\n" + "TEI Hypertext Guidelines.\" In Computing and the Humanities\n" + "29(3).\n" + "Reprinted in Text Encoding Initiative: Background and\n" + "Context,\n" + "ed. Nancy Ide and Jean ronis , ISBN 0-7923-3704-2. \n" + "\n" + "
\n" + "\n"; + +// Correct +const char *ok_data_4 = + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + " \n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "]>\n" + "\n" + "\n" + "\n" + "PP\n" + "\n" + "\n"; + +// Correct, with UTF-8 data +const char ok_data_5[] = + "\n" /*39 chars*/ + "\xC3\xA4"; + +// Erroneous +const char *error_data_1 = + "a"; // bogus character + +// Erroneous +const char *error_data_2 = + "\n" + ""; // XML tag not closed + +const char *bug_data_pr2855 = + "\n" + " notrim"; + +// byte order mark +const char *bug_data_pr1678 = + "\xEF\xBB\xBF"; + +// russian +const char *bug_data_pr5203 = + "\n" + "\xD0\xAE\xD0\xBD\xD0\xB8\xD1\x82"; + +// Quoting attributes +const char *bug_data_pr4840 = + "\n" + ""; + +#endif -- cgit v1.2.3