Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package testSuite;

import liquidjava.specification.Refinement;

// Facts about the int locals a loop changes that hold on every iteration are inferred (#537)
@SuppressWarnings("unused")
public class CorrectLoopInvariants {
static boolean next() {
return true;
}

static int read(byte[] buffer, @Refinement("_ >= 0") int offset, int length) {
return 0;
}

static void use(@Refinement("_ >= 0") int x) {}

static void counter() {
for (int i = 0; i < 10; i++) {
use(i);
}
}

static void accumulator() {
int total = 0;
while (total < 1024) {
use(total);
total = total + 1;
}
}

static void fill(byte[] buffer, int limit) {
int total = 0;
while (total < limit) {
int n = read(buffer, total, limit - total);
if (n < 0)
break;
total += n;
}
}

static void symbolicStart(int k) {
if (k < 0)
return;
for (int i = k; i < 100; i++) {
use(i);
}
}

static void nested() {
for (int i = 0; i < 10; i++) {
for (int j = i; j < 10; j++) {
use(i);
use(j);
}
}
}

// a continue goes on to the update
static void continueInFor() {
for (int i = 0; i < 10; i++) {
if (next())
continue;
use(i);
}
}

static void continueInWhile() {
int i = 0;
while (i < 10) {
use(i);
if (next()) {
i++;
continue;
}
i = i + 2;
}
}

static void continueInDoWhile() {
int i = 0;
do {
use(i);
if (next()) {
i++;
continue;
}
i = i + 2;
} while (i < 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package testSuite;

import liquidjava.specification.Refinement;

// Only facts that hold before a loop and are kept by every iteration are inferred for its int locals (#537)
@SuppressWarnings("unused")
public class ErrorLoopInvariants {
static boolean next() {
return true;
}

static void use(@Refinement("_ >= 0") int x) {}

static void atMostTen(@Refinement("_ <= 10") int x) {}

static void decrementing() {
for (int i = 10; i > -5; i--) {
use(i); // Expect: Refinement Error
}
}

static void resetInBody() {
int i = 0;
while (i < 10) {
use(i); // Expect: Refinement Error
i = i + 1;
if (next())
i = -1;
}
}

static void decreasedOnSomePath() {
int i = 0;
while (i < 10) {
use(i); // Expect: Refinement Error
if (next())
i = i - 2;
else
i++;
}
}

// i < 10 does not give i <= 10 at the next iteration, as i grows by 3
static void boundNotKept() {
int i = 0;
while (i < 10) {
i = i + 3;
atMostTen(i); // Expect: Refinement Error
}
}

static void decreasedBeforeContinue() {
int i = 0;
while (i < 10) {
use(i); // Expect: Refinement Error
if (next()) {
i = i - 1;
continue;
}
i++;
}
}

// the continue skips the use but not the update, which starts the next iteration from i - 5 + 1
static void decreasedBeforeContinueInFor() {
for (int i = 0; i < 10; i++) {
if (next()) {
i = i - 5;
continue;
}
use(i); // Expect: Refinement Error
}
}

static void decreasedInInnerLoop() {
int i = 0;
while (i < 10) {
use(i); // Expect: Refinement Error
for (int j = 0; j < 5; j++) {
i = i - 1;
}
i++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testSuite.classes.loop_list_index_correct;

import liquidjava.specification.ExternalRefinementsFor;
import liquidjava.specification.Ghost;
import liquidjava.specification.Refinement;
import liquidjava.specification.StateRefinement;

@ExternalRefinementsFor("java.util.ArrayList")
@Ghost("int size")
public interface ArrayListRefinements<E> {
@StateRefinement(to = "size(this) == 0")
void ArrayList();

@StateRefinement(to = "size(this) == size(old(this)) + 1")
boolean add(E e);

E get(@Refinement("_ >= 0 && _ < size(this)") int index);

@Refinement("_ == size(this) && _ >= 0")
int size();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testSuite.classes.loop_list_index_correct;

import java.util.ArrayList;

// A loop over the indices of a list knows the index is non-negative (#479)
public class Client {
public static void main(String[] args) {
ArrayList<String> l = new ArrayList<>();
l.add("a");
l.add("b");
for (int i = 0; i < l.size(); i++) {
String s = l.get(i);
}
}
}
Loading
Loading