summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/classic/test/symbols_tests.cpp
blob: a0bcc265284627a403330803a14686d3ca7afe42 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*=============================================================================
    Copyright (c) 1998-2003 Joel de Guzman
    Copyright (c)      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 <string>
#include <boost/detail/lightweight_test.hpp>
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_symbols.hpp>
#include <boost/detail/lightweight_test.hpp>

///////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace BOOST_SPIRIT_CLASSIC_NS;

///////////////////////////////////////////////////////////////////////////////

template <typename IteratorT>
bool
equal(IteratorT p, IteratorT q)
{
    while (*p && *p == *q)
    {
        ++p;
        ++q;
    }
    return *p == *q;
}

template <class SymbolsT, typename CharT>
void
docheck
(
    SymbolsT const &sym,
    CharT    const *candidate,
    bool           hit,
    CharT    const *result,
    int            length
)
{
    parse_info<CharT const*> info = parse(candidate, sym);

#define correctly_matched hit == info.hit
#define correct_match_length unsigned(length) == info.length
#define correct_tail equal(candidate + (hit?1:0)*length, result)

    BOOST_TEST(correctly_matched);

    if (hit)
    {
        BOOST_TEST(correct_match_length);
        BOOST_TEST(correct_tail);
    }
    else
    {
        BOOST_TEST(correct_tail);
    }
}

template <typename T>
struct store_action
{
    store_action(T const &v) : value(v) {}
    void operator()(T &v) const { v = value; }
private:
    T const value;
};

template <typename T>
store_action<T>
store(T const &v)
{
    return v;
}

template <typename T>
struct check_action
{
    check_action(T const &v) : value(v) {}

#define correct_value_stored (v==value)
    void operator()(T const &v) const { BOOST_TEST(correct_value_stored); }
private:
    T const value;
};

template <typename T>
check_action<T>
docheck(T const &v)
{
    return v;
}


static void
default_constructible()
{   // this actually a compile time test
    symbols<>                     ns1;
    symbols<int, wchar_t>         ws1;
    symbols<std::string, char>    ns2;
    symbols<std::string, wchar_t> ws2;

    (void)ns1; (void)ws1; (void)ns2; (void)ws2;
}

static void
narrow_match_tests()
{
    symbols<>   sym;
    sym = "pineapple", "orange", "banana", "applepie", "apple";

    docheck(sym, "pineapple", true, "", 9);
    docheck(sym, "orange", true, "", 6);
    docheck(sym, "banana", true, "", 6);
    docheck(sym, "apple", true, "", 5);
    docheck(sym, "pizza", false, "pizza", -1);
    docheck(sym, "steak", false, "steak", -1);
    docheck(sym, "applepie", true, "", 8);
    docheck(sym, "bananarama", true, "rama", 6);
    docheck(sym, "applet", true, "t", 5);
    docheck(sym, "applepi", true, "pi", 5);
    docheck(sym, "appl", false, "appl", -1);

    docheck(sym, "pineapplez", true, "z", 9);
    docheck(sym, "orangez", true, "z", 6);
    docheck(sym, "bananaz", true, "z", 6);
    docheck(sym, "applez", true, "z", 5);
    docheck(sym, "pizzaz", false, "pizzaz", -1);
    docheck(sym, "steakz", false, "steakz", -1);
    docheck(sym, "applepiez", true, "z", 8);
    docheck(sym, "bananaramaz", true, "ramaz", 6);
    docheck(sym, "appletz", true, "tz", 5);
    docheck(sym, "applepix", true, "pix", 5);
}

static void
narrow_copy_ctor_tests()
{
    symbols<>   sym;
    sym = "pineapple", "orange", "banana", "applepie", "apple";

    symbols<>   sym2(sym);
    docheck(sym2, "pineapple", true, "", 9);
    docheck(sym2, "pizza", false, "pizza", -1);
    docheck(sym2, "bananarama", true, "rama", 6);
}

static void
narrow_assigment_operator_tests()
{
    symbols<>   sym;
    sym = "pineapple", "orange", "banana", "applepie", "apple";

    symbols<>   sym2;
    sym2 = sym;

    docheck(sym2, "pineapple", true, "", 9);
    docheck(sym2, "pizza", false, "pizza", -1);
    docheck(sym2, "bananarama", true, "rama", 6);
}

static void
narrow_value_tests()
{   // also tests the add member functions
    symbols<>   sym;

    sym = "orange", "banana";
    sym.add("pineapple",1234);
    sym.add("lemon");

    parse("orange", sym[store(12345)]);
    parse("orange", sym[docheck(12345)]);
    parse("pineapple", sym[docheck(1234)]);
    parse("banana", sym[docheck(int())]);
    parse("lemon", sym[docheck(int())]);
}

static void
narrow_free_functions_tests()
{
    symbols<>   sym;

#define add_returned_non_null_value (res!=0)
#define add_returned_null (res==0)
#define find_returned_non_null_value (res!=0)
#define find_returned_null (res==0)

    int *res = add(sym,"pineapple");
    BOOST_TEST(add_returned_non_null_value);
    res = add(sym,"pineapple");
    BOOST_TEST(add_returned_null);

    res = find(sym, "pineapple");
    BOOST_TEST(find_returned_non_null_value);
    res = find(sym, "banana");
    BOOST_TEST(find_returned_null);
}

static void
wide_match_tests()
{
    symbols<int, wchar_t>   sym;
    sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";

    docheck(sym, L"pineapple", true, L"", 9);
    docheck(sym, L"orange", true, L"", 6);
    docheck(sym, L"banana", true, L"", 6);
    docheck(sym, L"apple", true, L"", 5);
    docheck(sym, L"pizza", false, L"pizza", -1);
    docheck(sym, L"steak", false, L"steak", -1);
    docheck(sym, L"applepie", true, L"", 8);
    docheck(sym, L"bananarama", true, L"rama", 6);
    docheck(sym, L"applet", true, L"t", 5);
    docheck(sym, L"applepi", true, L"pi", 5);
    docheck(sym, L"appl", false, L"appl", -1);

    docheck(sym, L"pineapplez", true, L"z", 9);
    docheck(sym, L"orangez", true, L"z", 6);
    docheck(sym, L"bananaz", true, L"z", 6);
    docheck(sym, L"applez", true, L"z", 5);
    docheck(sym, L"pizzaz", false, L"pizzaz", -1);
    docheck(sym, L"steakz", false, L"steakz", -1);
    docheck(sym, L"applepiez", true, L"z", 8);
    docheck(sym, L"bananaramaz", true, L"ramaz", 6);
    docheck(sym, L"appletz", true, L"tz", 5);
    docheck(sym, L"applepix", true, L"pix", 5);
}

static void
wide_copy_ctor_tests()
{
    symbols<int, wchar_t>   sym;
    sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";

    symbols<int, wchar_t>   sym2(sym);
    docheck(sym2, L"pineapple", true, L"", 9);
    docheck(sym2, L"pizza", false, L"pizza", -1);
    docheck(sym2, L"bananarama", true, L"rama", 6);
}

static void
wide_assigment_operator_tests()
{
    symbols<int, wchar_t>   sym;
    sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";

    symbols<int, wchar_t>   sym2;
    sym2 = sym;

    docheck(sym2, L"pineapple", true, L"", 9);
    docheck(sym2, L"pizza", false, L"pizza", -1);
    docheck(sym2, L"bananarama", true, L"rama", 6);
}

static void
wide_value_tests()
{   // also tests the add member functions
    symbols<int, wchar_t>   sym;

    sym = L"orange", L"banana";
    sym.add(L"pineapple",1234);
    sym.add(L"lemon");

    parse(L"orange", sym[store(12345)]);
    parse(L"orange", sym[docheck(12345)]);
    parse(L"pineapple", sym[docheck(1234)]);
    parse(L"banana", sym[docheck(int())]);
    parse(L"lemon", sym[docheck(int())]);
}

static void
wide_free_functions_tests()
{
    symbols<int, wchar_t>   sym;

    int *res = add(sym,L"pineapple");
    BOOST_TEST(add_returned_non_null_value);
    res = add(sym,L"pineapple");
    BOOST_TEST(add_returned_null);

    res = find(sym, L"pineapple");
    BOOST_TEST(find_returned_non_null_value);
    res = find(sym, L"banana");
    BOOST_TEST(find_returned_null);
}

static 
void free_add_find_functions_tests()
{
    symbols<> sym;
    BOOST_TEST(*add(sym, "a", 0) == 0);
    BOOST_TEST(*add(sym, "a2", 1) == 1);
    BOOST_TEST(find(sym, "a2"));
    BOOST_TEST(find(sym, "a"));
}

int
main()
{
    default_constructible();
    narrow_match_tests();
    narrow_copy_ctor_tests();
    narrow_assigment_operator_tests();
    narrow_value_tests();
    narrow_free_functions_tests();
    wide_match_tests();
    wide_copy_ctor_tests();
    wide_assigment_operator_tests();
    wide_value_tests();
    wide_free_functions_tests();
    free_add_find_functions_tests();
    return boost::report_errors();
}