File: LessThanValue.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (120 lines) | stat: -rw-r--r-- 3,757 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
116
117
118
119
120
package lessthan;

import org.checkerframework.checker.index.qual.IndexFor;
import org.checkerframework.checker.index.qual.LessThan;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.common.value.qual.*;

// Test for LessThanChecker
public class LessThanValue {

    void subtyping(int x, int y, @LessThan({"#1", "#2"}) int a, @LessThan("#1") int b) {
        @LessThan("x") int q = a;
        @LessThan({"x", "y"})
        // :: error: (assignment.type.incompatible)
        int r = b;
    }

    public static boolean flag;

    void lub(int x, int y, @LessThan({"#1", "#2"}) int a, @LessThan("#1") int b) {
        @LessThan("x") int r = flag ? a : b;
        @LessThan({"x", "y"})
        // :: error: (assignment.type.incompatible)
        int s = flag ? a : b;
    }

    void transitive(int a, int b, int c) {
        if (a < b) {
            if (b < c) {
                @LessThan("c") int x = a;
            }
        }
    }

    void calls() {
        isLessThan(0, 1);
        isLessThanOrEqual(0, 0);
    }

    void isLessThan(@LessThan("#2") @NonNegative int start, int end) {
        @NonNegative int x = end - start - 1;
        @Positive int y = end - start;
    }

    @NonNegative int isLessThanOrEqual(@LessThan("#2 + 1") @NonNegative int start, int end) {
        return end - start;
    }

    public void setMaximumItemCount(int maximum) {
        if (maximum < 0) {
            throw new IllegalArgumentException("Negative 'maximum' argument.");
        }
        int count = getCount();
        if (count > maximum) {
            @Positive int y = count - maximum;
            @NonNegative int deleteIndex = count - maximum - 1;
        }
    }

    int getCount() {
        throw new RuntimeException();
    }

    void method(@NonNegative int m) {
        boolean[] has_modulus = new boolean[m];
        @LessThan("m") int x = foo(m);
        @IndexFor("has_modulus") int rem = foo(m);
    }

    @LessThan("#1") @NonNegative int foo(int in) {
        throw new RuntimeException();
    }

    void test(int maximum, int count) {
        if (maximum < 0) {
            throw new IllegalArgumentException("Negative 'maximum' argument.");
        }
        if (count > maximum) {
            int deleteIndex = count - maximum - 1;
            // TODO: shouldn't error
            // :: error: (argument.type.incompatible)
            isLessThanOrEqual(0, deleteIndex);
        }
    }

    void count(int count) {
        if (count > 0) {
            if (count % 2 == 1) {

            } else {
                // TODO: improve value checker
                // :: error: (assignment.type.incompatible)
                @IntRange(from = 0) int countDivMinus = count / 2 - 1;
                // Reasign to update the value in the store.
                countDivMinus = countDivMinus;
                // :: error: (argument.type.incompatible)
                isLessThan(0, countDivMinus);
                isLessThanOrEqual(0, countDivMinus);
            }
        }
    }

    static @NonNegative @LessThan("#2 + 1") int expandedCapacity(
            @NonNegative int oldCapacity, @NonNegative int minCapacity) {
        if (minCapacity < 0) {
            throw new AssertionError("cannot store more than MAX_VALUE elements");
        }
        // careful of overflow!
        int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; // expand by %50
        if (newCapacity < minCapacity) {
            newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
        }
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
            // guaranteed to be >= newCapacity
        }
        return newCapacity;
    }
}