Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/test/epsilon_tests.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.9 KB
Line 
1/*=============================================================================
2    Copyright (c) 2002-2003 Martin Wille
3    http://spirit.sourceforge.net/
4
5    Use, modification and distribution is subject to the Boost Software
6    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7    http://www.boost.org/LICENSE_1_0.txt)
8=============================================================================*/
9#include <iostream>
10#include <cstring>
11#include <boost/detail/lightweight_test.hpp>
12
13// This test program only includes the epsilon.hpp header from Spirit
14#include <boost/spirit/core/composite/epsilon.hpp>
15#include <boost/detail/lightweight_test.hpp>
16#include "impl/var.hpp"
17#include "impl/string_length.hpp"
18
19using namespace test;
20static boost::spirit::parse_info<char const *> pi;
21
22////////////////////////////////////////////////
23// These macros are used with BOOST_TEST
24#define matches (pi.hit)
25#define full_match (pi.hit && pi.full)
26#define partial_match (pi.hit && !pi.full)
27#define no_match (!pi.hit && !pi.full)
28#define zero_length_match (pi.length == 0)
29#define stop_equals_start (pi.stop == s)
30
31template<typename ParserT>
32static void
33parse(char const *s, ParserT const &p, bool match)
34{
35   
36    pi = boost::spirit::parse(s, s + test_impl::string_length(s), p);
37    if (match)
38    {
39        BOOST_TEST(matches);
40        BOOST_TEST(zero_length_match);
41        BOOST_TEST(stop_equals_start);
42    }
43    else
44    {
45        BOOST_TEST(no_match);
46    }
47}
48
49static char const empty[] = "";
50static char const not_empty[] = "asdfgh";
51
52////////////////////////////////////////////////
53// Test wether epsilon_p/eps_p work as
54// primitive parsers
55static void
56epsilon_as_primitive()
57{
58    // This test case also is a compile time check wether
59    // both eps_p and epsilon_p are present.
60
61    parse(empty, boost::spirit::epsilon_p, true);
62    BOOST_TEST(full_match);
63    parse(not_empty, boost::spirit::epsilon_p, true);
64    BOOST_TEST(partial_match);
65
66    parse(empty, boost::spirit::eps_p, true);
67    BOOST_TEST(full_match);
68    parse(not_empty, boost::spirit::eps_p, true);
69    BOOST_TEST(partial_match);
70}
71
72////////////////////////////////////////////////
73// Test wether epsilon_p/eps_p work correctly as
74// a parser generator for creating parsers from
75// functors
76static void
77epsilon_as_parser_generator_for_functors()
78{
79    bool       flag = false;
80    parse(empty, boost::spirit::epsilon_p(var(flag)), flag);
81    BOOST_TEST(no_match);
82
83    flag = true;
84    parse(empty, boost::spirit::epsilon_p(var(flag)), flag);
85    BOOST_TEST(full_match);
86}
87
88////////////////////////////////////////////////
89// Test wether epsilon_p/eps_p work correctly as
90// a parser generator for creating parsers from
91// other parsers
92static void
93epsilon_as_parser_generator_for_parsers()
94{
95    // This test case uses a parser created by epsilon_p
96    // as body-parser for another invokation of epsilon_p
97
98    bool        flag = false;
99    parse(empty, boost::spirit::epsilon_p(
100            boost::spirit::epsilon_p(var(flag))), flag);
101    BOOST_TEST(no_match);
102
103    flag = true;
104    parse(empty, boost::spirit::epsilon_p(
105            boost::spirit::epsilon_p(var(flag))), flag);
106    BOOST_TEST(full_match);
107}
108
109////////////////////////////////////////////////
110// Test wether epsilon_p/eps_p support negation
111static void
112negation_operator_for_epsilon()
113{
114    bool       flag = false;
115    parse(empty, ~boost::spirit::epsilon_p(var(flag)), !flag);
116    BOOST_TEST(full_match);
117    parse(empty, ~~boost::spirit::epsilon_p(var(flag)), flag);
118    BOOST_TEST(no_match);
119
120    flag = true;
121    parse(empty, ~boost::spirit::epsilon_p(var(flag)), !flag);
122    BOOST_TEST(no_match);
123    parse(empty, ~~boost::spirit::epsilon_p(var(flag)), flag);
124    BOOST_TEST(full_match);
125}
126
127int
128main()
129{
130    epsilon_as_primitive();
131    epsilon_as_parser_generator_for_functors();
132    epsilon_as_parser_generator_for_parsers();
133    negation_operator_for_epsilon();
134
135    return boost::report_errors();
136}
Note: See TracBrowser for help on using the repository browser.