You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import java.util.List;
class Invariant {
// x is invariant
void x_is_invariant_ok(int size) {
int i = 0, x;
if (size > 10) {
x = 10;
} else {
x = 20;
}
while (i < size + x) {
i++;
}
}
// x shouldn't be invariant since it can have two different values
// depending on whether the inner conditional is executed or not.
// Currently, we are getting a quadratic, rather than a linear,
// because the value of x at the true branch of the if statement is
// precisely analyzed to [0,4+min(1,size.ub)], rather than [0,5].
void formal_not_invariant_FP(int size, int x) {
int i = 0;
while (i < size + x) {
if (x > i) {
x = 0;
}
i++;
}
}
// x shouldn't be invariant since it can have two different values
// depending on whether the inner conditional is executed or not
// Currently, we are getting T because of a problem in InferBo, see
// T32798161
void local_not_invariant_FP(int size) {
int i = 0;
int x = 5;
while (i < size + x) {
if (x > i) {
x = 0;
}
i++;
}
}
// m will be invariant
void do_while_invariant(int m, int k) {
int i = 0;
do {
m = k;
i++;
} while (i < m);
}
// before, we were getting items.size()^2 since all functions were
// assumed to be impure
void list_size_invariant(List<?> items) {
for (int i = 0; i < items.size(); i++) {}
}
}