Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Oct 28, 2024
1 parent b2195be commit bd0d593
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
60 changes: 31 additions & 29 deletions bbq/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ func TestCompileRecursionFib(t *testing.T) {
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 1,
byte(opcode.IntSubtract),
byte(opcode.Transfer), 0, 0,
byte(opcode.GetGlobal), 0, 0,
byte(opcode.Invoke),
byte(opcode.Invoke), 0, 0,
// fib(n - 2)
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 2,
byte(opcode.GetConstant), 0, 0,
byte(opcode.IntSubtract),
byte(opcode.Transfer), 0, 0,
byte(opcode.GetGlobal), 0, 0,
byte(opcode.Invoke),
byte(opcode.Invoke), 0, 0,
// return sum
byte(opcode.IntAdd),
byte(opcode.ReturnValue),
Expand All @@ -86,10 +88,6 @@ func TestCompileRecursionFib(t *testing.T) {
Data: []byte{0x1},
Kind: constantkind.Int,
},
{
Data: []byte{0x2},
Kind: constantkind.Int,
},
},
program.Constants,
)
Expand Down Expand Up @@ -124,39 +122,47 @@ func TestCompileImperativeFib(t *testing.T) {
[]byte{
// var fib1 = 1
byte(opcode.GetConstant), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 1,
// var fib2 = 1
byte(opcode.GetConstant), 0, 1,
byte(opcode.GetConstant), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 2,
// var fibonacci = fib1
byte(opcode.GetLocal), 0, 1,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 3,
// var i = 2
byte(opcode.GetConstant), 0, 2,
byte(opcode.GetConstant), 0, 1,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 4,
// while i < n
byte(opcode.GetLocal), 0, 4,
byte(opcode.GetLocal), 0, 0,
byte(opcode.IntLess),
byte(opcode.JumpIfFalse), 0, 69,
byte(opcode.JumpIfFalse), 0, 93,
// fibonacci = fib1 + fib2
byte(opcode.GetLocal), 0, 1,
byte(opcode.GetLocal), 0, 2,
byte(opcode.IntAdd),
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 3,
// fib1 = fib2
byte(opcode.GetLocal), 0, 2,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 1,
// fib2 = fibonacci
byte(opcode.GetLocal), 0, 3,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 2,
// i = i + 1
byte(opcode.GetLocal), 0, 4,
byte(opcode.GetConstant), 0, 3,
byte(opcode.GetConstant), 0, 0,
byte(opcode.IntAdd),
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 4,
// continue loop
byte(opcode.Jump), 0, 24,
byte(opcode.Jump), 0, 36,
// return fibonacci
byte(opcode.GetLocal), 0, 3,
byte(opcode.ReturnValue),
Expand All @@ -166,10 +172,6 @@ func TestCompileImperativeFib(t *testing.T) {

require.Equal(t,
[]*bbq.Constant{
{
Data: []byte{0x1},
Kind: constantkind.Int,
},
{
Data: []byte{0x1},
Kind: constantkind.Int,
Expand All @@ -178,10 +180,6 @@ func TestCompileImperativeFib(t *testing.T) {
Data: []byte{0x2},
Kind: constantkind.Int,
},
{
Data: []byte{0x1},
Kind: constantkind.Int,
},
},
program.Constants,
)
Expand Down Expand Up @@ -213,24 +211,26 @@ func TestCompileBreak(t *testing.T) {
[]byte{
// var i = 0
byte(opcode.GetConstant), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 0,
// while true
byte(opcode.True),
byte(opcode.JumpIfFalse), 0, 36,
byte(opcode.JumpIfFalse), 0, 42,
// if i > 3
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 1,
byte(opcode.IntGreater),
byte(opcode.JumpIfFalse), 0, 23,
byte(opcode.JumpIfFalse), 0, 26,
// break
byte(opcode.Jump), 0, 36,
byte(opcode.Jump), 0, 42,
// i = i + 1
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 2,
byte(opcode.IntAdd),
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 0,
// repeat
byte(opcode.Jump), 0, 6,
byte(opcode.Jump), 0, 9,
// return i
byte(opcode.GetLocal), 0, 0,
byte(opcode.ReturnValue),
Expand Down Expand Up @@ -284,26 +284,28 @@ func TestCompileContinue(t *testing.T) {
[]byte{
// var i = 0
byte(opcode.GetConstant), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 0,
// while true
byte(opcode.True),
byte(opcode.JumpIfFalse), 0, 39,
byte(opcode.JumpIfFalse), 0, 45,
// i = i + 1
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 1,
byte(opcode.IntAdd),
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 0,
// if i < 3
byte(opcode.GetLocal), 0, 0,
byte(opcode.GetConstant), 0, 2,
byte(opcode.IntLess),
byte(opcode.JumpIfFalse), 0, 33,
byte(opcode.JumpIfFalse), 0, 39,
// continue
byte(opcode.Jump), 0, 6,
byte(opcode.Jump), 0, 9,
// break
byte(opcode.Jump), 0, 39,
byte(opcode.Jump), 0, 45,
// repeat
byte(opcode.Jump), 0, 6,
byte(opcode.Jump), 0, 9,
// return i
byte(opcode.GetLocal), 0, 0,
byte(opcode.ReturnValue),
Expand Down
66 changes: 34 additions & 32 deletions tests/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ access(all) contract interface FungibleToken {
/// createEmptyVault allows any user to create a new Vault that has a zero balance
///
access(all) fun createEmptyVault(): @{FungibleToken.Vault} {
access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} {
post {
// result.getType() == vaultType: "The returned vault does not match the desired type"
result.getType() == vaultType: "The returned vault does not match the desired type"
result.balance == 0.0: "The newly created Vault must have zero balance"
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ access(all) contract FlowToken: FungibleToken {
// and store the returned Vault in their storage in order to allow their
// account to be able to receive deposits of this token type.
//
access(all) fun createEmptyVault(): @FlowToken.Vault {
access(all) fun createEmptyVault(vaultType: Type): @FlowToken.Vault {
return <-create Vault(balance: 0.0)
}
Expand Down Expand Up @@ -470,34 +470,35 @@ import FlowToken from 0x1
transaction {
prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {
var storagePath = /storage/flowTokenVault
if signer.storage.borrow<&FlowToken.Vault>(from: storagePath) != nil {
return
prepare(signer: auth(Storage, Capabilities) &Account) {
if signer.storage.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault) == nil {
// Create a new flowToken Vault and put it in storage
var vault <- FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>())
signer.storage.save(<- vault, to: /storage/flowTokenVault)
// Create a public capability to the Vault that only exposes
// the deposit function through the Receiver interface
let vaultCap = signer.capabilities.storage.issue<&FlowToken.Vault>(
/storage/flowTokenVault
)
signer.capabilities.publish(
vaultCap,
at: /public/flowTokenReceiver
)
// Create a public capability to the Vault that only exposes
// the balance field through the Balance interface
let balanceCap = signer.capabilities.storage.issue<&FlowToken.Vault>(
/storage/flowTokenVault
)
signer.capabilities.publish(
balanceCap,
at: /public/flowTokenBalance
)
}
// Create a new flowToken Vault and put it in storage
signer.storage.save(<-FlowToken.createEmptyVault(), to: storagePath)
// Create a public capability to the Vault that only exposes
// the deposit function through the Receiver interface
let vaultCap = signer.capabilities.storage.issue<&FlowToken.Vault>(storagePath)
signer.capabilities.publish(
vaultCap,
at: /public/flowTokenReceiver
)
// Create a public capability to the Vault that only exposes
// the balance field through the Balance interface
let balanceCap = signer.capabilities.storage.issue<&FlowToken.Vault>(storagePath)
signer.capabilities.publish(
balanceCap,
at: /public/flowTokenBalance
)
}
}
`
Expand All @@ -509,8 +510,6 @@ import FlowToken from 0x1
transaction(recipient: Address, amount: UFix64) {
let tokenAdmin: &FlowToken.Administrator
/// Reference to the Fungible Token Receiver of the recipient
let tokenReceiver: &{FungibleToken.Receiver}
prepare(signer: auth(BorrowValue) &Account) {
Expand Down Expand Up @@ -540,6 +539,8 @@ import FungibleToken from 0x1
import FlowToken from 0x1
transaction(amount: UFix64, to: Address) {
// The Vault resource that holds the tokens that are being transferred
let sentVault: @{FungibleToken.Vault}
prepare(signer: auth(BorrowValue) &Account) {
Expand All @@ -553,6 +554,7 @@ transaction(amount: UFix64, to: Address) {
}
execute {
// Get a reference to the recipient's Receiver
let receiverRef = getAccount(to)
.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
Expand Down

0 comments on commit bd0d593

Please sign in to comment.