|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r58042 - in trunk/libs/spirit/test: . karma
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-29 16:28:22
Author: hkaiser
Date: 2009-11-29 16:28:22 EST (Sun, 29 Nov 2009)
New Revision: 58042
URL: http://svn.boost.org/trac/boost/changeset/58042
Log:
Spirit: added karma::auto_ and related facilities, added test
Added:
trunk/libs/spirit/test/karma/auto.cpp (contents, props changed)
Text files modified:
trunk/libs/spirit/test/Jamfile | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Modified: trunk/libs/spirit/test/Jamfile
==============================================================================
--- trunk/libs/spirit/test/Jamfile (original)
+++ trunk/libs/spirit/test/Jamfile 2009-11-29 16:28:22 EST (Sun, 29 Nov 2009)
@@ -73,6 +73,7 @@
[ run karma/and_predicate.cpp : : : : karma_and_predicate ]
[ run karma/alternative.cpp : : : : karma_alternative ]
[ run karma/attribute.cpp : : : : karma_attribute ]
+ [ run karma/auto.cpp : : : : karma_auto ]
[ run karma/binary.cpp : : : : karma_binary ]
[ run karma/bool.cpp : : : : karma_bool ]
[ run karma/buffer.cpp : : : : karma_buffer ]
Added: trunk/libs/spirit/test/karma/auto.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/test/karma/auto.cpp 2009-11-29 16:28:22 EST (Sun, 29 Nov 2009)
@@ -0,0 +1,212 @@
+// Copyright (c) 2001-2009 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/fusion/include/std_pair.hpp>
+
+#include <boost/spirit/include/karma_bool.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/home/karma/auto/create_generator.hpp>
+#include <boost/spirit/home/karma/auto/auto.hpp>
+
+#include "test.hpp"
+
+namespace karma = boost::spirit::karma;
+
+///////////////////////////////////////////////////////////////////////////////
+template <typename T>
+std::string print(T const& t)
+{
+ std::string generated;
+ std::back_insert_iterator<std::string> sink(generated);
+
+ if (!karma::generate(sink, karma::create_generator<T>(), t))
+ return "";
+
+ return generated;
+}
+
+template <typename Char, typename Attribute>
+bool test_rule(Char const *expected, Attribute const& attr)
+{
+ typedef 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)
+{
+ typedef 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);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ {
+ // test primitive types
+ BOOST_TEST("true" == print(true));
+ BOOST_TEST("1" == print(1));
+ BOOST_TEST("1.1" == print(1.1));
+ BOOST_TEST("test" == print("test"));
+ BOOST_TEST("a" == print('a'));
+
+ // test containers
+ std::vector<int> v;
+ v.push_back(0);
+ v.push_back(1);
+ v.push_back(2);
+ BOOST_TEST("012" == print(v));
+
+ std::list<int> l;
+ l.push_back(0);
+ l.push_back(1);
+ l.push_back(2);
+ BOOST_TEST("012" == print(l));
+
+ // test optional
+ boost::optional<int> o;
+ BOOST_TEST("" == print(o));
+ o = 1;
+ BOOST_TEST("1" == print(o));
+
+ // test alternative
+ boost::variant<int, double, float, std::string> vv;
+ vv = 1;
+ BOOST_TEST("1" == print(vv));
+ vv = 1.0;
+ BOOST_TEST("1.0" == print(vv));
+ vv = 1.0f;
+ BOOST_TEST("1.0" == print(vv));
+ vv = "some string";
+ BOOST_TEST("some string" == print(vv));
+
+ // test fusion sequence
+ std::pair<int, double> p (1, 2.0);
+ BOOST_TEST("12.0" == print(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("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_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();
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk