ShintenScript/ShintenScriptTest/EvaluationTest.cs

92 lines
2.6 KiB
C#
Raw Permalink Normal View History

2023-02-02 14:05:53 +00:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShintenScript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShintenScriptTest
{
[TestClass]
public class EvaluationTest
{
[TestMethod]
public void TestBlock()
{
IASTNode ast = new ASTNodeBlock(new List<IASTNode>()
{
new ASTNodeLiteral(1f),
new ASTNodeLiteral(true),
new ASTNodeLiteral(5f)
});
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(5f, func(null));
}
[TestMethod]
public void TestAdd()
{
IASTNode ast = new ASTNodeAdd(new ASTNodeLiteral(2f), new ASTNodeLiteral(3f));
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(5f, func(null));
}
[TestMethod]
public void TestMul()
{
IASTNode ast = new ASTNodeMultiply(new ASTNodeLiteral(2f), new ASTNodeLiteral(3f));
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(6f, func(null));
}
[TestMethod]
public void TestSub()
{
IASTNode ast = new ASTNodeSubtract(new ASTNodeLiteral(2f), new ASTNodeLiteral(3f));
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(-1f, func(null));
}
[TestMethod]
public void TestDiv()
{
IASTNode ast = new ASTNodeDivide(new ASTNodeLiteral(7f), new ASTNodeLiteral(2f));
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(3.5f, func(null));
}
[TestMethod]
public void TestNegative()
{
IASTNode ast = new ASTNodeNegative(new ASTNodeLiteral(7f));
Assert.AreEqual(SSType.real, ast.Type());
Func<ExecutionContext, float> func = (Func<ExecutionContext, float>)ast.CreateFunction();
Assert.AreEqual(-7f, func(null));
}
}
}