From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/spirit/test/karma/test.hpp | 328 ++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 src/boost/libs/spirit/test/karma/test.hpp (limited to 'src/boost/libs/spirit/test/karma/test.hpp') diff --git a/src/boost/libs/spirit/test/karma/test.hpp b/src/boost/libs/spirit/test/karma/test.hpp new file mode 100644 index 00000000..be6ac500 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/test.hpp @@ -0,0 +1,328 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// +// 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) + +#if !defined(BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM) +#define BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace spirit_test +{ + /////////////////////////////////////////////////////////////////////////// + struct display_type + { + template + void operator()(T const &) const + { + std::cout << typeid(T).name() << std::endl; + } + + template + static void print() + { + std::cout << typeid(T).name() << std::endl; + } + }; + + display_type const display = {}; + + /////////////////////////////////////////////////////////////////////////// + template + struct output_iterator + { + typedef std::basic_string string_type; + typedef std::back_insert_iterator type; + }; + + /////////////////////////////////////////////////////////////////////////// + template + void print_if_failed(char const* func, bool result + , std::basic_string const& generated, T const& expected) + { + if (!result) + std::cerr << "in " << func << ": result is false" << std::endl; + else if (generated != expected) + std::cerr << "in " << func << ": generated \"" + << std::string(generated.begin(), generated.end()) + << "\"" << std::endl; + } + + /////////////////////////////////////////////////////////////////////////// + template + void print_binary_if_failed(char const* func, bool result + , std::basic_string const& generated, T const& expected) + { + if (!result) + std::cerr << "in " << func << ": result is false" << std::endl; + else if (generated.size() != expected.size() || + std::memcmp(generated.c_str(), expected.c_str(), generated.size())) + { + std::cerr << "in " << func << ": generated \""; + BOOST_FOREACH(int c, generated) + std::cerr << "\\x" << std::hex << std::setfill('0') << std::setw(2) << c; + std::cerr << "\"" << std::endl; + } + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool test(Char const *expected, Generator const& g) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g); + + print_if_failed("test", result, generated, expected); + return result && generated == expected; + } + + template + inline bool test(std::basic_string const& expected, Generator const& g) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g); + + print_if_failed("test", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool test(Char const *expected, Generator const& g, + Attribute const &attrib) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g, attrib); + + print_if_failed("test", result, generated, expected); + return result && generated == expected; + } + + template + inline bool test(std::basic_string const& expected, Generator const& g, + Attribute const &attrib) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g, attrib); + + print_if_failed("test", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool test_delimited(Char const *expected, Generator const& g, + Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d); + + print_if_failed("test_delimited", result, generated, expected); + return result && generated == expected; + } + + template + inline bool test_delimited(std::basic_string const& expected, + Generator const& g, Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d); + + print_if_failed("test_delimited", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool test_delimited(Char const *expected, Generator const& g, + Attribute const &attrib, Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d, attrib); + + print_if_failed("test_delimited", result, generated, expected); + return result && generated == expected; + } + + template + inline bool test_delimited(std::basic_string const& expected, + Generator const& g, Attribute const &attrib, Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d, attrib); + + print_if_failed("test_delimited", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool + binary_test(char const *expected, std::size_t size, + Generator const& g) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g); + + print_binary_if_failed("binary_test", result, generated + , std::string(expected, size)); + return result && generated.size() == size + && !std::memcmp(generated.c_str(), expected, size); + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool + binary_test(char const *expected, std::size_t size, + Generator const& g, Attribute const &attrib) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate(outit, g, attrib); + + print_binary_if_failed("binary_test", result, generated + , std::string(expected, size)); + return result && generated.size() == size + && !std::memcmp(generated.c_str(), expected, size); + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool + binary_test_delimited(char const *expected, std::size_t size, + Generator const& g, Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d); + + print_binary_if_failed("binary_test_delimited", result, generated + , std::string(expected, size)); + return result && generated.size() == size + && !std::memcmp(generated.c_str(), expected, size); + } + + /////////////////////////////////////////////////////////////////////////// + template + inline bool + binary_test_delimited(char const *expected, std::size_t size, + Generator const& g, Attribute const &attrib, Delimiter const& d) + { + namespace karma = boost::spirit::karma; + typedef std::basic_string string_type; + + // we don't care about the result of the "what" function. + // we only care that all generators have it: + karma::what(g); + + string_type generated; + std::back_insert_iterator outit(generated); + bool result = karma::generate_delimited(outit, g, d, attrib); + + print_binary_if_failed("binary_test_delimited", result, generated + , std::string(expected, size)); + return result && generated.size() == size + && !std::memcmp(generated.c_str(), expected, size); + } + +} // namespace spirit_test + +#endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM -- cgit v1.2.3