Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a test and Added another one. #39

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Library/tests/Contracts/IfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ public void IfWithMultipleArgs()
@"
contract test {
public sign(x:number, name:string): number {
if (x > 0 || name == ""hello"")
if (x > 5 && name == ""hello"")
{
return 1;
}
else if (x == 0 && name == ""world"")
{
return 5;
}else if (x < 5 || name == ""tomb"")
{
return 10;
}

return 0;
Expand All @@ -125,8 +128,8 @@ public sign(x:number, name:string): number {

// Test for the 1st case
var vm = new TestVM(contract, storage, countStuff);
vm.Stack.Push(VMObject.FromObject((BigInteger)10));
vm.Stack.Push(VMObject.FromObject("hello"));
vm.Stack.Push(VMObject.FromObject((BigInteger)10));
var result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);

Expand All @@ -136,8 +139,8 @@ public sign(x:number, name:string): number {

// test for 2nd case
vm = new TestVM(contract, storage, countStuff);
vm.Stack.Push(VMObject.FromObject(0));
vm.Stack.Push(VMObject.FromObject("world"));
vm.Stack.Push(VMObject.FromObject(0));
result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);
Assert.IsTrue(vm.Stack.Count == 1);
Expand All @@ -147,8 +150,19 @@ public sign(x:number, name:string): number {

// test for the 3rd case
vm = new TestVM(contract, storage, countStuff);
vm.Stack.Push(VMObject.FromObject(1234));
vm.Stack.Push(VMObject.FromObject("tomb"));
vm.Stack.Push(VMObject.FromObject(1234));
result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);
Assert.IsTrue(vm.Stack.Count == 1);

val = vm.Stack.Pop().AsNumber();
Assert.AreEqual((BigInteger)10, val);

// test for the 4th case
vm = new TestVM(contract, storage, countStuff);
vm.Stack.Push(VMObject.FromObject("tombcompiler"));
vm.Stack.Push(VMObject.FromObject(1234));
result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);
Assert.IsTrue(vm.Stack.Count == 1);
Expand Down
36 changes: 36 additions & 0 deletions Library/tests/Contracts/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,40 @@ public toUpper(s:string):string
result = vm.Stack.Pop().AsString();
Assert.IsTrue(result == "ABCD");
}


[Test]
public void StringParameters()
{
var sourceCode = @"
contract handlerstring {
import String;

public joinStrings(s:string, idx:number, k:string, y:string):string {
return s + k + y + idx;
}
}
";

var parser = new TombLangCompiler();
var contract = parser.Process(sourceCode).First();

var storage = new Dictionary<byte[], byte[]>(new ByteArrayComparer());

TestVM vm;

var test = contract.abi.FindMethod("joinStrings");
Assert.IsNotNull(test);

vm = new TestVM(contract, storage, test);
vm.Stack.Push(VMObject.FromObject("IJKL"));
vm.Stack.Push(VMObject.FromObject("EFGH"));
vm.Stack.Push(VMObject.FromObject(2));
vm.Stack.Push(VMObject.FromObject("ABCD"));
var state = vm.Execute();
Assert.IsTrue(state == ExecutionState.Halt);

var result = vm.Stack.Pop().AsString();
Assert.AreEqual("ABCDEFGHIJKL2", result );
}
}