diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/spirit/test/karma | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/spirit/test/karma')
88 files changed, 10614 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/test/karma/Jamfile b/src/boost/libs/spirit/test/karma/Jamfile new file mode 100644 index 00000000..953bdb02 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/Jamfile @@ -0,0 +1,137 @@ +#============================================================================== +# Copyright (c) 2001-2011 Joel de Guzman +# Copyright (c) 2001-2012 Hartmut Kaiser +# Copyright (c) 2011 Bryce Lelbach +# Copyright (c) 2016-2019 Nikita Kniazev +# +# Use, modification and distribution is subject to 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) +#============================================================================== + +import testing ; + +############################################################################### + +project spirit-karma + : requirements + <include>. + <c++-template-depth>512 + ; + +############################################################################### + +cpp-pch pch : pch.hpp : : : <include>. <toolset>msvc:<cxxflags>"/FIpch.hpp" ; + +explicit pch ; + +############################################################################### + +local subproject-name = karma ; + +rule run ( sources + : args * : input-files * + : requirements * : target-name ? : default-build * ) +{ + target-name ?= $(subproject-name)_$(sources[1]:D=:S=) ; + return [ testing.run $(sources) : $(args) : $(input-files) + : $(requirements) <pch>on-spirit:<source>pch : $(target-name) : $(default-build) ] ; +} + +rule compile ( sources + : requirements * : target-name ? ) +{ + target-name ?= $(subproject-name)_$(sources[1]:D=:S=) ; + return [ testing.compile $(sources) + : $(requirements) <pch>on-spirit:<source>pch : $(target-name) ] ; +} + +rule compile-fail ( sources + : requirements * : target-name ? ) +{ + target-name ?= $(subproject-name)_$(sources[1]:D=:S=) ; + return [ testing.compile-fail $(sources) + : $(requirements) <pch>on-spirit:<source>pch : $(target-name) ] ; +} + +############################################################################### + +compile-fail grammar_fail.cpp ; +compile-fail rule_fail.cpp ; +run actions.cpp ; +run alternative1.cpp ; +run alternative2.cpp ; +run and_predicate.cpp ; +run attribute.cpp ; +run auto1.cpp ; +run auto2.cpp ; +run auto3.cpp ; +run binary1.cpp ; +run binary2.cpp ; +run binary3.cpp ; +run bool.cpp ; +run buffer.cpp ; +run case_handling1.cpp ; +run case_handling2.cpp ; +run case_handling3.cpp ; +run center_alignment.cpp ; +run char1.cpp ; +run char2.cpp ; +run char3.cpp ; +run char_class.cpp ; +run columns.cpp ; +run debug.cpp : : : <pch>off ; +run delimiter.cpp ; +run duplicate.cpp ; +run encoding.cpp ; +run eol.cpp ; +run eps.cpp ; +run format_manip.cpp ; +run format_manip_attr.cpp ; +run format_pointer_container.cpp ; +run generate_attr.cpp ; +run grammar.cpp ; +run int1.cpp ; +run int2.cpp ; +run int3.cpp ; +run kleene.cpp ; +run lazy.cpp ; +run left_alignment.cpp ; +run list.cpp ; +run lit.cpp ; +run maxwidth.cpp ; +run not_predicate.cpp ; +run omit.cpp ; +run optional.cpp ; +run pattern1.cpp ; +run pattern2.cpp ; +run pattern3.cpp ; +run pattern4.cpp ; +run plus.cpp ; +run real1.cpp ; +run real2.cpp ; +run real3.cpp ; +run repeat1.cpp ; +run repeat2.cpp ; +run right_alignment.cpp ; +run sequence1.cpp ; +run sequence2.cpp ; +run stream.cpp ; +run symbols1.cpp ; +run symbols2.cpp ; +run symbols3.cpp ; +run tricky_alignment.cpp ; +run uint_radix.cpp ; +run utree1.cpp ; +run utree2.cpp ; +run utree3.cpp ; +run wstream.cpp ; + +compile regression_const_real_policies.cpp ; +run regression_adapt_adt.cpp ; +run regression_center_alignment.cpp ; +run regression_container_variant_sequence.cpp ; +run regression_iterator.cpp ; +run regression_optional_double.cpp ; +run regression_real_0.cpp ; +run regression_real_policy_sign.cpp ; +run regression_real_scientific.cpp ; +run regression_semantic_action_attribute.cpp ; +run regression_unicode_char.cpp : : : <pch>off ; diff --git a/src/boost/libs/spirit/test/karma/actions.cpp b/src/boost/libs/spirit/test/karma/actions.cpp new file mode 100644 index 00000000..2f3623e5 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/actions.cpp @@ -0,0 +1,137 @@ +/*============================================================================= + Copyright (c) 2001-2011 Hartmut Kaiser + Copyright (c) 2001-2011 Joel de Guzman + + 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) +=============================================================================*/ + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/bind.hpp> +#include <boost/lambda/lambda.hpp> + +#include <sstream> +#include "test.hpp" + +using namespace spirit_test; +using boost::spirit::unused_type; + +void read1(int& i) +{ + i = 42; +} + +void read2(int& i, unused_type) +{ + i = 42; +} + +void read3(int& i, unused_type, bool&) +{ + i = 42; +} + +void read3_fail(int& i, unused_type, bool& pass) +{ + i = 42; + pass = false; +} + +struct read_action +{ + void operator()(int& i, unused_type, unused_type) const + { + i = 42; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using boost::spirit::karma::int_; + { + BOOST_TEST(test("42", int_[&read1])); + BOOST_TEST(test_delimited("42 ", int_[&read1], ' ')); + BOOST_TEST(test("42", int_[&read2])); + BOOST_TEST(test_delimited("42 ", int_[&read2], ' ')); + BOOST_TEST(test("42", int_[&read3])); + BOOST_TEST(test_delimited("42 ", int_[&read3], ' ')); + BOOST_TEST(!test("42", int_[&read3_fail])); + BOOST_TEST(!test_delimited("42 ", int_[&read3_fail], ' ')); + } + + { + BOOST_TEST(test("42", int_[read_action()])); + BOOST_TEST(test_delimited("42 ", int_[read_action()], ' ')); + } + + { + BOOST_TEST(test("42", int_[boost::bind(&read1, _1)])); + BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read1, _1)], ' ')); + BOOST_TEST(test("42", int_[boost::bind(&read2, _1, _2)])); + BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read2, _1, _2)], ' ')); + BOOST_TEST(test("42", int_[boost::bind(&read3, _1, _2, _3)])); + BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read3, _1, _2, _3)], ' ')); + } + + { + namespace lambda = boost::lambda; + { + std::stringstream strm("42"); + BOOST_TEST(test("42", int_[strm >> lambda::_1])); + } + { + std::stringstream strm("42"); + BOOST_TEST(test_delimited("42 ", int_[strm >> lambda::_1], ' ')); + } + } + + { + BOOST_TEST(test("{42}", '{' << int_[&read1] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read1] << '}', ' ')); + BOOST_TEST(test("{42}", '{' << int_[&read2] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read2] << '}', ' ')); + BOOST_TEST(test("{42}", '{' << int_[&read3] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read3] << '}', ' ')); + } + + { + BOOST_TEST(test("{42}", '{' << int_[read_action()] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[read_action()] << '}', ' ')); + } + + { + BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read1, _1)] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", + '{' << int_[boost::bind(&read1, _1)] << '}', ' ')); + BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read2, _1, _2)] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", + '{' << int_[boost::bind(&read2, _1, _2)] << '}', ' ')); + BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}')); + BOOST_TEST(test_delimited("{ 42 } ", + '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}', ' ')); + } + + { + namespace lambda = boost::lambda; + { + std::stringstream strm("42"); + BOOST_TEST(test("{42}", '{' << int_[strm >> lambda::_1] << '}')); + } + { + std::stringstream strm("42"); + BOOST_TEST(test_delimited("{ 42 } ", + '{' << int_[strm >> lambda::_1] << '}', ' ')); + } + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/alternative1.cpp b/src/boost/libs/spirit/test/karma/alternative1.cpp new file mode 100644 index 00000000..461e4b06 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/alternative1.cpp @@ -0,0 +1,162 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test("x", char_('x') | char_('i'))); + BOOST_TEST(test("xi", char_('x') << char_('i') | char_('i'))); + BOOST_TEST(test("i", char_('i') | char_('x') << char_('i'))); + + BOOST_TEST(test("x", buffer[char_('x')] | char_('i'))); + + variant<int, char> v (10); + BOOST_TEST(test("10", char_ | int_, v)); + BOOST_TEST(test("10", int_ | char_, v)); + BOOST_TEST(test("a", lit('a') | char_ | int_, v)); + BOOST_TEST(test("a", char_ | lit('a') | int_, v)); + BOOST_TEST(test("10", int_ | lit('a') | char_, v)); + + v = 'c'; + BOOST_TEST(test("c", char_ | int_, v)); + BOOST_TEST(test("a", lit('a') | char_ | int_, v)); + BOOST_TEST(test("c", char_ | lit('a') | int_, v)); + BOOST_TEST(test("a", int_ | lit('a') | char_, v)); + BOOST_TEST(test("c", int_ | char_ | lit('a'), v)); + } + + // testing for alignment/truncation problems on little endian systems + // (big endian systems will fail one of the other tests below) + { + // test optional attribute + optional<variant<int, char> > v; + BOOST_TEST(!test("", char_ | int_, v)); + BOOST_TEST(!test("", int_ | char_, v)); + BOOST_TEST(test("a", lit('a') | char_ | int_, v)); + BOOST_TEST(test("a", char_ | lit('a') | int_, v)); + BOOST_TEST(test("a", int_ | lit('a') | char_, v)); + + v = 10; + BOOST_TEST(test("10", char_ | int_, v)); + BOOST_TEST(test("10", int_ | char_, v)); + BOOST_TEST(test("a", lit('a') | char_ | int_, v)); + BOOST_TEST(test("a", char_ | lit('a') | int_, v)); + BOOST_TEST(test("10", int_ | lit('a') | char_, v)); + + v = 'c'; + BOOST_TEST(test("c", char_ | int_, v)); + BOOST_TEST(test("a", lit('a') | char_ | int_, v)); + BOOST_TEST(test("c", char_ | lit('a') | int_, v)); + BOOST_TEST(test("a", int_ | lit('a') | char_, v)); + BOOST_TEST(test("c", int_ | char_ | lit('a'), v)); + } + + { + // more tests for optional attribute + optional<int> o; + BOOST_TEST(test("a", lit('a') | int_, o)); + BOOST_TEST(test("a", int_ | lit('a'), o)); + + o = 10; + BOOST_TEST(test("a", lit('a') | int_, o)); + BOOST_TEST(test("10", int_ | lit('a'), o)); + } + + { + int i = 10; + BOOST_TEST(test("a", lit('a') | int_, i)); + BOOST_TEST(test("10", int_ | lit('a'), i)); + } + + { + optional<std::string> o; + BOOST_TEST(test("xyzzy", ("(" << string << ")") | lit("xyzzy"), o)); + + o = "plugh"; + BOOST_TEST(test("(plugh)", ("(" << string << ")") | lit("xyzzy"), o)); + } + + { + BOOST_TEST(test("abc", string | int_, std::string("abc"))); + BOOST_TEST(test("1234", string | int_, 1234)); + BOOST_TEST(test("abc", int_ | string, std::string("abc"))); + BOOST_TEST(test("1234", int_ | string, 1234)); + } + + { + // testing for alignment/truncation problems on little endian systems + // (big endian systems will fail one of the other tests below) + std::basic_string<wchar_t> generated; + std::back_insert_iterator<std::basic_string<wchar_t> > outit(generated); + boost::variant<int, char> v(10); + bool result = karma::generate_delimited(outit + , karma::int_ | karma::char_, karma::char_(' '), v); + BOOST_TEST(result && generated == L"10 "); + } + + { + boost::optional<int> v; + BOOST_TEST(test("error", int_ | "error" << omit[-int_], v)); + BOOST_TEST(!test("error", int_ | "error" << omit[int_], v)); + BOOST_TEST(test("error", int_ | "error" << skip[int_], v)); + v = 1; + BOOST_TEST(test("1", int_ | "error" << omit[-int_], v)); + BOOST_TEST(test("1", int_ | "error" << omit[int_], v)); + BOOST_TEST(test("1", int_ | "error" << skip[int_], v)); + } + + { + typedef spirit_test::output_iterator<char>::type outiter_type; + namespace karma = boost::spirit::karma; + + karma::rule<outiter_type, int()> r = int_; + std::vector<int> v; + BOOST_TEST(test("", '>' << r % ',' | karma::eps, v)); + + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + BOOST_TEST(test(">1,2,3,4", '>' << r % ',' | karma::eps, v)); + } + + { + typedef spirit_test::output_iterator<char>::type outiter_type; + namespace karma = boost::spirit::karma; + + karma::rule<outiter_type, boost::optional<int>()> r = int_; + boost::optional<int> o; + BOOST_TEST(test("error", r | "error", o)); + + o = 10; + BOOST_TEST(test("10", r | "error", o)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/alternative2.cpp b/src/boost/libs/spirit/test/karma/alternative2.cpp new file mode 100644 index 00000000..7e90b165 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/alternative2.cpp @@ -0,0 +1,141 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + // test if alternatives with all components having unused + // attribute generate first alternative + fusion::vector<char, char> v('a', 'b'); + BOOST_TEST(test("axb", char_ << (lit('x') | lit('i')) << char_, v)); + BOOST_TEST(test("axib", + char_ << (lit('x') << lit('i') | lit('i')) << char_, v)); + } + + { + BOOST_TEST(test_delimited("x ", char_('x') | char_('i'), char_(' '))); + BOOST_TEST(test_delimited("x i ", + char_('x') << char_('i') | char_('i'), char_(' '))); + BOOST_TEST(test_delimited("i ", + char_('i') | char_('x') << char_('i'), char_(' '))); + + variant<int, char> v (10); + BOOST_TEST(test_delimited("10 ", char_ | int_, v, char_(' '))); + BOOST_TEST(test_delimited("10 ", int_ | char_, v, char_(' '))); + BOOST_TEST(test_delimited("a ", lit('a') | char_ | int_, v, char_(' '))); + BOOST_TEST(test_delimited("a ", char_ | lit('a') | int_, v, char_(' '))); + BOOST_TEST(test_delimited("10 ", int_ | lit('a') | char_, v, char_(' '))); + + v = 'c'; + BOOST_TEST(test_delimited("c ", char_ | int_, v, char_(' '))); + BOOST_TEST(test_delimited("a ", lit('a') | char_ | int_, v, char_(' '))); + BOOST_TEST(test_delimited("c ", char_ | lit('a') | int_, v, char_(' '))); + BOOST_TEST(test_delimited("a ", int_ | lit('a') | char_, v, char_(' '))); + BOOST_TEST(test_delimited("c ", int_ | char_ | lit('a'), v, char_(' '))); + } + +// this leads to infinite loops +// { +// variant<int, std::string> v(10); +// BOOST_TEST(test("10", int_ | +char_, v)); +// +// v = "abc"; +// BOOST_TEST(test("abc", int_ | +char_, v)); +// } + + { + // if nothing matches, the first explicit alternative will be chosen + variant<double, char const*> v (10.0); + BOOST_TEST(test("11", char_ | lit(11), v)); + BOOST_TEST(test("11", lit(11) | char_ , v)); + BOOST_TEST(test("10.0", double_ | lit(11), v)); + BOOST_TEST(test("11", lit(11) | double_, v)); + BOOST_TEST(!test("", char_ | int_, v)); + + v = "c"; + BOOST_TEST(test("11", char_ | lit(11), v)); + BOOST_TEST(test("11", double_ | lit(11), v)); + BOOST_TEST(!test("", char_ | int_, v)); + } + + { + // in strict mode if nothing matches, the alternative will fail + variant<double, char const*> v (10.0); + BOOST_TEST(!test("11", strict[char_ | lit(11)], v)); + BOOST_TEST(test("11", strict[lit(11) | char_] , v)); + + v = "c"; + BOOST_TEST(!test("11", strict[char_ | lit(11)], v)); + } + + { + // if nothing matches, the first explicit alternative will be chosen + variant<double, char const*> v (10.0); + BOOST_TEST(test_delimited("11 ", char_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", lit(11) | char_ , v, char_(' '))); + BOOST_TEST(test_delimited("10.0 ", double_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", lit(11) | double_, v, char_(' '))); + BOOST_TEST(!test_delimited("", char_ | int_, v, char_(' '))); + + v = "c"; + BOOST_TEST(test_delimited("11 ", char_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", double_ | lit(11), v, char_(' '))); + BOOST_TEST(!test_delimited("", char_ | int_, v, char_(' '))); + } + + { + // if nothing matches, the first explicit alternative will be chosen, + // optionals need to be accepted + optional<variant<double, char const*> > v (10.0); + BOOST_TEST(test_delimited("11 ", char_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", lit(11) | char_ , v, char_(' '))); + BOOST_TEST(test_delimited("10.0 ", double_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", lit(11) | double_, v, char_(' '))); + BOOST_TEST(!test_delimited("", char_ | int_, v, char_(' '))); + + v = "c"; + BOOST_TEST(test_delimited("11 ", char_ | lit(11), v, char_(' '))); + BOOST_TEST(test_delimited("11 ", double_ | lit(11), v, char_(' '))); + BOOST_TEST(!test_delimited("", char_ | int_, v, char_(' '))); + } + + { + std::vector<int> v; + BOOST_TEST(test("[]", '[' << (int_ % ", ") << ']' | "[]", v)); + BOOST_TEST(test("[]", '[' << -(int_ % ", ") << ']', v)); + BOOST_TEST(test("[]", '[' << ((int_ % ", ") | eps) << ']', v)); + + v.push_back(5); + v.push_back(5); + v.push_back(5); + BOOST_TEST(test("[5, 5, 5]", '[' << (int_ % ", ") << ']' | "[]", v)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/and_predicate.cpp b/src/boost/libs/spirit/test/karma/and_predicate.cpp new file mode 100644 index 00000000..83965856 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/and_predicate.cpp @@ -0,0 +1,36 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + + { + BOOST_TEST(test("1", int_(1) << &(int_(2) << &int_(3) << int_(4)))); + } + + { + using boost::spirit::ascii::char_; + + BOOST_TEST(test("b", &char_('a') << 'b' | 'c', 'a')); + BOOST_TEST(test("c", &char_('a') << 'b' | 'c', 'x')); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/attribute.cpp b/src/boost/libs/spirit/test/karma/attribute.cpp new file mode 100644 index 00000000..6fdafdc9 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/attribute.cpp @@ -0,0 +1,214 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/phoenix_limits.hpp> + +#include <boost/fusion/include/struct.hpp> +#include <boost/fusion/include/nview.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +struct test_data +{ + std::string s1; + std::string s2; + int i1; + double d1; + std::string s3; +}; + +BOOST_FUSION_ADAPT_STRUCT( + test_data, + (int, i1) + (std::string, s1) + (std::string, s2) + (std::string, s3) + (double, d1) +) + +/////////////////////////////////////////////////////////////////////////////// +// this is just a test structure we need to use in place of an int +struct test_int_data1 +{ + int i; +}; + +// so we provide a custom attribute transformation +namespace boost { namespace spirit { namespace traits +{ + template <> + struct transform_attribute<test_int_data1 const, int, karma::domain> + { + typedef int type; + static int pre(test_int_data1 const& d) { return d.i; } + }; +}}} + +/////////////////////////////////////////////////////////////////////////////// +// this is another test structure we need to use in place of an int, but this +// time we use a reference to the embedded element +struct test_int_data2 +{ + int i; +}; + +// so we provide a custom attribute transformation +namespace boost { namespace spirit { namespace traits +{ + template <> + struct transform_attribute<test_int_data2 const, int, karma::domain> + { + typedef int const& type; + static int const& pre(test_int_data2 const& d) { return d.i; } + }; +}}} + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + namespace fusion = boost::fusion; + namespace karma = boost::spirit::karma; + + test_data d1 = { "s11", "s12", 1, 2.5, "s13" }; + { + + BOOST_TEST(test("s121", + karma::string << karma::int_, + fusion::as_nview<2, 0>(d1))); + + BOOST_TEST(test_delimited("s12 1 ", + karma::string << karma::int_, + fusion::as_nview<2, 0>(d1), ' ')); + } + + { + test_data d2 = { "s21", "s22", 2, 3.4, "s23" }; + typedef fusion::result_of::as_nview<test_data const, 1, 2, 4>::type + test_view; + std::vector<test_data> v; + v.push_back(d1); + v.push_back(d2); + + karma::rule<output_iterator<char>::type, test_view()> r = + karma::string << karma::string << karma::double_; + + BOOST_TEST(test("s11s122.5\ns21s223.4", r % karma::eol, v)); + BOOST_TEST(test_delimited("s11s122.5 \n s21s223.4 ", + r % karma::eol, v, ' ')); + } + + { + test_int_data1 d = { 1 }; + BOOST_TEST(test("1", karma::attr_cast(karma::int_), d)); + BOOST_TEST(test("1", karma::attr_cast<test_int_data1>(karma::int_), d)); + BOOST_TEST(test("1", karma::attr_cast<test_int_data1, int>(karma::int_), d)); + } + + { + test_int_data1 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data1> v; + v.push_back(d[0]); + v.push_back(d[1]); + + BOOST_TEST(test("1,2", karma::attr_cast(karma::int_) % ',', v)); + BOOST_TEST(test("1,2" + , karma::attr_cast<test_int_data1>(karma::int_) % ',', v)); + BOOST_TEST(test("1,2" + , karma::attr_cast<test_int_data1, int>(karma::int_) % ',', v)); + } + + { + test_int_data1 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data1> v; + v.push_back(d[0]); + v.push_back(d[1]); + + karma::rule<output_iterator<char>::type, int()> r = karma::int_; + BOOST_TEST(test("1,2", r % ',', v)); + } + + { + test_int_data1 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data1> v; + v.push_back(d[0]); + v.push_back(d[1] ); + +// this won't compile as there is no defined transformation for +// test_int_data1 and double +// BOOST_TEST(test("1.0,2.0", karma::attr_cast(karma::double_) % ',', v)); +// BOOST_TEST(test("1.0,2.0" +// , karma::attr_cast<test_int_data1>(karma::double_) % ',', v)); + + BOOST_TEST(test("1.0,2.0" + , karma::attr_cast<test_int_data1, int>(karma::double_) % ',', v)); + + karma::rule<output_iterator<char>::type, int()> r = karma::double_; + BOOST_TEST(test("1.0,2.0", r % ',', v)); + } + + { + test_int_data2 d = { 1 }; + BOOST_TEST(test("1", karma::attr_cast(karma::int_), d)); + BOOST_TEST(test("1", karma::attr_cast<test_int_data2>(karma::int_), d)); + BOOST_TEST(test("1", karma::attr_cast<test_int_data2, int>(karma::int_), d)); + } + + { + test_int_data2 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data2> v; + v.push_back(d[0]); + v.push_back(d[1]); + + BOOST_TEST(test("1,2", karma::attr_cast(karma::int_) % ',', v)); + BOOST_TEST(test("1,2" + , karma::attr_cast<test_int_data2>(karma::int_) % ',', v)); + BOOST_TEST(test("1,2" + , karma::attr_cast<test_int_data2, int>(karma::int_) % ',', v)); + } + + { + test_int_data2 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data2> v; + v.push_back(d[0]); + v.push_back(d[1]); + + karma::rule<output_iterator<char>::type, int()> r = karma::int_; + BOOST_TEST(test("1,2", r % ',', v)); + } + + { + test_int_data2 d[] = {{ 1 }, { 2 }}; + std::vector<test_int_data2> v; + v.push_back(d[0]); + v.push_back(d[1] ); + +// this won't compile as there is no defined transformation for +// test_int_data2 and double +// BOOST_TEST(test("1.0,2.0", karma::attr_cast(karma::double_) % ',', v)); +// BOOST_TEST(test("1.0,2.0" +// , karma::attr_cast<test_int_data2>(karma::double_) % ',', v)); + + BOOST_TEST(test("1.0,2.0" + , karma::attr_cast<test_int_data2, int>(karma::double_) % ',', v)); + + karma::rule<output_iterator<char>::type, int()> r = karma::double_; + BOOST_TEST(test("1.0,2.0", r % ',', v)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/auto.hpp b/src/boost/libs/spirit/test/karma/auto.hpp new file mode 100644 index 00000000..a2d1ae79 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/auto.hpp @@ -0,0 +1,79 @@ +// 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_TEST_AUTO_HPP) +#define BOOST_SPIRIT_TEST_AUTO_HPP + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/fusion/include/std_pair.hpp> +#include <boost/spirit/include/karma_bool.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_auto.hpp> + +#include "test.hpp" + +namespace karma = boost::spirit::karma; +namespace traits = boost::spirit::traits; + +/////////////////////////////////////////////////////////////////////////////// +template <typename Char, typename T> +bool test_create_generator(Char const *expected, T const& t) +{ + std::basic_string<Char> generated; + std::back_insert_iterator<std::basic_string<Char> > sink(generated); + + BOOST_TEST((traits::meta_create_exists<karma::domain, T>::value)); + bool result = karma::generate(sink, karma::create_generator<T>(), t); + + spirit_test::print_if_failed("test_create_generator", result, generated, expected); + return result && generated == expected; +} + +template <typename Char, typename T> +bool test_create_generator_auto(Char const *expected, T const& t) +{ + std::basic_string<Char> generated; + std::back_insert_iterator<std::basic_string<Char> > sink(generated); + + BOOST_TEST((traits::meta_create_exists<karma::domain, T>::value)); + bool result = karma::generate(sink, t); + + spirit_test::print_if_failed("test_create_generator (auto)", result, generated, expected); + return result && generated == expected; +} + +template <typename Char, typename Attribute> +bool test_rule(Char const *expected, Attribute const& attr) +{ + BOOST_TEST((traits::meta_create_exists<karma::domain, Attribute>::value)); + + typedef typename spirit_test::output_iterator<Char>::type sink_type; + karma::rule<sink_type, Attribute()> r = + karma::create_generator<Attribute>(); + return spirit_test::test(expected, r, attr); +} + +template <typename Char, typename Attribute, typename Delimiter> +bool test_rule_delimited(Char const *expected, Attribute const& attr + , Delimiter const& d) +{ + BOOST_TEST((traits::meta_create_exists<karma::domain, Attribute>::value)); + + typedef typename spirit_test::output_iterator<Char>::type sink_type; + karma::rule<sink_type, Attribute(), Delimiter> r = + karma::create_generator<Attribute>(); + return spirit_test::test_delimited(expected, r, attr, d); +} + +struct my_type {}; + +#endif diff --git a/src/boost/libs/spirit/test/karma/auto1.cpp b/src/boost/libs/spirit/test/karma/auto1.cpp new file mode 100644 index 00000000..a3b4850f --- /dev/null +++ b/src/boost/libs/spirit/test/karma/auto1.cpp @@ -0,0 +1,104 @@ +// 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) + +#include "auto.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + { + BOOST_TEST((!traits::meta_create_exists<karma::domain, my_type>::value)); + } + + { + // test primitive types + BOOST_TEST(test_create_generator("true", true)); + BOOST_TEST(test_create_generator("1", 1)); + BOOST_TEST(test_create_generator("1.1", 1.1)); + BOOST_TEST(test_create_generator("test", std::string("test"))); + BOOST_TEST(test_create_generator("a", 'a')); + BOOST_TEST(test_create_generator(L"a", L'a')); + + // test containers + std::vector<int> v; + v.push_back(0); + v.push_back(1); + v.push_back(2); + BOOST_TEST(test_create_generator("012", v)); + + std::list<int> l; + l.push_back(0); + l.push_back(1); + l.push_back(2); + BOOST_TEST(test_create_generator("012", l)); + + // test optional + boost::optional<int> o; + BOOST_TEST(test_create_generator("", o)); + o = 1; + BOOST_TEST(test_create_generator("1", o)); + + // test alternative + boost::variant<int, double, float, std::string> vv; + vv = 1; + BOOST_TEST(test_create_generator("1", vv)); + vv = 1.0; + BOOST_TEST(test_create_generator("1.0", vv)); + vv = 1.0f; + BOOST_TEST(test_create_generator("1.0", vv)); + vv = "some string"; + BOOST_TEST(test_create_generator("some string", vv)); + + // test fusion sequence + std::pair<int, double> p (1, 2.0); + BOOST_TEST(test_create_generator("12.0", p)); + } + + { + // test primitive types +// BOOST_TEST(test_create_generator_auto("true", true)); +// BOOST_TEST(test_create_generator_auto("1", 1)); +// BOOST_TEST(test_create_generator_auto("1.1", 1.1)); +// BOOST_TEST(test_create_generator_auto("test", std::string("test"))); +// BOOST_TEST(test_create_generator_auto("a", 'a')); +// BOOST_TEST(test_create_generator_auto(L"a", L'a')); + + // test containers + std::vector<int> v; + v.push_back(0); + v.push_back(1); + v.push_back(2); + BOOST_TEST(test_create_generator_auto("012", v)); + + std::list<int> l; + l.push_back(0); + l.push_back(1); + l.push_back(2); + BOOST_TEST(test_create_generator_auto("012", l)); + + // test optional + boost::optional<int> o; + BOOST_TEST(test_create_generator_auto("", o)); + o = 1; + BOOST_TEST(test_create_generator_auto("1", o)); + + // test alternative + boost::variant<int, double, float, std::string> vv; + vv = 1; + BOOST_TEST(test_create_generator_auto("1", vv)); + vv = 1.0; + BOOST_TEST(test_create_generator_auto("1.0", vv)); + vv = 1.0f; + BOOST_TEST(test_create_generator_auto("1.0", vv)); + vv = "some string"; + BOOST_TEST(test_create_generator_auto("some string", vv)); + + // test fusion sequence + std::pair<int, double> p (1, 2.0); + BOOST_TEST(test_create_generator_auto("12.0", p)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/auto2.cpp b/src/boost/libs/spirit/test/karma/auto2.cpp new file mode 100644 index 00000000..4527b869 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/auto2.cpp @@ -0,0 +1,126 @@ +// 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) + +#include "auto.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + { + using karma::auto_; + using karma::upper; + using spirit_test::test; + using spirit_test::test_delimited; + + // test primitive types + BOOST_TEST(test("true", auto_, true)); + BOOST_TEST(test("1", auto_, 1)); + BOOST_TEST(test("1.1", auto_, 1.1)); + BOOST_TEST(test("test", auto_, "test")); + BOOST_TEST(test(L"test", auto_, L"test")); + BOOST_TEST(test("a", auto_, 'a')); + BOOST_TEST(test(L"a", auto_, L'a')); + + BOOST_TEST(test("TRUE", upper[auto_], true)); + BOOST_TEST(test("TEST", upper[auto_], "test")); + + // test containers + std::vector<int> v; + v.push_back(0); + v.push_back(1); + v.push_back(2); + BOOST_TEST(test("012", auto_, v)); + BOOST_TEST(test("0,1,2", auto_ % ',', v)); + BOOST_TEST(test_delimited("0,1,2,", auto_, v, ',')); + + std::list<int> l; + l.push_back(0); + l.push_back(1); + l.push_back(2); + BOOST_TEST(test("012", auto_, l)); + BOOST_TEST(test("0,1,2", auto_ % ',', l)); + BOOST_TEST(test_delimited("0,1,2,", auto_, l, ',')); + + // test optional + boost::optional<int> o; + BOOST_TEST(test("", auto_, o)); + o = 1; + BOOST_TEST(test("1", auto_, o)); + + // test alternative + boost::variant<int, double, float, std::string> vv; + vv = 1; + BOOST_TEST(test("1", auto_, vv)); + vv = 1.0; + BOOST_TEST(test("1.0", auto_, vv)); + vv = 1.0f; + BOOST_TEST(test("1.0", auto_, vv)); + vv = "some string"; + BOOST_TEST(test("some string", auto_, vv)); + + // test fusion sequence + std::pair<int, double> p (1, 2.0); + BOOST_TEST(test("12.0", auto_, p)); + BOOST_TEST(test_delimited("1,2.0,", auto_, p, ',')); + } + + { + using karma::auto_; + using karma::upper; + using spirit_test::test; + using spirit_test::test_delimited; + + // test primitive types + BOOST_TEST(test("true", auto_(true))); + BOOST_TEST(test("1", auto_(1))); + BOOST_TEST(test("1.1", auto_(1.1))); + BOOST_TEST(test("test", auto_("test"))); + BOOST_TEST(test(L"test", auto_(L"test"))); + BOOST_TEST(test("a", auto_('a'))); + BOOST_TEST(test(L"a", auto_(L'a'))); + + BOOST_TEST(test("TRUE", upper[auto_(true)])); + BOOST_TEST(test("TEST", upper[auto_("test")])); + + // test containers + std::vector<int> v; + v.push_back(0); + v.push_back(1); + v.push_back(2); + BOOST_TEST(test("012", auto_(v))); + BOOST_TEST(test_delimited("0,1,2,", auto_(v), ',')); + + std::list<int> l; + l.push_back(0); + l.push_back(1); + l.push_back(2); + BOOST_TEST(test("012", auto_(l))); + BOOST_TEST(test_delimited("0,1,2,", auto_(l), ',')); + + // test optional + boost::optional<int> o; + BOOST_TEST(test("", auto_(o))); + o = 1; + BOOST_TEST(test("1", auto_(o))); + + // test alternative + boost::variant<int, double, float, std::string> vv; + vv = 1; + BOOST_TEST(test("1", auto_(vv))); + vv = 1.0; + BOOST_TEST(test("1.0", auto_(vv))); + vv = 1.0f; + BOOST_TEST(test("1.0", auto_(vv))); + vv = "some string"; + BOOST_TEST(test("some string", auto_(vv))); + + // test fusion sequence + std::pair<int, double> p (1, 2.0); + BOOST_TEST(test("12.0", auto_, p)); + BOOST_TEST(test_delimited("1,2.0,", auto_(p), ',')); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/auto3.cpp b/src/boost/libs/spirit/test/karma/auto3.cpp new file mode 100644 index 00000000..ffbcdc9a --- /dev/null +++ b/src/boost/libs/spirit/test/karma/auto3.cpp @@ -0,0 +1,60 @@ +// 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) + +#include "auto.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + { + using spirit_test::test; + using spirit_test::test_delimited; + + // test primitive types + BOOST_TEST(test_rule("true", true)); + BOOST_TEST(test_rule("1", 1)); + BOOST_TEST(test_rule("1.1", 1.1)); + BOOST_TEST(test_rule("test", std::string("test"))); + + // test containers + std::vector<int> v; + v.push_back(0); + v.push_back(1); + v.push_back(2); + BOOST_TEST(test_rule("012", v)); + BOOST_TEST(test_rule_delimited("0,1,2,", v, ',')); + + std::list<int> l; + l.push_back(0); + l.push_back(1); + l.push_back(2); + BOOST_TEST(test_rule("012", l)); + BOOST_TEST(test_rule_delimited("0,1,2,", l, ',')); + + // test optional + boost::optional<int> o; + BOOST_TEST(test_rule("", o)); + o = 1; + BOOST_TEST(test_rule("1", o)); + + // test alternative + boost::variant<int, double, float, std::string> vv; + vv = 1; + BOOST_TEST(test_rule("1", vv)); + vv = 1.0; + BOOST_TEST(test_rule("1.0", vv)); + vv = 1.0f; + BOOST_TEST(test_rule("1.0", vv)); + vv = "some string"; + BOOST_TEST(test_rule("some string", vv)); + + // test fusion sequence + std::pair<int, double> p (1, 2.0); + BOOST_TEST(test_rule("12.0", p)); + BOOST_TEST(test_rule_delimited("1,2.0,", p, ',')); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/binary1.cpp b/src/boost/libs/spirit/test/karma/binary1.cpp new file mode 100644 index 00000000..336d2b42 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/binary1.cpp @@ -0,0 +1,113 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_binary.hpp> +#include <boost/spirit/include/karma_generate.hpp> + +#include <boost/predef/other/endian.h> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { // test native endian binaries +#if BOOST_ENDIAN_LITTLE_BYTE + BOOST_TEST(binary_test("\x01", 1, byte_, 0x01)); + BOOST_TEST(binary_test("\x80", 1, byte_, 0x80)); + BOOST_TEST(binary_test("\x01\x82", 2, word, 0x8201)); + BOOST_TEST(binary_test("\x81\x02", 2, word, 0x0281)); + BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, dword, 0x84030201)); + BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, dword, 0x04030281)); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, qword, + 0x8807060504030201LL)); + BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, qword, + 0x0807060504030281LL)); +#endif + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, 1.0f)); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double, 1.0)); + + BOOST_TEST(binary_test_delimited("\x01\x00\x00\x00", 4, byte_, 0x01, pad(4))); + BOOST_TEST(binary_test_delimited("\x01\x02\x00\x00", 4, word, 0x0201, pad(4))); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04", 4, dword, 0x04030201, pad(4))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", 10, + qword, 0x0807060504030201LL, pad(10))); +#endif + BOOST_TEST(binary_test_delimited("\x00\x00\x80\x3f", 4, bin_float, + 1.0f, pad(4))); + BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double, 1.0, pad(8))); + +#else // BOOST_ENDIAN_LITTLE_BYTE + + BOOST_TEST(binary_test("\x01", 1, byte_, 0x01)); + BOOST_TEST(binary_test("\x80", 1, byte_, 0x80)); + BOOST_TEST(binary_test("\x01\x82", 2, word, 0x0182)); + BOOST_TEST(binary_test("\x81\x02", 2, word, 0x8102)); + BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, dword, 0x01020384)); + BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, dword, 0x81020304)); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, qword, + 0x0102030405060788LL)); + BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, qword, + 0x8102030405060708LL)); +#endif + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, 1.0f)); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double, 1.0)); + + BOOST_TEST(binary_test_delimited("\x01\x00\x00\x00", 4, byte_, 0x01, pad(4))); + BOOST_TEST(binary_test_delimited("\x01\x02\x00\x00", 4, word, 0x0102, pad(4))); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04", 4, dword, 0x01020304, pad(4))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", 10, + qword, 0x0102030405060708LL, pad(10))); +#endif + BOOST_TEST(binary_test_delimited("\x3f\x80\x00\x00", 4, bin_float, + 1.0f, pad(4))); + BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double, 1.0, pad(8))); +#endif + } + + { // test native endian binaries +#if BOOST_ENDIAN_LITTLE_BYTE + BOOST_TEST(binary_test("\x01", 1, byte_(0x01))); + BOOST_TEST(binary_test("\x01\x02", 2, word(0x0201))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword(0x04030201))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, + qword(0x0807060504030201LL))); +#endif + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float(1.0f))); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double(1.0))); +#else + BOOST_TEST(binary_test("\x01", 1, byte_(0x01))); + BOOST_TEST(binary_test("\x01\x02", 2, word(0x0102))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword(0x01020304))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, + qword(0x0102030405060708LL))); +#endif + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float(1.0f))); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double(1.0))); +#endif + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/binary2.cpp b/src/boost/libs/spirit/test/karma/binary2.cpp new file mode 100644 index 00000000..00f82032 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/binary2.cpp @@ -0,0 +1,131 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_binary.hpp> +#include <boost/spirit/include/karma_generate.hpp> + +#include <boost/predef/other/endian.h> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { // test big endian binaries + BOOST_TEST(binary_test("\x01\x82", 2, big_word, 0x0182)); + BOOST_TEST(binary_test("\x81\x02", 2, big_word, 0x8102)); + BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, big_dword, 0x01020384)); + BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, big_dword, 0x81020304)); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, big_qword, + 0x0102030405060788LL)); + BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, big_qword, + 0x8102030405060708LL)); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", + 10, big_qword, 0x0102030405060708LL, pad(10))); +#endif + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float, 1.0f)); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + big_bin_double, 1.0)); + BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00\x00\x00", + 10, big_bin_double, 1.0, pad(10))); + } + + { + BOOST_TEST(binary_test("\x01\x02", 2, big_word(0x0102))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, big_dword(0x01020304))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, + big_qword(0x0102030405060708LL))); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", + 10, big_qword(0x0102030405060708LL), pad(10))); +#endif + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float(1.0f))); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + big_bin_double(1.0))); + BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00\x00\x00", + 10, big_bin_double(1.0), pad(10))); + } + + { // test little endian binaries + BOOST_TEST(binary_test("\x01\x82", 2, little_word, 0x8201)); + BOOST_TEST(binary_test("\x81\x02", 2, little_word, 0x0281)); + BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, little_dword, 0x84030201)); + BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, little_dword, 0x04030281)); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, little_qword, + 0x8807060504030201LL)); + BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, little_qword, + 0x0807060504030281LL)); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", + 10, little_qword, 0x0807060504030201LL, pad(10))); +#endif + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float, 1.0f)); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + little_bin_double, 1.0)); + BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00", + 10, little_bin_double, 1.0, pad(10))); + } + + { + BOOST_TEST(binary_test("\x01\x02", 2, little_word(0x0201))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, little_dword(0x04030201))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, + little_qword(0x0807060504030201LL))); + BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00", + 10, little_qword(0x0807060504030201LL), pad(10))); +#endif + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float(1.0f))); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + little_bin_double(1.0))); + BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00", + 10, little_bin_double(1.0), pad(10))); + } + + { // test native endian binaries + boost::optional<boost::uint8_t> v8; + boost::optional<boost::uint16_t> v16; + boost::optional<boost::uint32_t> v32; + boost::optional<float> vf; + boost::optional<double> vd; + +#if BOOST_ENDIAN_LITTLE_BYTE + + BOOST_TEST(!binary_test("", 0, byte_, v8)); + BOOST_TEST(!binary_test("", 0, word, v16)); + BOOST_TEST(!binary_test("", 0, dword, v32)); +#ifdef BOOST_HAS_LONG_LONG + boost::optional<boost::uint64_t> v64; + BOOST_TEST(!binary_test("", 0, qword, v64)); +#endif + BOOST_TEST(!binary_test("", 0, bin_float, vf)); + BOOST_TEST(!binary_test("", 0, bin_double, vd)); + +#else // BOOST_ENDIAN_LITTLE_BYTE + + BOOST_TEST(!binary_test("", 0, byte_, v8)); + BOOST_TEST(!binary_test("", 0, word, v16)); + BOOST_TEST(!binary_test("", 0, dword, v32)); +#ifdef BOOST_HAS_LONG_LONG + boost::optional<boost::uint64_t> v64; + BOOST_TEST(!binary_test("", 0, qword, v64)); +#endif + BOOST_TEST(!binary_test("", 0, bin_float, vf)); + BOOST_TEST(!binary_test("", 0, bin_double, vd)); + +#endif + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/binary3.cpp b/src/boost/libs/spirit/test/karma/binary3.cpp new file mode 100644 index 00000000..d666307e --- /dev/null +++ b/src/boost/libs/spirit/test/karma/binary3.cpp @@ -0,0 +1,168 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_binary.hpp> +#include <boost/spirit/include/karma_generate.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <boost/predef/other/endian.h> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::phoenix; + + { // test optional attributes + +#if BOOST_ENDIAN_LITTLE_BYTE + boost::optional<boost::uint8_t> v8 (0x01); + BOOST_TEST(binary_test("\x01", 1, byte_, v8)); + boost::optional<boost::uint16_t> v16 (0x0201); + BOOST_TEST(binary_test("\x01\x02", 2, word, v16)); + boost::optional<boost::uint32_t> v32 (0x04030201); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32)); +#ifdef BOOST_HAS_LONG_LONG + boost::optional<boost::uint64_t> v64 (0x0807060504030201LL); + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64)); +#endif + boost::optional<float> vf(1.0f); + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, vf)); + boost::optional<double> vd(1.0); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double, vd)); + +#else // BOOST_ENDIAN_LITTLE_BYTE + + boost::optional<boost::uint8_t> v8 (0x01); + BOOST_TEST(binary_test("\x01", 1, byte_, v8)); + boost::optional<boost::uint16_t> v16 (0x0102); + BOOST_TEST(binary_test("\x01\x02", 2, word, v16)); + boost::optional<boost::uint32_t> v32 (0x01020304); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32)); +#ifdef BOOST_HAS_LONG_LONG + boost::optional<boost::uint64_t> v64 (0x0102030405060708LL); + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64)); +#endif + boost::optional<float> vf(1.0f); + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, vf)); + boost::optional<double> vd(1.0); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double, vd)); + +#endif + } + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + { // test Phoenix expression attributes, only supported if + // karma_phoenix_attributes.hpp is included + namespace phoenix = boost::phoenix; + +#if BOOST_ENDIAN_LITTLE_BYTE + BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01))); + BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0201))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, + phoenix::val(0x04030201))); + + boost::uint8_t v8 (0x01); + BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8))); + BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8))); + + boost::uint16_t v16 (0x0201); + BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16))); + BOOST_TEST(binary_test("\x02\x02", 2, word, ++phoenix::ref(v16))); + + boost::uint32_t v32 (0x04030201); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32))); + BOOST_TEST(binary_test("\x02\x02\x03\x04", 4, dword, ++phoenix::ref(v32))); + +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, + phoenix::val(0x0807060504030201LL))); + + boost::uint64_t v64 (0x0807060504030201LL); + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, + phoenix::ref(v64))); + BOOST_TEST(binary_test("\x02\x02\x03\x04\x05\x06\x07\x08", 8, qword, + ++phoenix::ref(v64))); +#endif + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, + phoenix::val(1.0f))); + float vf(1.0f); + BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, + phoenix::ref(vf))); + BOOST_TEST(binary_test("\x00\x00\x00\x40", 4, bin_float, + ++phoenix::ref(vf))); + + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double, phoenix::val(1.0))); + double vd(1.0); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8, + bin_double, phoenix::ref(vd))); + BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\x00\x40", 8, + bin_double, ++phoenix::ref(vd))); + +#else // BOOST_ENDIAN_LITTLE_BYTE + + BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01))); + BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0102))); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, + phoenix::val(0x01020304))); + + boost::uint8_t v8 (0x01); + BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8))); + BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8))); + + boost::uint16_t v16 (0x0102); + BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16))); + BOOST_TEST(binary_test("\x01\x03", 2, word, ++phoenix::ref(v16))); + + boost::uint32_t v32 (0x01020304); + BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32))); + BOOST_TEST(binary_test("\x01\x02\x03\x05", 4, dword, ++phoenix::ref(v32))); + +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, + phoenix::val(0x0102030405060708LL))); + + boost::uint64_t v64 (0x0102030405060708LL); + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, + phoenix::ref(v64))); + BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x09", 8, qword, + ++phoenix::ref(v64))); +#endif + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, + phoenix::val(1.0f))); + float vf(1.0f); + BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, + phoenix::ref(vf))); + BOOST_TEST(binary_test("\x40\x00\x00\x00", 4, bin_float, + ++phoenix::ref(vf))); + + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double, phoenix::val(1.0))); + double vd(1.0); + BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, + bin_double, phoenix::ref(vd))); + BOOST_TEST(binary_test("\x40\x00\x00\x00\x00\x00\x00\x00", 8, + bin_double, ++phoenix::ref(vd))); + +#endif + } +#endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/bool.cpp b/src/boost/libs/spirit/test/karma/bool.cpp new file mode 100644 index 00000000..4627bd08 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/bool.cpp @@ -0,0 +1,184 @@ +/*============================================================================= + Copyright (c) 2001-2011 Hartmut Kaiser + Copyright (c) 2001-2011 Joel de Guzman + + 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) +=============================================================================*/ + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +// special bool output policy allowing to spell false as true backwards (eurt) +struct special_bool_policy : boost::spirit::karma::bool_policies<> +{ + template <typename CharEncoding, typename Tag + , typename OutputIterator> + static bool generate_false(OutputIterator& sink, bool) + { + // we want to spell the names of true and false backwards + return boost::spirit::karma::string_inserter<CharEncoding, Tag>:: + call(sink, "eurt"); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// special policy allowing to use any type as a boolean +struct test_bool_data +{ + explicit test_bool_data(bool b) : b(b) {} + + bool b; + + // we need to provide (safe) convertibility to bool +private: + struct dummy { void true_() {} }; + typedef void (dummy::*safe_bool)(); + +public: + operator safe_bool () const { return b ? &dummy::true_ : 0; } +}; + +struct test_bool_policy : boost::spirit::karma::bool_policies<> +{ + template <typename Inserter, typename OutputIterator, typename Policies> + static bool + call (OutputIterator& sink, test_bool_data b, Policies const& p) + { + // call the predefined inserter to do the job + return Inserter::call_n(sink, bool(b), p); + } +}; + + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using boost::spirit::karma::bool_; + using boost::spirit::karma::false_; + using boost::spirit::karma::true_; + using boost::spirit::karma::lit; + using boost::spirit::karma::lower; + using boost::spirit::karma::upper; + + // testing plain bool + { + BOOST_TEST(test("false", bool_, false)); + BOOST_TEST(test("true", bool_, true)); + BOOST_TEST(test("false", false_, false)); + BOOST_TEST(test("true", true_, true)); + BOOST_TEST(test("false", bool_(false))); + BOOST_TEST(test("true", bool_(true))); + BOOST_TEST(test("false", bool_(false), false)); + BOOST_TEST(test("true", bool_(true), true)); + BOOST_TEST(!test("", bool_(false), true)); + BOOST_TEST(!test("", bool_(true), false)); + BOOST_TEST(test("false", lit(false))); + BOOST_TEST(test("true", lit(true))); + } + + // test optional attributes + { + boost::optional<bool> optbool; + + BOOST_TEST(!test("", bool_, optbool)); + BOOST_TEST(test("", -bool_, optbool)); + optbool = false; + BOOST_TEST(test("false", bool_, optbool)); + BOOST_TEST(test("false", -bool_, optbool)); + optbool = true; + BOOST_TEST(test("true", bool_, optbool)); + BOOST_TEST(test("true", -bool_, optbool)); + } + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + // test Phoenix expression attributes (requires to include + // karma_phoenix_attributes.hpp) + { + namespace phoenix = boost::phoenix; + + BOOST_TEST(test("true", bool_, phoenix::val(true))); + + bool b = false; + BOOST_TEST(test("false", bool_, phoenix::ref(b))); + BOOST_TEST(test("true", bool_, !phoenix::ref(b))); + } +#endif + + { + BOOST_TEST(test("false", lower[bool_], false)); + BOOST_TEST(test("true", lower[bool_], true)); + BOOST_TEST(test("false", lower[bool_(false)])); + BOOST_TEST(test("true", lower[bool_(true)])); + BOOST_TEST(test("false", lower[bool_(false)], false)); + BOOST_TEST(test("true", lower[bool_(true)], true)); + BOOST_TEST(!test("", lower[bool_(false)], true)); + BOOST_TEST(!test("", lower[bool_(true)], false)); + BOOST_TEST(test("false", lower[lit(false)])); + BOOST_TEST(test("true", lower[lit(true)])); + } + + { + BOOST_TEST(test("FALSE", upper[bool_], false)); + BOOST_TEST(test("TRUE", upper[bool_], true)); + BOOST_TEST(test("FALSE", upper[bool_(false)])); + BOOST_TEST(test("TRUE", upper[bool_(true)])); + BOOST_TEST(test("FALSE", upper[bool_(false)], false)); + BOOST_TEST(test("TRUE", upper[bool_(true)], true)); + BOOST_TEST(!test("", upper[bool_(false)], true)); + BOOST_TEST(!test("", upper[bool_(true)], false)); + BOOST_TEST(test("FALSE", upper[lit(false)])); + BOOST_TEST(test("TRUE", upper[lit(true)])); + } + + { + typedef boost::spirit::karma::bool_generator<bool, special_bool_policy> + backwards_bool_type; + backwards_bool_type const backwards_bool = backwards_bool_type(); + + BOOST_TEST(test("eurt", backwards_bool, false)); + BOOST_TEST(test("true", backwards_bool, true)); + BOOST_TEST(test("eurt", backwards_bool(false))); + BOOST_TEST(test("true", backwards_bool(true))); + BOOST_TEST(test("eurt", backwards_bool(false), false)); + BOOST_TEST(test("true", backwards_bool(true), true)); + BOOST_TEST(!test("", backwards_bool(false), true)); + BOOST_TEST(!test("", backwards_bool(true), false)); + } + + { + typedef boost::spirit::karma::bool_generator< + test_bool_data, test_bool_policy> test_bool_type; + test_bool_type const test_bool = test_bool_type(); + + test_bool_data const test_false = test_bool_data(false); + test_bool_data const test_true = test_bool_data(true); + + BOOST_TEST(test("false", test_bool, test_false)); + BOOST_TEST(test("true", test_bool, test_true)); + BOOST_TEST(test("false", test_bool(test_false))); + BOOST_TEST(test("true", test_bool(test_true))); + BOOST_TEST(test("false", test_bool(test_false), test_false)); + BOOST_TEST(test("true", test_bool(test_true), test_true)); + BOOST_TEST(!test("", test_bool(test_false), test_true)); + BOOST_TEST(!test("", test_bool(test_true), test_false)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/buffer.cpp b/src/boost/libs/spirit/test/karma/buffer.cpp new file mode 100644 index 00000000..ef8d12a3 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/buffer.cpp @@ -0,0 +1,53 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_char.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + + { + using boost::spirit::karma::double_; + using boost::spirit::karma::buffer; + + std::vector<double> v; + BOOST_TEST(test("", -buffer['[' << +double_ << ']'], v)); + + v.push_back(1.0); + v.push_back(2.0); + BOOST_TEST(test("[1.02.0]", buffer['[' << +double_ << ']'], v)); + BOOST_TEST(test("[1.02.0]", buffer[buffer['[' << +double_ << ']']], v)); + } + + { + using boost::spirit::karma::double_; + using boost::spirit::karma::buffer; + using boost::spirit::ascii::space; + + std::vector<double> v; + BOOST_TEST(test_delimited("", + -buffer['[' << +double_ << ']'], v, space)); + + v.push_back(1.0); + v.push_back(2.0); + BOOST_TEST(test_delimited("[ 1.0 2.0 ] ", + buffer['[' << +double_ << ']'], v, space)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/case_handling1.cpp b/src/boost/libs/spirit/test/karma/case_handling1.cpp new file mode 100644 index 00000000..2af1dafe --- /dev/null +++ b/src/boost/libs/spirit/test/karma/case_handling1.cpp @@ -0,0 +1,96 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/detail/workaround.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("x", lower['X'])); + BOOST_TEST(test("x", lower['x'])); + + BOOST_TEST(test("x", lower[char_], 'X')); + BOOST_TEST(test("x", lower[char_], 'x')); + BOOST_TEST(test("x", lower[char_('X')])); + BOOST_TEST(test("x", lower[char_('x')])); + + BOOST_TEST(test(" ", lower[space])); + BOOST_TEST(test("\t", lower[space], '\t')); + + BOOST_TEST(test("x", lower[lower['X']])); + BOOST_TEST(test("x", lower[lower['x']])); + + BOOST_TEST(test("x", lower[lower[char_]], 'X')); + BOOST_TEST(test("x", lower[lower[char_]], 'x')); + BOOST_TEST(test("x", lower[lower[char_('X')]])); + BOOST_TEST(test("x", lower[lower[char_('x')]])); + + BOOST_TEST(test(" ", lower[lower[space]])); + BOOST_TEST(test("\t", lower[lower[space]], '\t')); + + BOOST_TEST(test("X", upper[lower['X']])); + BOOST_TEST(test("X", upper[lower['x']])); + + BOOST_TEST(test("X", upper[lower[char_]], 'X')); + BOOST_TEST(test("X", upper[lower[char_]], 'x')); + BOOST_TEST(test("X", upper[lower[char_('X')]])); + BOOST_TEST(test("X", upper[lower[char_('x')]])); + + BOOST_TEST(test(" ", upper[lower[space]])); + BOOST_TEST(test("\t", upper[lower[space]], '\t')); + + BOOST_TEST(test("X", upper['X'])); + BOOST_TEST(test("X", upper['x'])); + + BOOST_TEST(test("X", upper[char_], 'X')); + BOOST_TEST(test("X", upper[char_], 'x')); + BOOST_TEST(test("X", upper[char_('X')])); + BOOST_TEST(test("X", upper[char_('x')])); + + BOOST_TEST(test(" ", upper[space])); + BOOST_TEST(test("\t", upper[space], '\t')); + + BOOST_TEST(test("x", lower[upper['X']])); + BOOST_TEST(test("x", lower[upper['x']])); + + BOOST_TEST(test("x", lower[upper[char_]], 'X')); + BOOST_TEST(test("x", lower[upper[char_]], 'x')); + BOOST_TEST(test("x", lower[upper[char_('X')]])); + BOOST_TEST(test("x", lower[upper[char_('x')]])); + + BOOST_TEST(test(" ", lower[upper[space]])); + BOOST_TEST(test("\t", lower[upper[space]], '\t')); + + BOOST_TEST(test("X", upper[upper['X']])); + BOOST_TEST(test("X", upper[upper['x']])); + + BOOST_TEST(test("X", upper[upper[char_]], 'X')); + BOOST_TEST(test("X", upper[upper[char_]], 'x')); + BOOST_TEST(test("X", upper[upper[char_('X')]])); + BOOST_TEST(test("X", upper[upper[char_('x')]])); + + BOOST_TEST(test(" ", upper[upper[space]])); + BOOST_TEST(test("\t", upper[upper[space]], '\t')); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/case_handling2.cpp b/src/boost/libs/spirit/test/karma/case_handling2.cpp new file mode 100644 index 00000000..16687106 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/case_handling2.cpp @@ -0,0 +1,114 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/detail/workaround.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::standard_wide; + + BOOST_TEST(test(L"x", lower[L'X'])); + BOOST_TEST(test(L"x", lower[L'x'])); + + BOOST_TEST(test(L"x", lower[char_], L'X')); + BOOST_TEST(test(L"x", lower[char_], L'x')); + BOOST_TEST(test(L"x", lower[char_(L'X')])); + BOOST_TEST(test(L"x", lower[char_(L'x')])); + + BOOST_TEST(test(L" ", lower[space])); + BOOST_TEST(test(L"\t", lower[space], L'\t')); + + BOOST_TEST(test(L"x", lower[lower[L'X']])); + BOOST_TEST(test(L"x", lower[lower[L'x']])); + + BOOST_TEST(test(L"x", lower[lower[char_]], L'X')); + BOOST_TEST(test(L"x", lower[lower[char_]], L'x')); + BOOST_TEST(test(L"x", lower[lower[char_(L'X')]])); + BOOST_TEST(test(L"x", lower[lower[char_(L'x')]])); + + BOOST_TEST(test(L" ", lower[lower[space]])); + BOOST_TEST(test(L"\t", lower[lower[space]], L'\t')); + + BOOST_TEST(test(L"X", upper[lower[L'X']])); + BOOST_TEST(test(L"X", upper[lower[L'x']])); + + BOOST_TEST(test(L"X", upper[lower[char_]], L'X')); + BOOST_TEST(test(L"X", upper[lower[char_]], L'x')); + BOOST_TEST(test(L"X", upper[lower[char_(L'X')]])); + BOOST_TEST(test(L"X", upper[lower[char_(L'x')]])); + + BOOST_TEST(test(L" ", upper[lower[space]])); + BOOST_TEST(test(L"\t", upper[lower[space]], L'\t')); + + BOOST_TEST(test(L"X", upper[L'X'])); + BOOST_TEST(test(L"X", upper[L'x'])); + + BOOST_TEST(test(L"X", upper[char_], L'X')); + BOOST_TEST(test(L"X", upper[char_], L'x')); + BOOST_TEST(test(L"X", upper[char_(L'X')])); + BOOST_TEST(test(L"X", upper[char_(L'x')])); + + BOOST_TEST(test(L" ", upper[space])); + BOOST_TEST(test(L"\t", upper[space], L'\t')); + + BOOST_TEST(test(L"x", lower[upper[L'X']])); + BOOST_TEST(test(L"x", lower[upper[L'x']])); + + BOOST_TEST(test(L"x", lower[upper[char_]], L'X')); + BOOST_TEST(test(L"x", lower[upper[char_]], L'x')); + BOOST_TEST(test(L"x", lower[upper[char_(L'X')]])); + BOOST_TEST(test(L"x", lower[upper[char_(L'x')]])); + + BOOST_TEST(test(L" ", lower[upper[space]])); + BOOST_TEST(test(L"\t", lower[upper[space]], L'\t')); + + BOOST_TEST(test(L"X", upper[upper[L'X']])); + BOOST_TEST(test(L"X", upper[upper[L'x']])); + + BOOST_TEST(test(L"X", upper[upper[char_]], L'X')); + BOOST_TEST(test(L"X", upper[upper[char_]], L'x')); + BOOST_TEST(test(L"X", upper[upper[char_(L'X')]])); + BOOST_TEST(test(L"X", upper[upper[char_(L'x')]])); + + BOOST_TEST(test(L" ", upper[upper[space]])); + BOOST_TEST(test(L"\t", upper[upper[space]], L'\t')); + } + + { // test extended ASCII characters + using namespace boost::spirit::iso8859_1; + + BOOST_TEST(test("\xE4\xE4", lower["\xC4\xE4"])); + BOOST_TEST(test("\xE4\xE4", lower["\xC4\xE4"])); + + BOOST_TEST(test("\xC4\xC4", upper["\xC4\xE4"])); + BOOST_TEST(test("\xC4\xC4", upper["\xC4\xE4"])); + } + + { + using namespace boost::spirit::ascii; + using boost::phoenix::val; + + BOOST_TEST(test("x", lower[val('X')])); + BOOST_TEST(test("x", lower[val('x')])); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/case_handling3.cpp b/src/boost/libs/spirit/test/karma/case_handling3.cpp new file mode 100644 index 00000000..018a6184 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/case_handling3.cpp @@ -0,0 +1,110 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/detail/workaround.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { // test extended ASCII characters + using namespace boost::spirit::iso8859_1; + + BOOST_TEST(test("\xE4", lower['\xC4'])); + BOOST_TEST(test("\xE4", lower['\xE4'])); + + BOOST_TEST(test("\xC4", upper['\xC4'])); + BOOST_TEST(test("\xC4", upper['\xE4'])); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("a1- ", lower["a1- "])); + BOOST_TEST(test("a1- ", lower["a1- "])); + BOOST_TEST(test("a1- ", lower["a1- "])); + BOOST_TEST(test("a1- ", lower["A1- "])); + + BOOST_TEST(test("a1- ", lower[string], "a1- ")); + BOOST_TEST(test("a1- ", lower[string], "A1- ")); + BOOST_TEST(test("a1- ", lower[lit("a1- ")])); + BOOST_TEST(test("a1- ", lower[lit("A1- ")])); + BOOST_TEST(test("a1- ", lower[string("a1- ")])); + BOOST_TEST(test("a1- ", lower[string("A1- ")])); + + BOOST_TEST(test("a1- ", lower[lower["a1- "]])); + BOOST_TEST(test("a1- ", lower[lower["a1- "]])); + BOOST_TEST(test("a1- ", lower[lower["a1- "]])); + BOOST_TEST(test("a1- ", lower[lower["A1- "]])); + + BOOST_TEST(test("a1- ", lower[lower[string]], "a1- ")); + BOOST_TEST(test("a1- ", lower[lower[string]], "A1- ")); + BOOST_TEST(test("a1- ", lower[lower[lit("a1- ")]])); + BOOST_TEST(test("a1- ", lower[lower[lit("A1- ")]])); + BOOST_TEST(test("a1- ", lower[lower[string("a1- ")]])); + BOOST_TEST(test("a1- ", lower[lower[string("A1- ")]])); + + BOOST_TEST(test("A1- ", upper[lower["a1- "]])); + BOOST_TEST(test("A1- ", upper[lower["a1- "]])); + BOOST_TEST(test("A1- ", upper[lower["a1- "]])); + BOOST_TEST(test("A1- ", upper[lower["A1- "]])); + + BOOST_TEST(test("A1- ", upper[lower[string]], "a1- ")); + BOOST_TEST(test("A1- ", upper[lower[string]], "A1- ")); + BOOST_TEST(test("A1- ", upper[lower[lit("a1- ")]])); + BOOST_TEST(test("A1- ", upper[lower[lit("A1- ")]])); + BOOST_TEST(test("A1- ", upper[lower[string("a1- ")]])); + BOOST_TEST(test("A1- ", upper[lower[string("A1- ")]])); + + BOOST_TEST(test("A1- ", upper["a1- "])); + BOOST_TEST(test("A1- ", upper["a1- "])); + BOOST_TEST(test("A1- ", upper["a1- "])); + BOOST_TEST(test("A1- ", upper["A1- "])); + + BOOST_TEST(test("A1- ", upper[string], "a1- ")); + BOOST_TEST(test("A1- ", upper[string], "A1- ")); + BOOST_TEST(test("A1- ", upper[lit("a1- ")])); + BOOST_TEST(test("A1- ", upper[lit("A1- ")])); + + BOOST_TEST(test("a1- ", lower[upper["a1- "]])); + BOOST_TEST(test("a1- ", lower[upper["a1- "]])); + BOOST_TEST(test("a1- ", lower[upper["a1- "]])); + BOOST_TEST(test("a1- ", lower[upper["A1- "]])); + + BOOST_TEST(test("a1- ", lower[upper[string]], "a1- ")); + BOOST_TEST(test("a1- ", lower[upper[string]], "A1- ")); + BOOST_TEST(test("a1- ", lower[upper[lit("a1- ")]])); + BOOST_TEST(test("a1- ", lower[upper[lit("A1- ")]])); + BOOST_TEST(test("a1- ", lower[upper[string("a1- ")]])); + BOOST_TEST(test("a1- ", lower[upper[string("A1- ")]])); + + BOOST_TEST(test("A1- ", upper[upper["a1- "]])); + BOOST_TEST(test("A1- ", upper[upper["a1- "]])); + BOOST_TEST(test("A1- ", upper[upper["a1- "]])); + BOOST_TEST(test("A1- ", upper[upper["A1- "]])); + + BOOST_TEST(test("A1- ", upper[upper[string]], "a1- ")); + BOOST_TEST(test("A1- ", upper[upper[string]], "A1- ")); + BOOST_TEST(test("A1- ", upper[upper[lit("a1- ")]])); + BOOST_TEST(test("A1- ", upper[upper[lit("A1- ")]])); + BOOST_TEST(test("A1- ", upper[upper[string("a1- ")]])); + BOOST_TEST(test("A1- ", upper[upper[string("A1- ")]])); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/center_alignment.cpp b/src/boost/libs/spirit/test/karma/center_alignment.cpp new file mode 100644 index 00000000..a5e01efe --- /dev/null +++ b/src/boost/libs/spirit/test/karma/center_alignment.cpp @@ -0,0 +1,87 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_upper_lower_case.hpp> + +#include <string> +#include <iterator> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test(" x ", center[char_('x')])); + BOOST_TEST(test(" x ", center[char_], 'x')); + BOOST_TEST(test(" x ", center['x'])); + + BOOST_TEST(test(" x ", center(10)[char_('x')])); + BOOST_TEST(test(" x ", center(10)[char_], 'x')); + BOOST_TEST(test(" x ", center(10)['x'])); + + BOOST_TEST(test("*****x****", center(10, char_('*'))[char_('x')])); + BOOST_TEST(test("*****x****", center(10, '*')[char_], 'x')); + BOOST_TEST(test("*****x****", center(10, '*')['x'])); + BOOST_TEST(test("aaaaaxaaaa", lower[center(10, 'A')['X']])); + BOOST_TEST(test("AAAAAXAAAA", upper[center(10, 'a')['x']])); + + + BOOST_TEST(test("*****x****", center(char_('*'))[char_('x')])); + BOOST_TEST(test("*****x****", center(char_('*'))[char_], 'x')); + BOOST_TEST(test("*****x****", center(char_('*'))['x'])); + + BOOST_TEST(test(" x ", center(11)[char_('x')])); + + BOOST_TEST(test(" abc ", center[lit("abc")])); + BOOST_TEST(test(" abc ", center[string], "abc")); + + BOOST_TEST(test(" abc ", center(10)[lit("abc")])); + BOOST_TEST(test(" abc ", center(10)[string], "abc")); + BOOST_TEST(test(" abc ", center(10)["abc"])); + + BOOST_TEST(test(" abc ", center(11)[lit("abc")])); + BOOST_TEST(test(" ab ", center(11)[lit("ab")])); + + BOOST_TEST(test("****abc***", center(10, char_('*'))[lit("abc")])); + BOOST_TEST(test("****abc***", center(10, '*')[string], "abc")); + BOOST_TEST(test("****abc***", center(10, '*')["abc"])); + + BOOST_TEST(test("****abc***", center(char_('*'))[lit("abc")])); + BOOST_TEST(test("****abc***", center(char_('*'))[string], "abc")); + BOOST_TEST(test("****abc***", center(char_('*'))["abc"])); + + BOOST_TEST(test(" abc ", center(11)[lit("abc")])); + + BOOST_TEST(test(" 100 ", center[int_(100)])); + BOOST_TEST(test(" 100 ", center[int_], 100)); + + BOOST_TEST(test(" 100 ", center(10)[int_(100)])); + BOOST_TEST(test(" 100 ", center(10)[int_], 100)); + + BOOST_TEST(test("****100***", center(10, char_('*'))[int_(100)])); + BOOST_TEST(test("****100***", center(10, '*')[int_], 100)); + + BOOST_TEST(test(" 100 ", center(11)[int_(100)])); + + BOOST_TEST(test("****100***", center(char_('*'))[int_(100)])); + BOOST_TEST(test("****100***", center(char_('*'))[int_], 100)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/char1.cpp b/src/boost/libs/spirit/test/karma/char1.cpp new file mode 100644 index 00000000..e1c4dc40 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/char1.cpp @@ -0,0 +1,176 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/spirit/include/phoenix_statement.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::phoenix; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("x", 'x')); + BOOST_TEST(test(L"x", L'x')); + BOOST_TEST(!test("x", 'y')); + BOOST_TEST(!test(L"x", L'y')); + + BOOST_TEST(test("x", "x")); + BOOST_TEST(test(L"x", L"x")); + BOOST_TEST(!test("x", "y")); + BOOST_TEST(!test(L"x", L"y")); + + BOOST_TEST(test("x", char_, 'x')); + BOOST_TEST(test(L"x", char_, L'x')); + BOOST_TEST(!test("x", char_, 'y')); + BOOST_TEST(!test(L"x", char_, L'y')); + + BOOST_TEST(test("x", char_('x'))); + BOOST_TEST(!test("x", char_('y'))); + + BOOST_TEST(test("x", char_('x'), 'x')); + BOOST_TEST(!test("", char_('y'), 'x')); + + BOOST_TEST(test("x", char_("x"))); + + BOOST_TEST(test("a", char_('a', 'z'), 'a')); + BOOST_TEST(test("b", char_('a', 'z'), 'b')); + BOOST_TEST(!test("", char_('a', 'z'), 'A')); + + BOOST_TEST(test("a", char_("a-z"), 'a')); + BOOST_TEST(test("b", char_("a-z"), 'b')); + BOOST_TEST(!test("", char_("a-z"), 'A')); + +#if defined(KARMA_FAIL_COMPILATION) + BOOST_TEST(test("x", char_)); // anychar without a parameter doesn't make any sense + BOOST_TEST(test("", char_('a', 'z'))); // char sets without attribute neither +#endif + + BOOST_TEST(!test("", ~char_('x'))); + + BOOST_TEST(!test("", ~char_('x'), 'x')); + BOOST_TEST(test("x", ~char_('y'), 'x')); + + BOOST_TEST(!test("", ~char_("x"))); + + BOOST_TEST(!test("", ~char_('a', 'z'), 'a')); + BOOST_TEST(!test("", ~char_('a', 'z'), 'b')); + BOOST_TEST(test("A", ~char_('a', 'z'), 'A')); + + BOOST_TEST(!test("", ~char_("a-z"), 'a')); + BOOST_TEST(!test("", ~char_("a-z"), 'b')); + BOOST_TEST(test("A", ~char_("a-z"), 'A')); + + BOOST_TEST(test("x", ~~char_('x'))); + BOOST_TEST(!test("x", ~~char_('y'))); + + BOOST_TEST(test("x", ~~char_('x'), 'x')); + BOOST_TEST(!test("", ~~char_('y'), 'x')); + + BOOST_TEST(test("x", ~~char_("x"))); + + BOOST_TEST(test("a", ~~char_('a', 'z'), 'a')); + BOOST_TEST(test("b", ~~char_('a', 'z'), 'b')); + BOOST_TEST(!test("", ~~char_('a', 'z'), 'A')); + + BOOST_TEST(test("a", ~~char_("a-z"), 'a')); + BOOST_TEST(test("b", ~~char_("a-z"), 'b')); + BOOST_TEST(!test("", ~~char_("a-z"), 'A')); + } + + { + using namespace boost::spirit::standard_wide; + + BOOST_TEST(test(L"x", 'x')); + BOOST_TEST(test(L"x", L'x')); + BOOST_TEST(!test(L"x", 'y')); + BOOST_TEST(!test(L"x", L'y')); + + BOOST_TEST(test(L"x", "x")); + BOOST_TEST(test(L"x", L"x")); + BOOST_TEST(!test(L"x", "y")); + BOOST_TEST(!test(L"x", L"y")); + + BOOST_TEST(test(L"x", char_, 'x')); + BOOST_TEST(test(L"x", char_, L'x')); + BOOST_TEST(!test(L"x", char_, 'y')); + BOOST_TEST(!test(L"x", char_, L'y')); + + BOOST_TEST(test(L"x", char_('x'))); + BOOST_TEST(test(L"x", char_(L'x'))); + BOOST_TEST(!test(L"x", char_('y'))); + BOOST_TEST(!test(L"x", char_(L'y'))); + + BOOST_TEST(test(L"x", char_(L'x'), L'x')); + BOOST_TEST(!test(L"", char_('y'), L'x')); + + BOOST_TEST(test(L"x", char_(L"x"))); + + BOOST_TEST(test("a", char_("a", "z"), 'a')); + BOOST_TEST(test(L"a", char_(L"a", L"z"), L'a')); + +#if defined(KARMA_FAIL_COMPILATION) + BOOST_TEST(test("x", char_)); // anychar without a parameter doesn't make any sense +#endif + + BOOST_TEST(!test(L"", ~char_('x'))); + BOOST_TEST(!test(L"", ~char_(L'x'))); + + BOOST_TEST(!test(L"", ~char_(L'x'), L'x')); + BOOST_TEST(test(L"x", ~char_('y'), L'x')); + + BOOST_TEST(!test(L"", ~char_(L"x"))); + } + + { // lazy chars + namespace ascii = boost::spirit::ascii; + namespace wide = boost::spirit::standard_wide; + + using namespace boost::phoenix; + + BOOST_TEST((test("x", ascii::char_(val('x'))))); + BOOST_TEST((test(L"x", wide::char_(val(L'x'))))); + + BOOST_TEST((test("x", ascii::char_(val('x')), 'x'))); + BOOST_TEST((test(L"x", wide::char_(val(L'x')), L'x'))); + + BOOST_TEST((!test("", ascii::char_(val('y')), 'x'))); + BOOST_TEST((!test(L"", wide::char_(val(L'y')), L'x'))); + } + + // we can pass optionals as attributes to any generator + { + namespace ascii = boost::spirit::ascii; + namespace wide = boost::spirit::standard_wide; + + boost::optional<char> v; + boost::optional<wchar_t> w; + + BOOST_TEST(!test("", ascii::char_, v)); + BOOST_TEST(!test(L"", wide::char_, w)); + + BOOST_TEST(!test("", ascii::char_('x'), v)); + BOOST_TEST(!test(L"", wide::char_(L'x'), w)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/char2.cpp b/src/boost/libs/spirit/test/karma/char2.cpp new file mode 100644 index 00000000..aae587bf --- /dev/null +++ b/src/boost/libs/spirit/test/karma/char2.cpp @@ -0,0 +1,158 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/spirit/include/phoenix_statement.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::phoenix; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test(" ", space)); + BOOST_TEST(test(L" ", space)); + BOOST_TEST(!test("\t", space)); + BOOST_TEST(!test(L"\t", space)); + + BOOST_TEST(test(" ", space, ' ')); + BOOST_TEST(test(L" ", space, L' ')); + BOOST_TEST(test("\t", space, '\t')); + BOOST_TEST(test(L"\t", space, L'\t')); + + BOOST_TEST(!test("", space, 'x')); + BOOST_TEST(!test(L"", space, L'x')); + + BOOST_TEST(!test(" ", ~space, ' ')); + BOOST_TEST(!test(L" ", ~space, L' ')); + + BOOST_TEST(test("x", ~space, 'x')); + BOOST_TEST(test(L"x", ~space, L'x')); + } + + { + using namespace boost::spirit::standard_wide; + + BOOST_TEST(test(" ", space)); + BOOST_TEST(test(L" ", space)); + BOOST_TEST(!test("\t", space)); + BOOST_TEST(!test(L"\t", space)); + + BOOST_TEST(test(" ", space, ' ')); + BOOST_TEST(test(L" ", space, L' ')); + BOOST_TEST(test("\t", space, '\t')); + BOOST_TEST(test(L"\t", space, L'\t')); + + BOOST_TEST(!test("", space, 'x')); + BOOST_TEST(!test(L"", space, L'x')); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited("x ", 'x', ' ')); + BOOST_TEST(test_delimited(L"x ", L'x', L' ')); + BOOST_TEST(!test_delimited("x ", 'y', ' ')); + BOOST_TEST(!test_delimited(L"x ", L'y', L' ')); + + BOOST_TEST(test_delimited("x ", 'x', ' ')); + BOOST_TEST(test_delimited(L"x ", L'x', L' ')); + BOOST_TEST(!test_delimited("x ", 'y', ' ')); + BOOST_TEST(!test_delimited(L"x ", L'y', L' ')); + + BOOST_TEST(test_delimited("x ", char_, 'x', ' ')); + BOOST_TEST(test_delimited(L"x ", char_, L'x', L' ')); + BOOST_TEST(!test_delimited("x ", char_, 'y', ' ')); + BOOST_TEST(!test_delimited(L"x ", char_, L'y', L' ')); + + BOOST_TEST(test_delimited("x ", char_('x'), ' ')); + BOOST_TEST(!test_delimited("x ", char_('y'), ' ')); + + BOOST_TEST(test_delimited("x ", char_('x'), 'x', ' ')); + BOOST_TEST(!test_delimited("", char_('y'), 'x', ' ')); + + BOOST_TEST(test_delimited("x ", char_("x"), ' ')); + +#if defined(KARMA_FAIL_COMPILATION) + BOOST_TEST(test_delimited("x ", char_, ' ')); // anychar without a parameter doesn't make any sense +#endif + } + + { // pre-delimiting + { + std::string generated; + std::back_insert_iterator<std::string> it(generated); + BOOST_TEST(karma::generate_delimited(it, '_', '^' + , karma::delimit_flag::predelimit)); + BOOST_TEST(generated == "^_^"); + } + { + using namespace boost::spirit::standard_wide; + std::basic_string<wchar_t> generated; + std::back_insert_iterator<std::basic_string<wchar_t> > it(generated); + BOOST_TEST(karma::generate_delimited(it, char_, L'.' + , karma::delimit_flag::predelimit, L'x')); + BOOST_TEST(generated == L".x."); + } + } + + // action tests + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("x", char_[_1 = val('x')])); + BOOST_TEST(!test("x", char_[_1 = val('y')])); + } + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + // yes, we can use phoenix expressions as attributes as well + // but only if we include karma_phoenix_attributes.hpp + { + namespace ascii = boost::spirit::ascii; + namespace phoenix = boost::phoenix; + + BOOST_TEST(test("x", ascii::char_, phoenix::val('x'))); + + char c = 'x'; + BOOST_TEST(test("x", ascii::char_, phoenix::ref(c))); + BOOST_TEST(test("y", ascii::char_, ++phoenix::ref(c))); + } +#endif + + { + namespace ascii = boost::spirit::ascii; + namespace wide = boost::spirit::standard_wide; + + boost::optional<char> v ('x'); + boost::optional<wchar_t> w (L'x'); + + BOOST_TEST(test("x", ascii::char_, v)); + BOOST_TEST(test(L"x", wide::char_, w)); + BOOST_TEST(test("x", ascii::char_('x'), v)); + BOOST_TEST(test(L"x", wide::char_(L'x'), w)); + BOOST_TEST(!test("", ascii::char_('y'), v)); + BOOST_TEST(!test(L"", wide::char_(L'y'), w)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/char3.cpp b/src/boost/libs/spirit/test/karma/char3.cpp new file mode 100644 index 00000000..55e5978b --- /dev/null +++ b/src/boost/libs/spirit/test/karma/char3.cpp @@ -0,0 +1,103 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/spirit/include/phoenix_statement.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::phoenix; + using boost::spirit::karma::lit; + + { + BOOST_TEST(test("x", lit('x'))); + BOOST_TEST(!test("x", lit('y'))); + + BOOST_TEST(test("x", lit('x'), 'x')); + BOOST_TEST(!test("", lit('y'), 'x')); + +// BOOST_TEST(test("a", lit('a', 'z'), 'a')); +// BOOST_TEST(test("b", lit('a', 'z'), 'b')); +// BOOST_TEST(!test("", lit('a', 'z'), 'A')); + + BOOST_TEST(!test("", ~lit('x'))); + + BOOST_TEST(!test("", ~lit('x'), 'x')); + BOOST_TEST(test("x", ~lit('y'), 'x')); + +// BOOST_TEST(!test("", ~lit('a', 'z'), 'a')); +// BOOST_TEST(!test("", ~lit('a', 'z'), 'b')); +// BOOST_TEST(test("A", ~lit('a', 'z'), 'A')); + + BOOST_TEST(test("x", ~~lit('x'))); + BOOST_TEST(!test("x", ~~lit('y'))); + + BOOST_TEST(test("x", ~~lit('x'), 'x')); + BOOST_TEST(!test("", ~~lit('y'), 'x')); + +// BOOST_TEST(test("a", ~~lit('a', 'z'), 'a')); +// BOOST_TEST(test("b", ~~lit('a', 'z'), 'b')); +// BOOST_TEST(!test("", ~~lit('a', 'z'), 'A')); + } + + { + BOOST_TEST(test(L"x", lit('x'))); + BOOST_TEST(test(L"x", lit(L'x'))); + BOOST_TEST(!test(L"x", lit('y'))); + BOOST_TEST(!test(L"x", lit(L'y'))); + + BOOST_TEST(test(L"x", lit(L'x'), L'x')); + BOOST_TEST(!test(L"", lit('y'), L'x')); + +// BOOST_TEST(test("a", lit("a", "z"), 'a')); +// BOOST_TEST(test(L"a", lit(L"a", L"z"), L'a')); + + BOOST_TEST(!test(L"", ~lit('x'))); + BOOST_TEST(!test(L"", ~lit(L'x'))); + + BOOST_TEST(!test(L"", ~lit(L'x'), L'x')); + BOOST_TEST(test(L"x", ~lit('y'), L'x')); + } + + { // lazy chars + using namespace boost::phoenix; + + BOOST_TEST((test("x", lit(val('x'))))); + BOOST_TEST((test(L"x", lit(val(L'x'))))); + + BOOST_TEST((test("x", lit(val('x')), 'x'))); + BOOST_TEST((test(L"x", lit(val(L'x')), L'x'))); + + BOOST_TEST((!test("", lit(val('y')), 'x'))); + BOOST_TEST((!test(L"", lit(val(L'y')), L'x'))); + } + + // we can pass optionals as attributes to any generator + { + boost::optional<char> v; + boost::optional<wchar_t> w; + + BOOST_TEST(!test("", lit('x'), v)); + BOOST_TEST(!test(L"", lit(L'x'), w)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/char_class.cpp b/src/boost/libs/spirit/test/karma/char_class.cpp new file mode 100644 index 00000000..88c5ad6b --- /dev/null +++ b/src/boost/libs/spirit/test/karma/char_class.cpp @@ -0,0 +1,197 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include <string> +#include <iterator> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace spirit_test; + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + BOOST_TEST(test("a", alpha, 'a')); + BOOST_TEST(!test("", alpha, '1')); + BOOST_TEST(test(" ", blank, ' ')); + BOOST_TEST(!test("", blank, 'x')); + BOOST_TEST(test("1", digit, '1')); + BOOST_TEST(!test("", digit, 'x')); + BOOST_TEST(test("a", lower, 'a')); + BOOST_TEST(!test("", lower, 'A')); + BOOST_TEST(test("!", punct, '!')); + BOOST_TEST(!test("", punct, 'x')); + BOOST_TEST(test(" ", space)); + BOOST_TEST(test(" ", space, ' ')); + BOOST_TEST(!test("", space, '\n')); + BOOST_TEST(test("\r", space, '\r')); + BOOST_TEST(test("\t", space, '\t')); + BOOST_TEST(test("A", upper, 'A')); + BOOST_TEST(!test("", upper, 'a')); + BOOST_TEST(test("A", xdigit, 'A')); + BOOST_TEST(test("0", xdigit, '0')); + BOOST_TEST(test("f", xdigit, 'f')); + BOOST_TEST(!test("", xdigit, 'g')); + } + + { + using namespace boost::spirit::ascii; + BOOST_TEST(!test("", ~alpha, 'a')); + BOOST_TEST(test("1", ~alpha, '1')); + BOOST_TEST(!test("", ~blank, ' ')); + BOOST_TEST(test("x", ~blank, 'x')); + BOOST_TEST(!test("", ~digit, '1')); + BOOST_TEST(test("x", ~digit, 'x')); + BOOST_TEST(!test("", ~lower, 'a')); + BOOST_TEST(test("A", ~lower, 'A')); + BOOST_TEST(!test("", ~punct, '!')); + BOOST_TEST(test("x", ~punct, 'x')); + BOOST_TEST(!test("", ~space)); + BOOST_TEST(!test("", ~space, ' ')); + BOOST_TEST(!test("", ~space, '\r')); + BOOST_TEST(!test("", ~space, '\t')); + BOOST_TEST(!test("", ~upper, 'A')); + BOOST_TEST(test("a", ~upper, 'a')); + BOOST_TEST(!test("", ~xdigit, 'A')); + BOOST_TEST(!test("", ~xdigit, '0')); + BOOST_TEST(!test("", ~xdigit, 'f')); + BOOST_TEST(test("g", ~xdigit, 'g')); + } + + { + using namespace boost::spirit::ascii; + BOOST_TEST(test("a", ~~alpha, 'a')); + BOOST_TEST(!test("", ~~alpha, '1')); + BOOST_TEST(test(" ", ~~blank, ' ')); + BOOST_TEST(!test("", ~~blank, 'x')); + BOOST_TEST(test("1", ~~digit, '1')); + BOOST_TEST(!test("", ~~digit, 'x')); + BOOST_TEST(test("a", ~~lower, 'a')); + BOOST_TEST(!test("", ~~lower, 'A')); + BOOST_TEST(test("!", ~~punct, '!')); + BOOST_TEST(!test("", ~~punct, 'x')); + BOOST_TEST(test(" ", ~~space)); + BOOST_TEST(test(" ", ~~space, ' ')); + BOOST_TEST(!test("", ~~space, '\n')); + BOOST_TEST(test("\r", ~~space, '\r')); + BOOST_TEST(test("\t", ~~space, '\t')); + BOOST_TEST(test("A", ~~upper, 'A')); + BOOST_TEST(!test("", ~~upper, 'a')); + BOOST_TEST(test("A", ~~xdigit, 'A')); + BOOST_TEST(test("0", ~~xdigit, '0')); + BOOST_TEST(test("f", ~~xdigit, 'f')); + BOOST_TEST(!test("", ~~xdigit, 'g')); + } + + { + using namespace boost::spirit::ascii; + BOOST_TEST(test("a", lower[alpha], 'a')); + BOOST_TEST(!test("", lower[alpha], 'A')); + BOOST_TEST(!test("", lower[alpha], '1')); + BOOST_TEST(test("a", lower[alnum], 'a')); + BOOST_TEST(!test("", lower[alnum], 'A')); + BOOST_TEST(test("1", lower[alnum], '1')); + + BOOST_TEST(!test("", upper[alpha], 'a')); + BOOST_TEST(test("A", upper[alpha], 'A')); + BOOST_TEST(!test("", upper[alpha], '1')); + BOOST_TEST(!test("", upper[alnum], 'a')); + BOOST_TEST(test("A", upper[alnum], 'A')); + BOOST_TEST(test("1", upper[alnum], '1')); + } + + { + using namespace boost::spirit::iso8859_1; + BOOST_TEST(test("a", alpha, 'a')); + BOOST_TEST(!test("", alpha, '1')); + BOOST_TEST(test(" ", blank, ' ')); + BOOST_TEST(!test("", blank, 'x')); + BOOST_TEST(test("1", digit, '1')); + BOOST_TEST(!test("", digit, 'x')); + BOOST_TEST(test("a", lower, 'a')); + BOOST_TEST(!test("", lower, 'A')); + BOOST_TEST(test("!", punct, '!')); + BOOST_TEST(!test("", punct, 'x')); + BOOST_TEST(test(" ", space)); + BOOST_TEST(test(" ", space, ' ')); + BOOST_TEST(!test("", space, '\n')); + BOOST_TEST(test("\r", space, '\r')); + BOOST_TEST(test("\t", space, '\t')); + BOOST_TEST(test("A", upper, 'A')); + BOOST_TEST(!test("", upper, 'a')); + BOOST_TEST(test("A", xdigit, 'A')); + BOOST_TEST(test("0", xdigit, '0')); + BOOST_TEST(test("f", xdigit, 'f')); + BOOST_TEST(!test("", xdigit, 'g')); + + + // test extended ASCII characters + BOOST_TEST(test("\xE9", alpha, '\xE9')); + BOOST_TEST(test("\xE9", lower, '\xE9')); + BOOST_TEST(!test("", upper, '\xE9')); + } + + { + using namespace boost::spirit::standard; + BOOST_TEST(test("a", alpha, 'a')); + BOOST_TEST(!test("", alpha, '1')); + BOOST_TEST(test(" ", blank, ' ')); + BOOST_TEST(!test("", blank, 'x')); + BOOST_TEST(test("1", digit, '1')); + BOOST_TEST(!test("", digit, 'x')); + BOOST_TEST(test("a", lower, 'a')); + BOOST_TEST(!test("", lower, 'A')); + BOOST_TEST(test("!", punct, '!')); + BOOST_TEST(!test("", punct, 'x')); + BOOST_TEST(test(" ", space)); + BOOST_TEST(test(" ", space, ' ')); + BOOST_TEST(!test("", space, '\n')); + BOOST_TEST(test("\r", space, '\r')); + BOOST_TEST(test("\t", space, '\t')); + BOOST_TEST(test("A", upper, 'A')); + BOOST_TEST(!test("", upper, 'a')); + BOOST_TEST(test("A", xdigit, 'A')); + BOOST_TEST(test("0", xdigit, '0')); + BOOST_TEST(test("f", xdigit, 'f')); + BOOST_TEST(!test("", xdigit, 'g')); + } + + { + using namespace boost::spirit::standard_wide; + BOOST_TEST(test(L"a", alpha, L'a')); + BOOST_TEST(!test(L"", alpha, L'1')); + BOOST_TEST(test(L" ", blank, L' ')); + BOOST_TEST(!test(L"", blank, L'x')); + BOOST_TEST(test(L"1", digit, L'1')); + BOOST_TEST(!test(L"", digit, L'x')); + BOOST_TEST(test(L"a", lower, L'a')); + BOOST_TEST(!test(L"", lower, L'A')); + BOOST_TEST(test(L"!", punct, L'!')); + BOOST_TEST(!test(L"", punct, L'x')); + BOOST_TEST(test(L" ", space)); + BOOST_TEST(test(L" ", space, L' ')); + BOOST_TEST(!test(L"", space, L'\n')); + BOOST_TEST(test(L"\r", space, L'\r')); + BOOST_TEST(test(L"\t", space, L'\t')); + BOOST_TEST(test(L"A", upper, L'A')); + BOOST_TEST(!test(L"", upper, L'a')); + BOOST_TEST(test(L"A", xdigit, L'A')); + BOOST_TEST(test(L"0", xdigit, L'0')); + BOOST_TEST(test(L"f", xdigit, L'f')); + BOOST_TEST(!test(L"", xdigit, L'g')); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/columns.cpp b/src/boost/libs/spirit/test/karma/columns.cpp new file mode 100644 index 00000000..070724b4 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/columns.cpp @@ -0,0 +1,97 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> + +#include "test.hpp" + +using namespace spirit_test; +namespace karma = boost::spirit::karma; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + { + using karma::columns; + using karma::int_; + + std::vector<int> v; + for (int i = 0; i < 11; ++i) + v.push_back(i); + + BOOST_TEST(test("01234\n56789\n10\n", columns[*int_], v)); + BOOST_TEST(test_delimited("0 1 2 3 4 \n5 6 7 8 9 \n10 \n", columns[*int_] + , v, karma::space)); + } + + { + using karma::columns; + using karma::int_; + + std::vector<int> v; + for (int i = 0; i < 11; ++i) + v.push_back(i); + + BOOST_TEST(test("012\n345\n678\n910\n", columns(3)[*int_], v)); + BOOST_TEST(test_delimited("0 1 2 \n3 4 5 \n6 7 8 \n9 10 \n" + , columns(3)[*int_], v, karma::space)); + } + + { + using karma::columns; + using karma::int_; + using boost::phoenix::ref; + + std::vector<int> v; + for (int i = 0; i < 11; ++i) + v.push_back(i); + + int count = 3; + BOOST_TEST(test("012\n345\n678\n910\n", columns(ref(count))[*int_], v)); + BOOST_TEST(test_delimited("0 1 2 \n3 4 5 \n6 7 8 \n9 10 \n" + , columns(val(ref(count)))[*int_], v, karma::space)); + } + + { + using karma::columns; + using karma::int_; + using karma::lit; + + std::vector<int> v; + for (int i = 0; i < 11; ++i) + v.push_back(i); + + BOOST_TEST(test("01234\t56789\t10\t", columns(lit('\t'))[*int_], v)); + BOOST_TEST(test_delimited("0 1 2 3 4 \t5 6 7 8 9 \t10 \t" + , columns(lit('\t'))[*int_], v, karma::space)); + } + + { + using karma::columns; + using karma::int_; + using karma::lit; + + std::vector<int> v; + for (int i = 0; i < 11; ++i) + v.push_back(i); + + BOOST_TEST(test("012\t345\t678\t910\t", columns(3, lit('\t'))[*int_], v)); + BOOST_TEST(test_delimited("0 1 2 \t3 4 5 \t6 7 8 \t9 10 \t" + , columns(3, lit('\t'))[*int_], v, karma::space)); + } + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/debug.cpp b/src/boost/libs/spirit/test/karma/debug.cpp new file mode 100644 index 00000000..19137d44 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/debug.cpp @@ -0,0 +1,107 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2001-2011 Joel de Guzman +// +// 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) + +#define BOOST_SPIRIT_KARMA_DEBUG + +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <string> +#include <cstring> +#include <iostream> +#include "test.hpp" + +int main() +{ + using spirit_test::test; + using spirit_test::test_delimited; + + using namespace boost::spirit::ascii; + using namespace boost::spirit::karma::labels; + using boost::spirit::karma::locals; + using boost::spirit::karma::rule; + using boost::spirit::karma::char_; + using boost::spirit::karma::debug; + using boost::spirit::karma::space; + using boost::spirit::karma::eps; + + typedef spirit_test::output_iterator<char>::type outiter_type; + + { // basic tests + rule<outiter_type, char()> a, b, c; + rule<outiter_type, std::vector<char>()> start; + + std::vector<char> v; + v.push_back('a'); + v.push_back('b'); + v.push_back('a'); + v.push_back('c'); + v.push_back('a'); + v.push_back('b'); + v.push_back('b'); + v.push_back('a'); + + a = char_('a'); + b = char_('b'); + c = char_('c'); + BOOST_SPIRIT_DEBUG_NODE(a); + BOOST_SPIRIT_DEBUG_NODE(b); + BOOST_SPIRIT_DEBUG_NODE(c); + + start = *(a | b | c); + BOOST_SPIRIT_DEBUG_NODE(start); + BOOST_TEST(test("abacabba", start, v)); + + // ignore the delimiter + BOOST_TEST(test_delimited("abacabba ", start, v, space)); + + std::vector<char> v1; + v1.push_back('b'); + v1.push_back('c'); + + start = (a | b) << c; + BOOST_SPIRIT_DEBUG_NODE(start); + BOOST_TEST(test("bc", start, v1)); + } + + { // tests with locals + rule<outiter_type, char()> a, b, c; + rule<outiter_type, std::vector<char>(), locals<int, double> > start; + + std::vector<char> v; + v.push_back('a'); + v.push_back('b'); + v.push_back('a'); + v.push_back('c'); + v.push_back('a'); + v.push_back('b'); + v.push_back('b'); + v.push_back('a'); + + a = char_('a'); + b = char_('b'); + c = char_('c'); + BOOST_SPIRIT_DEBUG_NODE(a); + BOOST_SPIRIT_DEBUG_NODE(b); + BOOST_SPIRIT_DEBUG_NODE(c); + + start %= eps[_a = 0, _b = 2.0] << *(a[++_a] | b | c); + BOOST_SPIRIT_DEBUG_NODE(start); + BOOST_TEST(test("abacabba", start, v)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/delimiter.cpp b/src/boost/libs/spirit/test/karma/delimiter.cpp new file mode 100644 index 00000000..15133189 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/delimiter.cpp @@ -0,0 +1,83 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_string.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test("a b ", delimit[char_('a') << 'b'])); + BOOST_TEST(test("a*b*", delimit('*')[char_('a') << 'b'])); + + BOOST_TEST(test("ab c d", + char_('a') << delimit[char_('b') << 'c'] << 'd')); + BOOST_TEST(test("ab*c*d", + char_('a') << delimit('*')[char_('b') << 'c'] << 'd')); + + BOOST_TEST(test_delimited("a b ", delimit[char_('a') << 'b'], char_(' '))); + BOOST_TEST(test_delimited("a*b*", delimit('*')[char_('a') << 'b'], char_(' '))); + + BOOST_TEST(test_delimited("a b c d ", + char_('a') << delimit[char_('b') << 'c'] << 'd', char_(' '))); + BOOST_TEST(test_delimited("a b*c*d ", + char_('a') << delimit('*')[char_('b') << 'c'] << 'd', char_(' '))); + } + + { + BOOST_TEST(test("ab", verbatim[char_('a') << 'b'])); + BOOST_TEST(test("abcd", + char_('a') << verbatim[char_('b') << 'c'] << 'd')); + + BOOST_TEST(test_delimited("ab ", + verbatim[char_('a') << 'b'], char_(' '))); + BOOST_TEST(test_delimited("a bc d ", + char_('a') << verbatim[char_('b') << 'c'] << 'd', char_(' '))); + } + + { + BOOST_TEST(test("ab", no_delimit[char_('a') << 'b'])); + BOOST_TEST(test("abcd", + char_('a') << no_delimit[char_('b') << 'c'] << 'd')); + + BOOST_TEST(test_delimited("ab", + no_delimit[char_('a') << 'b'], char_(' '))); + BOOST_TEST(test_delimited("a bcd ", + char_('a') << no_delimit[char_('b') << 'c'] << 'd', char_(' '))); + } + + { + // The doubled delimiters at the end are generated by the 'b' generator + // and the verbatim[] directive. Currently, there is no easy way to + // avoid this. + BOOST_TEST(test("a b ", delimit[verbatim[delimit[char_('a') << 'b']]])); + BOOST_TEST(test_delimited("a*b**", + verbatim[delimit[char_('a') << 'b']], char_('*'))); + } + + { + karma::rule<output_iterator<char>::type, BOOST_TYPEOF(", ")> r = "abc"; + BOOST_TEST(test("abc, ", delimit(", ")[r])); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/duplicate.cpp b/src/boost/libs/spirit/test/karma/duplicate.cpp new file mode 100644 index 00000000..57f824b4 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/duplicate.cpp @@ -0,0 +1,56 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma.hpp> + +#include <iostream> +#include "test.hpp" + +using namespace spirit_test; + +int main() +{ + using boost::spirit::karma::double_; + using boost::spirit::karma::space; + using boost::spirit::karma::duplicate; + + // test for sequences + { + BOOST_TEST(test("2.02.0", duplicate[double_ << double_], 2.0)); + BOOST_TEST(test_delimited("2.0 2.0 ", + duplicate[double_ << double_], 2.0, space)); + BOOST_TEST(test("2.02.02.0", + duplicate[double_ << double_ << double_], 2.0)); + BOOST_TEST(test_delimited("2.0 2.0 2.0 ", + duplicate[double_ << double_ << double_], 2.0, space)); + } + + // test for non-sequences + { + BOOST_TEST(test("2.02.0", duplicate["2.0" << double_], 2.0)); + BOOST_TEST(test_delimited("2.0 2.0 ", + duplicate["2.0" << double_], 2.0, space)); + } + + // test for subjects exposing no attribute + { + BOOST_TEST(test("2.02.0", duplicate["2.0"] << double_, 2.0)); + BOOST_TEST(test_delimited("2.0 2.0 ", + duplicate["2.0"] << double_, 2.0, space)); + } + + // test for attribute reporting + { + BOOST_TEST(test("bar", (duplicate["bar"] | "foo"))); + BOOST_TEST(test("2.0", (duplicate[double_] | "foo"), 2.0)); + BOOST_TEST(test("2.02.0", + (duplicate[double_ << double_] | "foo"), 2.0)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/encoding.cpp b/src/boost/libs/spirit/test/karma/encoding.cpp new file mode 100644 index 00000000..05e8018c --- /dev/null +++ b/src/boost/libs/spirit/test/karma/encoding.cpp @@ -0,0 +1,55 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2001-2011 Joel de Guzman +// +// 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) + +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using spirit_test::test; + using boost::spirit::karma::lit; + using boost::spirit::karma::lower; + using boost::spirit::karma::upper; + using boost::spirit::karma::char_; + using boost::spirit::karma::encoding; + namespace char_encoding = boost::spirit::char_encoding; + + encoding<char_encoding::iso8859_1> iso8859_1; + + { // test extended ASCII characters + BOOST_TEST(test("\xE1", iso8859_1[lower['\xE1']])); + BOOST_TEST(test("\xC1", iso8859_1[upper['\xE1']])); + BOOST_TEST(test("\xE1", iso8859_1[lower[char_('\xE1')]])); + BOOST_TEST(test("\xC1", iso8859_1[upper[char_('\xE1')]])); + BOOST_TEST(test("\xE1", iso8859_1[lower[lit('\xE1')]])); + BOOST_TEST(test("\xC1", iso8859_1[upper[lit('\xE1')]])); + BOOST_TEST(test("\xE1", iso8859_1[lower[char_]], '\xE1')); + BOOST_TEST(test("\xC1", iso8859_1[upper[char_]], '\xE1')); + BOOST_TEST(test("\xE1", iso8859_1[lower['\xC1']])); + BOOST_TEST(test("\xC1", iso8859_1[upper['\xC1']])); + BOOST_TEST(test("\xE1", iso8859_1[lower[char_('\xC1')]])); + BOOST_TEST(test("\xC1", iso8859_1[upper[char_('\xC1')]])); + BOOST_TEST(test("\xE1", iso8859_1[lower[lit('\xC1')]])); + BOOST_TEST(test("\xC1", iso8859_1[upper[lit('\xC1')]])); + BOOST_TEST(test("\xE1", iso8859_1[lower[char_]], '\xC1')); + BOOST_TEST(test("\xC1", iso8859_1[upper[char_]], '\xC1')); + + BOOST_TEST(test("\xE4\xE4", iso8859_1[lower["\xC4\xE4"]])); + BOOST_TEST(test("\xE4\xE4", iso8859_1[lower[lit("\xC4\xE4")]])); + + BOOST_TEST(test("\xC4\xC4", iso8859_1[upper["\xC4\xE4"]])); + BOOST_TEST(test("\xC4\xC4", iso8859_1[upper[lit("\xC4\xE4")]])); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/eol.cpp b/src/boost/libs/spirit/test/karma/eol.cpp new file mode 100644 index 00000000..cf4e18d1 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/eol.cpp @@ -0,0 +1,33 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_generate.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST((test("\n", eol))); + BOOST_TEST((test("\n", eol))); + } + + { + BOOST_TEST((test_delimited("\n ", eol, space))); + BOOST_TEST((test_delimited("\n ", eol, space))); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/eps.cpp b/src/boost/libs/spirit/test/karma/eps.cpp new file mode 100644 index 00000000..6833d273 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/eps.cpp @@ -0,0 +1,59 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/phoenix_core.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("", eps)); + BOOST_TEST(test_delimited(" ", eps, space)); + + BOOST_TEST(!test("", !eps)); + BOOST_TEST(!test_delimited(" ", !eps, space)); + } + + { // test direct argument + using namespace boost::phoenix; + + BOOST_TEST(test("", eps(true))); + BOOST_TEST(!test("", eps(false))); + } + + { // test action + using namespace boost::phoenix; + + BOOST_TEST(test("", eps(val(true)))); + BOOST_TEST(!test("", eps(val(false)))); + } + + { // test no delimiter when argument is false + using namespace boost::spirit::ascii; + + std::string generated; + std::back_insert_iterator<std::string> outit(generated); + BOOST_TEST(!karma::generate_delimited(outit, eps(false), space)); + BOOST_TEST(generated.empty()); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/format_manip.cpp b/src/boost/libs/spirit/test/karma/format_manip.cpp new file mode 100644 index 00000000..494eee4a --- /dev/null +++ b/src/boost/libs/spirit/test/karma/format_manip.cpp @@ -0,0 +1,215 @@ +// 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) + +#include <boost/mpl/print.hpp> +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/karma_format.hpp> +#include <boost/spirit/include/karma_format_auto.hpp> +#include <boost/spirit/include/karma_stream.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <string> +#include <sstream> +#include <vector> +#include <list> + +#include <boost/detail/lightweight_test.hpp> +#include <boost/assign/std/vector.hpp> +#include <boost/assign/std/list.hpp> + +/////////////////////////////////////////////////////////////////////////////// +template <typename Char, typename Expr> +bool test(Char const *expected, Expr const& xpr) +{ + // Report invalid expression error as early as possible. + // If you got an error_invalid_expression error message here, + // then the expression (expr) is not a valid spirit karma expression. + BOOST_SPIRIT_ASSERT_MATCH(boost::spirit::karma::domain, Expr); + + std::ostringstream ostrm; + ostrm << boost::spirit::compile<boost::spirit::karma::domain>(xpr); + return ostrm.good() && ostrm.str() == expected; +} + +template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr + , typename Delimiter, typename Attribute> +bool test(Char const *expected, + boost::spirit::karma::detail::format_manip< + Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm) +{ + std::ostringstream ostrm; + ostrm << fm; + return ostrm.good() && ostrm.str() == expected; +} + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + namespace fusion = boost::fusion; + using namespace boost::phoenix; + + { + BOOST_TEST(test( "a", + char_('a') + )); + BOOST_TEST(test( "a", + char_[_1 = val('a')] + )); + BOOST_TEST(test( "a", + karma::format(char_[_1 = val('a')]) + )); + BOOST_TEST(test( "a ", + karma::format_delimited(char_[_1 = val('a')], space) + )); + BOOST_TEST(test( "a", + karma::format(char_, 'a') + )); + BOOST_TEST(test( "a ", + karma::format_delimited(char_, space, 'a') + )); + } + + { + BOOST_TEST(test( "ab", + char_[_1 = val('a')] << char_[_1 = val('b')] + )); + BOOST_TEST(test( "ab", + karma::format(char_[_1 = val('a')] << char_[_1 = val('b')]) + )); + BOOST_TEST(test( "a b ", + karma::format_delimited(char_[_1 = val('a')] << char_[_1 = val('b')], space) + )); + + fusion::vector<char, char> t('a', 'b'); + + BOOST_TEST(test( "ab", + karma::format(char_ << char_, t) + )); + BOOST_TEST(test( "a b ", + karma::format_delimited(char_ << char_, space, t) + )); + + BOOST_TEST(test( "ab", + karma::format(t) + )); + BOOST_TEST(test( "a b ", + karma::format_delimited(t, space) + )); + } + + { + BOOST_TEST(test( "abc", + char_[_1 = 'a'] << char_[_1 = 'b'] << char_[_1 = 'c'] + )); + BOOST_TEST(test( "abc", + karma::format(char_('a') << char_('b') << char_('c')) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(char_('a') << char_('b') << char_('c'), space) + )); + + fusion::vector<char, char, char> t('a', 'b', 'c'); + + BOOST_TEST(test( "abc", + karma::format(char_ << char_ << char_, t) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(char_ << char_ << char_, space, t) + )); + } + + { + BOOST_TEST(test( "a2", + (char_ << int_)[_1 = 'a', _2 = 2] + )); + + fusion::vector<char, int> t('a', 2); + + BOOST_TEST(test( "a2", + karma::format(char_ << int_, t) + )); + BOOST_TEST(test( "a 2 ", + karma::format_delimited(char_ << int_, space, t) + )); + } + + using namespace boost::assign; + + { + // output all elements of a vector + std::vector<char> v; + v += 'a', 'b', 'c'; + + BOOST_TEST(test( "abc", + (*char_)[_1 = v] + )); + BOOST_TEST(test( "abc", + karma::format(*char_, v) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(*char_, space, v) + )); + + BOOST_TEST(test( "abc", + karma::format(v) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(v, space) + )); + + // output a comma separated list of vector elements + BOOST_TEST(test( "a, b, c", + (char_ % lit(", "))[_0 = fusion::make_single_view(v)] + )); + BOOST_TEST(test( "a, b, c", + karma::format((char_ % lit(", "))[_0 = fusion::make_single_view(v)]) + )); + BOOST_TEST(test( "a , b , c ", + karma::format_delimited((char_ % ',')[_0 = fusion::make_single_view(v)], space) + )); + BOOST_TEST(test( "a,b,c", + karma::format(char_ % ',', v) + )); + BOOST_TEST(test( "a , b , c ", + karma::format_delimited(char_ % ',', space, v) + )); + + // output all elements of a list + std::list<char> l; + l += 'a', 'b', 'c'; + +// BOOST_TEST(test( "abc", +// (*char_)[_1 = l] +// )); +// BOOST_TEST(test( "abc", +// karma::format((*char_)[_1 = l]) +// )); +// BOOST_TEST(test( "a b c ", +// karma::format_delimited((*char_)[_1 = l], space) +// )); + BOOST_TEST(test( "abc", + karma::format(*char_, l) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(*char_, space, l) + )); + + BOOST_TEST(test( "abc", + karma::format(l) + )); + BOOST_TEST(test( "a b c ", + karma::format_delimited(l, space) + )); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/format_manip_attr.cpp b/src/boost/libs/spirit/test/karma/format_manip_attr.cpp new file mode 100644 index 00000000..75645aed --- /dev/null +++ b/src/boost/libs/spirit/test/karma/format_manip_attr.cpp @@ -0,0 +1,175 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_stream.hpp> +#include <boost/spirit/include/karma_format_attr.hpp> + +#include "test_manip_attr.hpp" + +#if SPIRIT_ARGUMENTS_LIMIT < 10 +# error SPIRIT_ARGUMENTS_LIMIT must be at least 10 to run the test +#endif + +using spirit_test::test; +using spirit_test::test_delimited; +using spirit_test::test_predelimited; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("1", char_, '1')); + BOOST_TEST(test("12", char_ << char_, '1', '2')); + BOOST_TEST(test("123", char_ << char_ << char_, '1', '2', '3')); + BOOST_TEST(test("1234" + , char_ << char_ << char_ << char_ + , '1', '2', '3', '4')); + BOOST_TEST(test("12345" + , char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5')); + BOOST_TEST(test("123456" + , char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test("1234567" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test("12345678" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test("123456789" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test("1234567890" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited("1 ", char_, space, '1')); + BOOST_TEST(test_delimited("1 2 " + , char_ << char_, space, '1', '2')); + BOOST_TEST(test_delimited("1 2 3 " + , char_ << char_ << char_, space, '1', '2', '3')); + BOOST_TEST(test_delimited("1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4')); + BOOST_TEST(test_delimited("1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + using boost::spirit::karma::delimit_flag; + + BOOST_TEST(test_predelimited(" 1 ", char_, space + , delimit_flag::predelimit, '1')); + BOOST_TEST(test_predelimited(" 1 2 " + , char_ << char_, space, delimit_flag::predelimit + , '1', '2')); + BOOST_TEST(test_predelimited(" 1 2 3 " + , char_ << char_ << char_, space + , delimit_flag::predelimit, '1', '2', '3')); + BOOST_TEST(test_predelimited(" 1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit, '1', '2', '3', '4')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit, '1', '2', '3', '4', '5')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + using boost::spirit::karma::delimit_flag; + + BOOST_TEST(test_predelimited("1 ", char_, space + , delimit_flag::dont_predelimit, '1')); + BOOST_TEST(test_predelimited("1 2 " + , char_ << char_, space, delimit_flag::dont_predelimit + , '1', '2')); + BOOST_TEST(test_predelimited("1 2 3 " + , char_ << char_ << char_, space + , delimit_flag::dont_predelimit, '1', '2', '3')); + BOOST_TEST(test_predelimited("1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit, '1', '2', '3', '4')); + BOOST_TEST(test_predelimited("1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit, '1', '2', '3', '4', '5')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/format_pointer_container.cpp b/src/boost/libs/spirit/test/karma/format_pointer_container.cpp new file mode 100644 index 00000000..98da2b36 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/format_pointer_container.cpp @@ -0,0 +1,59 @@ +// Copyright (c) 2009 Matthias Vallentin +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/iterator/indirect_iterator.hpp> +#include <boost/make_shared.hpp> +#include <boost/noncopyable.hpp> +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/karma_format.hpp> + +#include <sstream> +#include <string> +#include <vector> + +struct foo : boost::noncopyable +{ + foo() + : str("foo") + { + } + + std::string str; +}; + +template <typename Stream> +Stream& operator<<(Stream& out, const foo& f) +{ + out << f.str; + return out; +} + +int main() +{ + using namespace boost::spirit; + + typedef boost::shared_ptr<foo> foo_ptr; + std::vector<foo_ptr> v; + + std::size_t i = 10; + while (i--) + v.push_back(boost::make_shared<foo>()); + + typedef boost::indirect_iterator<std::vector<foo_ptr>::const_iterator> + iterator_type; + + std::stringstream strm; + strm + << karma::format(stream % ',', + boost::iterator_range<iterator_type>( + iterator_type(v.begin()), iterator_type(v.end()) + ) + ); + BOOST_TEST(strm.str() == "foo,foo,foo,foo,foo,foo,foo,foo,foo,foo"); + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/generate_attr.cpp b/src/boost/libs/spirit/test/karma/generate_attr.cpp new file mode 100644 index 00000000..7656f4c3 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/generate_attr.cpp @@ -0,0 +1,169 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_generate_attr.hpp> + +#include "test_attr.hpp" + +#if SPIRIT_ARGUMENTS_LIMIT < 10 +# error SPIRIT_ARGUMENTS_LIMIT must be at least 10 to run the test +#endif + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("1", char_, '1')); + BOOST_TEST(test("12", char_ << char_, '1', '2')); + BOOST_TEST(test("123", char_ << char_ << char_, '1', '2', '3')); + BOOST_TEST(test("1234" + , char_ << char_ << char_ << char_ + , '1', '2', '3', '4')); + BOOST_TEST(test("12345" + , char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5')); + BOOST_TEST(test("123456" + , char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test("1234567" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test("12345678" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test("123456789" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test("1234567890" + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited("1 ", char_, space, '1')); + BOOST_TEST(test_delimited("1 2 " + , char_ << char_, space, '1', '2')); + BOOST_TEST(test_delimited("1 2 3 " + , char_ << char_ << char_, space, '1', '2', '3')); + BOOST_TEST(test_delimited("1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4')); + BOOST_TEST(test_delimited("1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_delimited("1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + using boost::spirit::karma::delimit_flag; + + BOOST_TEST(test_predelimited(" 1 ", char_, space + , delimit_flag::predelimit, '1')); + BOOST_TEST(test_predelimited(" 1 2 " + , char_ << char_, space, delimit_flag::predelimit + , '1', '2')); + BOOST_TEST(test_predelimited(" 1 2 3 " + , char_ << char_ << char_, space + , delimit_flag::predelimit, '1', '2', '3')); + BOOST_TEST(test_predelimited(" 1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit, '1', '2', '3', '4')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit, '1', '2', '3', '4', '5')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_predelimited(" 1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + { + using namespace boost::spirit::ascii; + using boost::spirit::karma::delimit_flag; + + BOOST_TEST(test_predelimited("1 ", char_, space + , delimit_flag::dont_predelimit, '1')); + BOOST_TEST(test_predelimited("1 2 " + , char_ << char_, space, delimit_flag::dont_predelimit + , '1', '2')); + BOOST_TEST(test_predelimited("1 2 3 " + , char_ << char_ << char_, space + , delimit_flag::dont_predelimit, '1', '2', '3')); + BOOST_TEST(test_predelimited("1 2 3 4 " + , char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit, '1', '2', '3', '4')); + BOOST_TEST(test_predelimited("1 2 3 4 5 " + , char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit, '1', '2', '3', '4', '5')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 " + , char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 9 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9')); + BOOST_TEST(test_predelimited("1 2 3 4 5 6 7 8 9 0 " + , char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ << char_ + , space, delimit_flag::dont_predelimit + , '1', '2', '3', '4', '5', '6', '7', '8', '9', '0')); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/grammar.cpp b/src/boost/libs/spirit/test/karma/grammar.cpp new file mode 100644 index 00000000..d95956de --- /dev/null +++ b/src/boost/libs/spirit/test/karma/grammar.cpp @@ -0,0 +1,57 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> + +#include <string> +#include <iostream> + +#include "test.hpp" + +using namespace spirit_test; +using namespace boost::spirit; +using namespace boost::spirit::ascii; + +typedef spirit_test::output_iterator<char>::type outiter_type; + +struct num_list : karma::grammar<outiter_type, space_type> +{ + num_list() : num_list::base_type(start) + { + using boost::spirit::int_; + num1 = int_(123); + num2 = int_(456); + num3 = int_(789); + start = num1 << ',' << num2 << ',' << num3; + } + + karma::rule<outiter_type, space_type> start, num1, num2, num3; +}; + +int +main() +{ + { // simple grammar test + num_list nlist; + BOOST_TEST(test_delimited("123 , 456 , 789 ", nlist, space)); + } + + { // direct access to the rules + num_list def; + BOOST_TEST(test_delimited("123 ", def.num1, space)); + BOOST_TEST(test_delimited("123 , 456 , 789 ", def.start, space)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/grammar_fail.cpp b/src/boost/libs/spirit/test/karma/grammar_fail.cpp new file mode 100644 index 00000000..9af96765 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/grammar_fail.cpp @@ -0,0 +1,45 @@ +/*============================================================================= + 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) +=============================================================================*/ + +#include <boost/config/warning_disable.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_generate.hpp> + +#include "test.hpp" + +using namespace boost::spirit; +using namespace boost::spirit::ascii; + +typedef spirit_test::output_iterator<char>::type outiter_type; + +struct num_list : karma::grammar<outiter_type, karma::rule<outiter_type> > +{ + num_list() : num_list::base_type(start) + { + start = int_(1) << ',' << int_(0); + } + + karma::rule<outiter_type, karma::rule<outiter_type> > start; +}; + +// this test must fail compiling as the rule is used with an incompatible +// delimiter type +int main() +{ + std::string generated; + + std::back_insert_iterator<std::string> outit(generated); + num_list def; + generate_delimited(outit, def, char_('%') << '\n'); + + return 0; +} diff --git a/src/boost/libs/spirit/test/karma/int1.cpp b/src/boost/libs/spirit/test/karma/int1.cpp new file mode 100644 index 00000000..4bb40a83 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/int1.cpp @@ -0,0 +1,170 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/mpl/vector.hpp> +#include <boost/mpl/for_each.hpp> +#include <boost/mpl/if.hpp> +#include <boost/mpl/bool.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> + +#include <boost/limits.hpp> +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + // this is currently ambiguous with character literals + BOOST_TEST(test("0", 0)); + BOOST_TEST(test("123", 123)); + BOOST_TEST(test("-123", -123)); + + BOOST_TEST(test("0", int_, 0)); + BOOST_TEST(test("123", int_, 123)); + BOOST_TEST(test("-123", int_, -123)); + + BOOST_TEST(test_delimited("0 ", int_, 0, char_(' '))); + BOOST_TEST(test_delimited("123 ", int_, 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", int_, -123, char_(' '))); + + BOOST_TEST(test("0", lower[int_], 0)); + BOOST_TEST(test("123", lower[int_], 123)); + BOOST_TEST(test("-123", lower[int_], -123)); + + BOOST_TEST(test_delimited("0 ", lower[int_], 0, char_(' '))); + BOOST_TEST(test_delimited("123 ", lower[int_], 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", lower[int_], -123, char_(' '))); + + BOOST_TEST(test("0", upper[int_], 0)); + BOOST_TEST(test("123", upper[int_], 123)); + BOOST_TEST(test("-123", upper[int_], -123)); + + BOOST_TEST(test_delimited("0 ", upper[int_], 0, char_(' '))); + BOOST_TEST(test_delimited("123 ", upper[int_], 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", upper[int_], -123, char_(' '))); + + /////////////////////////////////////////////////////////////////////// + BOOST_TEST(test("0", int_(0))); + BOOST_TEST(test("123", int_(123))); + BOOST_TEST(test("-123", int_(-123))); + + BOOST_TEST(test_delimited("0 ", int_(0), char_(' '))); + BOOST_TEST(test_delimited("123 ", int_(123), char_(' '))); + BOOST_TEST(test_delimited("-123 ", int_(-123), char_(' '))); + + BOOST_TEST(test("0", lower[int_(0)])); + BOOST_TEST(test("123", lower[int_(123)])); + BOOST_TEST(test("-123", lower[int_(-123)])); + + BOOST_TEST(test_delimited("0 ", lower[int_(0)], char_(' '))); + BOOST_TEST(test_delimited("123 ", lower[int_(123)], char_(' '))); + BOOST_TEST(test_delimited("-123 ", lower[int_(-123)], char_(' '))); + + BOOST_TEST(test("0", upper[int_(0)])); + BOOST_TEST(test("123", upper[int_(123)])); + BOOST_TEST(test("-123", upper[int_(-123)])); + + BOOST_TEST(test_delimited("0 ", upper[int_(0)], char_(' '))); + BOOST_TEST(test_delimited("123 ", upper[int_(123)], char_(' '))); + BOOST_TEST(test_delimited("-123 ", upper[int_(-123)], char_(' '))); + } + + { // literals, make sure there are no ambiguities + BOOST_TEST(test("0", lit(short(0)))); + BOOST_TEST(test("0", lit(0))); + BOOST_TEST(test("0", lit(0L))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(test("0", lit(0LL))); +#endif + + BOOST_TEST(test("0", lit((unsigned short)0))); + BOOST_TEST(test("0", lit(0U))); + BOOST_TEST(test("0", lit(0UL))); +#ifdef BOOST_HAS_LONG_LONG + BOOST_TEST(test("0", lit(0ULL))); +#endif + + BOOST_TEST(test("a", lit('a'))); + BOOST_TEST(test("a", 'a')); + BOOST_TEST(test(L"a", L'a')); + } + + { // lazy numerics + using namespace boost::phoenix; + + BOOST_TEST(test("0", int_(val(0)))); + BOOST_TEST(test("123", int_(val(123)))); + BOOST_TEST(test("-123", int_(val(-123)))); + + int i1 = 0, i2 = 123, i3 = -123; + BOOST_TEST(test("0", int_(ref(i1)))); + BOOST_TEST(test("123", int_(ref(i2)))); + BOOST_TEST(test("-123", int_(ref(i3)))); + } + + { + /////////////////////////////////////////////////////////////////////// + using namespace boost::spirit::ascii; + BOOST_TEST(test("1234", uint_(1234))); + BOOST_TEST(test("ff", hex(0xff))); + BOOST_TEST(test("1234", oct(01234))); + BOOST_TEST(test("11110000", bin(0xf0))); + + BOOST_TEST(test_delimited("1234 ", uint_(1234), char_(' '))); + BOOST_TEST(test_delimited("ff ", hex(0xff), char_(' '))); + BOOST_TEST(test_delimited("1234 ", oct(01234), char_(' '))); + BOOST_TEST(test_delimited("11110000 ", bin(0xf0), char_(' '))); + + BOOST_TEST(test("1234", lower[uint_(1234)])); + BOOST_TEST(test("ff", lower[hex(0xff)])); + BOOST_TEST(test("1234", lower[oct(01234)])); + BOOST_TEST(test("11110000", lower[bin(0xf0)])); + + BOOST_TEST(test_delimited("1234 ", lower[uint_(1234)], char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[hex(0xff)], char_(' '))); + BOOST_TEST(test_delimited("1234 ", lower[oct(01234)], char_(' '))); + BOOST_TEST(test_delimited("11110000 ", lower[bin(0xf0)], char_(' '))); + + BOOST_TEST(test("1234", upper[uint_(1234)])); + BOOST_TEST(test("FF", upper[hex(0xff)])); + BOOST_TEST(test("1234", upper[oct(01234)])); + BOOST_TEST(test("11110000", upper[bin(0xf0)])); + + BOOST_TEST(test_delimited("1234 ", upper[uint_(1234)], char_(' '))); + BOOST_TEST(test_delimited("FF ", upper[hex(0xff)], char_(' '))); + BOOST_TEST(test_delimited("1234 ", upper[oct(01234)], char_(' '))); + BOOST_TEST(test_delimited("11110000 ", upper[bin(0xf0)], char_(' '))); + + BOOST_TEST(test("FF", upper[upper[hex(0xff)]])); + BOOST_TEST(test("FF", upper[lower[hex(0xff)]])); + BOOST_TEST(test("ff", lower[upper[hex(0xff)]])); + BOOST_TEST(test("ff", lower[lower[hex(0xff)]])); + + BOOST_TEST(test_delimited("FF ", upper[upper[hex(0xff)]], char_(' '))); + BOOST_TEST(test_delimited("FF ", upper[lower[hex(0xff)]], char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[upper[hex(0xff)]], char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[lower[hex(0xff)]], char_(' '))); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/int2.cpp b/src/boost/libs/spirit/test/karma/int2.cpp new file mode 100644 index 00000000..1912d73c --- /dev/null +++ b/src/boost/libs/spirit/test/karma/int2.cpp @@ -0,0 +1,177 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/mpl/vector.hpp> +#include <boost/mpl/for_each.hpp> +#include <boost/mpl/if.hpp> +#include <boost/mpl/bool.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_rule.hpp> + +#include <boost/limits.hpp> +#include "test.hpp" + +using namespace spirit_test; + + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + karma::int_generator<int, 10, true> const signed_int = + karma::int_generator<int, 10, true>(); + + /////////////////////////////////////////////////////////////////////// + BOOST_TEST(test(" 0", signed_int, 0)); + BOOST_TEST(test("+123", signed_int, 123)); + BOOST_TEST(test("-123", signed_int, -123)); + + BOOST_TEST(test_delimited(" 0 ", signed_int, 0, char_(' '))); + BOOST_TEST(test_delimited("+123 ", signed_int, 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", signed_int, -123, char_(' '))); + + BOOST_TEST(test(" 0", lower[signed_int], 0)); + BOOST_TEST(test("+123", lower[signed_int], 123)); + BOOST_TEST(test("-123", lower[signed_int], -123)); + + BOOST_TEST(test_delimited(" 0 ", lower[signed_int], 0, char_(' '))); + BOOST_TEST(test_delimited("+123 ", lower[signed_int], 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", lower[signed_int], -123, char_(' '))); + + BOOST_TEST(test(" 0", upper[signed_int], 0)); + BOOST_TEST(test("+123", upper[signed_int], 123)); + BOOST_TEST(test("-123", upper[signed_int], -123)); + + BOOST_TEST(test_delimited(" 0 ", upper[signed_int], 0, char_(' '))); + BOOST_TEST(test_delimited("+123 ", upper[signed_int], 123, char_(' '))); + BOOST_TEST(test_delimited("-123 ", upper[signed_int], -123, char_(' '))); + + /////////////////////////////////////////////////////////////////////// + BOOST_TEST(test(" 0", signed_int(0))); + BOOST_TEST(test("+123", signed_int(123))); + BOOST_TEST(test("-123", signed_int(-123))); + + BOOST_TEST(test_delimited(" 0 ", signed_int(0), char_(' '))); + BOOST_TEST(test_delimited("+123 ", signed_int(123), char_(' '))); + BOOST_TEST(test_delimited("-123 ", signed_int(-123), char_(' '))); + + BOOST_TEST(test(" 0", lower[signed_int(0)])); + BOOST_TEST(test("+123", lower[signed_int(123)])); + BOOST_TEST(test("-123", lower[signed_int(-123)])); + + BOOST_TEST(test_delimited(" 0 ", lower[signed_int(0)], char_(' '))); + BOOST_TEST(test_delimited("+123 ", lower[signed_int(123)], char_(' '))); + BOOST_TEST(test_delimited("-123 ", lower[signed_int(-123)], char_(' '))); + + BOOST_TEST(test(" 0", upper[signed_int(0)])); + BOOST_TEST(test("+123", upper[signed_int(123)])); + BOOST_TEST(test("-123", upper[signed_int(-123)])); + + BOOST_TEST(test_delimited(" 0 ", upper[signed_int(0)], char_(' '))); + BOOST_TEST(test_delimited("+123 ", upper[signed_int(123)], char_(' '))); + BOOST_TEST(test_delimited("-123 ", upper[signed_int(-123)], char_(' '))); + + using namespace boost::phoenix; + + BOOST_TEST(test(" 0", signed_int(val(0)))); + BOOST_TEST(test("+123", signed_int(val(123)))); + BOOST_TEST(test("-123", signed_int(val(-123)))); + + int i1 = 0, i2 = 123, i3 = -123; + BOOST_TEST(test(" 0", signed_int(ref(i1)))); + BOOST_TEST(test("+123", signed_int(ref(i2)))); + BOOST_TEST(test("-123", signed_int(ref(i3)))); + } + + { + /////////////////////////////////////////////////////////////////////// + using namespace boost::spirit::ascii; + + BOOST_TEST(test("1234", uint_, 1234)); + BOOST_TEST(test("ff", hex, 0xff)); + BOOST_TEST(test("1234", oct, 01234)); + BOOST_TEST(test("11110000", bin, 0xf0)); + + BOOST_TEST(test_delimited("1234 ", uint_, 1234, char_(' '))); + BOOST_TEST(test_delimited("ff ", hex, 0xff, char_(' '))); + BOOST_TEST(test_delimited("1234 ", oct, 01234, char_(' '))); + BOOST_TEST(test_delimited("11110000 ", bin, 0xf0, char_(' '))); + + // test unsigned generator with signed integral value + BOOST_TEST(test("ff", hex, (char)0xff)); + BOOST_TEST(test_delimited("ff ", hex, (char)0xff, char_(' '))); + + BOOST_TEST(test("1234", lower[uint_], 1234)); + BOOST_TEST(test("ff", lower[hex], 0xff)); + BOOST_TEST(test("1234", lower[oct], 01234)); + BOOST_TEST(test("11110000", lower[bin], 0xf0)); + + BOOST_TEST(test_delimited("1234 ", lower[uint_], 1234, char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[hex], 0xff, char_(' '))); + BOOST_TEST(test_delimited("1234 ", lower[oct], 01234, char_(' '))); + BOOST_TEST(test_delimited("11110000 ", lower[bin], 0xf0, char_(' '))); + + BOOST_TEST(test("1234", upper[uint_], 1234)); + BOOST_TEST(test("FF", upper[hex], 0xff)); + BOOST_TEST(test("1234", upper[oct], 01234)); + BOOST_TEST(test("11110000", upper[bin], 0xf0)); + + BOOST_TEST(test_delimited("1234 ", upper[uint_], 1234, char_(' '))); + BOOST_TEST(test_delimited("FF ", upper[hex], 0xff, char_(' '))); + BOOST_TEST(test_delimited("1234 ", upper[oct], 01234, char_(' '))); + BOOST_TEST(test_delimited("11110000 ", upper[bin], 0xf0, char_(' '))); + + // no generator transformation should occur for uint_'s + BOOST_TEST(test("1234", upper[upper[uint_]], 1234)); + BOOST_TEST(test("1234", upper[lower[uint_]], 1234)); + BOOST_TEST(test("1234", lower[upper[uint_]], 1234)); + BOOST_TEST(test("1234", lower[lower[uint_]], 1234)); + + BOOST_TEST(test_delimited("1234 ", upper[upper[uint_]], 1234, char_(' '))); + BOOST_TEST(test_delimited("1234 ", upper[lower[uint_]], 1234, char_(' '))); + BOOST_TEST(test_delimited("1234 ", lower[upper[uint_]], 1234, char_(' '))); + BOOST_TEST(test_delimited("1234 ", lower[lower[uint_]], 1234, char_(' '))); + + BOOST_TEST(test("FF", upper[upper[hex]], 0xff)); + BOOST_TEST(test("FF", upper[lower[hex]], 0xff)); + BOOST_TEST(test("ff", lower[upper[hex]], 0xff)); + BOOST_TEST(test("ff", lower[lower[hex]], 0xff)); + + BOOST_TEST(test_delimited("FF ", upper[upper[hex]], 0xff, char_(' '))); + BOOST_TEST(test_delimited("FF ", upper[lower[hex]], 0xff, char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[upper[hex]], 0xff, char_(' '))); + BOOST_TEST(test_delimited("ff ", lower[lower[hex]], 0xff, char_(' '))); + } + + /////////////////////////////////////////////////////////////////////////// + { + using boost::spirit::karma::int_; + using boost::spirit::karma::_1; + using boost::spirit::karma::_val; + using boost::spirit::karma::space; + + int i = 123; + int j = 456; + BOOST_TEST(test("123", int_[_1 = _val], i)); + BOOST_TEST(test_delimited("456 ", int_[_1 = _val], j, space)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/int3.cpp b/src/boost/libs/spirit/test/karma/int3.cpp new file mode 100644 index 00000000..47c5ad34 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/int3.cpp @@ -0,0 +1,126 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/lexical_cast.hpp> +#include <boost/mpl/vector.hpp> +#include <boost/mpl/for_each.hpp> +#include <boost/mpl/if.hpp> +#include <boost/mpl/bool.hpp> + +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> + +#include <boost/limits.hpp> +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +struct test_minmax +{ + template <typename T> + void operator()(T) const + { + using namespace boost::spirit; + using namespace boost::phoenix; + + T minval = (std::numeric_limits<T>::min)(); + T maxval = (std::numeric_limits<T>::max)(); + + std::string expected_minval = boost::lexical_cast<std::string>(minval); + std::string expected_maxval = boost::lexical_cast<std::string>(maxval); + + // create a correct generator type from the given integer type + typedef typename + boost::mpl::if_< + boost::mpl::bool_<std::numeric_limits<T>::is_signed>, + karma::int_generator<T>, + karma::uint_generator<T> + >::type + int_generator_type; + + int_generator_type const gen = int_generator_type(); + + BOOST_TEST(test(expected_maxval, gen, maxval)); + BOOST_TEST(test(expected_minval, gen, minval)); + BOOST_TEST(test(expected_maxval, gen(maxval))); + BOOST_TEST(test(expected_minval, gen(minval))); + BOOST_TEST(test(expected_maxval, gen(maxval), maxval)); + BOOST_TEST(test(expected_minval, gen(minval), minval)); + BOOST_TEST(!test("", gen(maxval), maxval-1)); + BOOST_TEST(!test("", gen(minval), minval+1)); + BOOST_TEST(test(expected_maxval, lit(maxval))); + BOOST_TEST(test(expected_minval, lit(minval))); + + BOOST_TEST(test_delimited(expected_maxval + " ", gen, maxval, char(' '))); + BOOST_TEST(test_delimited(expected_minval + " ", gen, minval, char(' '))); + BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), char(' '))); + BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), char(' '))); + BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), maxval, char(' '))); + BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), minval, char(' '))); + BOOST_TEST(!test_delimited("", gen(maxval), maxval-1, char(' '))); + BOOST_TEST(!test_delimited("", gen(minval), minval+1, char(' '))); + BOOST_TEST(test_delimited(expected_maxval + " ", lit(maxval), char(' '))); + BOOST_TEST(test_delimited(expected_minval + " ", lit(minval), char(' '))); + + // action tests + BOOST_TEST(test(expected_maxval, gen[_1 = val(maxval)])); + BOOST_TEST(test(expected_minval, gen[_1 = val(minval)])); + + // optional tests + boost::optional<T> optmin, optmax(maxval); + + BOOST_TEST(!test("", gen, optmin)); + BOOST_TEST(!test("", gen(minval), optmin)); + + optmin = minval; + BOOST_TEST(test(expected_minval, gen, optmin)); + BOOST_TEST(test(expected_maxval, gen, optmax)); + BOOST_TEST(test(expected_minval, gen(minval), optmin)); + BOOST_TEST(test(expected_maxval, gen(maxval), optmax)); + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + // Phoenix expression tests (only supported while including + // karma_phoenix_attributes.hpp + namespace phoenix = boost::phoenix; + + BOOST_TEST(test("1", gen, phoenix::val(1))); + + T val = 1; + BOOST_TEST(test("1", gen, phoenix::ref(val))); + BOOST_TEST(test("2", gen, ++phoenix::ref(val))); +#endif + } +}; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + +// test boundary values + typedef boost::mpl::vector< +#ifdef BOOST_HAS_LONG_LONG + boost::long_long_type, boost::ulong_long_type, +#endif + short, unsigned short, + int, unsigned int, + long, unsigned long + > integer_types; + boost::mpl::for_each<integer_types>(test_minmax()); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/kleene.cpp b/src/boost/libs/spirit/test/karma/kleene.cpp new file mode 100644 index 00000000..693c31e3 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/kleene.cpp @@ -0,0 +1,244 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/assign/std/vector.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/fusion/include/vector.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <boost/assign/std/vector.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +struct action +{ + action (std::vector<char>& vec) + : vec(vec), it(vec.begin()) + {} + + void operator()(unsigned& value, boost::spirit::unused_type, bool& pass) const + { + pass = (it != vec.end()); + if (pass) + value = *it++; + } + + std::vector<char>& vec; + mutable std::vector<char>::iterator it; +}; + +struct A +{ + double d1; + double d2; +}; + +BOOST_FUSION_ADAPT_STRUCT( + A, + (double, d1) + (double, d2) +) + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + namespace fusion = boost::fusion; + + { + std::string s1("aaaa"); + BOOST_TEST(test("aaaa", *char_, s1)); + BOOST_TEST(test_delimited("a a a a ", *char_, s1, ' ')); + + std::string s2(""); + BOOST_TEST(test("", *char_, s2)); + BOOST_TEST(test_delimited("", *char_, s2, ' ')); + } + + { + std::string s1("aaaaa"); + BOOST_TEST(test("aaaaa", char_ << *(char_ << char_), s1)); + BOOST_TEST(test_delimited("a a a a a ", + char_ << *(char_ << char_), s1, ' ')); + + s1 = "a"; + BOOST_TEST(test("a", char_ << *(char_ << char_), s1)); + s1 = "aa"; + BOOST_TEST(!test("", char_ << *(char_ << char_), s1)); +// BOOST_TEST(test("aa", char_ << *buffer[char_ << char_] << char_, s1)); + s1 = "aaaa"; + BOOST_TEST(!test("", char_ << *(char_ << char_), s1)); +// BOOST_TEST(test("aaaa", char_ << *buffer[char_ << char_] << char_, s1)); + } + + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + using namespace boost::assign; + + typedef std::pair<char, char> data; + std::vector<data> v1; + v1 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + r = &char_('a') << char_; + + BOOST_TEST(test("a", r << *(r << r), v1)); + BOOST_TEST(test("a", relaxed[r << *(r << r)], v1)); + BOOST_TEST(!test("", strict[r << *(r << r)], v1)); + + v1 += std::make_pair('a', 'a'); + + BOOST_TEST(!test("", r << *(r << r), v1)); + BOOST_TEST(!test("", relaxed[r << *(r << r)], v1)); + BOOST_TEST(!test("", strict[r << *(r << r)], v1)); + + v1 += std::make_pair('a', 'a'); + + BOOST_TEST(test("aaa", r << *(r << r), v1)); + BOOST_TEST(test("aaa", relaxed[r << *(r << r)], v1)); + BOOST_TEST(!test("", strict[r << *(r << r)], v1)); + } + + { + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c'; + + BOOST_TEST(test("abc", *char_, v)); + BOOST_TEST(test_delimited("a b c ", *char_, v, ' ')); + } + + { + using namespace boost::assign; + + std::vector<int> v; + v += 10, 20, 30; + + BOOST_TEST(test("102030", *int_, v)); + BOOST_TEST(test_delimited("10, 20, 30, ", *int_, v, lit(", "))); + + BOOST_TEST(test("10,20,30,", *(int_ << ','), v)); + BOOST_TEST(test_delimited("10 , 20 , 30 , ", *(int_ << ','), v, lit(' '))); + +// leads to infinite loops +// fusion::vector<char, char> cc ('a', 'c'); +// BOOST_TEST(test("ac", char_ << *(lit(' ') << ',') << char_, cc)); +// BOOST_TEST(test_delimited("a c ", +// char_ << *(lit(' ') << ',') << char_, cc, " ")); + } + + { // actions + using namespace boost::assign; + namespace phx = boost::phoenix; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + + BOOST_TEST(test("abcdefgh", (*char_)[_1 = phx::ref(v)])); + BOOST_TEST(test_delimited("a b c d e f g h ", + (*char_ )[_1 = phx::ref(v)], space)); + } + + // failing sub-generators + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + + using namespace boost::assign; + + typedef std::pair<char, char> data; + std::vector<data> v2; + v2 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + + r = &char_('d') << char_; + BOOST_TEST(test("d", *r, v2)); + BOOST_TEST(test("d", relaxed[*r], v2)); + BOOST_TEST(test("", strict[*r], v2)); + + r = &char_('a') << char_; + BOOST_TEST(test("a", *r, v2)); + BOOST_TEST(test("a", relaxed[*r], v2)); + BOOST_TEST(test("a", strict[*r], v2)); + + r = &char_('g') << char_; + BOOST_TEST(test("g", *r, v2)); + BOOST_TEST(test("g", relaxed[*r], v2)); + BOOST_TEST(test("", strict[*r], v2)); + + r = !char_('d') << char_; + BOOST_TEST(test("abcefg", *r, v2)); + BOOST_TEST(test("abcefg", relaxed[*r], v2)); + BOOST_TEST(test("abc", strict[*r], v2)); + + r = !char_('a') << char_; + BOOST_TEST(test("bcdefg", *r, v2)); + BOOST_TEST(test("bcdefg", relaxed[*r], v2)); + BOOST_TEST(test("", strict[*r], v2)); + + r = !char_('g') << char_; + BOOST_TEST(test("abcdef", *r, v2)); + BOOST_TEST(test("abcdef", relaxed[*r], v2)); + BOOST_TEST(test("abcdef", strict[*r], v2)); + + r = &char_('A') << char_; + BOOST_TEST(test("", *r, v2)); + } + + { + // make sure user defined end condition is applied if no attribute + // is passed in + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + BOOST_TEST(test("[6162636465666768]", '[' << *hex[action(v)] << ']')); + } + + { + using boost::spirit::karma::double_; + + std::vector<A> v(1); + v[0].d1 = 1.0; + v[0].d2 = 2.0; + BOOST_TEST(test("A1.02.0", 'A' << *(double_ << double_), v)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/lazy.cpp b/src/boost/libs/spirit/test/karma/lazy.cpp new file mode 100644 index 00000000..86476f01 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/lazy.cpp @@ -0,0 +1,52 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_action.hpp> +// #include <boost/spirit/include/karma_nonterminal.hpp> +// #include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/support_argument.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + namespace karma = boost::spirit::karma; + using spirit_test::test; + using namespace boost::spirit; + using namespace boost::spirit::karma; + namespace phx = boost::phoenix; + + { + BOOST_TEST(test("123", karma::lazy(phx::val(int_)), 123)); + } + + { + int result = 123; + BOOST_TEST(test("123", karma::lazy(phx::val(int_))[_1 = phx::ref(result)])); + } + +// { +// typedef spirit_test::output_iterator<char>::type outiter_type; +// rule<outiter_type, void(std::string)> r; +// +// r = char_('<') << karma::lazy(_r1) << '>' << "</" << karma::lazy(_r1) << '>'; +// +// std::string tag("tag"), foo("foo"); +// BOOST_TEST(test("<tag></tag>", r (phx::ref(tag)))); +// BOOST_TEST(!test("<foo></bar>", r (phx::ref(foo)))); +// } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/left_alignment.cpp b/src/boost/libs/spirit/test/karma/left_alignment.cpp new file mode 100644 index 00000000..5b189883 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/left_alignment.cpp @@ -0,0 +1,74 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_upper_lower_case.hpp> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test("x ", left_align[char_('x')])); + BOOST_TEST(test("x ", left_align[char_], 'x')); + BOOST_TEST(test("x ", left_align['x'])); + + BOOST_TEST(test("x ", left_align(10)[char_('x')])); + BOOST_TEST(test("x ", left_align(10)[char_], 'x')); + BOOST_TEST(test("x ", left_align(10)['x'])); + + BOOST_TEST(test("x*********", left_align(10, char_('*'))[char_('x')])); + BOOST_TEST(test("x*********", left_align(10, '*')[char_], 'x')); + BOOST_TEST(test("x*********", left_align(10, '*')['x'])); + BOOST_TEST(test("xaaaaaaaaa", lower[left_align(10, 'A')['X']])); + BOOST_TEST(test("XAAAAAAAAA", upper[left_align(10, 'a')['x']])); + + BOOST_TEST(test("x*********", left_align(char_('*'))[char_('x')])); + BOOST_TEST(test("x*********", left_align(char_('*'))[char_], 'x')); + BOOST_TEST(test("x*********", left_align(char_('*'))['x'])); + + BOOST_TEST(test("abc ", left_align[lit("abc")])); + BOOST_TEST(test("abc ", left_align[string], "abc")); + + BOOST_TEST(test("abc ", left_align(10)[lit("abc")])); + BOOST_TEST(test("abc ", left_align(10)[string], "abc")); + BOOST_TEST(test("abc ", left_align(10)["abc"])); + + BOOST_TEST(test("abc*******", left_align(10, char_('*'))[lit("abc")])); + BOOST_TEST(test("abc*******", left_align(10, '*')[string], "abc")); + BOOST_TEST(test("abc*******", left_align(10, '*')["abc"])); + + BOOST_TEST(test("abc*******", left_align(char_('*'))[lit("abc")])); + BOOST_TEST(test("abc*******", left_align(char_('*'))[string], "abc")); + BOOST_TEST(test("abc*******", left_align(char_('*'))["abc"])); + + BOOST_TEST(test("100 ", left_align[int_(100)])); + BOOST_TEST(test("100 ", left_align[int_], 100)); + + BOOST_TEST(test("100 ", left_align(10)[int_(100)])); + BOOST_TEST(test("100 ", left_align(10)[int_], 100)); + + BOOST_TEST(test("100*******", left_align(10, char_('*'))[int_(100)])); + BOOST_TEST(test("100*******", left_align(10, '*')[int_], 100)); + + BOOST_TEST(test("100*******", left_align(char_('*'))[int_(100)])); + BOOST_TEST(test("100*******", left_align(char_('*'))[int_], 100)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/list.cpp b/src/boost/libs/spirit/test/karma/list.cpp new file mode 100644 index 00000000..686b0ab4 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/list.cpp @@ -0,0 +1,151 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/mpl/print.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <boost/assign/std/vector.hpp> + +#include <string> +#include <vector> +#include <iostream> + +#include "test.hpp" + +using namespace spirit_test; +using boost::spirit::unused_type; + +/////////////////////////////////////////////////////////////////////////////// +struct action +{ + action (std::vector<char>& vec) + : vec(vec), it(vec.begin()) + {} + + void operator()(unsigned& value, unused_type const&, bool& pass) const + { + pass = (it != vec.end()); + if (pass) + value = *it++; + } + + std::vector<char>& vec; + mutable std::vector<char>::iterator it; +}; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + + { + BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', v)); + BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ", + char_ % ',', v, space)); + } + + { + std::string s ("abcdefgh"); + BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', s)); + BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ", + char_ % ',', s, space)); + } + + { + std::string s ("abcdefg"); + BOOST_TEST(test("abc,de,fg", char_ << ((char_ << char_) % ','), s)); + BOOST_TEST(test_delimited("a b c , d e , f g ", + char_ << ((char_ << char_) % ','), s, space)); + } + + { // actions + namespace phx = boost::phoenix; + + BOOST_TEST(test("a,b,c,d,e,f,g,h", (char_ % ',')[_1 = phx::ref(v)])); + BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ", + (char_ % ',')[_1 = phx::ref(v)], space)); + } + + // failing sub-generators + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + + typedef std::pair<char, char> data; + std::vector<data> v2; + v2 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + + r = &char_('d') << char_; + BOOST_TEST(test("d", r % ',', v2)); + BOOST_TEST(test("d", relaxed[r % ','], v2)); + BOOST_TEST(!test("", strict[r % ','], v2)); + + r = &char_('a') << char_; + BOOST_TEST(test("a", r % ',', v2)); + BOOST_TEST(test("a", relaxed[r % ','], v2)); + BOOST_TEST(test("a", strict[r % ','], v2)); + + r = &char_('g') << char_; + BOOST_TEST(test("g", r % ',', v2)); + BOOST_TEST(test("g", relaxed[r % ','], v2)); + BOOST_TEST(!test("", strict[r % ','], v2)); + + r = !char_('d') << char_; + BOOST_TEST(test("a,b,c,e,f,g", r % ',', v2)); + BOOST_TEST(test("a,b,c,e,f,g", relaxed[r % ','], v2)); + BOOST_TEST(test("a,b,c", strict[r % ','], v2)); + + r = !char_('a') << char_; + BOOST_TEST(test("b,c,d,e,f,g", r % ',', v2)); + BOOST_TEST(test("b,c,d,e,f,g", relaxed[r % ','], v2)); + BOOST_TEST(!test("", strict[r % ','], v2)); + + r = !char_('g') << char_; + BOOST_TEST(test("a,b,c,d,e,f", r % ',', v2)); + BOOST_TEST(test("a,b,c,d,e,f", relaxed[r % ','], v2)); + BOOST_TEST(test("a,b,c,d,e,f", strict[r % ','], v2)); + + r = &char_('A') << char_; + BOOST_TEST(!test("", r % ',', v2)); + } + + { + // make sure user defined end condition is applied if no attribute + // is passed in + BOOST_TEST(test("[61,62,63,64,65,66,67,68]", + '[' << hex[action(v)] % ',' << ']')); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/lit.cpp b/src/boost/libs/spirit/test/karma/lit.cpp new file mode 100644 index 00000000..9eb4d4f0 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/lit.cpp @@ -0,0 +1,194 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/support_argument.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("a", lit('a'))); + BOOST_TEST(!test("a", lit('b'))); + + BOOST_TEST(test("abc", "abc")); + BOOST_TEST(!test("abcd", "abc")); + + BOOST_TEST(test("abc", lit("abc"))); + BOOST_TEST(!test("abcd", lit("abc"))); + + BOOST_TEST(test("abc", string, "abc")); + BOOST_TEST(!test("abcd", string, "abc")); + + BOOST_TEST(test("abc", string("abc"))); + BOOST_TEST(!test("abcd", string("abc"))); + + BOOST_TEST(test("abc", string("abc"), "abc")); + BOOST_TEST(!test("", string("abc"), "abcd")); + BOOST_TEST(!test("", string("abcd"), "abc")); + BOOST_TEST(!test("", string("abc"), "abcd")); // don't match prefixes only + } + + { + using namespace boost::spirit::ascii; + + std::string str("abc"); + BOOST_TEST(test("abc", lit(str))); + BOOST_TEST(!test("abcd", lit(str))); + + BOOST_TEST(test("abc", string(str))); + BOOST_TEST(!test("abcd", string(str))); + + BOOST_TEST(test("abc", string, str)); + BOOST_TEST(!test("abcd", string, str)); + + BOOST_TEST(test("abc", str)); + BOOST_TEST(!test("abcd", str)); + } + + { + using namespace boost::spirit::standard_wide; + + std::basic_string<wchar_t> wstr(L"abc"); + BOOST_TEST(test(L"abc", lit(wstr))); + BOOST_TEST(!test(L"abcd", lit(wstr))); + + BOOST_TEST(test(L"abc", string, wstr)); + BOOST_TEST(!test(L"abcd", string, wstr)); + + BOOST_TEST(test(L"abc", wstr)); + BOOST_TEST(!test(L"abcd", wstr)); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test(L"a", lit(L'a'))); + BOOST_TEST(!test(L"a", lit(L'b'))); + + BOOST_TEST(test(L"abc", L"abc")); + BOOST_TEST(test(L"abc", "abc")); + BOOST_TEST(!test(L"abcd", L"abc")); + + BOOST_TEST(test(L"abc", lit(L"abc"))); + BOOST_TEST(!test(L"abcd", lit(L"abc"))); + + BOOST_TEST(test(L"abc", string(L"abc"))); + BOOST_TEST(!test(L"abcd", string(L"abc"))); + + BOOST_TEST(test(L"abc", string, L"abc")); + BOOST_TEST(!test(L"abcd", string, L"abc")); + + BOOST_TEST(test(L"abc", string, "abc")); + BOOST_TEST(!test(L"abcd", string, "abc")); + } + + { + using namespace boost::spirit::karma; + + const char test_data[] = "abc\x00s"; + const std::string str(test_data, sizeof(test_data) - 1); + + BOOST_TEST(test(str, lit(str))); + BOOST_TEST(!test("abc", lit(str))); + + BOOST_TEST(test(str, string(str))); + BOOST_TEST(!test("abc", string(str))); + + BOOST_TEST(test(str, string, str)); + BOOST_TEST(!test("abc", string, str)); + + BOOST_TEST(test(str, str)); + BOOST_TEST(!test("abc", str)); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited("a ", lit('a'), ' ')); + BOOST_TEST(!test_delimited("a ", lit('b'), ' ')); + + BOOST_TEST(test_delimited("abc ", "abc", ' ')); + BOOST_TEST(!test_delimited("abcd ", "abc", ' ')); + + BOOST_TEST(test_delimited("abc ", lit("abc"), ' ')); + BOOST_TEST(!test_delimited("abcd ", lit("abc"), ' ')); + + BOOST_TEST(test_delimited("abc ", string, "abc", ' ')); + BOOST_TEST(!test_delimited("abcd ", string, "abc", ' ')); + + BOOST_TEST(test_delimited("abc ", string("abc"), ' ')); + BOOST_TEST(!test_delimited("abcd ", string("abc"), ' ')); + + BOOST_TEST(test_delimited("abc ", string("abc"), "abc", ' ')); + BOOST_TEST(!test_delimited("", string("abc"), "abcd", ' ')); + BOOST_TEST(!test_delimited("", string("abcd"), "abc", ' ')); + BOOST_TEST(!test_delimited("", string("abc"), "abcd", ' ')); // don't match prefixes only + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited(L"a ", lit(L'a'), ' ')); + BOOST_TEST(!test_delimited(L"a ", lit(L'b'), ' ')); + + BOOST_TEST(test_delimited(L"abc ", L"abc", ' ')); + BOOST_TEST(!test_delimited(L"abcd ", L"abc", ' ')); + + BOOST_TEST(test_delimited(L"abc ", lit(L"abc"), ' ')); + BOOST_TEST(!test_delimited(L"abcd ", lit(L"abc"), ' ')); + + BOOST_TEST(test_delimited(L"abc ", string, L"abc", ' ')); + BOOST_TEST(!test_delimited(L"abcd ", string, L"abc", ' ')); + + BOOST_TEST(test_delimited(L"abc ", string(L"abc"), ' ')); + BOOST_TEST(!test_delimited(L"abcd ", string(L"abc"), ' ')); + } + + { // test action + namespace phx = boost::phoenix; + using namespace boost::spirit::ascii; + + std::string str("abc"); + BOOST_TEST(test("abc", string[_1 = phx::ref(str)])); + BOOST_TEST(test_delimited("abc ", string[_1 = phx::ref(str)], space)); + } + + { // lazy strings + namespace phx = boost::phoenix; + using namespace boost::spirit::ascii; + + std::basic_string<char> s("abc"); + BOOST_TEST((test("abc", lit(phx::val(s))))); + BOOST_TEST((test("abc", string(phx::val(s))))); + + BOOST_TEST(test("abc", string(phx::val(s)), "abc")); + BOOST_TEST(!test("", string(phx::val(s)), "abcd")); + BOOST_TEST(!test("", string(phx::val(s)), "abc")); + + std::basic_string<wchar_t> ws(L"abc"); + BOOST_TEST((test(L"abc", lit(phx::ref(ws))))); + BOOST_TEST((test(L"abc", string(phx::ref(ws))))); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/maxwidth.cpp b/src/boost/libs/spirit/test/karma/maxwidth.cpp new file mode 100644 index 00000000..75066a11 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/maxwidth.cpp @@ -0,0 +1,77 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test("0123456789", maxwidth[lit("0123456789")])); + BOOST_TEST(test("012345678", maxwidth[lit("012345678")])); + BOOST_TEST(test("0123456789", maxwidth[lit("01234567890")])); + + BOOST_TEST(test("0123456789", maxwidth[string], "0123456789")); + BOOST_TEST(test("012345678", maxwidth[string], "012345678")); + BOOST_TEST(test("0123456789", maxwidth[string], "01234567890")); + } + + { + BOOST_TEST(test("01234567", maxwidth(8)[lit("01234567")])); + BOOST_TEST(test("0123456", maxwidth(8)[lit("0123456")])); + BOOST_TEST(test("01234567", maxwidth(8)[lit("012345678")])); + + BOOST_TEST(test("01234567", maxwidth(8)[string], "01234567")); + BOOST_TEST(test("0123456", maxwidth(8)[string], "0123456")); + BOOST_TEST(test("01234567", maxwidth(8)[string], "012345678")); + } + + { + std::string str; + BOOST_TEST(test("01234567", + maxwidth(8, std::back_inserter(str))[lit("01234567")]) && + str.empty()); + + str = ""; + BOOST_TEST(test("0123456", + maxwidth(8, std::back_inserter(str))[lit("0123456")]) && + str.empty()); + + str = ""; + BOOST_TEST(test("01234567", + maxwidth(8, std::back_inserter(str))[lit("012345678")]) && + str == "8"); + } + + { + using namespace boost::phoenix; + + BOOST_TEST(test("01234567", maxwidth(val(8))[lit("01234567")])); + BOOST_TEST(test("0123456", maxwidth(val(8))[lit("0123456")])); + BOOST_TEST(test("01234567", maxwidth(val(8))[lit("012345678")])); + + int w = 8; + BOOST_TEST(test("01234567", maxwidth(ref(w))[string], "01234567")); + BOOST_TEST(test("0123456", maxwidth(ref(w))[string], "0123456")); + BOOST_TEST(test("01234567", maxwidth(ref(w))[string], "012345678")); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/not_predicate.cpp b/src/boost/libs/spirit/test/karma/not_predicate.cpp new file mode 100644 index 00000000..6b4abeb9 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/not_predicate.cpp @@ -0,0 +1,36 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> + +#include <iostream> +#include "test.hpp" + +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + + { + using boost::spirit::ascii::char_; + using boost::spirit::ascii::string; + + BOOST_TEST(test("c", !char_('a') << 'b' | 'c', 'a')); + BOOST_TEST(test("b", !char_('a') << 'b' | 'c', 'x')); + + BOOST_TEST(test("def", !string("123") << "abc" | "def", "123")); + BOOST_TEST(test("abc", !string("123") << "abc" | "def", "456")); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/omit.cpp b/src/boost/libs/spirit/test/karma/omit.cpp new file mode 100644 index 00000000..9a349697 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/omit.cpp @@ -0,0 +1,79 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/fusion/include/std_pair.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <iostream> +#include "test.hpp" + +using namespace spirit_test; + +int main() +{ + using boost::spirit::karma::_1; + using boost::spirit::karma::_a; + using boost::spirit::karma::double_; + using boost::spirit::karma::int_; + using boost::spirit::karma::omit; + using boost::spirit::karma::skip; + using boost::spirit::karma::rule; + using boost::spirit::karma::locals; + + typedef spirit_test::output_iterator<char>::type outiter_type; + typedef std::pair<std::vector<int>, int> attribute_type; + + rule<outiter_type, attribute_type(), locals<int> > r; + + attribute_type a; + a.first.push_back(0x01); + a.first.push_back(0x02); + a.first.push_back(0x04); + a.first.push_back(0x08); + a.second = 0; + + // omit[] is supposed to execute the embedded generator + { + std::pair<double, double> p (1.0, 2.0); + BOOST_TEST(test("2.0", omit[double_] << double_, p)); + + r %= omit[ *(int_[_a = _a + _1]) ] << int_[_1 = _a]; + BOOST_TEST(test("15", r, a)); + } + + // even if omit[] never fails, it has to honor the result of the + // embedded generator + { + typedef std::pair<double, double> attribute_type; + rule<outiter_type, attribute_type()> r; + + r %= omit[double_(1.0) << double_] | "42"; + + attribute_type p1 (1.0, 2.0); + BOOST_TEST(test("", r, p1)); + + attribute_type p2 (10.0, 2.0); + BOOST_TEST(test("42", r, p2)); + } + + // skip[] is not supposed to execute the embedded generator + { + using boost::spirit::karma::double_; + using boost::spirit::karma::skip; + + std::pair<double, double> p (1.0, 2.0); + BOOST_TEST(test("2.0", skip[double_] << double_, p)); + + r %= skip[ *(int_[_a = _a + _1]) ] << int_[_1 = _a]; + BOOST_TEST(test("0", r, a)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/optional.cpp b/src/boost/libs/spirit/test/karma/optional.cpp new file mode 100644 index 00000000..3369c1e6 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/optional.cpp @@ -0,0 +1,90 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include <iostream> +#include "test.hpp" + +int main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + boost::optional<int> opt; + BOOST_TEST(test("", -int_, opt)); + + opt = 10; + BOOST_TEST(test("10", -int_, opt)); + } + + { + int opt = 10; + BOOST_TEST(test("10", -int_, opt)); + } + + { + boost::optional<int> opt; + BOOST_TEST(test_delimited("", -int_, opt, space)); + + opt = 10; + BOOST_TEST(test_delimited("10 ", -int_, opt, space)); + } + + { + int opt = 10; + BOOST_TEST(test_delimited("10 ", -int_, opt, space)); + } + + { // test action + using namespace boost::phoenix; + namespace phoenix = boost::phoenix; + + boost::optional<int> n ; + BOOST_TEST(test("", (-int_)[_1 = phoenix::ref(n)])); + + n = 1234; + BOOST_TEST(test("1234", (-int_)[_1 = phoenix::ref(n)])); + } + + { // test action + using namespace boost::phoenix; + namespace phoenix = boost::phoenix; + + int n = 1234; + BOOST_TEST(test("1234", (-int_)[_1 = phoenix::ref(n)])); + } + + { // test action + using namespace boost::phoenix; + namespace phoenix = boost::phoenix; + + boost::optional<int> n; + BOOST_TEST(test_delimited("", (-int_)[_1 = phoenix::ref(n)], space)); + + n = 1234; + BOOST_TEST(test_delimited("1234 ", (-int_)[_1 = phoenix::ref(n)], space)); + } + + { // test action + using namespace boost::phoenix; + namespace phoenix = boost::phoenix; + + int n = 1234; + BOOST_TEST(test_delimited("1234 ", (-int_)[_1 = phoenix::ref(n)], space)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/pattern1.cpp b/src/boost/libs/spirit/test/karma/pattern1.cpp new file mode 100644 index 00000000..9892bb05 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/pattern1.cpp @@ -0,0 +1,112 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/spirit/include/phoenix_fusion.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + typedef spirit_test::output_iterator<char>::type outiter_type; + + // test rule parameter propagation + { + using boost::phoenix::at_c; + + karma::rule<outiter_type, fusion::vector<char, int, double>()> start; + fusion::vector<char, int, double> vec('a', 10, 12.4); + + start %= char_ << int_ << double_; + BOOST_TEST(test("a1012.4", start, vec)); + + karma::rule<outiter_type, char()> a; + karma::rule<outiter_type, int()> b; + karma::rule<outiter_type, double()> c; + + a %= char_ << eps; + b %= int_; + c %= double_; + start = a[_1 = at_c<0>(_r0)] << b[_1 = at_c<1>(_r0)] << c[_1 = at_c<2>(_r0)]; + BOOST_TEST(test("a1012.4", start, vec)); + + start = (a << b << c)[_1 = at_c<0>(_r0), _2 = at_c<1>(_r0), _3 = at_c<2>(_r0)]; + BOOST_TEST(test("a1012.4", start, vec)); + + start = a << b << c; + BOOST_TEST(test("a1012.4", start, vec)); + + start %= a << b << c; + BOOST_TEST(test("a1012.4", start, vec)); + } + + { + using boost::phoenix::at_c; + + karma::rule<outiter_type, space_type, fusion::vector<char, int, double>()> start; + fusion::vector<char, int, double> vec('a', 10, 12.4); + + start %= char_ << int_ << double_; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + + karma::rule<outiter_type, space_type, char()> a; + karma::rule<outiter_type, space_type, int()> b; + karma::rule<outiter_type, space_type, double()> c; + + a %= char_ << eps; + b %= int_; + c %= double_; + start = a[_1 = at_c<0>(_r0)] << b[_1 = at_c<1>(_r0)] << c[_1 = at_c<2>(_r0)]; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + + start = (a << b << c)[_1 = at_c<0>(_r0), _2 = at_c<1>(_r0), _3 = at_c<2>(_r0)]; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + + start = a << b << c; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + + start %= a << b << c; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + } + + // test direct initalization + { + using boost::phoenix::at_c; + + fusion::vector<char, int, double> vec('a', 10, 12.4); + karma::rule<outiter_type, space_type, fusion::vector<char, int, double>()> + start = char_ << int_ << double_;; + + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + + karma::rule<outiter_type, space_type, char()> a = char_ << eps; + karma::rule<outiter_type, space_type, int()> b = int_; + karma::rule<outiter_type, space_type, double()> c = double_; + + start = a[_1 = at_c<0>(_r0)] << b[_1 = at_c<1>(_r0)] << c[_1 = at_c<2>(_r0)]; + BOOST_TEST(test_delimited("a 10 12.4 ", start, vec, space)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/pattern2.cpp b/src/boost/libs/spirit/test/karma/pattern2.cpp new file mode 100644 index 00000000..4621065e --- /dev/null +++ b/src/boost/libs/spirit/test/karma/pattern2.cpp @@ -0,0 +1,153 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + typedef spirit_test::output_iterator<char>::type outiter_type; + + // locals test + { + karma::rule<outiter_type, locals<std::string> > start; + + start = string[_1 = "abc", _a = _1] << int_[_1 = 10] << string[_1 = _a]; + BOOST_TEST(test("abc10abc", start)); + } + + { + karma::rule<outiter_type, space_type, locals<std::string> > start; + + start = string[_1 = "abc", _a = _1] << int_[_1 = 10] << string[_1 = _a]; + BOOST_TEST(test_delimited("abc 10 abc ", start, space)); + } + + // alias tests + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, var_type()> d, start; + + d = start.alias(); // d will always track start + + start = (char_ | int_ | double_)[_1 = _val]; + + var_type v ('a'); + BOOST_TEST(test("a", d, v)); + v = 10; + BOOST_TEST(test("10", d, v)); + v = 12.4; + BOOST_TEST(test("12.4", d, v)); + } + + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, space_type, var_type()> d, start; + + d = start.alias(); // d will always track start + + start = (char_ | int_ | double_)[_1 = _val]; + + var_type v ('a'); + BOOST_TEST(test_delimited("a ", d, v, space)); + v = 10; + BOOST_TEST(test_delimited("10 ", d, v, space)); + v = 12.4; + BOOST_TEST(test_delimited("12.4 ", d, v, space)); + } + + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, var_type()> d, start; + + d = start.alias(); // d will always track start + + start %= char_ | int_ | double_; + + var_type v ('a'); + BOOST_TEST(test("a", d, v)); + v = 10; + BOOST_TEST(test("10", d, v)); + v = 12.4; + BOOST_TEST(test("12.4", d, v)); + + start = char_ | int_ | double_; + + v = 'a'; + BOOST_TEST(test("a", d, v)); + v = 10; + BOOST_TEST(test("10", d, v)); + v = 12.4; + BOOST_TEST(test("12.4", d, v)); + } + + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, space_type, var_type()> d, start; + + d = start.alias(); // d will always track start + + start %= char_ | int_ | double_; + + var_type v ('a'); + BOOST_TEST(test_delimited("a ", d, v, space)); + v = 10; + BOOST_TEST(test_delimited("10 ", d, v, space)); + v = 12.4; + BOOST_TEST(test_delimited("12.4 ", d, v, space)); + + start = char_ | int_ | double_; + + v = 'a'; + BOOST_TEST(test_delimited("a ", d, v, space)); + v = 10; + BOOST_TEST(test_delimited("10 ", d, v, space)); + v = 12.4; + BOOST_TEST(test_delimited("12.4 ", d, v, space)); + } + + /////////////////////////////////////////////////////////////////////////// + { + using boost::spirit::karma::int_; + using boost::spirit::karma::_1; + using boost::spirit::karma::_val; + using boost::spirit::karma::space; + using boost::spirit::karma::space_type; + + karma::rule<outiter_type, int()> r1 = int_; + karma::rule<outiter_type, space_type, int()> r2 = int_; + + int i = 123; + int j = 456; + BOOST_TEST(test("123", r1[_1 = _val], i)); + BOOST_TEST(test_delimited("456 ", r2[_1 = _val], j, space)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/pattern3.cpp b/src/boost/libs/spirit/test/karma/pattern3.cpp new file mode 100644 index 00000000..f5ff2bc0 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/pattern3.cpp @@ -0,0 +1,120 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + typedef spirit_test::output_iterator<char>::type outiter_type; + + // basic tests + { + karma::rule<outiter_type> start; + + start = char_[_1 = 'a'] << int_[_1 = 10] << double_[_1 = 12.4]; + BOOST_TEST(test("a1012.4", start)); + + start = (char_ << int_ << double_)[_1 = 'a', _2 = 10, _3 = 12.4]; + BOOST_TEST(test("a1012.4", start)); + + karma::rule<outiter_type> a, b, c; + a = char_[_1 = 'a']; + b = int_[_1 = 10]; + c = double_[_1 = 12.4]; + + start = a << b << c; + BOOST_TEST(test("a1012.4", start)); + } + + // basic tests with delimiter + { + karma::rule<outiter_type, space_type> start; + + start = char_[_1 = 'a'] << int_[_1 = 10] << double_[_1 = 12.4]; + BOOST_TEST(test_delimited("a 10 12.4 ", start, space)); + + start = (char_ << int_ << double_)[_1 = 'a', _2 = 10, _3 = 12.4]; + BOOST_TEST(test_delimited("a 10 12.4 ", start, space)); + + karma::rule<outiter_type, space_type> a, b, c; + a = char_[_1 = 'a']; + b = int_[_1 = 10]; + c = double_[_1 = 12.4]; + + start = a << b << c; + BOOST_TEST(test_delimited("a 10 12.4 ", start, space)); + } + + // basic tests involving a direct parameter + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, var_type()> start; + + start = (char_ | int_ | double_)[_1 = _r0]; + + var_type v ('a'); + BOOST_TEST(test("a", start, v)); + v = 10; + BOOST_TEST(test("10", start, v)); + v = 12.4; + BOOST_TEST(test("12.4", start, v)); + } + + { + typedef variant<char, int, double> var_type; + + karma::rule<outiter_type, space_type, var_type()> start; + + start = (char_ | int_ | double_)[_1 = _r0]; + + var_type v ('a'); + BOOST_TEST(test_delimited("a ", start, v, space)); + v = 10; + BOOST_TEST(test_delimited("10 ", start, v, space)); + v = 12.4; + BOOST_TEST(test_delimited("12.4 ", start, v, space)); + } + + // test handling of single element fusion sequences + { + using boost::fusion::vector; + karma::rule<outiter_type, vector<int>()> r = int_; + + vector<int> v(1); + BOOST_TEST(test("1", r, v)); + } + + { + using boost::fusion::vector; + karma::rule<outiter_type, space_type, vector<int>()> r = int_; + + vector<int> v(1); + BOOST_TEST(test_delimited("1 ", r, v, space)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/pattern4.cpp b/src/boost/libs/spirit/test/karma/pattern4.cpp new file mode 100644 index 00000000..b67939c6 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/pattern4.cpp @@ -0,0 +1,141 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + typedef spirit_test::output_iterator<char>::type outiter_type; + + { + karma::rule<outiter_type, void(char, int, double)> start; + fusion::vector<char, int, double> vec('a', 10, 12.4); + + start = char_[_1 = _r1] << int_[_1 = _r2] << double_[_1 = _r3]; + BOOST_TEST(test("a1012.4", start('a', 10, 12.4))); + + start = (char_ << int_ << double_)[_1 = _r1, _2 = _r2, _3 = _r3]; + BOOST_TEST(test("a1012.4", start('a', 10, 12.4))); + + karma::rule<outiter_type, void(char)> a; + karma::rule<outiter_type, void(int)> b; + karma::rule<outiter_type, void(double)> c; + + a = char_[_1 = _r1]; + b = int_[_1 = _r1]; + c = double_[_1 = _r1]; + start = a(_r1) << b(_r2) << c(_r3); + BOOST_TEST(test("a1012.4", start('a', 10, 12.4))); + } + + { + karma::rule<outiter_type, space_type, void(char, int, double)> start; + fusion::vector<char, int, double> vec('a', 10, 12.4); + + start = char_[_1 = _r1] << int_[_1 = _r2] << double_[_1 = _r3]; + BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space)); + + start = (char_ << int_ << double_)[_1 = _r1, _2 = _r2, _3 = _r3]; + BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space)); + + karma::rule<outiter_type, space_type, void(char)> a; + karma::rule<outiter_type, space_type, void(int)> b; + karma::rule<outiter_type, space_type, void(double)> c; + + a = char_[_1 = _r1]; + b = int_[_1 = _r1]; + c = double_[_1 = _r1]; + start = a(_r1) << b(_r2) << c(_r3); + BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space)); + } + + // copy tests + { + karma::rule<outiter_type> a, b, c, start; + + a = 'a'; + b = int_(10); + c = double_(12.4); + + // The FF is the dynamic equivalent of start = a << b << c; + start = a; + start = start.copy() << b; + start = start.copy() << c; + start = start.copy(); + + BOOST_TEST(test("a1012.4", start)); + } + + { + karma::rule<outiter_type, space_type> a, b, c, start; + + a = 'a'; + b = int_(10); + c = double_(12.4); + + // The FF is the dynamic equivalent of start = a << b << c; + start = a; + start = start.copy() << b; + start = start.copy() << c; + start = start.copy(); + + BOOST_TEST(test_delimited("a 10 12.4 ", start, space)); + } + + { // specifying the encoding + using karma::lower; + using karma::upper; + using karma::string; + + typedef boost::spirit::char_encoding::iso8859_1 iso8859_1; + karma::rule<outiter_type, iso8859_1> r; + + r = lower['\xE1']; + BOOST_TEST(test("\xE1", r)); + r = lower[char_('\xC1')]; + BOOST_TEST(test("\xE1", r)); + r = upper['\xE1']; + BOOST_TEST(test("\xC1", r)); + r = upper[char_('\xC1')]; + BOOST_TEST(test("\xC1", r)); + + r = lower["\xE1\xC1"]; + BOOST_TEST(test("\xE1\xE1", r)); + r = lower[lit("\xE1\xC1")]; + BOOST_TEST(test("\xE1\xE1", r)); + r = lower[string("\xE1\xC1")]; + BOOST_TEST(test("\xE1\xE1", r)); + r = upper["\xE1\xC1"]; + BOOST_TEST(test("\xC1\xC1", r)); + r = upper[lit("\xE1\xC1")]; + BOOST_TEST(test("\xC1\xC1", r)); + r = upper[string("\xE1\xC1")]; + BOOST_TEST(test("\xC1\xC1", r)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/pch.hpp b/src/boost/libs/spirit/test/karma/pch.hpp new file mode 100644 index 00000000..dd711908 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/pch.hpp @@ -0,0 +1,21 @@ +/*============================================================================= + Copyright (c) 2019 Nikita Kniazev + + 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) +=============================================================================*/ +#ifdef BOOST_BUILD_PCH_ENABLED + +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/support_argument.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/optional.hpp> +#include <boost/variant.hpp> +#include <vector> +#include <string> +#include <sstream> +#include <iostream> + +#endif diff --git a/src/boost/libs/spirit/test/karma/plus.cpp b/src/boost/libs/spirit/test/karma/plus.cpp new file mode 100644 index 00000000..1bd2daab --- /dev/null +++ b/src/boost/libs/spirit/test/karma/plus.cpp @@ -0,0 +1,223 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/assign/std/vector.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/fusion/include/vector.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +struct action +{ + action (std::vector<char>& vec) + : vec(vec), it(vec.begin()) + {} + + void operator()(unsigned& value, boost::spirit::unused_type, bool& pass) const + { + pass = (it != vec.end()); + if (pass) + value = *it++; + } + + std::vector<char>& vec; + mutable std::vector<char>::iterator it; +}; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + namespace fusion = boost::fusion; + + { + std::string s1("aaaa"); + BOOST_TEST(test("aaaa", +char_, s1)); + BOOST_TEST(test_delimited("a a a a ", +char_, s1, ' ')); + + std::string s2("a"); + BOOST_TEST(test("a", +char_, s2)); + BOOST_TEST(test_delimited("a ", +char_, s2, ' ')); + + std::string s3(""); + BOOST_TEST(!test("", +char_, s2)); + BOOST_TEST(!test_delimited("", +char_, s3, ' ')); + } + + { + std::string s1("aaaaa"); + BOOST_TEST(test("aaaaa", char_ << +(char_ << char_), s1)); + BOOST_TEST(test_delimited("a a a a a ", + char_ << +(char_ << char_), s1, ' ')); + + s1 = "a"; + BOOST_TEST(!test("", char_ << +(char_ << char_), s1)); + s1 = "aa"; + BOOST_TEST(!test("", char_ << +(char_ << char_), s1)); + s1 = "aaaa"; + BOOST_TEST(!test("", char_ << +(char_ << char_), s1)); + } + + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + using namespace boost::assign; + + typedef std::pair<char, char> data; + std::vector<data> v1; + v1 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + r = &char_('a') << char_; + + BOOST_TEST(!test("", r << +(r << r), v1)); + BOOST_TEST(!test("", relaxed[r << +(r << r)], v1)); + BOOST_TEST(!test("", strict[r << +(r << r)], v1)); + + v1 += std::make_pair('a', 'a'); + + BOOST_TEST(!test("", r << *(r << r), v1)); + BOOST_TEST(!test("", relaxed[r << +(r << r)], v1)); + BOOST_TEST(!test("", strict[r << +(r << r)], v1)); + + v1 += std::make_pair('a', 'a'); + + BOOST_TEST(test("aaa", r << +(r << r), v1)); + BOOST_TEST(test("aaa", relaxed[r << +(r << r)], v1)); + BOOST_TEST(!test("", strict[r << +(r << r)], v1)); + } + + { + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c'; + + BOOST_TEST(test("abc", +char_, v)); + BOOST_TEST(test_delimited("a b c ", +char_, v, ' ')); + } + + { + using namespace boost::assign; + + std::vector<int> v; + v += 10, 20, 30; + + BOOST_TEST(test("102030", +int_, v)); + BOOST_TEST(test_delimited("10, 20, 30, ", +int_, v, lit(", "))); + + BOOST_TEST(test("10,20,30,", +(int_ << ','), v)); + BOOST_TEST(test_delimited("10 , 20 , 30 , ", +(int_ << ','), v, lit(' '))); + +// leads to infinite loops +// fusion::vector<char, char> cc ('a', 'c'); +// BOOST_TEST(test("ac", char_ << !+(lit(' ') << ',') << char_, cc)); +// BOOST_TEST(test_delimited("a c ", +// char_ << !+(lit(' ') << ',') << char_, cc, " ")); + } + + { // actions + using namespace boost::assign; + namespace phx = boost::phoenix; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + + BOOST_TEST(test("abcdefgh", (+char_)[_1 = phx::ref(v)])); + BOOST_TEST(test_delimited("a b c d e f g h ", + (+char_ )[_1 = phx::ref(v)], space)); + } + + // failing sub-generators + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + + using namespace boost::assign; + + typedef std::pair<char, char> data; + std::vector<data> v2; + v2 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + + r = &char_('d') << char_; + BOOST_TEST(test("d", +r, v2)); + BOOST_TEST(test("d", relaxed[+r], v2)); + BOOST_TEST(!test("", strict[+r], v2)); + + r = &char_('a') << char_; + BOOST_TEST(test("a", +r, v2)); + BOOST_TEST(test("a", relaxed[+r], v2)); + BOOST_TEST(test("a", strict[+r], v2)); + + r = &char_('g') << char_; + BOOST_TEST(test("g", +r, v2)); + BOOST_TEST(test("g", relaxed[+r], v2)); + BOOST_TEST(!test("", strict[+r], v2)); + + r = !char_('d') << char_; + BOOST_TEST(test("abcefg", +r, v2)); + BOOST_TEST(test("abcefg", relaxed[+r], v2)); + BOOST_TEST(test("abc", strict[+r], v2)); + + r = !char_('a') << char_; + BOOST_TEST(test("bcdefg", +r, v2)); + BOOST_TEST(test("bcdefg", relaxed[+r], v2)); + BOOST_TEST(!test("", strict[+r], v2)); + + r = !char_('g') << char_; + BOOST_TEST(test("abcdef", +r, v2)); + BOOST_TEST(test("abcdef", +r, v2)); + BOOST_TEST(test("abcdef", +r, v2)); + + r = &char_('A') << char_; + BOOST_TEST(!test("", +r, v2)); + } + + { + // make sure user defined end condition is applied if no attribute + // is passed in + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + BOOST_TEST(test("[6162636465666768]", '[' << +hex[action(v)] << ']')); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/real.hpp b/src/boost/libs/spirit/test/karma/real.hpp new file mode 100644 index 00000000..0acc7ed8 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/real.hpp @@ -0,0 +1,115 @@ +/*============================================================================== + Copyright (c) 2001-2010 Hartmut Kaiser + Copyright (c) 2010 Bryce Lelbach + + 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_TEST_REAL_NUMERICS_HPP) +#define BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP + +#include <boost/version.hpp> +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/math/concepts/real_concept.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include <boost/limits.hpp> +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces the scientific notation +template <typename T> +struct scientific_policy : boost::spirit::karma::real_policies<T> +{ + // we want the numbers always to be in scientific format + typedef boost::spirit::karma::real_policies<T> base_type; + static int floatfield(T) { return base_type::fmtflags::scientific; } +}; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces the fixed notation +template <typename T> +struct fixed_policy : boost::spirit::karma::real_policies<T> +{ + typedef boost::spirit::karma::real_policies<T> base_type; + + // we want the numbers always to be in fixed format + static int floatfield(T) { return base_type::fmtflags::fixed; } +}; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces to output trailing zeros in the +// fractional part +template <typename T> +struct trailing_zeros_policy : boost::spirit::karma::real_policies<T> // 4 digits +{ + // we want the numbers always to contain trailing zeros up to 4 digits in + // the fractional part + static bool trailing_zeros(T) { return true; } + + // we want to generate up to 4 fractional digits + static unsigned int precision(T) { return 4; } +}; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces the sign to be generated +template <typename T> +struct signed_policy : boost::spirit::karma::real_policies<T> +{ + // we want to always have a sign generated + static bool force_sign(T) + { + return true; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces to output trailing zeros in the +// fractional part +template <typename T> +struct bordercase_policy : boost::spirit::karma::real_policies<T> +{ + // we want to generate up to the maximum significant amount of fractional + // digits + static unsigned int precision(T) + { + return std::numeric_limits<T>::digits10 + 1; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// policy for real_generator, which forces to output trailing zeros in the +// fractional part +template <typename T> +struct statefull_policy : boost::spirit::karma::real_policies<T> +{ + statefull_policy(int precision = 4, bool trailingzeros = false) + : precision_(precision), trailingzeros_(trailingzeros) + {} + + // we want to generate up to the maximum significant amount of fractional + // digits + unsigned int precision(T) const + { + return precision_; + } + + bool trailing_zeros(T) const + { + return trailingzeros_; + } + + int precision_; + bool trailingzeros_; +}; + +#endif // BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP + diff --git a/src/boost/libs/spirit/test/karma/real1.cpp b/src/boost/libs/spirit/test/karma/real1.cpp new file mode 100644 index 00000000..8bb41102 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/real1.cpp @@ -0,0 +1,180 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Bryce Lelbach +// +// 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) + +#include "real.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + + { + /////////////////////////////////////////////////////////////////////// + // use the default real_policies + BOOST_TEST(test("0.0", double_, 0.0)); + BOOST_TEST(test("1.0", double_, 1.0)); + BOOST_TEST(test("1.0", double_, 1.0001)); + BOOST_TEST(test("1.001", double_, 1.001)); + BOOST_TEST(test("1.01", double_, 1.010)); + BOOST_TEST(test("1.1", double_, 1.100)); + + BOOST_TEST(test("1.234e-04", double_, 0.00012345)); + BOOST_TEST(test("0.001", double_, 0.0012345)); + BOOST_TEST(test("0.012", double_, 0.012345)); + BOOST_TEST(test("0.123", double_, 0.12345)); + BOOST_TEST(test("1.234", double_, 1.2345)); + BOOST_TEST(test("12.346", double_, 12.346)); + BOOST_TEST(test("123.46", double_, 123.46)); + BOOST_TEST(test("1234.5", double_, 1234.5)); + BOOST_TEST(test("12342.0", double_, 12342.)); + BOOST_TEST(test("1.234e05", double_, 123420.)); + + BOOST_TEST(test("-1.0", double_, -1.0)); + BOOST_TEST(test("-1.234", double_, -1.2345)); + BOOST_TEST(test("-1.235", double_, -1.2346)); + BOOST_TEST(test("-1234.2", double_, -1234.2)); + + BOOST_TEST(test("1.0", double_(1.0))); + BOOST_TEST(test("1.0", double_(1.0001))); + BOOST_TEST(test("1.001", double_(1.001))); + BOOST_TEST(test("1.01", double_(1.010))); + BOOST_TEST(test("1.1", double_(1.100))); + + BOOST_TEST(test("1.234e-04", double_(0.00012345))); + BOOST_TEST(test("0.001", double_(0.0012345))); + BOOST_TEST(test("0.012", double_(0.012345))); + BOOST_TEST(test("0.123", double_(0.12345))); + BOOST_TEST(test("1.234", double_(1.2345))); + BOOST_TEST(test("12.346", double_(12.346))); + BOOST_TEST(test("123.46", double_(123.46))); + BOOST_TEST(test("1234.5", double_(1234.5))); + BOOST_TEST(test("12342.0", double_(12342.))); + BOOST_TEST(test("1.234e05", double_(123420.))); + } + + { + /////////////////////////////////////////////////////////////////////// + // test NaN and Inf + BOOST_TEST(test("nan", double_, std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("-nan", double_, -std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("inf", double_, std::numeric_limits<double>::infinity())); + BOOST_TEST(test("-inf", double_, -std::numeric_limits<double>::infinity())); + + typedef karma::real_generator<double, signed_policy<double> > signed_type; + signed_type const signed_ = signed_type(); + + BOOST_TEST(test("+nan", signed_, std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("-nan", signed_, -std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("+inf", signed_, std::numeric_limits<double>::infinity())); + BOOST_TEST(test("-inf", signed_, -std::numeric_limits<double>::infinity())); +#if defined(BOOST_SPIRIT_ZERO_PROBLEM) + BOOST_TEST(test(" 0.0", signed_, 0.0)); +#endif + + BOOST_TEST(test("+nan", signed_(std::numeric_limits<double>::quiet_NaN()))); + BOOST_TEST(test("-nan", signed_(-std::numeric_limits<double>::quiet_NaN()))); + BOOST_TEST(test("+inf", signed_(std::numeric_limits<double>::infinity()))); + BOOST_TEST(test("-inf", signed_(-std::numeric_limits<double>::infinity()))); +#if defined(BOOST_SPIRIT_ZERO_PROBLEM) + BOOST_TEST(test(" 0.0", signed_(0.0))); +#endif + } + + { + /////////////////////////////////////////////////////////////////////// + typedef karma::real_generator<double, statefull_policy<double> > + statefull_type; + + statefull_policy<double> policy(5, true); + statefull_type const statefull = statefull_type(policy); + + BOOST_TEST(test("0.00000", statefull, 0.0)); + BOOST_TEST(test("0.00000", statefull(0.0))); + + using namespace boost::phoenix; + BOOST_TEST(test("0.00000", statefull(val(0.0)))); + } + + { + /////////////////////////////////////////////////////////////////////// + typedef karma::real_generator<double, trailing_zeros_policy<double> > + trailing_zeros_type; + trailing_zeros_type const trail_zeros = trailing_zeros_type(); + + BOOST_TEST(test("0.0000", trail_zeros, 0.0)); + BOOST_TEST(test("1.0000", trail_zeros, 1.0)); + BOOST_TEST(test("1.0001", trail_zeros, 1.0001)); + BOOST_TEST(test("1.0010", trail_zeros, 1.001)); + BOOST_TEST(test("1.0100", trail_zeros, 1.010)); + BOOST_TEST(test("1.1000", trail_zeros, 1.100)); + + BOOST_TEST(test("1.2345e-04", trail_zeros, 0.00012345)); + BOOST_TEST(test("0.0012", trail_zeros, 0.0012345)); + BOOST_TEST(test("0.0123", trail_zeros, 0.012345)); + BOOST_TEST(test("0.1235", trail_zeros, 0.12345)); + BOOST_TEST(test("1.2345", trail_zeros, 1.2345)); + BOOST_TEST(test("12.3460", trail_zeros, 12.346)); + BOOST_TEST(test("123.4600", trail_zeros, 123.46)); + BOOST_TEST(test("1234.5000", trail_zeros, 1234.5)); + BOOST_TEST(test("12342.0000", trail_zeros, 12342.)); + BOOST_TEST(test("1.2342e05", trail_zeros, 123420.)); + + BOOST_TEST(test("-1.0000", trail_zeros, -1.0)); + BOOST_TEST(test("-1.2345", trail_zeros, -1.2345)); + BOOST_TEST(test("-1.2346", trail_zeros, -1.2346)); + BOOST_TEST(test("-1234.2000", trail_zeros, -1234.2)); + + BOOST_TEST(test("1.0000", trail_zeros(1.0))); + BOOST_TEST(test("1.0001", trail_zeros(1.0001))); + BOOST_TEST(test("1.0010", trail_zeros(1.001))); + BOOST_TEST(test("1.0100", trail_zeros(1.010))); + BOOST_TEST(test("1.1000", trail_zeros(1.100))); + + BOOST_TEST(test("1.2345e-04", trail_zeros(0.00012345))); + BOOST_TEST(test("0.0012", trail_zeros(0.0012345))); + BOOST_TEST(test("0.0123", trail_zeros(0.012345))); + BOOST_TEST(test("0.1235", trail_zeros(0.12345))); + BOOST_TEST(test("1.2345", trail_zeros(1.2345))); + BOOST_TEST(test("12.3460", trail_zeros(12.346))); + BOOST_TEST(test("123.4600", trail_zeros(123.46))); + BOOST_TEST(test("1234.5000", trail_zeros(1234.5))); + BOOST_TEST(test("12342.0000", trail_zeros(12342.))); + BOOST_TEST(test("1.2342e05", trail_zeros(123420.))); + } + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + // test NaN and Inf + BOOST_TEST(test("NAN", upper[double_], + std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("-NAN", upper[double_], + -std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("INF", upper[double_], + std::numeric_limits<double>::infinity())); + BOOST_TEST(test("-INF", upper[double_], + -std::numeric_limits<double>::infinity())); + + typedef karma::real_generator<double, signed_policy<double> > signed_type; + signed_type const signed_ = signed_type(); + + BOOST_TEST(test("+NAN", upper[signed_], + std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("-NAN", upper[signed_], + -std::numeric_limits<double>::quiet_NaN())); + BOOST_TEST(test("+INF", upper[signed_], + std::numeric_limits<double>::infinity())); + BOOST_TEST(test("-INF", upper[signed_], + -std::numeric_limits<double>::infinity())); +#if defined(BOOST_SPIRIT_ZERO_PROBLEM) + BOOST_TEST(test(" 0.0", upper[signed_], 0.0)); +#endif + } + + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/real2.cpp b/src/boost/libs/spirit/test/karma/real2.cpp new file mode 100644 index 00000000..62ff60d6 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/real2.cpp @@ -0,0 +1,163 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Bryce Lelbach +// +// 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) + +#include "real.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + BOOST_TEST(test_delimited("0.0 ", double_, 0.0, char_(' '))); + BOOST_TEST(test_delimited("1.0 ", double_, 1.0, char_(' '))); + BOOST_TEST(test_delimited("1.0 ", double_, 1.0001, char_(' '))); + BOOST_TEST(test_delimited("1.001 ", double_, 1.001, char_(' '))); + BOOST_TEST(test_delimited("1.01 ", double_, 1.010, char_(' '))); + BOOST_TEST(test_delimited("1.1 ", double_, 1.100, char_(' '))); + + BOOST_TEST(test_delimited("1.234e-04 ", double_, 0.00012345, char_(' '))); + BOOST_TEST(test_delimited("0.001 ", double_, 0.0012345, char_(' '))); + BOOST_TEST(test_delimited("0.012 ", double_, 0.012345, char_(' '))); + BOOST_TEST(test_delimited("0.123 ", double_, 0.12345, char_(' '))); + BOOST_TEST(test_delimited("1.234 ", double_, 1.2345, char_(' '))); + BOOST_TEST(test_delimited("12.346 ", double_, 12.346, char_(' '))); + BOOST_TEST(test_delimited("123.46 ", double_, 123.46, char_(' '))); + BOOST_TEST(test_delimited("1234.5 ", double_, 1234.5, char_(' '))); + BOOST_TEST(test_delimited("12342.0 ", double_, 12342., char_(' '))); + BOOST_TEST(test_delimited("1.234e05 ", double_, 123420., char_(' '))); + + BOOST_TEST(test_delimited("-1.0 ", double_, -1.0, char_(' '))); + BOOST_TEST(test_delimited("-1.234 ", double_, -1.2345, char_(' '))); + BOOST_TEST(test_delimited("-1.235 ", double_, -1.2346, char_(' '))); + BOOST_TEST(test_delimited("-1234.2 ", double_, -1234.2, char_(' '))); + + BOOST_TEST(test_delimited("1.0 ", double_(1.0), char_(' '))); + BOOST_TEST(test_delimited("1.0 ", double_(1.0001), char_(' '))); + BOOST_TEST(test_delimited("1.001 ", double_(1.001), char_(' '))); + BOOST_TEST(test_delimited("1.01 ", double_(1.010), char_(' '))); + BOOST_TEST(test_delimited("1.1 ", double_(1.100), char_(' '))); + + BOOST_TEST(test_delimited("1.234e-04 ", double_(0.00012345), char_(' '))); + BOOST_TEST(test_delimited("0.001 ", double_(0.0012345), char_(' '))); + BOOST_TEST(test_delimited("0.012 ", double_(0.012345), char_(' '))); + BOOST_TEST(test_delimited("0.123 ", double_(0.12345), char_(' '))); + BOOST_TEST(test_delimited("1.234 ", double_(1.2345), char_(' '))); + BOOST_TEST(test_delimited("12.346 ", double_(12.346), char_(' '))); + BOOST_TEST(test_delimited("123.46 ", double_(123.46), char_(' '))); + BOOST_TEST(test_delimited("1234.5 ", double_(1234.5), char_(' '))); + BOOST_TEST(test_delimited("12342.0 ", double_(12342.), char_(' '))); + BOOST_TEST(test_delimited("1.234e05 ", double_(123420.), char_(' '))); + } + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + // test NaN and Inf + BOOST_TEST(test_delimited("nan ", double_, + std::numeric_limits<double>::quiet_NaN(), char_(' '))); + BOOST_TEST(test_delimited("-nan ", double_, + -std::numeric_limits<double>::quiet_NaN(), char_(' '))); + BOOST_TEST(test_delimited("inf ", double_, + std::numeric_limits<double>::infinity(), char_(' '))); + BOOST_TEST(test_delimited("-inf ", double_, + -std::numeric_limits<double>::infinity(), char_(' '))); + + typedef karma::real_generator<double, signed_policy<double> > signed_type; + signed_type const signed_ = signed_type(); + + BOOST_TEST(test_delimited("+nan ", signed_, + std::numeric_limits<double>::quiet_NaN(), char_(' '))); + BOOST_TEST(test_delimited("-nan ", signed_, + -std::numeric_limits<double>::quiet_NaN(), char_(' '))); + BOOST_TEST(test_delimited("+inf ", signed_, + std::numeric_limits<double>::infinity(), char_(' '))); + BOOST_TEST(test_delimited("-inf ", signed_, + -std::numeric_limits<double>::infinity(), char_(' '))); +#if defined(BOOST_SPIRIT_ZERO_PROBLEM) + BOOST_TEST(test_delimited(" 0.0 ", signed_, 0.0, char_(' '))); +#endif + } + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + typedef karma::real_generator<double, scientific_policy<double> > + science_type; + science_type const science = science_type(); + + BOOST_TEST(test("0.0e00", science, 0.0)); + BOOST_TEST(test("1.0e00", science, 1.0)); + + BOOST_TEST(test("1.234e-05", science, 0.000012345)); + BOOST_TEST(test("1.234e-04", science, 0.00012345)); + BOOST_TEST(test("1.234e-03", science, 0.0012345)); + BOOST_TEST(test("1.234e-02", science, 0.012345)); + BOOST_TEST(test("1.235e-01", science, 0.12345)); // note the rounding error! + BOOST_TEST(test("1.234e00", science, 1.2345)); + BOOST_TEST(test("1.235e01", science, 12.346)); + BOOST_TEST(test("1.235e02", science, 123.46)); + BOOST_TEST(test("1.234e03", science, 1234.5)); + BOOST_TEST(test("1.234e04", science, 12342.)); + BOOST_TEST(test("1.234e05", science, 123420.)); + + BOOST_TEST(test("-1.234e-05", science, -0.000012345)); + BOOST_TEST(test("-1.234e-04", science, -0.00012345)); + BOOST_TEST(test("-1.234e-03", science, -0.0012345)); + BOOST_TEST(test("-1.234e-02", science, -0.012345)); + BOOST_TEST(test("-1.235e-01", science, -0.12345)); // note the rounding error! + BOOST_TEST(test("-1.234e00", science, -1.2345)); + BOOST_TEST(test("-1.235e01", science, -12.346)); + BOOST_TEST(test("-1.235e02", science, -123.46)); + BOOST_TEST(test("-1.234e03", science, -1234.5)); + BOOST_TEST(test("-1.234e04", science, -12342.)); + BOOST_TEST(test("-1.234e05", science, -123420.)); + + BOOST_TEST(test("1.234E-05", upper[science], 0.000012345)); + BOOST_TEST(test("1.234E-04", upper[science], 0.00012345)); + BOOST_TEST(test("1.234E-03", upper[science], 0.0012345)); + BOOST_TEST(test("1.234E-02", upper[science], 0.012345)); + BOOST_TEST(test("1.235E-01", upper[science], 0.12345)); // note the rounding error! + BOOST_TEST(test("1.234E00", upper[science], 1.2345)); + BOOST_TEST(test("1.235E01", upper[science], 12.346)); + BOOST_TEST(test("1.235E02", upper[science], 123.46)); + BOOST_TEST(test("1.234E03", upper[science], 1234.5)); + BOOST_TEST(test("1.234E04", upper[science], 12342.)); + BOOST_TEST(test("1.234E05", upper[science], 123420.)); + + BOOST_TEST(test("-1.234E-05", upper[science], -0.000012345)); + BOOST_TEST(test("-1.234E-04", upper[science], -0.00012345)); + BOOST_TEST(test("-1.234E-03", upper[science], -0.0012345)); + BOOST_TEST(test("-1.234E-02", upper[science], -0.012345)); + BOOST_TEST(test("-1.235E-01", upper[science], -0.12345)); // note the rounding error! + BOOST_TEST(test("-1.234E00", upper[science], -1.2345)); + BOOST_TEST(test("-1.235E01", upper[science], -12.346)); + BOOST_TEST(test("-1.235E02", upper[science], -123.46)); + BOOST_TEST(test("-1.234E03", upper[science], -1234.5)); + BOOST_TEST(test("-1.234E04", upper[science], -12342.)); + BOOST_TEST(test("-1.234E05", upper[science], -123420.)); + } + + { + BOOST_TEST(test("1.0", lit(1.0))); + BOOST_TEST(test("1.0", lit(1.0f))); + BOOST_TEST(test("1.0", lit(1.0l))); + + BOOST_TEST(test("1.0", double_(1.0), 1.0)); + BOOST_TEST(test("1.0", float_(1.0), 1.0f)); + BOOST_TEST(test("1.0", long_double(1.0), 1.0l)); + BOOST_TEST(!test("", double_(1.0), 2.0)); + BOOST_TEST(!test("", float_(1.0), 2.0f)); + BOOST_TEST(!test("", long_double(1.0), 2.0l)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/real3.cpp b/src/boost/libs/spirit/test/karma/real3.cpp new file mode 100644 index 00000000..fda1508c --- /dev/null +++ b/src/boost/libs/spirit/test/karma/real3.cpp @@ -0,0 +1,185 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Bryce Lelbach +// +// 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) + +#include <boost/spirit/include/version.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include "real.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// the customization points below have been added only recently +#if SPIRIT_VERSION >= 0x2050 +// does not provide proper std::numeric_limits, we need to roll our own +namespace boost { namespace spirit { namespace traits +{ + template <> + struct is_nan<boost::math::concepts::real_concept> + { + static bool call(boost::math::concepts::real_concept n) + { + return test_nan(n.value()); + } + }; + + template <> + struct is_infinite<boost::math::concepts::real_concept> + { + static bool call(boost::math::concepts::real_concept n) + { + return test_infinite(n.value()); + } + }; +}}} +#endif + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + + { + using namespace boost::spirit::ascii; + + /////////////////////////////////////////////////////////////////////// + typedef karma::real_generator<double, fixed_policy<double> > fixed_type; + fixed_type const fixed = fixed_type(); + + BOOST_TEST(test("0.0", fixed, 0.0)); + BOOST_TEST(test("1.0", fixed, 1.0)); + + BOOST_TEST(test("0.0", fixed, 0.000012345)); + BOOST_TEST(test("0.0", fixed, 0.00012345)); + BOOST_TEST(test("0.001", fixed, 0.0012345)); + BOOST_TEST(test("0.012", fixed, 0.012345)); + BOOST_TEST(test("0.123", fixed, 0.12345)); + BOOST_TEST(test("1.234", fixed, 1.2345)); + BOOST_TEST(test("12.345", fixed, 12.345)); + BOOST_TEST(test("123.45", fixed, 123.45)); + BOOST_TEST(test("1234.5", fixed, 1234.5)); + BOOST_TEST(test("12342.0", fixed, 12342.)); + BOOST_TEST(test("123420.0", fixed, 123420.)); + BOOST_TEST(test("123420000000000000000.0", fixed, 1.23420e20)); + + BOOST_TEST(test("0.0", fixed, -0.000012345)); + BOOST_TEST(test("0.0", fixed, -0.00012345)); + BOOST_TEST(test("-0.001", fixed, -0.0012345)); + BOOST_TEST(test("-0.012", fixed, -0.012345)); + BOOST_TEST(test("-0.123", fixed, -0.12345)); + BOOST_TEST(test("-1.234", fixed, -1.2345)); + BOOST_TEST(test("-12.346", fixed, -12.346)); + BOOST_TEST(test("-123.46", fixed, -123.46)); + BOOST_TEST(test("-1234.5", fixed, -1234.5)); + BOOST_TEST(test("-12342.0", fixed, -12342.)); + BOOST_TEST(test("-123420.0", fixed, -123420.)); + BOOST_TEST(test("-123420000000000000000.0", fixed, -1.23420e20)); + } + +// support for using real_concept with a Karma generator has been implemented +// in Boost versions > 1.36 only, additionally real_concept is available only +// if BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS is not defined +#if BOOST_VERSION > 103600 && !defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS) + { + using boost::math::concepts::real_concept; + typedef karma::real_generator<real_concept> custom_type; + custom_type const custom = custom_type(); + + BOOST_TEST(test("0.0", custom, real_concept(0.0))); + BOOST_TEST(test("1.0", custom, real_concept(1.0))); + BOOST_TEST(test("1.0", custom, real_concept(1.0001))); + BOOST_TEST(test("1.001", custom, real_concept(1.001))); + BOOST_TEST(test("1.01", custom, real_concept(1.010))); + BOOST_TEST(test("1.1", custom, real_concept(1.100))); + + BOOST_TEST(test("1.234e-04", custom, real_concept(0.00012345))); + BOOST_TEST(test("0.001", custom, real_concept(0.0012345))); + BOOST_TEST(test("0.012", custom, real_concept(0.012345))); + BOOST_TEST(test("0.123", custom, real_concept(0.12345))); + BOOST_TEST(test("1.234", custom, real_concept(1.2345))); + BOOST_TEST(test("12.346", custom, real_concept(12.346))); + BOOST_TEST(test("123.46", custom, real_concept(123.46))); + BOOST_TEST(test("1234.5", custom, real_concept(1234.5))); + BOOST_TEST(test("12342.0", custom, real_concept(12342.))); + BOOST_TEST(test("1.234e05", custom, real_concept(123420.))); + + BOOST_TEST(test("-1.0", custom, real_concept(-1.0))); + BOOST_TEST(test("-1.234", custom, real_concept(-1.2345))); + BOOST_TEST(test("-1.235", custom, real_concept(-1.2346))); + BOOST_TEST(test("-1234.2", custom, real_concept(-1234.2))); + + BOOST_TEST(test("1.0", custom(real_concept(1.0)))); + BOOST_TEST(test("1.0", custom(real_concept(1.0001)))); + BOOST_TEST(test("1.001", custom(real_concept(1.001)))); + BOOST_TEST(test("1.01", custom(real_concept(1.010)))); + BOOST_TEST(test("1.1", custom(real_concept(1.100)))); + + BOOST_TEST(test("1.234e-04", custom(real_concept(0.00012345)))); + BOOST_TEST(test("0.001", custom(real_concept(0.0012345)))); + BOOST_TEST(test("0.012", custom(real_concept(0.012345)))); + BOOST_TEST(test("0.123", custom(real_concept(0.12345)))); + BOOST_TEST(test("1.234", custom(real_concept(1.2345)))); + BOOST_TEST(test("12.346", custom(real_concept(12.346)))); + BOOST_TEST(test("123.46", custom(real_concept(123.46)))); + BOOST_TEST(test("1234.5", custom(real_concept(1234.5)))); + BOOST_TEST(test("12342.0", custom(real_concept(12342.)))); + BOOST_TEST(test("1.234e05", custom(real_concept(123420.)))); + } +#endif + +// this appears to be broken on Apple Tiger x86 with gcc4.0.1 +#if (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__ != 40001) || \ + !defined(__APPLE__) + { + /////////////////////////////////////////////////////////////////////// + typedef karma::real_generator<double, bordercase_policy<double> > + bordercase_type; + bordercase_type const bordercase = bordercase_type(); + +// BOOST_TEST(test("-5.7222349715140557e307", +// bordercase(-5.7222349715140557e307))); + + BOOST_TEST(test("1.7976931348623158e308", + bordercase(1.7976931348623158e308))); // DBL_MAX + BOOST_TEST(test("-1.7976931348623158e308", + bordercase(-1.7976931348623158e308))); // -DBL_MAX + BOOST_TEST(test("2.2250738585072014e-308", + bordercase(2.2250738585072014e-308))); // DBL_MIN + BOOST_TEST(test("-2.2250738585072014e-308", + bordercase(-2.2250738585072014e-308))); // -DBL_MIN + } +#endif + + { + boost::optional<double> v; + BOOST_TEST(!test("", double_, v)); + BOOST_TEST(!test("", double_(1.0), v)); + + v = 1.0; + BOOST_TEST(test("1.0", double_, v)); + BOOST_TEST(test("1.0", double_(1.0), v)); + } + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + { // Phoenix expression tests (requires to include + // karma_phoenix_attributes.hpp) + namespace phoenix = boost::phoenix; + + BOOST_TEST(test("1.0", double_, phoenix::val(1.0))); + + double d = 1.2; + BOOST_TEST(test("1.2", double_, phoenix::ref(d))); + BOOST_TEST(test("2.2", double_, ++phoenix::ref(d))); + } +#endif + + // test for denormalized numbers + { + BOOST_TEST(test("4.941e-324", double_, std::numeric_limits<double>::denorm_min())); + } + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/regression_adapt_adt.cpp b/src/boost/libs/spirit/test/karma/regression_adapt_adt.cpp new file mode 100644 index 00000000..41e88dc5 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_adapt_adt.cpp @@ -0,0 +1,176 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Colin Rundel +// +// 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) + +#include <boost/mpl/print.hpp> +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/fusion/include/adapt_adt.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/support_adapt_adt_attributes.hpp> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +class data1 +{ +private: + int width_; + int height_; + +public: + data1() + : width_(400), height_(400) + {} + + data1(int width, int height) + : width_(width), height_(height) + {} + + int width() const { return width_;} + int height() const { return height_;} + + void set_width(int width) { width_ = width;} + void set_height(int height) { height_ = height;} +}; + +BOOST_FUSION_ADAPT_ADT( + data1, + (int, int, obj.width(), obj.set_width(val)) + (int, int, obj.height(), obj.set_height(val)) +) + +/////////////////////////////////////////////////////////////////////////////// +class data2 +{ +private: + std::string data_; + +public: + data2() + : data_("test") + {} + + data2(std::string const& data) + : data_(data) + {} + + std::string const& data() const { return data_;} + void set_data(std::string const& data) { data_ = data;} +}; + +BOOST_FUSION_ADAPT_ADT( + data2, + (std::string, std::string const&, obj.data(), obj.set_data(val)) +) + +/////////////////////////////////////////////////////////////////////////////// +class data3 +{ +private: + double data_; + +public: + data3(double data = 0.0) + : data_(data) + {} + + double data() const { return data_;} + void set_data(double data) { data_ = data;} +}; + +BOOST_FUSION_ADAPT_ADT( + data3, + (double, double, obj.data(), obj.set_data(val)) +) + +/////////////////////////////////////////////////////////////////////////////// +class data4 +{ +public: + boost::optional<int> a_; + boost::optional<double> b_; + boost::optional<std::string> c_; + + boost::optional<int> const& a() const { return a_; } + boost::optional<double> const& b() const { return b_; } + boost::optional<std::string> const& c() const { return c_; } +}; + + +BOOST_FUSION_ADAPT_ADT( + data4, + (boost::optional<int>, boost::optional<int> const&, obj.a(), /**/) + (boost::optional<double>, boost::optional<double> const&, obj.b(), /**/) + (boost::optional<std::string>, boost::optional<std::string> const&, obj.c(), /**/) +) + +/////////////////////////////////////////////////////////////////////////////// +int main () +{ + using spirit_test::test; + + { + using boost::spirit::karma::int_; + + data1 b(800, 600); + BOOST_TEST(test("width: 800\nheight: 600\n", + "width: " << int_ << "\n" << "height: " << int_ << "\n", b)); + } + + { + using boost::spirit::karma::char_; + using boost::spirit::karma::string; + + data2 d("test"); + BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d)); + BOOST_TEST(test("data: test\n", "data: " << string << "\n", d)); + } + + { + using boost::spirit::karma::double_; + + BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0))); + BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1))); + BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10))); + +#if defined(_MSC_VER) && _MSC_VER < 1900 +# pragma warning(push) +# pragma warning(disable: 4127) // conditional expression is constant +#endif + BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n", + data3(std::numeric_limits<double>::infinity()))); + if (std::numeric_limits<double>::has_quiet_NaN) { + BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", + data3(std::numeric_limits<double>::quiet_NaN()))); + } + if (std::numeric_limits<double>::has_signaling_NaN) { + BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", + data3(std::numeric_limits<double>::signaling_NaN()))); + } +#if defined(_MSC_VER) && _MSC_VER < 1900 +# pragma warning(pop) +#endif + } + + { + using boost::spirit::karma::double_; + using boost::spirit::karma::int_; + using boost::spirit::karma::string; + + data4 d; + d.b_ = 10; + + BOOST_TEST(test( + "Testing: b: 10.0\n", + "Testing: " << -("a: " << int_ << "\n") + << -("b: " << double_ << "\n") + << -("c: " << string << "\n"), d)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/regression_center_alignment.cpp b/src/boost/libs/spirit/test/karma/regression_center_alignment.cpp new file mode 100644 index 00000000..a2ff3b3c --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_center_alignment.cpp @@ -0,0 +1,66 @@ +// Copyright (c) 2001-2012 Hartmut Kaiser +// Copyright (c) 2012 yyyy yyyy <typhoonking77@hotmail.com> +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <string> +#include <vector> + +#include<boost/spirit/include/karma.hpp> + +int main() +{ + namespace karma = boost::spirit::karma; + + int num[] = {0, 1, 2, 3, 4, 5}; + std::vector<int> contents(num, num + sizeof(num) / sizeof(int)); + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center[karma::int_], contents)); + BOOST_TEST(result == " 0 1 2 3 4 5 "); + } + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center(5)[karma::int_], contents)); + BOOST_TEST(result == " 0 1 2 3 4 5 "); + } + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center("_")[karma::int_], contents)); + BOOST_TEST(result == "_____0_________1_________2_________3_________4_________5____"); + } + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center(5, "_")[karma::int_], contents)); + BOOST_TEST(result == "__0____1____2____3____4____5__"); + } + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center(karma::char_("_"))[karma::int_], contents)); + BOOST_TEST(result == "_____0_________1_________2_________3_________4_________5____"); + } + + { + std::string result; + BOOST_TEST(karma::generate(std::back_inserter(result), + *karma::center(5, karma::char_("_"))[karma::int_], contents)); + BOOST_TEST(result == "__0____1____2____3____4____5__"); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/regression_const_real_policies.cpp b/src/boost/libs/spirit/test/karma/regression_const_real_policies.cpp new file mode 100644 index 00000000..122ceea1 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_const_real_policies.cpp @@ -0,0 +1,25 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Jeroen Habraken +// +// 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) + +// compile test verifying it's possible to use const types for real_policies. + +#include <boost/spirit/include/karma.hpp> + +#include <iterator> +#include <string> + +int main() +{ + using namespace boost::spirit::karma; + + typedef real_generator<double const, real_policies<double const> > + double_const_type; + + std::string generated; + generate(std::back_inserter(generated), double_const_type(), 1.0); + + return 0; +} diff --git a/src/boost/libs/spirit/test/karma/regression_container_variant_sequence.cpp b/src/boost/libs/spirit/test/karma/regression_container_variant_sequence.cpp new file mode 100644 index 00000000..56df14e2 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_container_variant_sequence.cpp @@ -0,0 +1,71 @@ +// Copyright (c) 2001-2012 Hartmut Kaiser +// Copyright (c) 2012 Benjamin Schindler +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/fusion/include/adapt_struct.hpp> +#include <set> + +namespace generator +{ + struct Enum + { + std::string enumName; + std::vector<std::string> enumEntries; + }; + typedef boost::variant<std::string, Enum> VariantType; + + namespace karma = boost::spirit::karma; + + // Our grammar definition + template<typename Iterator> + struct SettingsHeaderGenerator: karma::grammar<Iterator, VariantType()> + { + SettingsHeaderGenerator() : SettingsHeaderGenerator::base_type(baseRule) + { + using karma::lit; + using karma::string; + + enumRule = lit("enum ") << string << lit("\n{\n") << string % ",\n" << "}"; + declarationRule = lit("class ") << string << ';'; + baseRule = (declarationRule | enumRule) << lit("\n"); + + baseRule.name("base"); + enumRule.name("enum"); + declarationRule.name("declaration"); + } + + karma::rule<Iterator, std::string()> declarationRule; + karma::rule<Iterator, Enum()> enumRule; + karma::rule<Iterator, VariantType()> baseRule; + }; + + template <typename OutputIterator> + bool generate_header(OutputIterator& sink, VariantType& data) + { + SettingsHeaderGenerator<OutputIterator> generator; + return karma::generate(sink, generator, data); + } +} + +BOOST_FUSION_ADAPT_STRUCT(generator::Enum, + (std::string, enumName) + (std::vector<std::string>, enumEntries) +) + +int main() +{ + generator::VariantType variant = "bla"; + std::string generated; + std::back_insert_iterator<std::string> sink(generated); + BOOST_TEST(generator::generate_header(sink, variant)); + BOOST_TEST(generated == "class bla;\n"); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/regression_iterator.cpp b/src/boost/libs/spirit/test/karma/regression_iterator.cpp new file mode 100644 index 00000000..915d61aa --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_iterator.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2013 Louis Dionne +// Copyright (c) 2001-2013 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/range/adaptor/transformed.hpp> +#include <boost/spirit/include/karma.hpp> + +#include <iostream> + +// Note how the return is made by value instead of by reference. +template <typename T> T identity(T const& t) { return t; } + +template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr + , typename Delimiter, typename Attribute> +bool test(Char const *expected, + boost::spirit::karma::detail::format_manip< + Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm) +{ + std::ostringstream ostrm; + ostrm << fm; + return ostrm.good() && ostrm.str() == expected; +} + +int main() +{ + namespace karma = boost::spirit::karma; + namespace adaptors = boost::adaptors; + int ints[] = {0, 1, 2, 3, 4}; + + BOOST_TEST((test("0 1 2 3 4", + karma::format(karma::int_ % ' ', + ints | adaptors::transformed(&identity<int>))) + )); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/regression_optional_double.cpp b/src/boost/libs/spirit/test/karma/regression_optional_double.cpp new file mode 100644 index 00000000..b1fc422a --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_optional_double.cpp @@ -0,0 +1,52 @@ +// Copyright (c) 2010 Olaf Peter +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/karma.hpp> +#include <boost/detail/lightweight_test.hpp> + +namespace client +{ + namespace karma = boost::spirit::karma; + + template <typename OutputIterator> + struct grammar + : karma::grammar<OutputIterator, boost::optional<double>()> + { + grammar() + : grammar::base_type(start) + { + using karma::double_; + + u = double_ << "U"; + start = ( !double_ << "NA" ) | u; + + start.name("start"); + u.name("u"); + } + + karma::rule<OutputIterator, double()> u; + karma::rule<OutputIterator, boost::optional<double>()> start; + }; +} + +int main() +{ + namespace karma = boost::spirit::karma; + + typedef std::back_insert_iterator<std::string> sink_type; + + boost::optional<double> d1, d2; + d2 = 1.0; + + std::string generated1, generated2; + client::grammar<sink_type> g; + + BOOST_TEST(karma::generate(sink_type(generated1), g, d1) && generated1 == "NA"); + BOOST_TEST(karma::generate(sink_type(generated2), g, d2) && generated2 == "1.0U"); + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/regression_real_0.cpp b/src/boost/libs/spirit/test/karma/regression_real_0.cpp new file mode 100644 index 00000000..c04c1424 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_real_0.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2012 Agustin K-ballo Berge +// Copyright (c) 2001-2012 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/karma.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <iterator> +#include <sstream> +#include <string> + +int main() +{ + namespace karma = boost::spirit::karma; + std::string output; + std::back_insert_iterator< std::string > sink = std::back_inserter( output ); + karma::generate( sink, karma::double_, 0 ); // prints ¨inf¨ instead of ¨0.0¨ + BOOST_TEST((output == "0.0")); + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/regression_real_policy_sign.cpp b/src/boost/libs/spirit/test/karma/regression_real_policy_sign.cpp new file mode 100644 index 00000000..e4225f7f --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_real_policy_sign.cpp @@ -0,0 +1,43 @@ +// Copyright (c) 2013 Alex Korobka +// +// 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) + +// This test checks whether #8970 was fixed + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <iostream> +#include <boost/spirit/include/karma.hpp> + +#include "test.hpp" + +using namespace spirit_test; +using namespace boost::spirit; + +template <typename Num> +struct signed_policy : karma::real_policies<Num> +{ + static bool force_sign(Num /*n*/) { return true; } +}; + +int main() +{ + karma::real_generator<double, signed_policy<double> > const force_sign_double = + karma::real_generator<double, signed_policy<double> >(); + + BOOST_TEST(test("-0.123", force_sign_double, -0.123)); + BOOST_TEST(test("-1.123", force_sign_double, -1.123)); + BOOST_TEST(test("0.0", force_sign_double, 0)); + BOOST_TEST(test("+0.123", force_sign_double, 0.123)); + BOOST_TEST(test("+1.123", force_sign_double, 1.123)); + + using karma::double_; + BOOST_TEST(test("-0.123", double_, -0.123)); + BOOST_TEST(test("-1.123", double_, -1.123)); + BOOST_TEST(test("0.0", double_, 0)); + BOOST_TEST(test("0.123", double_, 0.123)); + BOOST_TEST(test("1.123", double_, 1.123)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/regression_real_scientific.cpp b/src/boost/libs/spirit/test/karma/regression_real_scientific.cpp new file mode 100644 index 00000000..45f67965 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_real_scientific.cpp @@ -0,0 +1,40 @@ +// Copyright (c) 2010 Lars Kielhorn +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma.hpp> + +#include <iostream> +#include <string> +#include <iterator> + +namespace karma = boost::spirit::karma; + +// define a new real number formatting policy +template <typename Num> +struct scientific_policy : karma::real_policies<Num> +{ + // we want the numbers always to be in scientific format + static int floatfield(Num) { return std::ios_base::scientific; } +}; + +int main() +{ + // define a new generator type based on the new policy + typedef karma::real_generator<double, scientific_policy<double> > + science_type; + science_type const scientific = science_type(); + + std::string output; + typedef std::back_insert_iterator<std::string> output_iterator; + output_iterator sink(output); + + // should output: 1.0e-01, but will output: 10.0e-02 + BOOST_TEST(karma::generate(sink, scientific, 0.1) && output == "1.0e-01"); + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/regression_semantic_action_attribute.cpp b/src/boost/libs/spirit/test/karma/regression_semantic_action_attribute.cpp new file mode 100644 index 00000000..01265d9f --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_semantic_action_attribute.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2010 Michael Caisse +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <string> +#include <vector> +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +namespace karma = boost::spirit::karma; +namespace phx = boost::phoenix; + +int main() +{ + using karma::int_; + using karma::_1; + + BOOST_TEST(test("16909060", int_[ _1 = phx::val(0x01020304) ])); + + // make sure the passed attribute type does not enforce the attribute type + // for the semantic action + unsigned char char_value = 8; + BOOST_TEST(test("16909060", int_[ _1 = phx::val(0x01020304) ], char_value)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/regression_unicode_char.cpp b/src/boost/libs/spirit/test/karma/regression_unicode_char.cpp new file mode 100644 index 00000000..ab76acab --- /dev/null +++ b/src/boost/libs/spirit/test/karma/regression_unicode_char.cpp @@ -0,0 +1,63 @@ +// Copyright (c) 2012 David Bailey +// Copyright (c) 2001-2012 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#ifndef BOOST_SPIRIT_UNICODE +#define BOOST_SPIRIT_UNICODE +#endif + +#include <boost/spirit/home/karma/nonterminal/grammar.hpp> +#include <boost/spirit/home/karma/nonterminal/rule.hpp> +#include <boost/spirit/home/karma/char.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +template <typename OutputIterator> +struct unicode_char_grammar_ + : public boost::spirit::karma::grammar< + OutputIterator, boost::spirit::char_encoding::unicode::char_type()> +{ + unicode_char_grammar_() : unicode_char_grammar_::base_type(thechar) + { + using boost::spirit::karma::unicode::char_; + thechar = char_; + } + + boost::spirit::karma::rule< + OutputIterator, boost::spirit::char_encoding::unicode::char_type() + > thechar; +}; + +int +main() +{ + using namespace boost::spirit; + + { + typedef std::basic_string<char_encoding::unicode::char_type> unicode_string; + typedef std::back_insert_iterator<unicode_string> unicode_back_insert_iterator_type; + + using namespace boost::spirit::unicode; + + BOOST_TEST(test("x", char_, 'x')); + BOOST_TEST(test(L"x", char_, L'x')); + + char_encoding::unicode::char_type unicodeCharacter = 0x00000078u; + std::basic_string<char_encoding::unicode::char_type> expected; + expected.push_back(unicodeCharacter); + + unicode_char_grammar_<unicode_back_insert_iterator_type> unichar; + + BOOST_TEST(test(expected, unichar, unicodeCharacter)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/repeat1.cpp b/src/boost/libs/spirit/test/karma/repeat1.cpp new file mode 100644 index 00000000..ed0bc5d9 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/repeat1.cpp @@ -0,0 +1,144 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/assign/std/vector.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> +#include <boost/spirit/include/support_argument.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <string> +#include <iostream> +#include <vector> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +struct action +{ + action (std::vector<char>& vec) + : vec(vec), it(vec.begin()) + {} + + void operator()(unsigned& value, boost::spirit::unused_type, bool& pass) const + { + pass = (it != vec.end()); + if (pass) + value = *it++; + } + + std::vector<char>& vec; + mutable std::vector<char>::iterator it; +}; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit::ascii; + using boost::spirit::karma::repeat; + using boost::spirit::karma::inf; + using boost::spirit::karma::int_; + using boost::spirit::karma::hex; + using boost::spirit::karma::_1; + + { // lazy repeats + using boost::phoenix::val; + + std::string str8("aaaaaaaa"); + BOOST_TEST(test("aaaaaaaa", repeat[char_], str8)); // kleene synonym + BOOST_TEST(test("aaaaaaaa", repeat(val(8))[char_], str8)); + BOOST_TEST(test("aaa", repeat(val(3))[char_], str8)); + BOOST_TEST(!test("aaaaaaaa", repeat(val(9))[char_], str8)); + + std::string str3("aaa"); + BOOST_TEST(test("aaaaa", repeat(val(3), val(5))[char_], str8)); + BOOST_TEST(test("aaa", repeat(val(3), val(5))[char_], str3)); + BOOST_TEST(!test("aaa", repeat(val(4), val(5))[char_], str3)); + + BOOST_TEST(test("aaa", repeat(val(3), val(inf))[char_], str3)); + BOOST_TEST(test("aaaaaaaa", repeat(val(3), val(inf))[char_], str8)); + BOOST_TEST(!test("aaa", repeat(val(4), val(inf))[char_], str3)); + } + + + { + std::string str8("aaaaaaaa"); + BOOST_TEST(test("aaaaaaaa", repeat[char_], str8)); // kleene synonym + BOOST_TEST(test("aaaaaaaa", repeat(8)[char_], str8)); + BOOST_TEST(test("aaa", repeat(3)[char_], str8)); + BOOST_TEST(!test("aaaaaaaa", repeat(9)[char_], str8)); + + std::string str3("aaa"); + BOOST_TEST(test("aaaaa", repeat(3, 5)[char_], str8)); + BOOST_TEST(test("aaa", repeat(3, 5)[char_], str3)); + BOOST_TEST(!test("aaa", repeat(4, 5)[char_], str3)); + + BOOST_TEST(test("aaa", repeat(3, inf)[char_], str3)); + BOOST_TEST(test("aaaaaaaa", repeat(3, inf)[char_], str8)); + BOOST_TEST(!test("aaa", repeat(4, inf)[char_], str3)); + } + + { + std::string str8("aaaaaaaa"); + BOOST_TEST(test_delimited("a a a a a a a a ", repeat[char_], str8, space)); + BOOST_TEST(test_delimited("a a a a a a a a ", repeat(8)[char_], str8, space)); + BOOST_TEST(test_delimited("a a a ", repeat(3)[char_], str8, space)); + BOOST_TEST(!test_delimited("a a a a a a a a ", repeat(9)[char_], str8, space)); + + std::string str3("aaa"); + BOOST_TEST(test_delimited("a a a a a ", repeat(3, 5)[char_], str8, space)); + BOOST_TEST(test_delimited("a a a ", repeat(3, 5)[char_], str3, space)); + BOOST_TEST(!test_delimited("a a a ", repeat(4, 5)[char_], str3, space)); + + BOOST_TEST(test_delimited("a a a ", repeat(3, inf)[char_], str3, space)); + BOOST_TEST(test_delimited("a a a a a a a a ", repeat(3, inf)[char_], str8, space)); + BOOST_TEST(!test_delimited("a a a ", repeat(4, inf)[char_], str3, space)); + } + + { + // make sure user defined end condition is applied if no attribute + // is passed in + using namespace boost::assign; + + std::vector<char> v; + v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; + BOOST_TEST(test("[6162636465666768]", + '[' << repeat[hex[action(v)]] << ']')); + } + +// we support Phoenix attributes only starting with V2.2 +#if SPIRIT_VERSION >= 0x2020 + { + namespace ascii = boost::spirit::ascii; + namespace phoenix = boost::phoenix; + + char c = 'a'; + BOOST_TEST(test("bcd", repeat(3)[ascii::char_[_1 = ++phoenix::ref(c)]])); + + c = 'a'; + BOOST_TEST(test("bcd", repeat(3)[ascii::char_], ++phoenix::ref(c))); + } +#endif + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/repeat2.cpp b/src/boost/libs/spirit/test/karma/repeat2.cpp new file mode 100644 index 00000000..68464f33 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/repeat2.cpp @@ -0,0 +1,152 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/assign/std/vector.hpp> + +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_phoenix_attributes.hpp> +#include <boost/spirit/include/support_argument.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <string> +#include <iostream> +#include <vector> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit::ascii; + using boost::spirit::karma::repeat; + using boost::spirit::karma::inf; + using boost::spirit::karma::int_; + using boost::spirit::karma::hex; + using boost::spirit::karma::_1; + + { + std::string str("aBcdeFGH"); + BOOST_TEST(test("abcdefgh", lower[repeat(8)[char_]], str)); + BOOST_TEST(test_delimited("A B C D E F G H ", upper[repeat(8)[char_]], str, space)); + } + + { + std::string s1 = "aaaaa"; + BOOST_TEST(test("aaaaa", char_ << repeat(2)[char_ << char_], s1)); + s1 = "aaa"; + BOOST_TEST(test("aaa", char_ << repeat(1, 2)[char_ << char_], s1)); + s1 = "aa"; + BOOST_TEST(!test("", char_ << repeat(1)[char_ << char_], s1)); + } + + { // actions + namespace phx = boost::phoenix; + + std::vector<char> v; + v.push_back('a'); + v.push_back('a'); + v.push_back('a'); + v.push_back('a'); + BOOST_TEST(test("aaaa", repeat(4)[char_][_1 = phx::ref(v)])); + } + + { // more actions + namespace phx = boost::phoenix; + + std::vector<int> v; + v.push_back(123); + v.push_back(456); + v.push_back(789); + BOOST_TEST(test_delimited("123 456 789 ", repeat(3)[int_][_1 = phx::ref(v)], space)); + } + + // failing sub-generators + { + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + + using namespace boost::assign; + namespace karma = boost::spirit::karma; + + typedef std::pair<char, char> data; + std::vector<data> v2, v3; + v2 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'), + std::make_pair('e', 'e'), + std::make_pair('f', 'f'), + std::make_pair('g', 'g'); + v3 += std::make_pair('a', 'a'), + std::make_pair('b', 'b'), + std::make_pair('c', 'c'), + std::make_pair('d', 'd'); + + karma::rule<spirit_test::output_iterator<char>::type, data()> r; + + r = &char_('d') << char_; + BOOST_TEST(test("d", repeat[r], v2)); + BOOST_TEST(test("d", relaxed[repeat[r]], v2)); + BOOST_TEST(test("", strict[repeat[r]], v2)); + + r = !char_('d') << char_; + BOOST_TEST(test("abcefg", repeat(6)[r], v2)); + BOOST_TEST(!test("", repeat(5)[r], v2)); + BOOST_TEST(test("abcefg", relaxed[repeat(6)[r]], v2)); + BOOST_TEST(!test("", relaxed[repeat(5)[r]], v2)); + BOOST_TEST(!test("", strict[repeat(6)[r]], v2)); + BOOST_TEST(!test("", strict[repeat(5)[r]], v2)); + + r = !char_('c') << char_; + BOOST_TEST(test("abd", repeat(3)[r], v2)); + BOOST_TEST(test("abd", relaxed[repeat(3)[r]], v2)); + BOOST_TEST(!test("", strict[repeat(3)[r]], v2)); + + r = !char_('a') << char_; + BOOST_TEST(test("bcdef", repeat(3, 5)[r], v2)); + BOOST_TEST(test("bcd", repeat(3, 5)[r], v3)); + BOOST_TEST(!test("", repeat(4, 5)[r], v3)); + BOOST_TEST(test("bcdef", relaxed[repeat(3, 5)[r]], v2)); + BOOST_TEST(test("bcd", relaxed[repeat(3, 5)[r]], v3)); + BOOST_TEST(!test("", relaxed[repeat(4, 5)[r]], v3)); + BOOST_TEST(!test("", strict[repeat(3, 5)[r]], v2)); + BOOST_TEST(!test("", strict[repeat(3, 5)[r]], v3)); + BOOST_TEST(!test("", strict[repeat(4, 5)[r]], v3)); + + BOOST_TEST(test("bcd", repeat(3, inf)[r], v3)); + BOOST_TEST(test("bcdefg", repeat(3, inf)[r], v2)); + BOOST_TEST(!test("", repeat(4, inf)[r], v3)); + + r = !char_('g') << char_; + BOOST_TEST(test("abcde", repeat(3, 5)[r], v2)); + BOOST_TEST(test("abcd", repeat(3, 5)[r], v3)); + BOOST_TEST(!test("", repeat(4, 5)[r], v3)); + BOOST_TEST(test("abcde", relaxed[repeat(3, 5)[r]], v2)); + BOOST_TEST(test("abcd", relaxed[repeat(3, 5)[r]], v3)); + BOOST_TEST(!test("", relaxed[repeat(4, 5)[r]], v3)); + BOOST_TEST(test("abcde", strict[repeat(3, 5)[r]], v2)); + BOOST_TEST(test("abcd", strict[repeat(3, 5)[r]], v3)); + BOOST_TEST(!test("", strict[repeat(5)[r]], v3)); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/right_alignment.cpp b/src/boost/libs/spirit/test/karma/right_alignment.cpp new file mode 100644 index 00000000..ba505b66 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/right_alignment.cpp @@ -0,0 +1,74 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_upper_lower_case.hpp> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test(" x", right_align[char_('x')])); + BOOST_TEST(test(" x", right_align[char_], 'x')); + BOOST_TEST(test(" x", right_align['x'])); + + BOOST_TEST(test(" x", right_align(10)[char_('x')])); + BOOST_TEST(test(" x", right_align(10)[char_], 'x')); + BOOST_TEST(test(" x", right_align(10)['x'])); + + BOOST_TEST(test("*********x", right_align(10, char_('*'))[char_('x')])); + BOOST_TEST(test("*********x", right_align(10, '*')[char_], 'x')); + BOOST_TEST(test("*********x", right_align(10, '*')['x'])); + BOOST_TEST(test("aaaaaaaaax", lower[right_align(10, 'A')['X']])); + BOOST_TEST(test("AAAAAAAAAX", upper[right_align(10, 'a')['x']])); + + BOOST_TEST(test("*********x", right_align(char_('*'))[char_('x')])); + BOOST_TEST(test("*********x", right_align(char_('*'))[char_], 'x')); + BOOST_TEST(test("*********x", right_align(char_('*'))['x'])); + + BOOST_TEST(test(" abc", right_align[lit("abc")])); + BOOST_TEST(test(" abc", right_align[string], "abc")); + + BOOST_TEST(test(" abc", right_align(10)[lit("abc")])); + BOOST_TEST(test(" abc", right_align(10)[string], "abc")); + BOOST_TEST(test(" abc", right_align(10)["abc"])); + + BOOST_TEST(test("*******abc", right_align(10, char_('*'))[lit("abc")])); + BOOST_TEST(test("*******abc", right_align(10, '*')[string], "abc")); + BOOST_TEST(test("*******abc", right_align(10, '*')["abc"])); + + BOOST_TEST(test("*******abc", right_align(char_('*'))[lit("abc")])); + BOOST_TEST(test("*******abc", right_align(char_('*'))[string], "abc")); + BOOST_TEST(test("*******abc", right_align(char_('*'))["abc"])); + + BOOST_TEST(test(" 100", right_align[int_(100)])); + BOOST_TEST(test(" 100", right_align[int_], 100)); + + BOOST_TEST(test(" 100", right_align(10)[int_(100)])); + BOOST_TEST(test(" 100", right_align(10)[int_], 100)); + + BOOST_TEST(test("*******100", right_align(10, char_('*'))[int_(100)])); + BOOST_TEST(test("*******100", right_align(10, '*')[int_], 100)); + + BOOST_TEST(test("*******100", right_align(char_('*'))[int_(100)])); + BOOST_TEST(test("*******100", right_align(char_('*'))[int_], 100)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/rule_fail.cpp b/src/boost/libs/spirit/test/karma/rule_fail.cpp new file mode 100644 index 00000000..36143416 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/rule_fail.cpp @@ -0,0 +1,36 @@ +/*============================================================================= + 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) +=============================================================================*/ + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/function_output_iterator.hpp> + +#include "test.hpp" + +using namespace boost::spirit; +using namespace boost::spirit::ascii; + +// this test must fail compiling as the rule is used with an incompatible +// delimiter type +int main() +{ + typedef spirit_test::output_iterator<char>::type outiter_type; + + std::string generated; + + karma::rule<outiter_type, karma::rule<outiter_type> > def; + def = int_(1) << ',' << int_(0); + + std::back_insert_iterator<std::string> outit(generated); + generate_delimited(outit, def, char_('%') << '\n'); + + return 0; +} diff --git a/src/boost/libs/spirit/test/karma/sequence1.cpp b/src/boost/libs/spirit/test/karma/sequence1.cpp new file mode 100644 index 00000000..e35efe80 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/sequence1.cpp @@ -0,0 +1,140 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/support_unused.hpp> +#include <boost/fusion/include/vector.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + namespace fusion = boost::fusion; + + { + BOOST_TEST(test("xi", char_('x') << char_('i'))); + BOOST_TEST(!test("xi", char_('x') << char_('o'))); + } + + { + BOOST_TEST(test_delimited("x i ", char_('x') << 'i', char(' '))); + BOOST_TEST(!test_delimited("x i ", + char_('x') << char_('o'), char(' '))); + } + + { + BOOST_TEST(test_delimited("Hello , World ", + lit("Hello") << ',' << "World", char(' '))); + } + + { + // a single element + char attr = 'a'; + BOOST_TEST((test("ab", char_ << 'b', attr))); + } + + { + // a single element fusion sequence + fusion::vector<char> attr('a'); + BOOST_TEST((test("ab", char_ << 'b', attr))); + } + + { + fusion::vector<char, char, std::string> p ('a', 'b', "cdefg"); + BOOST_TEST(test("abcdefg", char_ << char_ << string, p)); + BOOST_TEST(test_delimited("a b cdefg ", + char_ << char_ << string, p, char(' '))); + } + + { + fusion::vector<char, int, char> p ('a', 12, 'c'); + BOOST_TEST(test("a12c", char_ << int_ << char_, p)); + BOOST_TEST(test_delimited("a 12 c ", + char_ << int_ << char_, p, char(' '))); + } + + { + // element sequence can be shorter and longer than the attribute + // sequence + using boost::spirit::karma::strict; + using boost::spirit::karma::relaxed; + + fusion::vector<char, int, char> p ('a', 12, 'c'); + BOOST_TEST(test("a12", char_ << int_, p)); + BOOST_TEST(test_delimited("a 12 ", char_ << int_, p, char(' '))); + + BOOST_TEST(test("a12", relaxed[char_ << int_], p)); + BOOST_TEST(test_delimited("a 12 ", relaxed[char_ << int_], p, char(' '))); + + BOOST_TEST(!test("", strict[char_ << int_], p)); + BOOST_TEST(!test_delimited("", strict[char_ << int_], p, char(' '))); + + fusion::vector<char, int> p1 ('a', 12); + BOOST_TEST(test("a12c", char_ << int_ << char_('c'), p1)); + BOOST_TEST(test_delimited("a 12 c ", char_ << int_ << char_('c'), + p1, char(' '))); + + BOOST_TEST(test("a12c", relaxed[char_ << int_ << char_('c')], p1)); + BOOST_TEST(test_delimited("a 12 c ", + relaxed[char_ << int_ << char_('c')], p1, char(' '))); + + BOOST_TEST(!test("", strict[char_ << int_ << char_('c')], p1)); + BOOST_TEST(!test_delimited("", strict[char_ << int_ << char_('c')], + p1, char(' '))); + + BOOST_TEST(test("a12", strict[char_ << int_], p1)); + BOOST_TEST(test_delimited("a 12 ", strict[char_ << int_], p1, char(' '))); + + std::string value("foo ' bar"); + BOOST_TEST(test("\"foo ' bar\"", '"' << strict[*(~char_('*'))] << '"', value)); + BOOST_TEST(test("\"foo ' bar\"", strict['"' << *(~char_('*')) << '"'], value)); + } + + { + // if all elements of a sequence have unused parameters, the whole + // sequence has an unused parameter as well + fusion::vector<char, char> p ('a', 'e'); + BOOST_TEST(test("abcde", + char_ << (lit('b') << 'c' << 'd') << char_, p)); + BOOST_TEST(test_delimited("a b c d e ", + char_ << (lit('b') << 'c' << 'd') << char_, p, char(' '))); + } + + { + // literal generators do not need an attribute + fusion::vector<char, char> p('a', 'c'); + BOOST_TEST(test("abc", char_ << 'b' << char_, p)); + BOOST_TEST(test_delimited("a b c ", + char_ << 'b' << char_, p, char(' '))); + } + + { + // literal generators do not need an attribute, not even at the end + fusion::vector<char, char> p('a', 'c'); + BOOST_TEST(test("acb", char_ << char_ << 'b', p)); + BOOST_TEST(test_delimited("a c b ", + char_ << char_ << 'b', p, char(' '))); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/sequence2.cpp b/src/boost/libs/spirit/test/karma/sequence2.cpp new file mode 100644 index 00000000..926d2487 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/sequence2.cpp @@ -0,0 +1,183 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_action.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/support_unused.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/spirit/include/phoenix_function.hpp> +#include <boost/fusion/include/vector.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +/////////////////////////////////////////////////////////////////////////////// +// lazy version of fusion::size +struct seqsize_impl +{ + template <typename Sequence> + struct result + : boost::fusion::result_of::size<Sequence> + {}; + + template <typename This, typename Sequence> + struct result<This(Sequence)> + : result<typename boost::proto::detail::uncvref<Sequence>::type> + {}; + + template <typename Sequence> + typename result<Sequence>::type + operator()(Sequence const& seq) const + { + return boost::fusion::size(seq); + } +}; + +boost::phoenix::function<seqsize_impl> const seqsize = seqsize_impl(); + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + using namespace boost::spirit; + using namespace boost::spirit::ascii; + namespace fusion = boost::fusion; + + { + std::list<int> v; + v.push_back(1); + v.push_back(2); + v.push_back(3); + BOOST_TEST(test("123", int_ << int_ << int_, v)); + BOOST_TEST(test_delimited("1 2 3 ", int_ << int_ << int_, v, ' ')); + BOOST_TEST(test("1,2,3", int_ << ',' << int_ << ',' << int_, v)); + BOOST_TEST(test_delimited("1 , 2 , 3 ", int_ << ',' << int_ << ',' << int_, v, ' ')); + } + + { + BOOST_TEST(test("aa", lower[char_('A') << 'a'])); + BOOST_TEST(test_delimited("BEGIN END ", + upper[lit("begin") << "end"], char(' '))); + BOOST_TEST(!test_delimited("BEGIN END ", + upper[lit("begin") << "nend"], char(' '))); + + BOOST_TEST(test("Aa ", left_align[char_('A') << 'a'])); + BOOST_TEST(test(" Aa ", center[char_('A') << 'a'])); + BOOST_TEST(test(" Aa", right_align[char_('A') << 'a'])); + } + + { + // make sure single element tuples get passed through if the rhs + // has a single element tuple as its attribute + typedef spirit_test::output_iterator<char>::type iterator_type; + fusion::vector<double, int> fv(2.0, 1); + karma::rule<iterator_type, fusion::vector<double, int>()> r; + r %= double_ << ',' << int_; + BOOST_TEST(test("test:2.0,1", "test:" << r, fv)); + } + + // action tests + { + using namespace boost::phoenix; + + BOOST_TEST(test("abcdefg", + (char_ << char_ << string)[_1 = 'a', _2 = 'b', _3 = "cdefg"])); + BOOST_TEST(test_delimited("a b cdefg ", + (char_ << char_ << string)[_1 = 'a', _2 = 'b', _3 = "cdefg"], + char(' '))); + + BOOST_TEST(test_delimited("a 12 c ", + (char_ << lit(12) << char_)[_1 = 'a', _2 = 'c'], char(' '))); + + char c = 'c'; + BOOST_TEST(test("abc", + (char_[_1 = 'a'] << 'b' << char_)[_1 = 'x', _2 = ref(c)])); + BOOST_TEST(test_delimited("a b c ", + (char_[_1 = 'a'] << 'b' << char_)[_2 = ref(c)], char(' '))); + + BOOST_TEST(test("aa", lower[char_ << 'A'][_1 = 'A'])); + BOOST_TEST(test("AA", upper[char_ << 'a'][_1 = 'a'])); + + BOOST_TEST(test("Aa ", left_align[char_ << 'a'][_1 = 'A'])); + BOOST_TEST(test(" Aa ", center[char_ << 'a'][_1 = 'A'])); + BOOST_TEST(test(" Aa", right_align[char_ << 'a'][_1 = 'A'])); + } + + // test special case where sequence has a one element vector attribute + // sequence and this element is a rule (attribute has to be passed through + // without change) + { + typedef spirit_test::output_iterator<char>::type outiter_type; + namespace karma = boost::spirit::karma; + + karma::rule<outiter_type, std::vector<int>()> r = -(int_ % ','); + std::vector<int> v; + BOOST_TEST(test(">", '>' << r, v)); + + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + BOOST_TEST(test(">1,2,3,4", '>' << r, v)); + } + + { + namespace karma = boost::spirit::karma; + typedef spirit_test::output_iterator<char>::type outiter_type; + + karma::rule<outiter_type, std::string()> e = karma::string; + karma::rule<outiter_type, std::vector<std::string>()> l = e << *(',' << e); + + std::vector<std::string> v; + v.push_back("abc1"); + v.push_back("abc2"); + v.push_back("abc3"); + BOOST_TEST(test("abc1,abc2,abc3", l, v)); + } + + { + namespace karma = boost::spirit::karma; + namespace phoenix = boost::phoenix; + + typedef spirit_test::output_iterator<char>::type outiter_type; + typedef fusion::vector<char, char, char> vector_type; + + vector_type p ('a', 'b', 'c'); + BOOST_TEST(test("ab", char_ << char_, p)); + + karma::rule<outiter_type, vector_type()> r; + r %= char_ << char_ << &karma::eps[seqsize(_val) == 3]; + BOOST_TEST(!test("", r, p)); + + r %= char_ << char_ << char_ << &karma::eps[seqsize(_val) == 3]; + BOOST_TEST(test("abc", r, p)); + } + + { + std::list<int> v; + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + + BOOST_TEST(test("1234", repeat(2)[int_] << *int_, v)); + BOOST_TEST(test_delimited("1 2 3 4 ", repeat(2)[int_] << *int_, v, char(' '))); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/spirit/test/karma/stream.cpp b/src/boost/libs/spirit/test/karma/stream.cpp new file mode 100644 index 00000000..36ab6e6c --- /dev/null +++ b/src/boost/libs/spirit/test/karma/stream.cpp @@ -0,0 +1,134 @@ +// 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) + +#include <cwchar> +#include <streambuf> +#include <iostream> + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/cstdint.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_stream.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +// a simple complex number representation z = a + bi +struct complex +{ + complex (double a, double b) + : a(a), b(b) + {} + + double a; + double b; + + template <typename Char> + friend std::basic_ostream<Char>& + operator<< (std::basic_ostream<Char>& os, complex z) + { + os << "{" << z.a << "," << z.b << "}"; + return os; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + BOOST_TEST(test("x", stream, 'x')); + BOOST_TEST(test("xyz", stream, "xyz")); + BOOST_TEST(test("xyz", stream, std::string("xyz"))); + BOOST_TEST(test("1", stream, 1)); + BOOST_TEST(test("1.1", stream, 1.1)); + BOOST_TEST(test("{1.2,2.4}", stream, complex(1.2, 2.4))); + } + + { + BOOST_TEST(test("x", stream('x'))); + BOOST_TEST(test("xyz", stream("xyz"))); + BOOST_TEST(test("xyz", stream(std::string("xyz")))); + BOOST_TEST(test("1", stream(1))); + BOOST_TEST(test("1.1", stream(1.1))); + BOOST_TEST(test("{1.2,2.4}", stream(complex(1.2, 2.4)))); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test("x", lower[stream], 'X')); + BOOST_TEST(test("xyz", lower[stream], "XYZ")); + BOOST_TEST(test("xyz", lower[stream], std::string("XYZ"))); + BOOST_TEST(test("X", upper[stream], 'x')); + BOOST_TEST(test("XYZ", upper[stream], "xyz")); + BOOST_TEST(test("XYZ", upper[stream], std::string("xyz"))); + } + + { + BOOST_TEST(test_delimited("x ", stream, 'x', ' ')); + BOOST_TEST(test_delimited("xyz ", stream, "xyz", ' ')); + BOOST_TEST(test_delimited("xyz ", stream, std::string("xyz"), ' ')); + BOOST_TEST(test_delimited("1 ", stream, 1, ' ')); + BOOST_TEST(test_delimited("1.1 ", stream, 1.1, ' ')); + BOOST_TEST(test_delimited("{1.2,2.4} ", stream, complex(1.2, 2.4), ' ')); + } + + { + typedef karma::stream_generator<utf8_char> utf8_stream_type; + utf8_stream_type const utf8_stream = utf8_stream_type(); + + BOOST_TEST(test_delimited("x ", utf8_stream, 'x', ' ')); + BOOST_TEST(test_delimited("xyz ", utf8_stream, "xyz", ' ')); + BOOST_TEST(test_delimited("xyz ", utf8_stream, std::string("xyz"), ' ')); + BOOST_TEST(test_delimited("1 ", utf8_stream, 1, ' ')); + BOOST_TEST(test_delimited("1.1 ", utf8_stream, 1.1, ' ')); + BOOST_TEST(test_delimited("{1.2,2.4} ", utf8_stream, complex(1.2, 2.4), ' ')); + + BOOST_TEST(test("x", utf8_stream('x'))); + BOOST_TEST(test("xyz", utf8_stream("xyz"))); + BOOST_TEST(test("xyz", utf8_stream(std::string("xyz")))); + BOOST_TEST(test("1", utf8_stream(1))); + BOOST_TEST(test("1.1", utf8_stream(1.1))); + BOOST_TEST(test("{1.2,2.4}", utf8_stream(complex(1.2, 2.4)))); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited("x ", lower[stream], 'X', ' ')); + BOOST_TEST(test_delimited("xyz ", lower[stream], "XYZ", ' ')); + BOOST_TEST(test_delimited("xyz ", lower[stream], std::string("XYZ"), ' ')); + BOOST_TEST(test_delimited("X ", upper[stream], 'x', ' ')); + BOOST_TEST(test_delimited("XYZ ", upper[stream], "xyz", ' ')); + BOOST_TEST(test_delimited("XYZ ", upper[stream], std::string("xyz"), ' ')); + } + + { // lazy streams + namespace phx = boost::phoenix; + + std::basic_string<char> s("abc"); + BOOST_TEST((test("abc", stream(phx::val(s))))); + BOOST_TEST((test("abc", stream(phx::ref(s))))); + } + + { + boost::optional<char> c; + BOOST_TEST(!test("", stream, c)); + c = 'x'; + BOOST_TEST(test("x", stream, c)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/symbols1.cpp b/src/boost/libs/spirit/test/karma/symbols1.cpp new file mode 100644 index 00000000..0fcea278 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/symbols1.cpp @@ -0,0 +1,106 @@ +// 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) + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> + +#include <boost/core/lightweight_test.hpp> +#include <boost/core/lightweight_test_trait.hpp> + +#include "test.hpp" + +namespace fusion = boost::fusion; + +template <typename T> +inline std::vector<T> +make_vector(T const& t1, T const& t2) +{ + std::vector<T> v; + v.push_back(t1); + v.push_back(t2); + return v; +} + +int main() +{ + using spirit_test::test; + using boost::spirit::karma::symbols; + + { // basics + symbols<char, std::string> sym; + + sym.add + ('j', "Joel") + ('h', "Hartmut") + ('t', "Tom") + ('k', "Kim") + ; + + BOOST_TEST_TRAIT_TRUE(( + boost::spirit::traits::is_generator< + symbols<char, std::string> >)); + + BOOST_TEST((test("Joel", sym, 'j'))); + BOOST_TEST((test("Hartmut", sym, 'h'))); + BOOST_TEST((test("Tom", sym, 't'))); + BOOST_TEST((test("Kim", sym, 'k'))); + BOOST_TEST((!test("", sym, 'x'))); + + // test copy + symbols<char, std::string> sym2; + sym2 = sym; + BOOST_TEST((test("Joel", sym2, 'j'))); + BOOST_TEST((test("Hartmut", sym2, 'h'))); + BOOST_TEST((test("Tom", sym2, 't'))); + BOOST_TEST((test("Kim", sym2, 'k'))); + BOOST_TEST((!test("", sym2, 'x'))); + + // make sure it plays well with other generators + BOOST_TEST((test("Joelyo", sym << "yo", 'j'))); + + sym.remove + ('j') + ('h') + ; + + BOOST_TEST((!test("", sym, 'j'))); + BOOST_TEST((!test("", sym, 'h'))); + } + + { // lower/upper handling + using namespace boost::spirit::ascii; + using boost::spirit::karma::lower; + using boost::spirit::karma::upper; + + symbols<char, std::string> sym; + sym.add + ('j', "Joel") + ('h', "Hartmut") + ('t', "Tom") + ('k', "Kim") + ; + + BOOST_TEST((test("joel", lower[sym], 'j'))); + BOOST_TEST((test("hartmut", lower[sym], 'h'))); + BOOST_TEST((test("tom", lower[sym], 't'))); + BOOST_TEST((test("kim", lower[sym], 'k'))); + + BOOST_TEST((test("JOEL", upper[sym], 'j'))); + BOOST_TEST((test("HARTMUT", upper[sym], 'h'))); + BOOST_TEST((test("TOM", upper[sym], 't'))); + BOOST_TEST((test("KIM", upper[sym], 'k'))); + + // make sure it plays well with other generators + BOOST_TEST((test("joelyo", lower[sym] << "yo", 'j'))); + BOOST_TEST((test("JOELyo", upper[sym] << "yo", 'j'))); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/symbols2.cpp b/src/boost/libs/spirit/test/karma/symbols2.cpp new file mode 100644 index 00000000..e2fdcd53 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/symbols2.cpp @@ -0,0 +1,146 @@ +// 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) + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> + +#include <boost/core/lightweight_test.hpp> +#include <boost/core/lightweight_test_trait.hpp> + +#include "test.hpp" + +namespace fusion = boost::fusion; + +template <typename T> +inline std::vector<T> +make_vector(T const& t1, T const& t2) +{ + std::vector<T> v; + v.push_back(t1); + v.push_back(t2); + return v; +} + +int main() +{ + using spirit_test::test; + using boost::spirit::karma::symbols; + + { // advanced + using boost::spirit::karma::rule; + using boost::spirit::karma::lit; + + typedef spirit_test::output_iterator<char>::type output_iterator_type; + + symbols<char, rule<output_iterator_type> > sym; + + rule<output_iterator_type> r1 = lit("Joel"); + rule<output_iterator_type> r2 = lit("Hartmut"); + rule<output_iterator_type> r3 = lit("Tom"); + rule<output_iterator_type> r4 = lit("Kim"); + + sym.add + ('j', r1.alias()) + ('h', r2.alias()) + ('t', r3.alias()) + ('k', r4.alias()) + ; + + BOOST_TEST_TRAIT_TRUE(( + boost::spirit::traits::is_generator< + symbols<char, rule<output_iterator_type> > >)); + + BOOST_TEST((test("Joel", sym, 'j'))); + BOOST_TEST((test("Hartmut", sym, 'h'))); + BOOST_TEST((test("Tom", sym, 't'))); + BOOST_TEST((test("Kim", sym, 'k'))); + BOOST_TEST((!test("", sym, 'x'))); + + // test copy + symbols<char, rule<output_iterator_type> > sym2; + sym2 = sym; + BOOST_TEST((test("Joel", sym2, 'j'))); + BOOST_TEST((test("Hartmut", sym2, 'h'))); + BOOST_TEST((test("Tom", sym2, 't'))); + BOOST_TEST((test("Kim", sym2, 'k'))); + BOOST_TEST((!test("", sym2, 'x'))); + + // make sure it plays well with other generators + BOOST_TEST((test("Joelyo", sym << "yo", 'j'))); + + sym.remove + ('j') + ('h') + ; + + BOOST_TEST((!test("", sym, 'j'))); + BOOST_TEST((!test("", sym, 'h'))); + } + + { // more advanced + using boost::spirit::karma::rule; + using boost::spirit::karma::lit; + using boost::spirit::karma::string; + + typedef spirit_test::output_iterator<char>::type output_iterator_type; + + symbols<char, rule<output_iterator_type, std::string()> > sym; + rule<output_iterator_type, std::string()> r1 = string; + + sym.add + ('j', r1.alias()) + ('h', r1.alias()) + ('t', r1.alias()) + ('k', r1.alias()) + ; + + BOOST_TEST_TRAIT_TRUE(( + boost::spirit::traits::is_generator< + symbols<char, std::string> >)); + + BOOST_TEST((test("Joel", sym, fusion::make_vector('j', "Joel")))); + BOOST_TEST((test("Hartmut", sym, fusion::make_vector('h', "Hartmut")))); + BOOST_TEST((test("Tom", sym, fusion::make_vector('t', "Tom")))); + BOOST_TEST((test("Kim", sym, fusion::make_vector('k', "Kim")))); + BOOST_TEST((!test("", sym, 'x'))); + + // test copy + symbols<char, rule<output_iterator_type, std::string()> > sym2; + sym2 = sym; + BOOST_TEST((test("Joel", sym2, fusion::make_vector('j', "Joel")))); + BOOST_TEST((test("Hartmut", sym2, fusion::make_vector('h', "Hartmut")))); + BOOST_TEST((test("Tom", sym2, fusion::make_vector('t', "Tom")))); + BOOST_TEST((test("Kim", sym2, fusion::make_vector('k', "Kim")))); + BOOST_TEST((!test("", sym2, 'x'))); + + // make sure it plays well with other generators + BOOST_TEST((test("Joelyo", sym << "yo", fusion::make_vector('j', "Joel")))); + + sym.remove + ('j') + ('h') + ; + + BOOST_TEST((!test("", sym, 'j'))); + BOOST_TEST((!test("", sym, 'h'))); + } + + { // test for proto problem with rvalue references (10-11-2011) + symbols<char, std::string> sym; + + sym += std::make_pair('j', "Joel"); + sym += std::make_pair('h', "Hartmut"); + + BOOST_TEST((test("Joel", sym, 'j'))); + BOOST_TEST((test("Hartmut", sym, 'h'))); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/symbols3.cpp b/src/boost/libs/spirit/test/karma/symbols3.cpp new file mode 100644 index 00000000..fd07dbf5 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/symbols3.cpp @@ -0,0 +1,139 @@ +// 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) + +#include <boost/spirit/include/karma_auxiliary.hpp> +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_operator.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_nonterminal.hpp> + +#include <boost/core/lightweight_test.hpp> +#include <boost/core/lightweight_test_trait.hpp> + +#include "test.hpp" + +namespace fusion = boost::fusion; + +template <typename T> +inline std::vector<T> +make_vector(T const& t1, T const& t2) +{ + std::vector<T> v; + v.push_back(t1); + v.push_back(t2); + return v; +} + +int main() +{ + using spirit_test::test; + using boost::spirit::karma::symbols; + + { // more advanced + using boost::spirit::karma::rule; + using boost::spirit::karma::lit; + using boost::spirit::karma::char_; + + typedef spirit_test::output_iterator<char>::type output_iterator_type; + + symbols<char, rule<output_iterator_type, char()> > sym; + rule<output_iterator_type, char()> r1 = char_; + + sym.add + ('j', r1.alias()) + ('h', r1.alias()) + ('t', r1.alias()) + ('k', r1.alias()) + ; + + BOOST_TEST_TRAIT_TRUE(( + boost::spirit::traits::is_generator< + symbols<char, rule<output_iterator_type, char()> > >)); + + BOOST_TEST((test("J", sym, make_vector('j', 'J')))); + BOOST_TEST((test("H", sym, make_vector('h', 'H')))); + BOOST_TEST((test("T", sym, make_vector('t', 'T')))); + BOOST_TEST((test("K", sym, make_vector('k', 'K')))); + BOOST_TEST((!test("", sym, 'x'))); + + // test copy + symbols<char, rule<output_iterator_type, char()> > sym2; + sym2 = sym; + BOOST_TEST((test("J", sym2, make_vector('j', 'J')))); + BOOST_TEST((test("H", sym2, make_vector('h', 'H')))); + BOOST_TEST((test("T", sym2, make_vector('t', 'T')))); + BOOST_TEST((test("K", sym2, make_vector('k', 'K')))); + BOOST_TEST((!test("", sym2, 'x'))); + + // make sure it plays well with other generators + BOOST_TEST((test("Jyo", sym << "yo", make_vector('j', 'J')))); + + sym.remove + ('j') + ('h') + ; + + BOOST_TEST((!test("", sym, 'j'))); + BOOST_TEST((!test("", sym, 'h'))); + } + + { // basics + symbols<std::string> sym; + + sym.add + ("Joel") + ("Hartmut") + ("Tom") + ("Kim") + ; + + BOOST_TEST_TRAIT_TRUE(( + boost::spirit::traits::is_generator< + symbols<char, std::string> >)); + + BOOST_TEST((test("Joel", sym, "Joel"))); + BOOST_TEST((test("Hartmut", sym, "Hartmut"))); + BOOST_TEST((test("Tom", sym, "Tom"))); + BOOST_TEST((test("Kim", sym, "Kim"))); + BOOST_TEST((!test("", sym, "X"))); + + // test copy + symbols<std::string> sym2; + sym2 = sym; + BOOST_TEST((test("Joel", sym2, "Joel"))); + BOOST_TEST((test("Hartmut", sym2, "Hartmut"))); + BOOST_TEST((test("Tom", sym2, "Tom"))); + BOOST_TEST((test("Kim", sym2, "Kim"))); + BOOST_TEST((!test("", sym2, "X"))); + + // make sure it plays well with other generators + BOOST_TEST((test("Joelyo", sym << "yo", "Joel"))); + + sym.remove + ("Joel") + ("Hartmut") + ; + + BOOST_TEST((!test("", sym, "Joel"))); + BOOST_TEST((!test("", sym, "Hartmut"))); + } + + { // name + symbols <std::string> sym("test1"), sym2; + BOOST_TEST(sym.name() == "test1"); + + sym.name("test"); + BOOST_TEST(sym.name() == "test"); + sym2 = sym; + BOOST_TEST(sym2.name() == "test"); + + symbols <std::string> sym3(sym); + BOOST_TEST(sym3.name() == "test"); + } + + return boost::report_errors(); +} 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 <cstring> +#include <string> +#include <iterator> +#include <iostream> +#include <iomanip> +#include <typeinfo> + +#include <boost/foreach.hpp> +#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 T> + void print_binary_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.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 <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 &attrib) + { + 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, attrib); + + 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 &attrib) + { + 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, attrib); + + 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 &attrib, 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, attrib); + + 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 &attrib, 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, attrib); + + 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); + + 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 <typename Generator, typename Attribute> + 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<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, 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 <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); + + 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 <typename Generator, typename Attribute, typename Delimiter> + 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<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, 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 diff --git a/src/boost/libs/spirit/test/karma/test_attr.hpp b/src/boost/libs/spirit/test/karma/test_attr.hpp new file mode 100644 index 00000000..ad0f0441 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/test_attr.hpp @@ -0,0 +1,137 @@ +// 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_PP_IS_ITERATING) + +#if !defined(BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM) +#define BOOST_SPIRIT_KARMA_TEST_ATTR_APR_23_2009_0605PM + +#include <cstring> +#include <string> +#include <iterator> +#include <iostream> +#include <typeinfo> + +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_what.hpp> + +#include <boost/preprocessor/iterate.hpp> +#include <boost/preprocessor/repetition/enum.hpp> +#include <boost/preprocessor/repetition/enum_params.hpp> +#include <boost/preprocessor/repetition/enum_binary_params.hpp> + +namespace spirit_test +{ + /////////////////////////////////////////////////////////////////////////// + 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; + } +} + +#define BOOST_PP_FILENAME_1 "test_attr.hpp" +#define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT) +#include BOOST_PP_ITERATE() + +#endif + +/////////////////////////////////////////////////////////////////////////////// +// +// Preprocessor vertical repetition code +// +/////////////////////////////////////////////////////////////////////////////// +#else // defined(BOOST_PP_IS_ITERATING) + +#define N BOOST_PP_ITERATION() + +namespace spirit_test +{ + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test(Char const *expected, Generator const& g + , BOOST_PP_ENUM_BINARY_PARAMS(N, A, 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, BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator, typename Delimiter + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test_delimited(Char const *expected, Generator const& g + , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, 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_delimited(outit, g, d + , BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test_delimited", result, generated, expected); + return result && generated == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator, typename Delimiter + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test_predelimited(Char const *expected, Generator const& g + , Delimiter const& d + , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit + , BOOST_PP_ENUM_BINARY_PARAMS(N, A, 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_delimited(outit, g, d + , pre_delimit, BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test_predelimited", result, generated, expected); + return result && generated == expected; + } + +} // namespace spirit_test + +#undef N + +#endif diff --git a/src/boost/libs/spirit/test/karma/test_manip_attr.hpp b/src/boost/libs/spirit/test/karma/test_manip_attr.hpp new file mode 100644 index 00000000..4b8e7be0 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/test_manip_attr.hpp @@ -0,0 +1,109 @@ +// 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_PP_IS_ITERATING) + +#if !defined(BOOST_SPIRIT_KARMA_TEST_MANIP_ATTR_APR_24_2009_0834AM) +#define BOOST_SPIRIT_KARMA_TEST_MANIP_ATTR_APR_24_2009_0834AM + +#include <cstring> +#include <string> +#include <iterator> +#include <iostream> +#include <typeinfo> + +#include <boost/spirit/include/karma_stream.hpp> + +#include <boost/preprocessor/iterate.hpp> +#include <boost/preprocessor/repetition/enum.hpp> +#include <boost/preprocessor/repetition/enum_params.hpp> +#include <boost/preprocessor/repetition/enum_binary_params.hpp> + +namespace spirit_test +{ + /////////////////////////////////////////////////////////////////////////// + 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; + } +} + +#define BOOST_PP_FILENAME_1 "test_manip_attr.hpp" +#define BOOST_PP_ITERATION_LIMITS (1, SPIRIT_ARGUMENTS_LIMIT) +#include BOOST_PP_ITERATE() + +#endif + +/////////////////////////////////////////////////////////////////////////////// +// +// Preprocessor vertical repetition code +// +/////////////////////////////////////////////////////////////////////////////// +#else // defined(BOOST_PP_IS_ITERATING) + +#define N BOOST_PP_ITERATION() + +namespace spirit_test +{ + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test(Char const *expected, Generator const& g + , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) + { + namespace karma = boost::spirit::karma; + + std::ostringstream ostrm; + ostrm << karma::format(g, BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test", ostrm.good(), ostrm.str(), expected); + return ostrm.good() && ostrm.str() == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator, typename Delimiter + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test_delimited(Char const *expected, Generator const& g + , Delimiter const& d, BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) + { + namespace karma = boost::spirit::karma; + + std::ostringstream ostrm; + ostrm << karma::format_delimited(g, d, BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test_delimited", ostrm.good(), ostrm.str(), expected); + return ostrm.good() && ostrm.str() == expected; + } + + /////////////////////////////////////////////////////////////////////////// + template <typename Char, typename Generator, typename Delimiter + , BOOST_PP_ENUM_PARAMS(N, typename A)> + inline bool test_predelimited(Char const *expected, Generator const& g + , Delimiter const& d + , BOOST_SCOPED_ENUM(boost::spirit::karma::delimit_flag) pre_delimit + , BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& attr)) + { + namespace karma = boost::spirit::karma; + + std::ostringstream ostrm; + ostrm << karma::format_delimited(g, d, pre_delimit + , BOOST_PP_ENUM_PARAMS(N, attr)); + + print_if_failed("test_predelimited", ostrm.good(), ostrm.str(), expected); + return ostrm.good() && ostrm.str() == expected; + } + +} // namespace spirit_test + +#undef N + +#endif diff --git a/src/boost/libs/spirit/test/karma/tricky_alignment.cpp b/src/boost/libs/spirit/test/karma/tricky_alignment.cpp new file mode 100644 index 00000000..f851f9d6 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/tricky_alignment.cpp @@ -0,0 +1,130 @@ +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_numeric.hpp> +#include <boost/spirit/include/karma_generate.hpp> +#include <boost/spirit/include/karma_directive.hpp> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace spirit_test; + using namespace boost::spirit; + using namespace boost::spirit::ascii; + + { + BOOST_TEST(test("x *****", left_align(15, '*')[left_align[char_('x')]])); + BOOST_TEST(test(" x *****", left_align(15, '*')[center[char_('x')]])); + BOOST_TEST(test(" x*****", left_align(15, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("*****x ", right_align(15, '*')[left_align[char_('x')]])); + BOOST_TEST(test("***** x ", right_align(15, '*')[center[char_('x')]])); + BOOST_TEST(test("***** x", right_align(15, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("***x **", center(15, '*')[left_align[char_('x')]])); + BOOST_TEST(test("*** x **", center(15, '*')[center[char_('x')]])); + BOOST_TEST(test("*** x**", center(15, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("ab *****", left_align(15, '*')[left_align[lit("ab")]])); + BOOST_TEST(test(" ab *****", left_align(15, '*')[center[lit("ab")]])); + BOOST_TEST(test(" ab*****", left_align(15, '*')[right_align[lit("ab")]])); + + BOOST_TEST(test("*****ab ", right_align(15, '*')[left_align[lit("ab")]])); + BOOST_TEST(test("***** ab ", right_align(15, '*')[center[lit("ab")]])); + BOOST_TEST(test("***** ab", right_align(15, '*')[right_align[lit("ab")]])); + + BOOST_TEST(test("***ab **", center(15, '*')[left_align[lit("ab")]])); + BOOST_TEST(test("*** ab **", center(15, '*')[center[lit("ab")]])); + BOOST_TEST(test("*** ab**", center(15, '*')[right_align[lit("ab")]])); + } + + { + BOOST_TEST(test("x ******", left_align(16, '*')[left_align[char_('x')]])); + BOOST_TEST(test(" x ******", left_align(16, '*')[center[char_('x')]])); + BOOST_TEST(test(" x******", left_align(16, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("******x ", right_align(16, '*')[left_align[char_('x')]])); + BOOST_TEST(test("****** x ", right_align(16, '*')[center[char_('x')]])); + BOOST_TEST(test("****** x", right_align(16, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("***x ***", center(16, '*')[left_align[char_('x')]])); + BOOST_TEST(test("*** x ***", center(16, '*')[center[char_('x')]])); + BOOST_TEST(test("*** x***", center(16, '*')[right_align[char_('x')]])); + + BOOST_TEST(test("ab ******", left_align(16, '*')[left_align[lit("ab")]])); + BOOST_TEST(test(" ab ******", left_align(16, '*')[center[lit("ab")]])); + BOOST_TEST(test(" ab******", left_align(16, '*')[right_align[lit("ab")]])); + + BOOST_TEST(test("******ab ", right_align(16, '*')[left_align[lit("ab")]])); + BOOST_TEST(test("****** ab ", right_align(16, '*')[center[lit("ab")]])); + BOOST_TEST(test("****** ab", right_align(16, '*')[right_align[lit("ab")]])); + + BOOST_TEST(test("***ab ***", center(16, '*')[left_align[lit("ab")]])); + BOOST_TEST(test("*** ab ***", center(16, '*')[center[lit("ab")]])); + BOOST_TEST(test("*** ab***", center(16, '*')[right_align[lit("ab")]])); + } + + { + BOOST_TEST(test("x ****", left_align(15, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test(" x ****", left_align(15, '*')[center(11)[char_('x')]])); + BOOST_TEST(test(" x****", left_align(15, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("****x ", right_align(15, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test("**** x ", right_align(15, '*')[center(11)[char_('x')]])); + BOOST_TEST(test("**** x", right_align(15, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("**x **", center(15, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test("** x **", center(15, '*')[center(11)[char_('x')]])); + BOOST_TEST(test("** x**", center(15, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("ab ****", left_align(15, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test(" ab ****", left_align(15, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test(" ab****", left_align(15, '*')[right_align(11)[lit("ab")]])); + + BOOST_TEST(test("****ab ", right_align(15, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test("**** ab ", right_align(15, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test("**** ab", right_align(15, '*')[right_align(11)[lit("ab")]])); + + BOOST_TEST(test("**ab **", center(15, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test("** ab **", center(15, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test("** ab**", center(15, '*')[right_align(11)[lit("ab")]])); + } + + { + BOOST_TEST(test("x *****", left_align(16, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test(" x *****", left_align(16, '*')[center(11)[char_('x')]])); + BOOST_TEST(test(" x*****", left_align(16, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("*****x ", right_align(16, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test("***** x ", right_align(16, '*')[center(11)[char_('x')]])); + BOOST_TEST(test("***** x", right_align(16, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("***x **", center(16, '*')[left_align(11)[char_('x')]])); + BOOST_TEST(test("*** x **", center(16, '*')[center(11)[char_('x')]])); + BOOST_TEST(test("*** x**", center(16, '*')[right_align(11)[char_('x')]])); + + BOOST_TEST(test("ab *****", left_align(16, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test(" ab *****", left_align(16, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test(" ab*****", left_align(16, '*')[right_align(11)[lit("ab")]])); + + BOOST_TEST(test("*****ab ", right_align(16, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test("***** ab ", right_align(16, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test("***** ab", right_align(16, '*')[right_align(11)[lit("ab")]])); + + BOOST_TEST(test("***ab **", center(16, '*')[left_align(11)[lit("ab")]])); + BOOST_TEST(test("*** ab **", center(16, '*')[center(11)[lit("ab")]])); + BOOST_TEST(test("*** ab**", center(16, '*')[right_align(11)[lit("ab")]])); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/uint_radix.cpp b/src/boost/libs/spirit/test/karma/uint_radix.cpp new file mode 100644 index 00000000..a39838fb --- /dev/null +++ b/src/boost/libs/spirit/test/karma/uint_radix.cpp @@ -0,0 +1,507 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2011 Jan Frederick Eick +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/spirit/include/karma_numeric.hpp> + +#include <boost/cstdint.hpp> + +#include "test.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// +// *** BEWARE PLATFORM DEPENDENT!!! *** +// *** The following assumes 32 bit boost::uint32_tegers. +// *** Modify these constants when appropriate. +// +/////////////////////////////////////////////////////////////////////////////// + +char const* max_unsigned_base2 = "11111111111111111111111111111111"; +char const* max_unsigned_base3 = "102002022201221111210"; +char const* max_unsigned_base4 = "3333333333333333"; +char const* max_unsigned_base5 = "32244002423140"; +char const* max_unsigned_base6 = "1550104015503"; +char const* max_unsigned_base7 = "211301422353"; +char const* max_unsigned_base8 = "37777777777"; +char const* max_unsigned_base9 = "12068657453"; +char const* max_unsigned_base11 = "1904440553"; +char const* max_unsigned_base12 = "9ba461593"; +char const* max_unsigned_base13 = "535a79888"; +char const* max_unsigned_base14 = "2ca5b7463"; +char const* max_unsigned_base15 = "1a20dcd80"; +char const* max_unsigned_base16 = "ffffffff"; +char const* max_unsigned_base17 = "a7ffda90"; +char const* max_unsigned_base18 = "704he7g3"; +char const* max_unsigned_base19 = "4f5aff65"; +char const* max_unsigned_base20 = "3723ai4f"; +char const* max_unsigned_base21 = "281d55i3"; +char const* max_unsigned_base22 = "1fj8b183"; +char const* max_unsigned_base23 = "1606k7ib"; +char const* max_unsigned_base24 = "mb994af"; +char const* max_unsigned_base25 = "hek2mgk"; +char const* max_unsigned_base26 = "dnchbnl"; +char const* max_unsigned_base27 = "b28jpdl"; +char const* max_unsigned_base28 = "8pfgih3"; +char const* max_unsigned_base29 = "76beigf"; +char const* max_unsigned_base30 = "5qmcpqf"; +char const* max_unsigned_base31 = "4q0jto3"; +char const* max_unsigned_base32 = "3vvvvvv"; +char const* max_unsigned_base33 = "3aokq93"; +char const* max_unsigned_base34 = "2qhxjlh"; +char const* max_unsigned_base35 = "2br45qa"; +char const* max_unsigned_base36 = "1z141z3"; + +int +main() +{ + using spirit_test::test; + using boost::spirit::karma::uint_generator; + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 2) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 2> base2_generator; + + BOOST_TEST(test("1100111100100110010", base2_generator(424242))); + BOOST_TEST(test("1100111100100110010", base2_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base2, base2_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base2, base2_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 3) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 3> base3_generator; + + BOOST_TEST(test("210112221200", base3_generator(424242))); + BOOST_TEST(test("210112221200", base3_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base3, base3_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base3, base3_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 4) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 4> base4_generator; + + BOOST_TEST(test("1213210302", base4_generator(424242))); + BOOST_TEST(test("1213210302", base4_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base4, base4_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base4, base4_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 5) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 5> base5_generator; + + BOOST_TEST(test("102033432", base5_generator(424242))); + BOOST_TEST(test("102033432", base5_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base5, base5_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base5, base5_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 6) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 6> base6_generator; + + BOOST_TEST(test("13032030", base6_generator(424242))); + BOOST_TEST(test("13032030", base6_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base6, base6_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base6, base6_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 7) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 7> base7_generator; + + BOOST_TEST(test("3414600", base7_generator(424242))); + BOOST_TEST(test("3414600", base7_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base7, base7_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base7, base7_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 8) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 8> base8_generator; + + BOOST_TEST(test("1474462", base8_generator(424242))); + BOOST_TEST(test("1474462", base8_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base8, base8_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base8, base8_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 9) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 9> base9_generator; + + BOOST_TEST(test("715850", base9_generator(424242))); + BOOST_TEST(test("715850", base9_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base9, base9_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base9, base9_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 11) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 11> base11_generator; + + BOOST_TEST(test("26a815", base11_generator(424242))); + BOOST_TEST(test("26a815", base11_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base11, base11_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base11, base11_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 12) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 12> base12_generator; + + BOOST_TEST(test("185616", base12_generator(424242))); + BOOST_TEST(test("185616", base12_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base12, base12_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base12, base12_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 13) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 13> base13_generator; + + BOOST_TEST(test("11b140", base13_generator(424242))); + BOOST_TEST(test("11b140", base13_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base13, base13_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base13, base13_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 14) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 14> base14_generator; + + BOOST_TEST(test("b0870", base14_generator(424242))); + BOOST_TEST(test("b0870", base14_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base14, base14_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base14, base14_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 15) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 15> base15_generator; + + BOOST_TEST(test("85a7c", base15_generator(424242))); + BOOST_TEST(test("85a7c", base15_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base15, base15_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base15, base15_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 16) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 16> base16_generator; + + BOOST_TEST(test("67932", base16_generator(424242))); + BOOST_TEST(test("67932", base16_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base16, base16_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base16, base16_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 17) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 17> base17_generator; + + BOOST_TEST(test("515g7", base17_generator(424242))); + BOOST_TEST(test("515g7", base17_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base17, base17_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base17, base17_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 18) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 18> base18_generator; + + BOOST_TEST(test("40d70", base18_generator(424242))); + BOOST_TEST(test("40d70", base18_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base18, base18_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base18, base18_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 19) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 19> base19_generator; + + BOOST_TEST(test("34g3a", base19_generator(424242))); + BOOST_TEST(test("34g3a", base19_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base19, base19_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base19, base19_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 20) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 20> base20_generator; + + BOOST_TEST(test("2d0c2", base20_generator(424242))); + BOOST_TEST(test("2d0c2", base20_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base20, base20_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base20, base20_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 21) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 21> base21_generator; + + BOOST_TEST(test("23h00", base21_generator(424242))); + BOOST_TEST(test("23h00", base21_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base21, base21_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base21, base21_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 22) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 22> base22_generator; + + BOOST_TEST(test("1hibg", base22_generator(424242))); + BOOST_TEST(test("1hibg", base22_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base22, base22_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base22, base22_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 23) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 23> base23_generator; + + BOOST_TEST(test("1bjm7", base23_generator(424242))); + BOOST_TEST(test("1bjm7", base23_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base23, base23_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base23, base23_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 24) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 24> base24_generator; + + BOOST_TEST(test("16gci", base24_generator(424242))); + BOOST_TEST(test("16gci", base24_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base24, base24_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base24, base24_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 25) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 25> base25_generator; + + BOOST_TEST(test("123jh", base25_generator(424242))); + BOOST_TEST(test("123jh", base25_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base25, base25_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base25, base25_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 26) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 26> base26_generator; + + BOOST_TEST(test("o3f0", base26_generator(424242))); + BOOST_TEST(test("o3f0", base26_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base26, base26_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base26, base26_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 27) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 27> base27_generator; + + BOOST_TEST(test("lepi", base27_generator(424242))); + BOOST_TEST(test("lepi", base27_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base27, base27_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base27, base27_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 28) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 28> base28_generator; + + BOOST_TEST(test("j93e", base28_generator(424242))); + BOOST_TEST(test("j93e", base28_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base28, base28_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base28, base28_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 29) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 29> base29_generator; + + BOOST_TEST(test("hbd1", base29_generator(424242))); + BOOST_TEST(test("hbd1", base29_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base29, base29_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base29, base29_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 30) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 30> base30_generator; + + BOOST_TEST(test("flbc", base30_generator(424242))); + BOOST_TEST(test("flbc", base30_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base30, base30_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base30, base30_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 31) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 31> base31_generator; + + BOOST_TEST(test("e7e7", base31_generator(424242))); + BOOST_TEST(test("e7e7", base31_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base31, base31_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base31, base31_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 32) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 32> base32_generator; + + BOOST_TEST(test("cu9i", base32_generator(424242))); + BOOST_TEST(test("cu9i", base32_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base32, base32_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base32, base32_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 33) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 33> base33_generator; + + BOOST_TEST(test("bqir", base33_generator(424242))); + BOOST_TEST(test("bqir", base33_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base33, base33_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base33, base33_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 34) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 34> base34_generator; + + BOOST_TEST(test("aqxo", base34_generator(424242))); + BOOST_TEST(test("aqxo", base34_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base34, base34_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base34, base34_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 35) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 35> base35_generator; + + BOOST_TEST(test("9vb7", base35_generator(424242))); + BOOST_TEST(test("9vb7", base35_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base35, base35_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base35, base35_generator, 0xffffffffu)); + } + + /////////////////////////////////////////////////////////////////////////// + // arbitrary radix test (base 36) + /////////////////////////////////////////////////////////////////////////// + { + uint_generator<boost::uint32_t, 36> base36_generator; + + BOOST_TEST(test("93ci", base36_generator(424242))); + BOOST_TEST(test("93ci", base36_generator, 424242)); + + BOOST_TEST(test(max_unsigned_base36, base36_generator(0xffffffffu))); + BOOST_TEST(test(max_unsigned_base36, base36_generator, 0xffffffffu)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/utree1.cpp b/src/boost/libs/spirit/test/karma/utree1.cpp new file mode 100644 index 00000000..5728b103 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/utree1.cpp @@ -0,0 +1,128 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2001-2011 Joel de Guzman +// Copyright (c) 2010 Bryce Lelbach +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/mpl/print.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/support_utree.hpp> + +#include <sstream> + +#include "test.hpp" + +int main() +{ + using spirit_test::test; + using spirit_test::test_delimited; + using boost::spirit::utree; + using boost::spirit::utree_type; + using boost::spirit::utf8_string_range_type; + using boost::spirit::utf8_string_type; + using boost::spirit::utf8_symbol_type; + + using boost::spirit::karma::char_; + using boost::spirit::karma::bool_; + using boost::spirit::karma::int_; + using boost::spirit::karma::double_; + using boost::spirit::karma::string; + using boost::spirit::karma::space; + using boost::spirit::karma::rule; + + typedef spirit_test::output_iterator<char>::type output_iterator; + + // primitive data types + { + utree ut('x'); + BOOST_TEST(test("x", char_, ut)); + + ut = false; + BOOST_TEST(test("false", bool_, ut)); + + ut = 123; + BOOST_TEST(test("123", int_, ut)); + + ut = 123.45; + BOOST_TEST(test("123.45", double_, ut)); + + ut = "abc"; + BOOST_TEST(test("abc", string, ut)); + + ut = utf8_symbol_type("xyz"); + BOOST_TEST(test("xyz", string, ut)); + } + + // sequences + { + using boost::spirit::karma::digit; + using boost::spirit::karma::repeat; + + utree ut; + ut.push_back('x'); + ut.push_back('y'); + BOOST_TEST(test("xy", char_ << char_, ut)); + + ut.clear(); + ut.push_back(123); + ut.push_back(456); + BOOST_TEST(test_delimited("123 456 ", int_ << int_, ut, space)); + + ut.clear(); + ut.push_back(1.23); + ut.push_back(4.56); + BOOST_TEST(test_delimited("1.23 4.56 ", double_ << double_, ut, space)); + + ut.clear(); + ut.push_back(1.23); + ut.push_back("ab"); + BOOST_TEST(test("1.23ab", double_ << string, ut)); + + ut.clear(); + + rule<output_iterator, double()> r1 = double_; + rule<output_iterator, utree()> r2 = double_; + + // ( 1.23 "a" "b" ) + ut.push_back(1.23); + ut.push_back('a'); + ut.push_back('b'); + BOOST_TEST(test("1.23ab", double_ << *char_, ut)); + BOOST_TEST(test("1.23ab", r1 << *char_, ut)); + BOOST_TEST(test("1.23ab", r2 << *char_, ut)); + + // ( ( 1.23 ) "a" "b" ) + ut.clear(); + utree ut1; + ut1.push_back(1.23); + ut.push_back(ut1); + ut.push_back('a'); + ut.push_back('b'); + BOOST_TEST(test("1.23ab", r1 << *char_, ut)); + BOOST_TEST(test("1.23ab", r2 << *char_, ut)); + + // ( "a" "b" 1.23 ) + ut.clear(); + ut.push_back('a'); + ut.push_back('b'); + ut.push_back(1.23); + BOOST_TEST(test("ab1.23", repeat(2)[~digit] << double_, ut)); + BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r1, ut)); + BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r2, ut)); + + // ( "a" "b" ( 1.23 ) ) + ut.clear(); + ut.push_back('a'); + ut.push_back('b'); + ut.push_back(ut1); + BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r1, ut)); + BOOST_TEST(test("ab1.23", repeat(2)[~digit] << r2, ut)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/utree2.cpp b/src/boost/libs/spirit/test/karma/utree2.cpp new file mode 100644 index 00000000..429ddda1 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/utree2.cpp @@ -0,0 +1,155 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2001-2011 Joel de Guzman +// Copyright (c) 2010 Bryce Lelbach +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/mpl/print.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/support_utree.hpp> + +#include <sstream> + +#include "test.hpp" + +int main() +{ + using spirit_test::test; + using spirit_test::test_delimited; + using boost::spirit::utree; + using boost::spirit::utree_type; + using boost::spirit::utf8_string_range_type; + using boost::spirit::utf8_string_type; + using boost::spirit::utf8_symbol_type; + + using boost::spirit::karma::char_; + using boost::spirit::karma::bool_; + using boost::spirit::karma::int_; + using boost::spirit::karma::double_; + using boost::spirit::karma::string; + using boost::spirit::karma::space; + using boost::spirit::karma::rule; + + typedef spirit_test::output_iterator<char>::type output_iterator; + + // kleene star + { + utree ut; + ut.push_back('a'); + ut.push_back('b'); + BOOST_TEST(test("ab", *char_, ut)); + + ut.clear(); + ut.push_back(123); + ut.push_back(456); + BOOST_TEST(test_delimited("123 456 ", *int_, ut, space)); + + ut.clear(); + ut.push_back(1.23); + ut.push_back(4.56); + BOOST_TEST(test_delimited("1.23 4.56 ", *double_, ut, space)); + } + + // lists + { + rule<output_iterator, utree()> r1, r1ref; + rule<output_iterator, utf8_string_range_type()> r1str; + rule<output_iterator, utree::const_range()> r1list; + + r1 = double_ | int_ | r1str | r1list | r1ref; + + r1ref = r1.alias(); + + r1str = string; + + r1list = '(' << -(r1 % ',') << ')'; + + // ( "abc" "def" ) + utree ut; + ut.push_back("abc"); + ut.push_back("def"); + BOOST_TEST(test("abc,def", string % ',', ut)); + BOOST_TEST(test("(abc,def)", r1, ut)); + + // ( ( "abc" "def" ) ) + utree ut1; + ut1.push_back(ut); + BOOST_TEST(test("((abc,def))", r1, ut1)); + +// rule<output_iterator, std::vector<char>()> r2 = char_ % ','; +// BOOST_TEST(test("abc,def", r2, ut)); +// BOOST_TEST(test("abc,def", r2, ut1)); + + // ( ( "abc" "def" ) ( "abc" "def" ) ) + ut1.push_back(ut); + BOOST_TEST(test("(abc,def) (abc,def)", r1 << ' ' << r1, ut1)); + + // ( 123 456 ) + ut.clear(); + ut.push_back(123); + ut.push_back(456); + BOOST_TEST(test("123,456", int_ % ',', ut)); + BOOST_TEST(test("(123,456)", r1, ut)); + + // ( ( 123 456 ) ) + ut1.clear(); + ut1.push_back(ut); + BOOST_TEST(test("((123,456))", r1, ut1)); + +// rule<output_iterator, std::vector<int>()> r4 = int_ % ','; +// BOOST_TEST(test("123,456", r4, ut)); +// BOOST_TEST(test("123,456", r4, ut1)); + + // ( ( 123 456 ) ( 123 456 ) ) + ut1.push_back(ut); + BOOST_TEST(test("(123,456) (123,456)", r1 << ' ' << r1, ut1)); + + // ( 1.23 4.56 ) + ut.clear(); + ut.push_back(1.23); + ut.push_back(4.56); + BOOST_TEST(test("1.23,4.56", double_ % ',', ut)); + BOOST_TEST(test("(1.23,4.56)", r1, ut)); + + // ( ( 1.23 4.56 ) ) + ut1.clear(); + ut1.push_back(ut); + BOOST_TEST(test("((1.23,4.56))", r1, ut1)); + +// rule<output_iterator, std::vector<double>()> r6 = double_ % ','; +// BOOST_TEST(test("1.23,4.56", r6, ut)); +// BOOST_TEST(test("1.23,4.56", r6, ut1)); + + // ( ( 1.23 4.56 ) ( 1.23 4.56 ) ) + ut1.push_back(ut); + BOOST_TEST(test("(1.23,4.56) (1.23,4.56)", r1 <<' ' << r1, ut1)); + } + + // alternatives + { + rule<output_iterator, utree()> r1 = int_ | double_; + utree ut(10); + BOOST_TEST(test("10", int_ | double_, ut)); + BOOST_TEST(test("10", r1, ut)); + + ut = 10.2; + BOOST_TEST(test("10.2", int_ | double_, ut)); + BOOST_TEST(test("10.2", r1, ut)); + } + + // optionals + { + utree ut('x'); + BOOST_TEST(test("x", -char_, ut)); + + ut.clear(); + BOOST_TEST(test("", -char_, ut)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/utree3.cpp b/src/boost/libs/spirit/test/karma/utree3.cpp new file mode 100644 index 00000000..96f84e31 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/utree3.cpp @@ -0,0 +1,134 @@ +// Copyright (c) 2001-2011 Hartmut Kaiser +// Copyright (c) 2001-2011 Joel de Guzman +// Copyright (c) 2010 Bryce Lelbach +// +// 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) + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/mpl/print.hpp> + +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/support_utree.hpp> + +#include <sstream> + +#include "test.hpp" + +int main() +{ + using spirit_test::test; + using spirit_test::test_delimited; + using boost::spirit::utree; + using boost::spirit::utree_type; + using boost::spirit::utf8_string_range_type; + using boost::spirit::utf8_string_type; + using boost::spirit::utf8_symbol_type; + + using boost::spirit::karma::char_; + using boost::spirit::karma::bool_; + using boost::spirit::karma::int_; + using boost::spirit::karma::double_; + using boost::spirit::karma::string; + using boost::spirit::karma::space; + using boost::spirit::karma::rule; + + typedef spirit_test::output_iterator<char>::type output_iterator; + + // as_string + { + using boost::spirit::karma::digit; + using boost::spirit::karma::as_string; + + utree ut("xy"); + BOOST_TEST(test("xy", string, ut)); + BOOST_TEST(test("xy", as_string[*char_], ut)); + BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut)); + + ut.clear(); + ut.push_back("ab"); + ut.push_back(1.2); + BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut)); + BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut)); + } + + // as + { + using boost::spirit::karma::digit; + using boost::spirit::karma::as; + + typedef as<std::string> as_string_type; + as_string_type const as_string = as_string_type(); + + typedef as<utf8_symbol_type> as_symbol_type; + as_symbol_type const as_symbol = as_symbol_type(); + + utree ut("xy"); + BOOST_TEST(test("xy", string, ut)); + BOOST_TEST(test("xy", as_string[*char_], ut)); + BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut)); + + ut.clear(); + ut.push_back("ab"); + ut.push_back(1.2); + BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut)); + BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut)); + + ut = utf8_symbol_type("xy"); + BOOST_TEST(test("xy", string, ut)); + BOOST_TEST(test("xy", as_symbol[*char_], ut)); + BOOST_TEST(test("x,y", as_symbol[char_ << ',' << char_], ut)); + + ut.clear(); + ut.push_back(utf8_symbol_type("ab")); + ut.push_back(1.2); + BOOST_TEST(test("ab1.2", as_symbol[*~digit] << double_, ut)); + BOOST_TEST(test("a,b1.2", as_symbol[~digit % ','] << double_, ut)); + } + + // typed basic_string rules + { + utree ut("buzz"); + + rule<output_iterator, utf8_string_type()> r1 = string; + rule<output_iterator, utf8_symbol_type()> r2 = string; + + BOOST_TEST(test("buzz", r1, ut)); + + ut = utf8_symbol_type("bar"); + BOOST_TEST(test("bar", r2, ut)); + } + + // parameterized karma::string + { + utree ut("foo"); + + rule<output_iterator, utf8_string_type()> r1 = string("foo"); + BOOST_TEST(test("foo", string("foo"), ut)); + BOOST_TEST(test("foo", r1, ut)); + } + + { + using boost::spirit::karma::verbatim; + using boost::spirit::karma::repeat; + using boost::spirit::karma::space; + using boost::spirit::karma::digit; + + utree ut; + ut.push_back('x'); + ut.push_back('y'); + ut.push_back('c'); + BOOST_TEST(test_delimited("xy c ", verbatim[repeat(2)[char_]] << char_, ut, space)); + BOOST_TEST(test_delimited("x yc ", char_ << verbatim[*char_], ut, space)); + + ut.clear(); + ut.push_back('a'); + ut.push_back('b'); + ut.push_back(1.2); + BOOST_TEST(test_delimited("ab 1.2 ", verbatim[repeat(2)[~digit]] << double_, ut, space)); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/spirit/test/karma/wstream.cpp b/src/boost/libs/spirit/test/karma/wstream.cpp new file mode 100644 index 00000000..1f4ba199 --- /dev/null +++ b/src/boost/libs/spirit/test/karma/wstream.cpp @@ -0,0 +1,115 @@ +// 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) + +#include <cwchar> +#include <streambuf> +#include <iostream> + +#include <boost/config/warning_disable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/cstdint.hpp> + +#include <boost/spirit/include/karma_char.hpp> +#include <boost/spirit/include/karma_string.hpp> +#include <boost/spirit/include/karma_stream.hpp> +#include <boost/spirit/include/karma_directive.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> + +#include "test.hpp" + +using namespace spirit_test; + +// a simple complex number representation z = a + bi +struct complex +{ + complex (double a, double b) + : a(a), b(b) + {} + + double a; + double b; + + template <typename Char> + friend std::basic_ostream<Char>& + operator<< (std::basic_ostream<Char>& os, complex z) + { + os << "{" << z.a << "," << z.b << "}"; + return os; + } +}; + +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + using namespace boost::spirit; + + { + BOOST_TEST(test(L"x", wstream, L'x')); + BOOST_TEST(test(L"xyz", wstream, L"xyz")); + BOOST_TEST(test(L"xyz", wstream, std::basic_string<wchar_t>(L"xyz"))); + BOOST_TEST(test(L"1", wstream, 1)); + BOOST_TEST(test(L"1.1", wstream, 1.1)); + BOOST_TEST(test(L"{1.2,2.4}", wstream, complex(1.2, 2.4))); + } + + { + BOOST_TEST(test(L"x", wstream(L'x'))); + BOOST_TEST(test(L"xyz", wstream(L"xyz"))); + BOOST_TEST(test(L"xyz", wstream(std::basic_string<wchar_t>(L"xyz")))); + BOOST_TEST(test(L"1", wstream(1))); + BOOST_TEST(test(L"1.1", wstream(1.1))); + BOOST_TEST(test(L"{1.2,2.4}", wstream(complex(1.2, 2.4)))); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test(L"x", lower[wstream], L'X')); + BOOST_TEST(test(L"xyz", lower[wstream], L"XYZ")); + BOOST_TEST(test(L"xyz", lower[wstream], std::basic_string<wchar_t>(L"XYZ"))); + BOOST_TEST(test(L"X", upper[wstream], L'x')); + BOOST_TEST(test(L"XYZ", upper[wstream], L"xyz")); + BOOST_TEST(test(L"XYZ", upper[wstream], std::basic_string<wchar_t>(L"xyz"))); + } + + { + BOOST_TEST(test_delimited(L"x ", wstream, L'x', L' ')); + BOOST_TEST(test_delimited(L"xyz ", wstream, L"xyz", L' ')); + BOOST_TEST(test_delimited(L"xyz ", wstream, std::basic_string<wchar_t>(L"xyz"), L' ')); + BOOST_TEST(test_delimited(L"1 ", wstream, 1, ' ')); + BOOST_TEST(test_delimited(L"1.1 ", wstream, 1.1, ' ')); + BOOST_TEST(test_delimited(L"{1.2,2.4} ", wstream, complex(1.2, 2.4), ' ')); + } + + { + using namespace boost::spirit::ascii; + + BOOST_TEST(test_delimited(L"x ", lower[wstream], L'X', L' ')); + BOOST_TEST(test_delimited(L"xyz ", lower[wstream], L"XYZ", L' ')); + BOOST_TEST(test_delimited(L"xyz ", lower[wstream], std::basic_string<wchar_t>(L"XYZ"), L' ')); + BOOST_TEST(test_delimited(L"X ", upper[wstream], L'x', L' ')); + BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], L"xyz", ' ')); + BOOST_TEST(test_delimited(L"XYZ ", upper[wstream], std::basic_string<wchar_t>(L"xyz"), L' ')); + } + + { // lazy streams + namespace phx = boost::phoenix; + + std::basic_string<wchar_t> ws(L"abc"); + BOOST_TEST((test(L"abc", wstream(phx::val(ws))))); + BOOST_TEST((test(L"abc", wstream(phx::ref(ws))))); + } + + { + boost::optional<wchar_t> c; + BOOST_TEST(!test(L"", wstream, c)); + c = L'x'; + BOOST_TEST(test(L"x", wstream, c)); + } + + return boost::report_errors(); +} |