2018-11-26 15:55:10 +00:00
|
|
|
using Upsilon;
|
2018-11-23 13:38:45 +00:00
|
|
|
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
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(15, actual);
|
2018-11-23 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NumericForLoopWithDifferentStepTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
a = 0
|
|
|
|
for i=0,10,2 do
|
|
|
|
a = a + i
|
|
|
|
end
|
|
|
|
return a
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(30, actual);
|
2018-11-23 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NumericForLoopWithNegativeStepTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
a = 0
|
|
|
|
for i=5,0,-1 do
|
|
|
|
a = a + i
|
|
|
|
end
|
|
|
|
return a";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(15, actual);
|
2018-11-23 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 14:27:48 +00:00
|
|
|
[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
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(15, actual);
|
2018-11-23 14:27:48 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 17:18:07 +00:00
|
|
|
[Fact]
|
|
|
|
public void GenericForLoopValueTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
arr = {100, 56, 28}
|
|
|
|
value = 0
|
2018-11-24 13:35:23 +00:00
|
|
|
for key, val in ipairs(arr) do
|
2018-11-23 17:18:07 +00:00
|
|
|
value = value + val
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(184, actual);
|
2018-11-23 17:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GenericForLoopKeyTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
arr = {100, 56, 28}
|
|
|
|
value = 0
|
2018-11-24 13:35:23 +00:00
|
|
|
for key, val in ipairs(arr) do
|
2018-11-23 17:18:07 +00:00
|
|
|
local keyInt = tonumber(key)
|
|
|
|
value = value + keyInt
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var actual = Executor.EvaluateScript<long>(input, Options);
|
|
|
|
Assert.Equal(6, actual);
|
2018-11-23 17:18:07 +00:00
|
|
|
}
|
2018-11-23 13:38:45 +00:00
|
|
|
}
|
|
|
|
}
|