From b5842d439399cf33a26aac55ba378790ce517219 Mon Sep 17 00:00:00 2001 From: WeetHet Date: Wed, 7 Aug 2024 20:45:53 +0300 Subject: [PATCH] More tests --- 051-remove-vowels.dfy | 1 - 052-below_treshold.dfy | 16 +++++++++++++++ 092-any_int.dfy | 5 +++++ 095-check_dict_case.dfy | 44 +++++++++++++++++++++++++++++++++++++++++ 152-compare.dfy | 27 +++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 052-below_treshold.dfy create mode 100644 092-any_int.dfy create mode 100644 095-check_dict_case.dfy create mode 100644 152-compare.dfy diff --git a/051-remove-vowels.dfy b/051-remove-vowels.dfy index fba5625..a6ea988 100644 --- a/051-remove-vowels.dfy +++ b/051-remove-vowels.dfy @@ -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 diff --git a/052-below_treshold.dfy b/052-below_treshold.dfy new file mode 100644 index 0000000..50b058a --- /dev/null +++ b/052-below_treshold.dfy @@ -0,0 +1,16 @@ +method belowThreshold(l: seq, 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; + } +} \ No newline at end of file diff --git a/092-any_int.dfy b/092-any_int.dfy new file mode 100644 index 0000000..9eb4d66 --- /dev/null +++ b/092-any_int.dfy @@ -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; +} \ No newline at end of file diff --git a/095-check_dict_case.dfy b/095-check_dict_case.dfy new file mode 100644 index 0000000..5bc9b7d --- /dev/null +++ b/095-check_dict_case.dfy @@ -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) 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; +} \ No newline at end of file diff --git a/152-compare.dfy b/152-compare.dfy new file mode 100644 index 0000000..91c5bf3 --- /dev/null +++ b/152-compare.dfy @@ -0,0 +1,27 @@ +method Compare(scores: array, guesses: array) returns (result: array) + 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 +} \ No newline at end of file