Upsilon/UpsilonTests/GeneralTests/ForLoopTests.cs

119 lines
3.0 KiB
C#

using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests.GeneralTests
{
public class ForLoopTests : TestClass
{
public ForLoopTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact]
public void BasicNumericForLoopTest()
{
const string input = @"
a = 0
for i=0,5 do
a = a + i
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
[Fact]
public void NumericForLoopWithDifferentStepTest()
{
const string input = @"
a = 0
for i=0,10,2 do
a = a + i
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(30, result);
}
[Fact]
public void NumericForLoopWithNegativeStepTest()
{
const string input = @"
a = 0
for i=5,0,-1 do
a = a + i
end
return a";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
[Fact]
public void NumericForLoopBreakTest()
{
const string input = @"
a = 0
for i=0,10 do
a = a + i
if i == 5 then
break
end
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
[Fact]
public void GenericForLoopValueTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
value = value + val
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(184, result);
}
[Fact]
public void GenericForLoopKeyTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
local keyInt = tonumber(key)
value = value + keyInt
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(6, result);
}
}
}