| 1 |
import java.util.BitSet; |
| 2 |
import org.checkerframework.common.value.qual.*; |
| 3 |
|
| 4 |
class Binaries { |
| 5 |
private BitSet bitmap; |
| 6 |
|
| 7 |
public void test() { |
| 8 |
int length = bitmap.length(); |
| 9 |
for (int i = 0, t = 0; i < length; i++) { |
| 10 |
t |= (bitmap.get(i) ? (1 << (7 - i % 8)) : 0); |
| 11 |
if (i % 8 == 7 || i == length - 1) { |
| 12 |
write(t); |
| 13 |
t = 0; |
| 14 |
} |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
void write(int t) {} |
| 19 |
|
| 20 |
// Test widenedUpperBound is working. |
| 21 |
public void loop(int c) { |
| 22 |
double v = 0; |
| 23 |
int decexp = 0; |
| 24 |
int seendot = 0; |
| 25 |
while (true) { |
| 26 |
if (c == '.' && seendot == 0) seendot = 1; |
| 27 |
else if ('0' <= c && c <= '9') { |
| 28 |
v = v * 10 + (c - '0'); |
| 29 |
decexp += seendot; |
| 30 |
} else break; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public void testIntRange( |
| 35 |
@IntVal({1, 2}) int values, |
| 36 |
@IntRange(from = 3, to = 4) int range1, |
| 37 |
@IntRange(from = 5, to = 20) int range2, |
| 38 |
@BottomVal int bottom, |
| 39 |
@UnknownVal int top) { |
| 40 |
|
| 41 |
/* IntRange + IntRange */ |
| 42 |
@IntRange(from = 8, to = 24) int a = range1 + range2; |
| 43 |
|
| 44 |
/* IntRange * IntVal */ |
| 45 |
@IntRange(from = 3, to = 8) int b = values * range1; |
| 46 |
|
| 47 |
/* IntRange * BottomVal */ |
| 48 |
int c = range1 * bottom; |
| 49 |
|
| 50 |
/* IntRange * UnknownVal */ |
| 51 |
@IntRange(from = 0) |
| 52 |
// :: error: (assignment.type.incompatible) |
| 53 |
int d = range1 + top; |
| 54 |
} |
| 55 |
|
| 56 |
public void add() { |
| 57 |
int a = 1; |
| 58 |
if (true) { |
| 59 |
a = 2; |
| 60 |
} |
| 61 |
@IntVal({3, 4}) int b = a + 2; |
| 62 |
|
| 63 |
double c = 1.0; |
| 64 |
if (true) { |
| 65 |
c = 2.0; |
| 66 |
} |
| 67 |
@DoubleVal({3.0, 4.0}) double d = c + 2; |
| 68 |
|
| 69 |
char e = '1'; |
| 70 |
if (true) { |
| 71 |
e = '2'; |
| 72 |
} |
| 73 |
@IntVal({'3', '4'}) char f = (char) (e + 2); |
| 74 |
|
| 75 |
String g = "A"; |
| 76 |
if (true) { |
| 77 |
g = "B"; |
| 78 |
} |
| 79 |
@StringVal({"AC", "BC"}) String h = g + "C"; |
| 80 |
} |
| 81 |
|
| 82 |
public void subtract() { |
| 83 |
int a = 1; |
| 84 |
if (true) { |
| 85 |
a = 2; |
| 86 |
} |
| 87 |
@IntVal({-1, 0}) int b = a - 2; |
| 88 |
|
| 89 |
double c = 1.0; |
| 90 |
if (true) { |
| 91 |
c = 2.0; |
| 92 |
} |
| 93 |
@DoubleVal({-1.0, 0.0}) double d = c - 2; |
| 94 |
|
| 95 |
char e = '2'; |
| 96 |
if (true) { |
| 97 |
e = '3'; |
| 98 |
} |
| 99 |
|
| 100 |
@IntVal({'0', '1'}) char f = (char) (e - 2); |
| 101 |
} |
| 102 |
|
| 103 |
public void multiply() { |
| 104 |
int a = 1; |
| 105 |
if (true) { |
| 106 |
a = 2; |
| 107 |
} |
| 108 |
@IntVal({2, 4}) int b = a * 2; |
| 109 |
|
| 110 |
double c = 1.0; |
| 111 |
if (true) { |
| 112 |
c = 2.0; |
| 113 |
} |
| 114 |
@DoubleVal({2.0, 4.0}) double d = (double) (c * 2); |
| 115 |
|
| 116 |
char e = (char) 25; |
| 117 |
if (true) { |
| 118 |
|
| 119 |
e = (char) 26; |
| 120 |
} |
| 121 |
|
| 122 |
@IntVal({'2', '4'}) char f = (char) (e * 2); |
| 123 |
|
| 124 |
@DoubleVal(0.75) float g = 1 * 0.75f; |
| 125 |
} |
| 126 |
|
| 127 |
public void divide() { |
| 128 |
int a = 2; |
| 129 |
if (true) { |
| 130 |
a = 4; |
| 131 |
} |
| 132 |
@IntVal({1, 2}) int b = a / 2; |
| 133 |
|
| 134 |
double c = 1.0; |
| 135 |
if (true) { |
| 136 |
c = 2.0; |
| 137 |
} |
| 138 |
@DoubleVal({0.5, 1.0}) double d = c / 2; |
| 139 |
|
| 140 |
char e = (char) 96; |
| 141 |
if (true) { |
| 142 |
e = (char) 98; |
| 143 |
} |
| 144 |
|
| 145 |
@IntVal({'0', '1'}) char f = (char) (e / 2); |
| 146 |
|
| 147 |
@IntVal(0) int g = 2 / 3; |
| 148 |
@IntVal(0) int h = (Integer.MAX_VALUE - 1) / Integer.MAX_VALUE; |
| 149 |
@IntVal(0) long l = (Long.MAX_VALUE - 1) / Long.MAX_VALUE; |
| 150 |
} |
| 151 |
|
| 152 |
public void remainder() { |
| 153 |
int a = 4; |
| 154 |
if (true) { |
| 155 |
a = 5; |
| 156 |
} |
| 157 |
@IntVal({1, 2}) int b = a % 3; |
| 158 |
|
| 159 |
double c = 4.0; |
| 160 |
if (true) { |
| 161 |
c = 5.0; |
| 162 |
} |
| 163 |
@DoubleVal({1.0, 2.0}) double d = c % 3; |
| 164 |
|
| 165 |
char e = (char) 98; |
| 166 |
if (true) { |
| 167 |
e = (char) 99; |
| 168 |
} |
| 169 |
|
| 170 |
@IntVal({'0', '1'}) char f = (char) (e % 50); |
| 171 |
} |
| 172 |
|
| 173 |
public boolean flag = true; |
| 174 |
|
| 175 |
public void and() { |
| 176 |
boolean a = true; |
| 177 |
if (flag) { |
| 178 |
a = false; |
| 179 |
} |
| 180 |
// :: error: (assignment.type.incompatible) |
| 181 |
@BoolVal({true}) boolean b = a & true; |
| 182 |
|
| 183 |
int c = 4; |
| 184 |
if (true) { |
| 185 |
c = 5; |
| 186 |
} |
| 187 |
@IntVal({0, 1}) int d = c & 3; |
| 188 |
|
| 189 |
char e = (char) 48; |
| 190 |
if (true) { |
| 191 |
|
| 192 |
e = (char) 51; |
| 193 |
} |
| 194 |
|
| 195 |
@IntVal({'0', '2'}) char f = (char) (e & 50); |
| 196 |
} |
| 197 |
|
| 198 |
public void or() { |
| 199 |
boolean a = true; |
| 200 |
if (true) { |
| 201 |
a = false; |
| 202 |
} |
| 203 |
// TODO: we could detect this case |
| 204 |
// :: error: (assignment.type.incompatible) |
| 205 |
@BoolVal({true}) boolean b = a | true; |
| 206 |
|
| 207 |
int c = 4; |
| 208 |
if (true) { |
| 209 |
c = 5; |
| 210 |
} |
| 211 |
@IntVal({7}) int d = c | 3; |
| 212 |
|
| 213 |
char e = (char) 48; |
| 214 |
if (true) { |
| 215 |
e = (char) 51; |
| 216 |
} |
| 217 |
|
| 218 |
@IntVal({'1', '3'}) char f = (char) (e | 1); |
| 219 |
} |
| 220 |
|
| 221 |
public void xor() { |
| 222 |
boolean a = true; |
| 223 |
if (true) { |
| 224 |
a = false; |
| 225 |
} |
| 226 |
// :: error: (assignment.type.incompatible) |
| 227 |
@BoolVal({true}) boolean b = a ^ true; |
| 228 |
|
| 229 |
int c = 4; |
| 230 |
if (true) { |
| 231 |
c = 5; |
| 232 |
} |
| 233 |
@IntVal({7, 6}) int d = c ^ 3; |
| 234 |
|
| 235 |
char e = (char) 48; |
| 236 |
if (true) { |
| 237 |
|
| 238 |
e = (char) 51; |
| 239 |
} |
| 240 |
|
| 241 |
@IntVal({'1', '2'}) char f = (char) (e ^ 1); |
| 242 |
} |
| 243 |
|
| 244 |
public void boolAnd() { |
| 245 |
@BoolVal({false}) boolean a = true && false; |
| 246 |
@BoolVal({true}) boolean b = false || true; |
| 247 |
} |
| 248 |
|
| 249 |
public void conditionals() { |
| 250 |
@BoolVal({false}) boolean a = 1.0f == '1'; |
| 251 |
@BoolVal({true}) boolean b = 1 != 2.0; |
| 252 |
@BoolVal({true}) boolean c = 1 > 0.5; |
| 253 |
@BoolVal({true}) boolean d = 1 >= 1.0; |
| 254 |
@BoolVal({true}) boolean e = 1 < 1.1f; |
| 255 |
@BoolVal({true}) boolean f = (char) 2 <= 2.0; |
| 256 |
@IntVal('!') Character BANG = '!'; |
| 257 |
@BoolVal(true) boolean g = (BANG == '!'); |
| 258 |
char bangChar = '!'; |
| 259 |
@BoolVal(true) boolean h = (BANG == bangChar); |
| 260 |
|
| 261 |
Character bang = '!'; |
| 262 |
// Reference equalitiy is used |
| 263 |
// :: error: (assignment.type.incompatible) |
| 264 |
@BoolVal(false) boolean i = (BANG == bang); |
| 265 |
} |
| 266 |
|
| 267 |
public void loop() throws InterruptedException { |
| 268 |
int spurious_count = 0; |
| 269 |
while (true) { |
| 270 |
wait(); |
| 271 |
if (System.currentTimeMillis() == 0) { |
| 272 |
spurious_count++; |
| 273 |
if (spurious_count > 1024) { |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
public void shifts() { |
| 281 |
int a = -8; |
| 282 |
if (true) { |
| 283 |
a = 4; |
| 284 |
} |
| 285 |
@IntVal({1, -2}) int b = a >> 2; |
| 286 |
|
| 287 |
int c = 1; |
| 288 |
if (true) { |
| 289 |
c = 2; |
| 290 |
} |
| 291 |
@IntVal({4, 8}) int d = c << 2; |
| 292 |
|
| 293 |
int e = -8; |
| 294 |
if (true) { |
| 295 |
e = 4; |
| 296 |
} |
| 297 |
@IntVal({Integer.MAX_VALUE / 2 - 1, 1}) int f = e >>> 2; |
| 298 |
|
| 299 |
char g = (char) 24; |
| 300 |
if (true) { |
| 301 |
g = (char) 25; |
| 302 |
} |
| 303 |
|
| 304 |
@IntVal({'0', '2'}) char h = (char) (g << 1); |
| 305 |
} |
| 306 |
|
| 307 |
public void chains() { |
| 308 |
char a = 2; |
| 309 |
int b = 3; |
| 310 |
double c = 5; |
| 311 |
|
| 312 |
@DoubleVal({1}) double d = a * b - c; |
| 313 |
|
| 314 |
@DoubleVal({3}) double e = a * c - 2 * b - (char) 1; |
| 315 |
} |
| 316 |
|
| 317 |
public void compareWithNull() { |
| 318 |
String s = "1"; |
| 319 |
// TODO |
| 320 |
// :: error: (assignment.type.incompatible) |
| 321 |
@BoolVal(true) boolean b = (s != null); |
| 322 |
} |
| 323 |
|
| 324 |
public void nullConcatenation(@StringVal({"a", "b"}) String arg) { |
| 325 |
String n1 = null; |
| 326 |
String n2 = "null"; |
| 327 |
String k = "const"; |
| 328 |
|
| 329 |
// @StringVal("nullnull") String a1 = n1 + null; |
| 330 |
@StringVal("nullnull") String a2 = n1 + "null"; |
| 331 |
// @StringVal("nullnull") String a3 = n1 + n1; |
| 332 |
@StringVal("nullnull") String a4 = n1 + n2; |
| 333 |
@StringVal("nullconst") String a5 = n1 + k; |
| 334 |
@StringVal("nullconst") String a6 = n1 + "const"; |
| 335 |
|
| 336 |
@StringVal("nullnull") String b1 = n2 + null; |
| 337 |
@StringVal("nullnull") String b2 = n2 + "null"; |
| 338 |
@StringVal("null") String b3 = n2 + n1; |
| 339 |
@StringVal("nullnull") String b4 = n2 + n2; |
| 340 |
@StringVal({"nullconst", "nullnull"}) String b5 = n2 + k; |
| 341 |
@StringVal("nullconst") String b6 = n2 + "const"; |
| 342 |
|
| 343 |
@StringVal({"anull", "bnull"}) String c1 = arg + null; |
| 344 |
@StringVal({"anull", "bnull"}) String c2 = arg + "null"; |
| 345 |
@StringVal({"anull", "bnull"}) String c3 = arg + n1; |
| 346 |
@StringVal({"anull", "bnull"}) String c4 = arg + n2; |
| 347 |
@StringVal({"aconst", "bconst"}) String c5 = arg + k; |
| 348 |
@StringVal({"aconst", "bconst"}) String c6 = arg + "const"; |
| 349 |
@StringVal({"a2147483647", "b2147483647"}) String c7 = arg + Integer.MAX_VALUE; |
| 350 |
} |
| 351 |
|
| 352 |
public void conditionalComparisions() { |
| 353 |
@BoolVal(true) boolean a1 = true || false; |
| 354 |
@BoolVal(true) boolean a2 = true || true; |
| 355 |
@BoolVal(false) boolean a3 = false || false; |
| 356 |
@BoolVal(true) boolean a4 = false || true; |
| 357 |
|
| 358 |
@BoolVal(false) boolean a5 = true && false; |
| 359 |
@BoolVal(true) boolean a6 = true && true; |
| 360 |
@BoolVal(false) boolean a7 = false && false; |
| 361 |
@BoolVal(false) boolean a8 = false && true; |
| 362 |
|
| 363 |
boolean unknown = flag ? true : false; |
| 364 |
@BoolVal(true) boolean a9 = true || unknown; |
| 365 |
@BoolVal(true) boolean a11 = unknown || true; |
| 366 |
// :: error: (assignment.type.incompatible) |
| 367 |
@BoolVal(false) boolean a12 = unknown || false; |
| 368 |
// :: error: (assignment.type.incompatible) |
| 369 |
@BoolVal(true) boolean a13 = false || unknown; |
| 370 |
|
| 371 |
// :: error: (assignment.type.incompatible) |
| 372 |
@BoolVal(true) boolean a14 = true && unknown; |
| 373 |
// :: error: (assignment.type.incompatible) |
| 374 |
@BoolVal(true) boolean a15 = unknown && true; |
| 375 |
@BoolVal(false) boolean a16 = unknown && false; |
| 376 |
@BoolVal(false) boolean a17 = false && unknown; |
| 377 |
} |
| 378 |
} |