File: epsilon_tests.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (136 lines) | stat: -rw-r--r-- 4,140 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*=============================================================================
    Copyright (c) 2002-2003 Martin Wille
    http://spirit.sourceforge.net/

    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)
=============================================================================*/
#include <iostream>
#include <cstring>
#include <boost/detail/lightweight_test.hpp>

// This test program only includes the epsilon.hpp header from Spirit
#include <boost/spirit/include/classic_epsilon.hpp>
#include <boost/detail/lightweight_test.hpp>
#include "impl/var.hpp"
#include "impl/string_length.hpp"

using namespace test;
static BOOST_SPIRIT_CLASSIC_NS::parse_info<char const *> pi;

////////////////////////////////////////////////
// These macros are used with BOOST_TEST
#define matches (pi.hit)
#define full_match (pi.hit && pi.full)
#define partial_match (pi.hit && !pi.full)
#define no_match (!pi.hit && !pi.full)
#define zero_length_match (pi.length == 0)
#define stop_equals_start (pi.stop == s)

template<typename ParserT>
static void
parse(char const *s, ParserT const &p, bool match)
{
    
    pi = BOOST_SPIRIT_CLASSIC_NS::parse(s, s + test_impl::string_length(s), p);
    if (match)
    {
        BOOST_TEST(matches);
        BOOST_TEST(zero_length_match);
        BOOST_TEST(stop_equals_start);
    }
    else
    {
        BOOST_TEST(no_match);
    }
}

static char const empty[] = "";
static char const not_empty[] = "asdfgh";

////////////////////////////////////////////////
// Test wether epsilon_p/eps_p work as
// primitive parsers
static void
epsilon_as_primitive()
{
    // This test case also is a compile time check wether
    // both eps_p and epsilon_p are present.

    parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
    BOOST_TEST(full_match);
    parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
    BOOST_TEST(partial_match);

    parse(empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
    BOOST_TEST(full_match);
    parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
    BOOST_TEST(partial_match);
}

////////////////////////////////////////////////
// Test wether epsilon_p/eps_p work correctly as
// a parser generator for creating parsers from
// functors
static void
epsilon_as_parser_generator_for_functors()
{
    bool       flag = false;
    parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
    BOOST_TEST(no_match);

    flag = true;
    parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
    BOOST_TEST(full_match);
}

////////////////////////////////////////////////
// Test wether epsilon_p/eps_p work correctly as
// a parser generator for creating parsers from
// other parsers
static void
epsilon_as_parser_generator_for_parsers()
{
    // This test case uses a parser created by epsilon_p
    // as body-parser for another invokation of epsilon_p

    bool        flag = false;
    parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
            BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
    BOOST_TEST(no_match);

    flag = true;
    parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
            BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
    BOOST_TEST(full_match);
}

////////////////////////////////////////////////
// Test wether epsilon_p/eps_p support negation
static void
negation_operator_for_epsilon()
{
    bool       flag = false;
    parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
    BOOST_TEST(full_match);
    parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
    BOOST_TEST(no_match);

    flag = true;
    parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
    BOOST_TEST(no_match);
    parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
    BOOST_TEST(full_match);
}

int
main()
{
    epsilon_as_primitive();
    epsilon_as_parser_generator_for_functors();
    epsilon_as_parser_generator_for_parsers();
    negation_operator_for_epsilon();

    return boost::report_errors();
}