File: test_assert.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (94 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download | duplicates (18)
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
///////////////////////////////////////////////////////////////////////////////
// test_assert.cpp
//
//  Copyright 2008 Eric Niebler. 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 <iostream>
#include <boost/xpressive/xpressive_static.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <boost/test/unit_test.hpp>

using namespace boost::xpressive;

bool three_or_six(ssub_match const &sub)
{
    return sub.length() == 3 || sub.length() == 6;
}

///////////////////////////////////////////////////////////////////////////////
// test1
//  simple custom assert that checks the length of a matched sub-expression
void test1()
{
    std::string str("foo barbaz fink");
    // match words of 3 characters or 6 characters.
    sregex rx = (bow >> +_w >> eow)[ check(&three_or_six) ] ;

    sregex_iterator first(str.begin(), str.end(), rx), last;
    BOOST_CHECK_EQUAL(std::distance(first, last), 2);
}

///////////////////////////////////////////////////////////////////////////////
// test2
//  same as above, but using a lambda
void test2()
{
    std::string str("foo barbaz fink");
    // match words of 3 characters or 6 characters.
    sregex rx = (bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ] ;

    sregex_iterator first(str.begin(), str.end(), rx), last;
    BOOST_CHECK_EQUAL(std::distance(first, last), 2);
}

///////////////////////////////////////////////////////////////////////////////
// test3
//  more complicated use of custom assertions to validate a date
void test3()
{
    int const days_per_month[] =
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31};

    mark_tag month(1), day(2);
    // find a valid date of the form month/day/year.
    sregex date =
        (
            // Month must be between 1 and 12 inclusive
            (month= _d >> !_d)     [ check(as<int>(_) >= 1
                                        && as<int>(_) <= 12) ]
        >>  '/'
            // Day must be between 1 and 31 inclusive
        >>  (day=   _d >> !_d)     [ check(as<int>(_) >= 1
                                        && as<int>(_) <= 31) ]
        >>  '/'
            // Only consider years between 1970 and 2038
        >>  (_d >> _d >> _d >> _d) [ check(as<int>(_) >= 1970
                                        && as<int>(_) <= 2038) ]
        )
        // Ensure the month actually has that many days.
        [ check( ref(days_per_month)[as<int>(month)-1] >= as<int>(day) ) ]
    ;

    smatch what;
    std::string str("99/99/9999 2/30/2006 2/28/2006");

    BOOST_REQUIRE(regex_search(str, what, date));
    BOOST_CHECK_EQUAL(what[0], "2/28/2006");
}

using namespace boost::unit_test;

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    test_suite *test = BOOST_TEST_SUITE("test_assert");
    test->add(BOOST_TEST_CASE(&test1));
    test->add(BOOST_TEST_CASE(&test2));
    test->add(BOOST_TEST_CASE(&test3));
    return test;
}