summaryrefslogtreecommitdiff
path: root/src/regex/RegexTest.java
blob: f149d6231a411cd58b3a3a19cb92ef2697569176 (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
/*
 * provide names and student id numbers here
 */

package regex;

import dk.brics.automaton.RegExp;
import dk.brics.automaton.RunAutomaton;

public class RegexTest {
    
    private final RunAutomaton r;

    public RegexTest(String regex) {
    	System.out.println("regular expression = " + regex);
        r =  new RunAutomaton(new RegExp(regex).toAutomaton());
    }
        
    public long dfaMatch(String input, int index) {
        long start = System.nanoTime();
        int length = r.run(input, index);
        long end = System.nanoTime();
        
        if (length == -1) {
            System.out.println("No match found!");
        } else {
            String s = input.substring(index, index + length);
            System.out.println("Found: " + s);
        }
        
        return end - start;
    }
        
    void runTest(String input, int index) {
    	System.out.println("input string = " + input);
    	System.out.println("index = " + index);
    	
        long dfaMatchTime = dfaMatch(input, index);
        System.out.println("dfaMatchTime " + dfaMatchTime);
    }

    private void check(String input, boolean expectOk) {
        boolean accepted = r.run(input);
        System.out.println("Testing input: " + input);
        if (expectOk && !accepted || !expectOk && accepted) {
            throw new RuntimeException("Unexpected result for " + input);
        }
    }

    void assertOk(String input) {
        check(input, true);
    }

    void assertFail(String input) {
        check(input, false);
    }

    static void checkId() {
        RegexTest reId = new RegexTest("[a-z][a-z0-9]*");
        reId.assertOk("id");
        reId.assertOk("d0d");
        reId.assertFail("Id");
        reId.assertFail("0");
        reId.assertFail("0d");
        reId.assertFail("aD");
        System.out.println();
    }
    
    static void checkNAT() {
        RegexTest reNAT = new RegexTest("[0]|[1-9][0-9]*");
        reNAT.assertOk("2");
        reNAT.assertOk("14451");
        reNAT.assertFail("Id");
        reNAT.assertFail("-145711");
        reNAT.assertFail("01");
        System.out.println();
    }

    static void checkString() {
        // String           ::= (UnescapedChar | "\" EscapedChar)*
        // UnescapedChar    ::= Char - ["] = "\"
        //                      (* All Unicode chars but quote and backslash *)
        // EscapedChar      ::= ["] | "\"
        RegexTest reString = new RegexTest("([^\\\"\\\\]|\\\\[\\\"\\\\])*");
        reString.assertOk("");
        reString.assertOk("abc");
        reString.assertOk("a\\\"b\\\"c");
        reString.assertOk("\\\"");
        reString.assertFail("a\"b\"c");
        reString.assertFail("\"");
        reString.assertFail("\\");
        reString.assertFail("\\x");
        reString.assertFail("\\\\\"");
        System.out.println();
    }

    public static void main(String[] args) {
        checkId();
        checkNAT();
        checkString();
        System.out.println("Passed.");
    }
}