Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58048 - in trunk/libs/spirit/test: . karma qi
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-29 18:30:51


Author: hkaiser
Date: 2009-11-29 18:30:50 EST (Sun, 29 Nov 2009)
New Revision: 58048
URL: http://svn.boost.org/trac/boost/changeset/58048

Log:
Spirit: fixes to karma::auto_, added qi::auto_, added tests
Added:
   trunk/libs/spirit/test/qi/auto.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/test/Jamfile | 1
   trunk/libs/spirit/test/karma/auto.cpp | 52 +++++++++++++++++++--------------------
   2 files changed, 26 insertions(+), 27 deletions(-)

Modified: trunk/libs/spirit/test/Jamfile
==============================================================================
--- trunk/libs/spirit/test/Jamfile (original)
+++ trunk/libs/spirit/test/Jamfile 2009-11-29 18:30:50 EST (Sun, 29 Nov 2009)
@@ -31,6 +31,7 @@
     [ run qi/attr.cpp : : : : ]
     [ run qi/attribute.cpp : : : : ]
     [ run qi/and_predicate.cpp : : : : ]
+ [ run qi/auto.cpp : : : : ]
     [ run qi/binary.cpp : : : : ]
     [ run qi/bool.cpp : : : : ]
     [ run qi/char.cpp : : : : ]

Modified: trunk/libs/spirit/test/karma/auto.cpp
==============================================================================
--- trunk/libs/spirit/test/karma/auto.cpp (original)
+++ trunk/libs/spirit/test/karma/auto.cpp 2009-11-29 18:30:50 EST (Sun, 29 Nov 2009)
@@ -5,34 +5,33 @@
 
 #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_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/home/karma/auto/create_generator.hpp>
-#include <boost/spirit/home/karma/auto/auto.hpp>
+#include <boost/spirit/include/karma_auto.hpp>
 
 #include "test.hpp"
 
 namespace karma = boost::spirit::karma;
 
 ///////////////////////////////////////////////////////////////////////////////
-template <typename T>
-std::string print(T const& t)
+template <typename Char, typename T>
+bool test_create_generator(Char const *expected, T const& t)
 {
- std::string generated;
- std::back_insert_iterator<std::string> sink(generated);
+ std::basic_string<Char> generated;
+ std::back_insert_iterator<std::basic_string<Char> > sink(generated);
 
- if (!karma::generate(sink, karma::create_generator<T>(), t))
- return "";
+ bool result = karma::generate(sink, karma::create_generator<T>(), t);
 
- return generated;
+ spirit_test::print_if_failed("test_create_generator", result, generated, expected);
+ return result && generated == expected;
 }
 
 template <typename Char, typename Attribute>
@@ -59,45 +58,46 @@
 {
     {
         // 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'));
+ 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("012" == print(v));
+ 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("012" == print(l));
+ BOOST_TEST(test_create_generator("012", l));
 
         // test optional
         boost::optional<int> o;
- BOOST_TEST("" == print(o));
+ BOOST_TEST(test_create_generator("", o));
         o = 1;
- BOOST_TEST("1" == print(o));
+ BOOST_TEST(test_create_generator("1", o));
 
         // test alternative
         boost::variant<int, double, float, std::string> vv;
         vv = 1;
- BOOST_TEST("1" == print(vv));
+ BOOST_TEST(test_create_generator("1", vv));
         vv = 1.0;
- BOOST_TEST("1.0" == print(vv));
+ BOOST_TEST(test_create_generator("1.0", vv));
         vv = 1.0f;
- BOOST_TEST("1.0" == print(vv));
+ BOOST_TEST(test_create_generator("1.0", vv));
         vv = "some string";
- BOOST_TEST("some string" == print(vv));
+ BOOST_TEST(test_create_generator("some string", vv));
 
         // test fusion sequence
         std::pair<int, double> p (1, 2.0);
- BOOST_TEST("12.0" == print(p));
+ BOOST_TEST(test_create_generator("12.0", p));
     }
 
     {
@@ -159,8 +159,6 @@
     }
 
     {
- using karma::auto_;
- using karma::upper;
         using spirit_test::test;
         using spirit_test::test_delimited;
 

Added: trunk/libs/spirit/test/qi/auto.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/test/qi/auto.cpp 2009-11-29 18:30:50 EST (Sun, 29 Nov 2009)
@@ -0,0 +1,203 @@
+// 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/fusion/include/std_pair.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+
+#include <boost/spirit/include/qi_bool.hpp>
+#include <boost/spirit/include/qi_char.hpp>
+#include <boost/spirit/include/qi_numeric.hpp>
+#include <boost/spirit/include/qi_string.hpp>
+#include <boost/spirit/include/qi_nonterminal.hpp>
+#include <boost/spirit/include/qi_operator.hpp>
+#include <boost/spirit/include/qi_directive.hpp>
+#include <boost/spirit/include/qi_auto.hpp>
+
+#include "test.hpp"
+
+namespace qi = boost::spirit::qi;
+
+///////////////////////////////////////////////////////////////////////////////
+template <typename Char, typename T>
+bool test_create_parser(Char const *in, T& t)
+{
+ Char const* last = in;
+ while (*last)
+ last++;
+
+ return qi::phrase_parse(in, last, qi::create_parser<T>(), qi::space, t);
+}
+
+template <typename Char, typename Attribute>
+bool test_rule(Char const* in, Attribute const& expected)
+{
+ Attribute attr = Attribute();
+ qi::rule<Char const*, Attribute()> r = qi::create_parser<Attribute>();
+ return spirit_test::test_attr(in, r, attr) && attr == expected;
+}
+
+template <typename Char, typename Attribute, typename Skipper>
+bool test_rule(Char const* in, Attribute const& expected, Skipper const& skipper)
+{
+ Attribute attr = Attribute();
+ qi::rule<Char const*, Attribute(), Skipper> r = qi::create_parser<Attribute>();
+ return spirit_test::test_attr(in, r, attr, skipper) && attr == expected;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ {
+ // test primitive types
+ bool b = false;
+ BOOST_TEST(test_create_parser("true", b) && b == true);
+ int i = 0;
+ BOOST_TEST(test_create_parser("1", i) && i == 1);
+ double d = 0;
+ BOOST_TEST(test_create_parser("1.1", d) && d == 1.1);
+ std::string s;
+ BOOST_TEST(test_create_parser("test", s) && s == "test");
+ char c = '\0';
+ BOOST_TEST(test_create_parser("a", c) && c == 'a');
+ wchar_t wc = L'\0';
+ BOOST_TEST(test_create_parser(L"a", wc) && wc == L'a');
+
+ // test containers
+ std::vector<int> v;
+ BOOST_TEST(test_create_parser("0 1 2", v) && v.size() == 3 &&
+ v[0] == 0 && v[1] == 1 && v[2] == 2);
+
+ std::list<int> l;
+ BOOST_TEST(test_create_parser("0 1 2", l) && l.size() == 3 &&
+ *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);
+
+ // test optional
+ boost::optional<int> o;
+ BOOST_TEST(test_create_parser("", o) && !o);
+ BOOST_TEST(test_create_parser("1", o) && !!o && boost::get<int>(o) == 1);
+
+ // test alternative
+ boost::variant<double, bool, std::vector<char> > vv;
+ BOOST_TEST(test_create_parser("true", vv) && vv.which() == 1 &&
+ boost::get<bool>(vv) == true);
+ BOOST_TEST(test_create_parser("1.0", vv) && vv.which() == 0 &&
+ boost::get<double>(vv) == 1.0);
+ BOOST_TEST(test_create_parser("some_string", vv) && vv.which() == 2 &&
+ boost::equals(boost::get<std::vector<char> >(vv), "some_string"));
+
+ // test fusion sequence
+ std::pair<int, double> p;
+ BOOST_TEST(test_create_parser("1 2.0", p) &&
+ p.first == 1 && p.second == 2.0);
+ }
+
+ {
+ using qi::auto_;
+ using qi::no_case;
+ using spirit_test::test_attr;
+
+ // test primitive types
+ bool b = false;
+ BOOST_TEST(test_attr("true", auto_, b) && b == true);
+ int i = 0;
+ BOOST_TEST(test_attr("1", auto_, i) && i == 1);
+ double d = 0;
+ BOOST_TEST(test_attr("1.1", auto_, d) && d == 1.1);
+ std::string s;
+ BOOST_TEST(test_attr("test", auto_, s) && s == "test");
+ std::basic_string<wchar_t> ws;
+ BOOST_TEST(test_attr(L"test", auto_, ws) && ws == L"test");
+ char c = '\0';
+ BOOST_TEST(test_attr("a", auto_, c) && c == 'a');
+ wchar_t wc = L'\0';
+ BOOST_TEST(test_attr(L"a", auto_, wc) && wc == L'a');
+
+ b = false;
+ BOOST_TEST(test_attr("TRUE", no_case[auto_], b) && b == true);
+ s.clear();
+ BOOST_TEST(test_attr("TEST", no_case[auto_], s) && s == "TEST");
+
+ // test containers
+ std::vector<int> v;
+ BOOST_TEST(test_attr("0 1 2", auto_, v, qi::space) && v.size() == 3 &&
+ v[0] == 0 && v[1] == 1 && v[2] == 2);
+ v.clear();
+ BOOST_TEST(test_attr("0,1,2", auto_ % ',', v) && v.size() == 3 &&
+ v[0] == 0 && v[1] == 1 && v[2] == 2);
+
+ std::list<int> l;
+ BOOST_TEST(test_attr("0 1 2", auto_, l, qi::space) && l.size() == 3 &&
+ *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);
+ l.clear();
+ BOOST_TEST(test_attr("0,1,2", auto_ % ',', l) && l.size() == 3 &&
+ *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);
+
+ // test optional
+ boost::optional<int> o;
+ BOOST_TEST(test_attr("", auto_, o) && !o);
+ BOOST_TEST(test_attr("1", auto_, o) && !!o && boost::get<int>(o) == 1);
+
+ // test alternative
+ boost::variant<double, bool, std::vector<char> > vv;
+ BOOST_TEST(test_attr("true", auto_, vv) && vv.which() == 1 &&
+ boost::get<bool>(vv) == true);
+ BOOST_TEST(test_attr("1.0", auto_, vv) && vv.which() == 0 &&
+ boost::get<double>(vv) == 1.0);
+ BOOST_TEST(test_create_parser("some_string", vv) && vv.which() == 2 &&
+ boost::equals(boost::get<std::vector<char> >(vv), "some_string"));
+
+ // test fusion sequence
+ std::pair<int, double> p;
+ BOOST_TEST(test_attr("1 2.0", auto_, p, qi::space) &&
+ p.first == 1 && p.second == 2.0);
+ }
+
+ {
+ // 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("0 1 2", v, qi::space));
+
+ std::list<int> l;
+ l.push_back(0);
+ l.push_back(1);
+ l.push_back(2);
+ BOOST_TEST(test_rule("0 1 2", l, qi::space));
+
+ // 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("1 2.0", p, qi::space));
+ }
+
+ 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