Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet committed Aug 7, 2024
1 parent 07b7fb4 commit b5842d4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
1 change: 0 additions & 1 deletion 051-remove-vowels.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

method remove_vowels(text : string) returns (s : string)
ensures forall i : int :: i >= 0 && i < |s| ==> s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u'
ensures forall i : int :: i >= 0 && i < |s| ==> s[i] in text
Expand Down
16 changes: 16 additions & 0 deletions 052-below_treshold.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
method belowThreshold(l: seq<int>, t: int) returns (result: bool)
ensures result <==> forall i :: 0 <= i < |l| ==> l[i] < t
{
result := true;
var i := 0;
while i < |l|
invariant 0 <= i <= |l|
invariant result <==> forall j :: 0 <= j < i ==> l[j] < t
{
if l[i] >= t {
result := false;
return;
}
i := i + 1;
}
}
5 changes: 5 additions & 0 deletions 092-any_int.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
method any_int(a: int, b: int, c: int) returns (r: bool)
ensures r == (a == b + c || b == a + c || c == a + b)
{
r := a == b + c || b == a + c || c == a + b;
}
44 changes: 44 additions & 0 deletions 095-check_dict_case.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
predicate IsLowerCase(s: string)
{
forall i :: 0 <= i < |s| ==> 'a' <= s[i] <= 'z'
}

predicate IsUpperCase(s: string)
{
forall i :: 0 <= i < |s| ==> 'A' <= s[i] <= 'Z'
}

method CheckDictCase(dict: map<string, string>) returns (result: bool)
ensures dict == map[] ==> !result
ensures result ==> (forall k :: k in dict ==> IsLowerCase(k)) || (forall k :: k in dict ==> IsUpperCase(k))
ensures !result ==> dict == map[] || ((exists k :: k in dict && !IsLowerCase(k)) && (exists k :: k in dict && !IsUpperCase(k)))
{
if |dict| == 0 {
return false;
}

var allLower := true;
var allUpper := true;

var keys := dict.Keys;
while keys != {}
invariant allLower ==> forall j :: j in dict.Keys - keys ==> IsLowerCase(j)
invariant allUpper ==> forall j :: j in dict.Keys - keys ==> IsUpperCase(j)
invariant !allLower ==> exists j :: j in dict.Keys - keys && !IsLowerCase(j)
invariant !allUpper ==> exists j :: j in dict.Keys - keys && !IsUpperCase(j)
{
var k :| k in keys;
if !IsLowerCase(k) {
allLower := false;
}
if !IsUpperCase(k) {
allUpper := false;
}
if !allLower && !allUpper {
return false;
}
keys := keys - {k};
}

result := allLower || allUpper;
}
27 changes: 27 additions & 0 deletions 152-compare.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
method Compare(scores: array<int>, guesses: array<int>) returns (result: array<int>)
requires scores.Length == guesses.Length
ensures result.Length == scores.Length
ensures forall i :: 0 <= i < result.Length ==>
result[i] == if scores[i] == guesses[i] then 0 else abs(scores[i] - guesses[i])
{
result := new int[scores.Length];
var i := 0;
while i < scores.Length
invariant 0 <= i <= scores.Length
invariant result.Length == scores.Length
invariant forall k :: 0 <= k < i ==>
result[k] == if scores[k] == guesses[k] then 0 else abs(scores[k] - guesses[k])
{
if scores[i] == guesses[i] {
result[i] := 0;
} else {
result[i] := abs(scores[i] - guesses[i]);
}
i := i + 1;
}
}

function abs(x: int): int
{
if x < 0 then -x else x
}

0 comments on commit b5842d4

Please sign in to comment.