String: Implement String != regex operator
[libacl.git] / test / string_regex.cpp
1 /*******************************************************
2  *  Unit test for the String class
3  *
4  * test regular expression operations
5  ******************************************************
6  *
7  */
8
9 #include "String.h"
10 #include <assert.h>
11
12 int main()
13 {
14    // TODO: test regex constructors
15
16    // A simple regular expression
17    regex nr("[0-9]+");
18
19    // Regular expression matching
20    String s1 = "abcdef";
21    String s2 = "abd123def";
22
23    std::cout << "Regular expression matching.\n";
24    assert((s1 == nr) == false);
25    assert((s2 == nr) == true);
26
27    assert((s1 != nr) == true);
28    assert((s2 != nr) == false);
29
30    // Select a substring with a regex
31
32    String match;
33
34    match = s2(nr);
35    std::cout << "The matching part of \"" << s2 << "\" is \"" << match << "\"\n";
36    assert(match == "123");
37
38    // Test a regex with an error
39
40    bool exception_caught = false;
41
42    try
43    {
44       regex wrong_nr("[0-9+");
45       std::cout << "No regex error detected.\n";
46    }
47    catch (RegexException rexc)
48    {
49       std::cout << "Regex exception: " << rexc.what() << "\n";
50       exception_caught = true;
51    }
52
53    assert(exception_caught);
54
55    return 0;
56 }
57