summaryrefslogtreecommitdiff
path: root/Test/VSI-Benchmarks/b1.dfy
blob: cd4a18c27866140fb1228eba2566635b70617d9a (plain)
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
// Spec# and Boogie and Chalice:  The program will be
// the same, except that these languages do not check
// for any kind of termination.  Also, in Spec#, there
// is an issue of potential overflows.

// Benchmark1

method Add(x: int, y: int) returns (r: int)
  ensures r == x+y;
{
  r := x;
  if (y < 0) {
    var n := y;
    while (n != 0)
      invariant r == x+y-n && 0 <= -n;
    {
      r := r - 1;
      n := n + 1;
    }
  } else {
    var n := y;
    while (n != 0)
      invariant r == x+y-n && 0 <= n;
    {
      r := r + 1;
      n := n - 1;
    }
  }
}

method Mul(x: int, y: int) returns (r: int)
  ensures r == x*y;
  decreases x < 0, x;
{
  if (x == 0) {
    r := 0;
  } else if (x < 0) {
    call r := Mul(-x, y);
    r := -r;
  } else {
    call r := Mul(x-1, y);
    call r := Add(r, y);
  }
}

// ---------------------------

method Main() {
  call TestAdd(3, 180);
  call TestAdd(3, -180);
  call TestAdd(0, 1);

  call TestMul(3, 180);
  call TestMul(3, -180);
  call TestMul(180, 3);
  call TestMul(-180, 3);
  call TestMul(0, 1);
  call TestMul(1, 0);
}

method TestAdd(x: int, y: int) {
  print x, " + ", y, " = ";
  call z := Add(x, y);
  print z, "\n";
}

method TestMul(x: int, y: int) {
  print x, " * ", y, " = ";
  call z := Mul(x, y);
  print z, "\n";
}