Skip to content
Snippets Groups Projects
Commit d2a5eb79 authored by Régis Blanc's avatar Régis Blanc
Browse files

convert testcases from Map to Array

parent 3fcf0733
Branches
Tags
No related merge requests found
...@@ -4,29 +4,29 @@ import leon.Utils._ ...@@ -4,29 +4,29 @@ import leon.Utils._
object LinearSearch { object LinearSearch {
def linearSearch(a: Map[Int, Int], size: Int, c: Int): Boolean = ({ def linearSearch(a: Array[Int], c: Int): Boolean = ({
require(size <= 5 && isArray(a, size)) require(a.length >= 0)
var i = 0 var i = 0
var found = false var found = false
(while(i < size) { (while(i < a.length) {
if(a(i) == c) if(a(i) == c)
found = true found = true
i = i + 1 i = i + 1
}) invariant(i <= size && }) invariant(
i >= 0 && i <= a.length &&
(if(found) contains(a, i, c) else !contains(a, i, c)) i >= 0 &&
) (if(found) contains(a, i, c) else !contains(a, i, c))
)
found found
}) ensuring(res => if(res) contains(a, size, c) else !contains(a, size, c)) }) ensuring(res => if(res) contains(a, a.length, c) else !contains(a, a.length, c))
def contains(a: Map[Int, Int], size: Int, c: Int): Boolean = { def contains(a: Array[Int], size: Int, c: Int): Boolean = {
require(isArray(a, size) && size <= 5) require(a.length >= 0 && size >= 0 && size <= a.length)
content(a, size).contains(c) content(a, size).contains(c)
} }
def content(a: Array[Int], size: Int): Set[Int] = {
def content(a: Map[Int, Int], size: Int): Set[Int] = { require(a.length >= 0 && size >= 0 && size <= a.length)
require(isArray(a, size) && size <= 5)
var set = Set.empty[Int] var set = Set.empty[Int]
var i = 0 var i = 0
(while(i < size) { (while(i < size) {
...@@ -36,18 +36,4 @@ object LinearSearch { ...@@ -36,18 +36,4 @@ object LinearSearch {
set set
} }
def isArray(a: Map[Int, Int], size: Int): Boolean = {
def rec(i: Int): Boolean = {
require(i >= 0)
if(i >= size) true else {
if(a.isDefinedAt(i)) rec(i+1) else false
}
}
if(size < 0)
false
else
rec(0)
}
} }
...@@ -4,44 +4,26 @@ import leon.Utils._ ...@@ -4,44 +4,26 @@ import leon.Utils._
object MaxSum { object MaxSum {
def maxSum(a: Array[Int]): (Int, Int) = ({
def maxSum(a: Map[Int, Int], size: Int): (Int, Int) = ({ require(a.length >= 0 && isPositive(a))
require(isArray(a, size) && size < 5 && isPositive(a, size))
var sum = 0 var sum = 0
var max = 0 var max = 0
var i = 0 var i = 0
(while(i < size) { (while(i < a.length) {
if(max < a(i)) if(max < a(i))
max = a(i) max = a(i)
sum = sum + a(i) sum = sum + a(i)
i = i + 1 i = i + 1
}) invariant (sum <= i * max && /*isGreaterEq(a, i, max) &&*/ /*(if(i == 0) max == 0 else true) &&*/ i >= 0 && i <= size) }) invariant (sum <= i * max && i >= 0 && i <= a.length)
(sum, max) (sum, max)
}) ensuring(res => res._1 <= size * res._2) }) ensuring(res => res._1 <= a.length * res._2)
/*
def isGreaterEq(a: Map[Int, Int], size: Int, n: Int): Boolean = {
require(size <= 5 && isArray(a, size))
def rec(i: Int): Boolean = {
require(i >= 0)
if(i >= size)
true
else {
if(a(i) > n)
false
else
rec(i+1)
}
}
rec(0)
}
*/
def isPositive(a: Map[Int, Int], size: Int): Boolean = { def isPositive(a: Array[Int]): Boolean = {
require(size <= 5 && isArray(a, size)) require(a.length >= 0)
def rec(i: Int): Boolean = { def rec(i: Int): Boolean = {
require(i >= 0) require(i >= 0)
if(i >= size) if(i >= a.length)
true true
else { else {
if(a(i) < 0) if(a(i) < 0)
...@@ -53,18 +35,4 @@ object MaxSum { ...@@ -53,18 +35,4 @@ object MaxSum {
rec(0) rec(0)
} }
def isArray(a: Map[Int, Int], size: Int): Boolean = {
def rec(i: Int): Boolean = {
require(i >= 0)
if(i >= size) true else {
if(a.isDefinedAt(i)) rec(i+1) else false
}
}
if(size < 0)
false
else
rec(0)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment