File: test_regexp.cpp

package info (click to toggle)
libwibble 0.1.9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 500 kB
  • ctags: 1,183
  • sloc: cpp: 5,760; sh: 113; makefile: 71
file content (82 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download
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
#include <wibble/config.h>
#include <wibble/regexp.h>
using namespace std;
using namespace wibble;

#include <wibble/tests/tut-wibble.h>

namespace tut {

struct regexp_shar {};
TESTGRP( regexp );

// Test normal regular expression matching
template<> template<>
void to::test< 1 >() {
	Regexp re("^fo\\+bar()$");
	ensure(re.match("fobar()"));
	ensure(re.match("foobar()"));
	ensure(re.match("fooobar()"));
	ensure(!re.match("fbar()"));
	ensure(!re.match(" foobar()"));
	ensure(!re.match("foobar() "));
}

// Test extended regular expression matching
template<> template<>
void to::test< 2 >() {
	ERegexp re("^fo+bar()$");
	ensure(re.match("fobar"));
	ensure(re.match("foobar"));
	ensure(re.match("fooobar"));
	ensure(!re.match("fbar"));
	ensure(!re.match(" foobar"));
	ensure(!re.match("foobar "));
}

// Test capturing results
template<> template<>
void to::test< 3 >() {
	ERegexp re("^f(o+)bar([0-9]*)$", 3);
	ensure(re.match("fobar"));
	ensure_equals(re[0], string("fobar"));
	ensure_equals(re[1], string("o"));
	ensure_equals(re[2], string(""));
	ensure_equals(re.matchStart(0), 0u);
	ensure_equals(re.matchEnd(0), 5u);
	ensure_equals(re.matchLength(0), 5u);
	ensure_equals(re.matchStart(1), 1u);
	ensure_equals(re.matchEnd(1), 2u);
	ensure_equals(re.matchLength(1), 1u);

	ensure(re.match("foobar42"));
	ensure_equals(re[0], string("foobar42"));
	ensure_equals(re[1], string("oo"));
	ensure_equals(re[2], string("42"));
}

// Test tokenizer
template<> template<>
void to::test< 4 >() {
	string str("antani blinda la supercazzola!");
	Tokenizer tok(str, "[a-z]+", REG_EXTENDED);
	Tokenizer::const_iterator i = tok.begin();

	ensure(i != tok.end());
	ensure_equals(*i, "antani");
	++i;
	ensure(i != tok.end());
	ensure_equals(*i, "blinda");
	++i;
	ensure(i != tok.end());
	ensure_equals(*i, "la");
	++i;
	ensure(i != tok.end());
	ensure_equals(*i, "supercazzola");
	++i;
	ensure(i == tok.end());
}

}

// vim:set ts=4 sw=4: