File: GroupCounts.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (115 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download | duplicates (3)
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.regex.RegexUtil;
import org.checkerframework.checker.regex.qual.Regex;

public class GroupCounts {
    void testGroupCount() {
        @Regex(0) String s1 = "abc";
        @Regex(1) String s2 = "(abc)";
        @Regex(2) String s3 = "()(abc)";
        @Regex(3) String s4 = "(abc())()";
        @Regex(4) String s5 = "((((abc))))";

        @Regex(0) String s7 = "(abc)";
        @Regex String s9 = "()()(())";
        @Regex(2) String s10 = "()()(())";
        @Regex(3) String s11 = "()()(())";

        // :: error: (assignment.type.incompatible)
        @Regex(2) String s6 = "nonregex("; // error
        // :: error: (assignment.type.incompatible)
        @Regex(1) String s8 = "abc"; // error
        // :: error: (assignment.type.incompatible)
        @Regex(3) String s12 = "()()"; // error
        // :: error: (assignment.type.incompatible)
        @Regex(4) String s13 = "(())()"; // error
    }

    void testPatternCompileGroupCount(@Regex String r, @Regex(3) String r3, @Regex(5) String r5) {
        @Regex(5) Pattern p1 = Pattern.compile(r5);
        @Regex Pattern p2 = Pattern.compile(r5);
        @Regex Pattern p3 = Pattern.compile(r);

        // :: error: (assignment.type.incompatible)
        @Regex(6) Pattern p4 = Pattern.compile(r5); // error
        // :: error: (assignment.type.incompatible)
        @Regex(6) Pattern p5 = Pattern.compile(r3); // error

        // Make sure Pattern.compile still works when passed an @UnknownRegex String
        // that's actually a regex, with the warning suppressed.
        @SuppressWarnings("regex:argument.type.incompatible")
        Pattern p6 = Pattern.compile("(" + r + ")");
    }

    void testConcatenationGroupCount(@Regex String r, @Regex(3) String r3, @Regex(5) String r5) {
        @Regex(0) String s1 = r + r;
        @Regex(3) String s2 = r + r3;
        @Regex(8) String s3 = r3 + r5;

        // :: error: (assignment.type.incompatible)
        @Regex(1) String s4 = r + r;
        // :: error: (assignment.type.incompatible)
        @Regex(4) String s5 = r + r3;
        // :: error: (assignment.type.incompatible)
        @Regex(9) String s6 = r3 + r5;
    }

    void testCompoundConcatenationWithGroups(
            @Regex String s0, @Regex(1) String s1, @Regex(3) String s3) {
        s0 += s0;
        @Regex String test0 = s0;
        // :: error: (assignment.type.incompatible)
        @Regex(1) String test01 = s0;

        s0 += s1;
        @Regex(1) String test1 = s0;
        // :: error: (assignment.type.incompatible)
        @Regex(2) String test12 = s0;

        s1 += s3;
        @Regex(4) String test4 = s1;
        // :: error: (assignment.type.incompatible)
        @Regex(5) String test45 = s1;
    }

    void testAsRegexGroupCounts(String s) {
        @Regex String test1 = RegexUtil.asRegex(s);
        // :: error: (assignment.type.incompatible)
        @Regex(1) String test2 = RegexUtil.asRegex(s);

        @Regex(3) String test3 = RegexUtil.asRegex(s, 3);
        // :: error: (assignment.type.incompatible)
        @Regex(4) String test4 = RegexUtil.asRegex(s, 3);
    }

    void testMatcherGroupCounts(
            @Regex Matcher m0, @Regex(1) Matcher m1, @Regex(4) Matcher m4, int n) {
        m0.end(0);
        m0.group(0);
        m0.start(0);

        // :: error: (group.count.invalid)
        m0.end(1);
        // :: error: (group.count.invalid)
        m0.group(1);
        // :: error: (group.count.invalid)
        m0.start(1);

        m1.start(0);
        m1.start(1);

        // :: error: (group.count.invalid)
        m1.start(2);

        m4.start(0);
        m4.start(2);
        m4.start(4);

        // :: error: (group.count.invalid)
        m4.start(5);

        // :: warning: (group.count.unknown)
        m0.start(n);
    }
}