Skip to content
Snippets Groups Projects
Commit 816f5e34 authored by Regis Blanc's avatar Regis Blanc
Browse files

banking testcases

parent d12b44a6
No related branches found
No related tags found
No related merge requests found
import leon.lang._
object BankSimpleTransactions {
def okTransaction(): Unit = {
var balance: BigInt = 0
def balanceInvariant: Boolean = balance >= 0
def deposit(x: BigInt): Unit = {
require(balanceInvariant && x >= 0)
balance += x
} ensuring(_ => balance == old(balance) + x && balanceInvariant)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= balance)
balance -= x
} ensuring(_ => balance == old(balance) - x && balanceInvariant)
deposit(35)
withdrawal(30)
}
def invalidTransaction(): Unit = {
var balance: BigInt = 0
def balanceInvariant: Boolean = balance >= 0
def deposit(x: BigInt): Unit = {
require(balanceInvariant && x >= 0)
balance += x
} ensuring(_ => balance == old(balance) + x && balanceInvariant)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= balance)
balance -= x
} ensuring(_ => balance == old(balance) - x && balanceInvariant)
deposit(35)
withdrawal(40)
}
def internalTransfer(): Unit = {
var checking: BigInt = 0
var saving: BigInt = 0
def balance = checking + saving
def balanceInvariant: Boolean = balance >= 0
def deposit(x: BigInt): Unit = {
require(balanceInvariant && x >= 0)
checking += x
} ensuring(_ => checking == old(checking) + x && balanceInvariant)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= checking)
checking -= x
} ensuring(_ => checking == old(checking) - x && balanceInvariant)
def checkingToSaving(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && checking >= x)
checking -= x
saving += x
} ensuring(_ => checking + saving == old(checking) + old(saving) && balanceInvariant)
deposit(50)
withdrawal(30)
checkingToSaving(10)
}
}
...@@ -2,72 +2,39 @@ import leon.lang._ ...@@ -2,72 +2,39 @@ import leon.lang._
object BankTransfer { object BankTransfer {
def okTransaction(): Unit = { case class BankAccount(var checking: BigInt, var saving: BigInt) {
var balance: BigInt = 0 require(checking >= 0 && saving >= 0)
def balanceInvariant: Boolean = balance >= 0 def balance: BigInt = checking + saving
def deposit(x: BigInt): Unit = {
require(balanceInvariant && x >= 0)
balance += x
} ensuring(_ => balance == old(balance) + x && balanceInvariant)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= balance)
balance -= x
} ensuring(_ => balance == old(balance) - x && balanceInvariant)
deposit(35)
withdrawal(30)
} }
def invalidTransaction(): Unit = { //TODO: support for map references to mutable items
var balance: BigInt = 0 //case class Bank(accounts: Map[BigInt, BankAccount])
def balanceInvariant: Boolean = balance >= 0
def deposit(x: BigInt): Unit = { def deposit(x: BigInt, account: BankAccount): Unit = {
require(balanceInvariant && x >= 0) require(x > 0)
balance += x account.checking = account.checking + x
} ensuring(_ => balance == old(balance) + x && balanceInvariant) } ensuring(_ => account.balance == old(account).balance + x)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= balance)
balance -= x
} ensuring(_ => balance == old(balance) - x && balanceInvariant)
deposit(35)
withdrawal(40)
}
def withdrawal(x: BigInt, account: BankAccount): Unit = {
require(x > 0 && account.checking >= x)
account.checking = account.checking - x
} ensuring(_ => account.balance == old(account).balance - x)
def internalTransfer(): Unit = { def transfer(x: BigInt, a1: BankAccount, a2: BankAccount): Unit = {
var checking: BigInt = 0 require(x > 0 && a1.checking >= x)
var saving: BigInt = 0 withdrawal(x, a1)
deposit(x, a2)
} ensuring(_ => a1.balance + a2.balance == old(a1).balance + old(a2).balance &&
a1.balance == old(a1).balance - x &&
a2.balance == old(a2).balance + x)
def balance = checking + saving
def balanceInvariant: Boolean = balance >= 0 def save(x: BigInt, account: BankAccount): Unit = {
require(x > 0 && account.checking >= x)
def deposit(x: BigInt): Unit = { account.checking = account.checking - x
require(balanceInvariant && x >= 0) account.saving = account.saving + x
checking += x } ensuring(_ => account.balance == old(account).balance)
} ensuring(_ => checking == old(checking) + x && balanceInvariant)
def withdrawal(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && x <= checking)
checking -= x
} ensuring(_ => checking == old(checking) - x && balanceInvariant)
def checkingToSaving(x: BigInt): Unit = {
require(balanceInvariant && x >= 0 && checking >= x)
checking -= x
saving += x
} ensuring(_ => checking + saving == old(checking) + old(saving) && balanceInvariant)
deposit(50)
withdrawal(30)
checkingToSaving(10)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment