123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- // Copyright (c) 2001-2010 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 <cstring>
- #include <string>
- #include <iterator>
- #include <iostream>
- #include <typeinfo>
- #include <boost/spirit/include/karma_generate.hpp>
- #include <boost/spirit/include/karma_what.hpp>
- namespace spirit_test
- {
- ///////////////////////////////////////////////////////////////////////////
- struct display_type
- {
- template<typename T>
- void operator()(T const &) const
- {
- std::cout << typeid(T).name() << std::endl;
- }
- template<typename T>
- static void print()
- {
- std::cout << typeid(T).name() << std::endl;
- }
- };
- display_type const display = {};
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char>
- struct output_iterator
- {
- typedef std::basic_string<Char> string_type;
- typedef std::back_insert_iterator<string_type> type;
- };
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char, typename T>
- void print_if_failed(char const* func, bool result
- , std::basic_string<Char> 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 <typename Char, typename Generator>
- inline bool test(Char const *expected, Generator const& g)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g);
- print_if_failed("test", result, generated, expected);
- return result && generated == expected;
- }
- template <typename Char, typename Generator>
- inline bool test(std::basic_string<Char> const& expected, Generator const& g)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g);
- print_if_failed("test", result, generated, expected);
- return result && generated == expected;
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char, typename Generator, typename Attribute>
- inline bool test(Char const *expected, Generator const& g,
- Attribute const &attr)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g, attr);
- print_if_failed("test", result, generated, expected);
- return result && generated == expected;
- }
- template <typename Char, typename Generator, typename Attribute>
- inline bool test(std::basic_string<Char> const& expected, Generator const& g,
- Attribute const &attr)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g, attr);
- print_if_failed("test", result, generated, expected);
- return result && generated == expected;
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char, typename Generator, typename Delimiter>
- inline bool test_delimited(Char const *expected, Generator const& g,
- Delimiter const& d)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d);
- print_if_failed("test_delimited", result, generated, expected);
- return result && generated == expected;
- }
- template <typename Char, typename Generator, typename Delimiter>
- inline bool test_delimited(std::basic_string<Char> const& expected,
- Generator const& g, Delimiter const& d)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d);
- print_if_failed("test_delimited", result, generated, expected);
- return result && generated == expected;
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Char, typename Generator, typename Attribute,
- typename Delimiter>
- inline bool test_delimited(Char const *expected, Generator const& g,
- Attribute const &attr, Delimiter const& d)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d, attr);
- print_if_failed("test_delimited", result, generated, expected);
- return result && generated == expected;
- }
- template <typename Char, typename Generator, typename Attribute,
- typename Delimiter>
- inline bool test_delimited(std::basic_string<Char> const& expected,
- Generator const& g, Attribute const &attr, Delimiter const& d)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<Char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d, attr);
- print_if_failed("test_delimited", result, generated, expected);
- return result && generated == expected;
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Generator>
- inline bool
- binary_test(char const *expected, std::size_t size,
- Generator const& g)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g);
- return result && !std::memcmp(generated.c_str(), expected, size);
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Generator, typename Attribute>
- inline bool
- binary_test(char const *expected, std::size_t size,
- Generator const& g, Attribute const &attr)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<char> 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<string_type> outit(generated);
- bool result = karma::generate(outit, g, attr);
- return result && !std::memcmp(generated.c_str(), expected, size);
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Generator, typename Delimiter>
- 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<char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d);
- return result && !std::memcmp(generated.c_str(), expected, size);
- }
- ///////////////////////////////////////////////////////////////////////////
- template <typename Generator, typename Attribute, typename Delimiter>
- inline bool
- binary_test_delimited(char const *expected, std::size_t size,
- Generator const& g, Attribute const &attr, Delimiter const& d)
- {
- namespace karma = boost::spirit::karma;
- typedef std::basic_string<char> 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<string_type> outit(generated);
- bool result = karma::generate_delimited(outit, g, d, attr);
- return result && !std::memcmp(generated.c_str(), expected, size);
- }
- } // namespace spirit_test
- #endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
|