test.py 1.22 KB
Newer Older
1

2 3
from random import randint

4 5 6 7
def f(x):
  #@ ensures result > x
  return x+1

8 9
def swap(a, i, j):
  #@ requires 0 <= i < len(a) and 0 <= j < len(a)
10
  #@ ensures  a[i] == old(a[j])
11 12 13
  t = a[i]
  a[i] = a[j]
  a[j] = t
14

15 16 17 18 19 20
def t10():
  s = 0
  for i in range(0, 10):
    #@ invariant 2 * s == i * (i-1)
    s = s + i
  #@ assert s == 55
21

22 23 24
i = randint(0, 10)
#@ assert 0 <= i <= 10

25 26 27
a = 0
b = 1
while b < 100:
28 29 30
  #@ invariant b >= a >= 0
  #@ invariant b >= 1
  #@ variant 200 - b - a
31 32
  print(a)
  b = a+b
33
  #@ assert b >= 1
34
  a = b-a
35 36

# lists
37
l = range(0, 10)
38
#@ assert forall i. 0 <= i < 10 -> l[i] >= 0
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
39
l[2] = 42
40
#@ assert l[2] == 42
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
41 42
i = 0
while i < 10:
43 44 45
  #@ invariant 0 <= i
  #@ invariant forall j. 0 <= j < i -> l[j] == 0
  #@ variant 10 - i
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
46 47
  l[i] = 0
  i = i+1
48

49
sum = 0
50
for x in [42]*3:
51
  #@ invariant sum >= 0
52
  print(x)
53 54
  #@ assert x >= 0
  sum = sum+x
55

56 57 58 59 60
foo = [1,2,3]
#@ assert len(foo)==3
#@ assert foo[1]==2


61 62 63 64 65
# Python's division is neither Euclidean division, nor computer division
#@ assert  4 //  3 ==  1 and  4 %  3 ==  1
#@ assert -4 //  3 == -2 and -4 %  3 ==  2
#@ assert  4 // -3 == -2 and  4 % -3 == -2
#@ assert -4 // -3 ==  1 and -4 % -3 == -1
66

67
# Local Variables:
68
# compile-command: "make -C ../.. && why3 prove -P alt-ergo test.py"
69
# End:
70