123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /*=============================================================================
- 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();
- }
|