Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/test/primitives_tests.cpp @ 12

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

added boost

File size: 3.5 KB
Line 
1/*=============================================================================
2    Copyright (c) 1998-2003 Joel de Guzman
3    Copyright (c) 2002-2003 Martin Wille
4    http://spirit.sourceforge.net/
5
6    Use, modification and distribution is subject to the Boost Software
7    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10#include <iostream>
11
12using namespace std;
13#include <boost/spirit/core.hpp>
14#include <boost/detail/lightweight_test.hpp>
15#include "impl/string_length.hpp"
16using namespace boost::spirit;
17
18///////////////////////////////////////////////////////////////////////////////
19//
20//  Primitives tests
21//
22///////////////////////////////////////////////////////////////////////////////
23void
24primitives_tests()
25{
26    char const* cp = "xyz.Jambalaya";
27    char const* cp_first = cp;
28    char const* cp_last = cp + string_length(cp);
29
30    chlit<> cpx('x');
31    parse_info<char const*> pi = parse(cp_first, cp_last, cpx);
32    BOOST_TEST(pi.hit);
33    BOOST_TEST(!pi.full);
34    BOOST_TEST(pi.length == 1);
35    BOOST_TEST(pi.stop == &cp[1]);
36
37    pi = parse(pi.stop, ch_p('y'));
38    BOOST_TEST(pi.hit);
39    BOOST_TEST(!pi.full);
40    BOOST_TEST(pi.length == 1);
41    BOOST_TEST(pi.stop == &cp[2]);
42
43    scanner<char const*> scan(pi.stop, cp_last);
44    match<char> hit = ch_p('z').parse(scan);
45    BOOST_TEST(hit);
46    BOOST_TEST(hit.value() == 'z');
47    BOOST_TEST(pi.stop == &cp[3]);
48
49    pi = parse(pi.stop, eps_p);
50    BOOST_TEST(pi.hit);
51    BOOST_TEST(!pi.full);
52    BOOST_TEST(pi.length == 0);
53    BOOST_TEST(pi.stop == &cp[3]);
54
55    pi = parse(pi.stop, nothing_p);
56    BOOST_TEST(!pi.hit);
57    BOOST_TEST(!pi.full);
58    BOOST_TEST(pi.stop == &cp[3]);
59
60    pi = parse(pi.stop, anychar_p);
61    BOOST_TEST(pi.hit);
62    BOOST_TEST(!pi.full);
63    BOOST_TEST(pi.length == 1);
64    BOOST_TEST(pi.stop == &cp[4]);
65
66    scan.first = pi.stop;
67    hit = range_p('A','Z').parse(scan);
68    BOOST_TEST(hit);
69    BOOST_TEST(hit.value() == 'J');
70    BOOST_TEST(pi.stop == &cp[5]);
71
72    strlit<char const*>     sp("ambalaya");
73    strlit<wchar_t const*>  wsp(L"ambalaya");
74
75    char const* save = pi.stop;
76    pi = parse(save, sp);
77    BOOST_TEST(pi.hit);
78    BOOST_TEST(pi.full);
79    BOOST_TEST(pi.length == 8);
80    BOOST_TEST(pi.stop == cp_last);
81
82    pi = parse(save, wsp);
83    BOOST_TEST(pi.hit);
84    BOOST_TEST(pi.full);
85    BOOST_TEST(pi.length == 8);
86    BOOST_TEST(pi.stop == cp_last);
87
88    pi = parse("\n", eol_p);
89    BOOST_TEST(pi.hit);
90    BOOST_TEST(pi.full);
91
92    pi = parse("\r", eol_p);
93    BOOST_TEST(pi.hit);
94    BOOST_TEST(pi.full);
95
96    pi = parse("\r\n", eol_p);
97    BOOST_TEST(pi.hit);
98    BOOST_TEST(pi.full);
99
100    pi = parse("\n\r", eol_p);
101    BOOST_TEST(pi.hit);
102    BOOST_TEST(!pi.full);
103
104    pi = parse("", end_p);
105    BOOST_TEST(pi.hit);
106    BOOST_TEST(pi.full);
107
108    pi = parse("1", ~alpha_p);
109    BOOST_TEST(pi.hit);
110    BOOST_TEST(pi.full);
111
112    pi = parse("a", ~alpha_p);
113    BOOST_TEST(!pi.hit);
114
115    pi = parse("a", ~~alpha_p);
116    BOOST_TEST(pi.hit);
117    BOOST_TEST(pi.full);
118
119    pi = parse("1", ~~alpha_p);
120    BOOST_TEST(!pi.hit);
121}
122
123///////////////////////////////////////////////////////////////////////////////
124//
125//  Main
126//
127///////////////////////////////////////////////////////////////////////////////
128int
129main()
130{
131    primitives_tests();
132    if (boost::report_errors() == 0)
133        cout << "Tests concluded successfully\n";
134    return boost::report_errors();
135}
136
Note: See TracBrowser for help on using the repository browser.