Unverified Commit 1cc74393 authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

* Giant cleanup/reshuffle of all files. (#265)

* * Giant cleanup/reshuffle of all files.

* * Update upstream `KeraLua` to `0.1.14`

* Fixed .NET Core build.

* Add runsettings file

* * Fixed nuspec `dependencies` node

* Ignore _ in branch names for package names.

* * Fixed nuspec.
parent 3f254585
-- fibonacci function with cache
-- very inefficient fibonacci function
function fib(n)
N=N+1
if n<2 then
return n
else
return fib(n-1)+fib(n-2)
end
end
-- a general-purpose value cache
function cache(f)
local c={}
return function (x)
local y=c[x]
if not y then
y=f(x)
c[x]=y
end
return y
end
end
-- run and time it
function test(s,f)
N=0
local c=os.clock()
local v=f(n)
local t=os.clock()-c
print(s,n,v,t,N)
return v
end
n= 24 -- for other values, do lua fib.lua XX
n=tonumber(n)
print("","n","value","time","evals")
v = test("plain",fib)
assert (v == 46368)
fib=cache(fib)
v = test("cached",fib)
assert (v == 46368)
-- example of for with generator functions
local fibs = {
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
89,
144,
233,
377,
610,
987,
}
function generatefib (n)
return coroutine.wrap(function ()
local a,b = 1, 1
while a <= n do
coroutine.yield(a)
a, b = b, a+b
end
end)
end
local j = 1
for i in generatefib(1000) do
print(i.." ".. fibs [j])
assert (i == fibs [j])
j = j + 1
end
-- life.lua
-- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l
-- modified to use ANSI terminal escape sequences
-- modified to use for instead of while
local write=print
ALIVE="" DEAD=""
ALIVE="O" DEAD="-"
function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary
for i=1,10000 do end
-- local i=os.clock()+1 while(os.clock()<i) do end
end
function ARRAY2D(w,h)
local t = {w=w,h=h}
for y=1,h do
t[y] = {}
for x=1,w do
t[y][x]=0
end
end
return t
end
_CELLS = {}
-- give birth to a "shape" within the cell array
function _CELLS:spawn(shape,left,top)
for y=0,shape.h-1 do
for x=0,shape.w-1 do
self[top+y][left+x] = shape[y*shape.w+x+1]
end
end
end
-- run the CA and produce the next generation
function _CELLS:evolve(next)
local ym1,y,yp1,yi=self.h-1,self.h,1,self.h
while yi > 0 do
local xm1,x,xp1,xi=self.w-1,self.w,1,self.w
while xi > 0 do
local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] +
self[y][xm1] + self[y][xp1] +
self[yp1][xm1] + self[yp1][x] + self[yp1][xp1]
next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0
xm1,x,xp1,xi = x,xp1,xp1+1,xi-1
end
ym1,y,yp1,yi = y,yp1,yp1+1,yi-1
end
end
-- output the array to screen
function _CELLS:draw()
local out="" -- accumulate to reduce flicker
for y=1,self.h do
for x=1,self.w do
out=out..(((self[y][x]>0) and ALIVE) or DEAD)
end
out=out.."\n"
end
write(out)
end
-- constructor
function CELLS(w,h)
local c = ARRAY2D(w,h)
c.spawn = _CELLS.spawn
c.evolve = _CELLS.evolve
c.draw = _CELLS.draw
return c
end
--
-- shapes suitable for use with spawn() above
--
HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 }
GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 }
EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 }
FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 }
BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 }
-- the main routine
function LIFE(w,h)
-- create two arrays
local thisgen = CELLS(w,h)
local nextgen = CELLS(w,h)
-- create some life
-- about 1000 generations of fun, then a glider steady-state
thisgen:spawn(GLIDER,5,4)
thisgen:spawn(EXPLODE,25,10)
thisgen:spawn(FISH,4,12)
-- run until break
local gen=1
write("\027[2J") -- ANSI clear screen
while 1 do
thisgen:evolve(nextgen)
thisgen,nextgen = nextgen,thisgen
write("\027[H") -- ANSI home cursor
thisgen:draw()
write("Life - generation ".." "..gen.." ".."\n")
gen=gen+1
if gen>2000 then break end
--delay() -- no delay
end
end
LIFE(40,20)
-- an implementation of printf
function sprintf(...)
return string.format(...)
end
function printf (...)
print(sprintf(...))
end
x = sprintf("Hello %s from %s on %s", "there", "Lua Tests", "XYZ")
assert (x == "Hello there from Lua Tests on XYZ")
print(x)
-- the sieve of of Eratosthenes programmed with coroutines
-- typical usage: lua -e N=1000 sieve.lua | column
local siev = {
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
349,
353,
359,
367,
373,
379,
383,
389,
397,
401,
409,
419,
421,
431,
433,
439,
443,
449,
457,
461,
463,
467,
479,
487,
491,
499,
503,
509,
521,
523,
541,
547,
557,
563,
569,
571,
577,
587,
593,
599,
601,
607,
613,
617,
619,
631,
641,
643,
647,
653,
659,
661,
673,
677,
683,
691,
701,
709,
719,
727,
733,
739,
743,
751,
757,
761,
769,
773,
787,
797,
809,
811,
821,
823,
827,
829,
839,
853,
857,
859,
863,
877,
881,
883,
887,
907,
911,
919,
929,
937,
941,
947,
953,
967,
971,
977,
983,
991,
997,
}
-- generate all the numbers from 2 to n
function gen (n)
return coroutine.wrap(function ()
for i=2,n do coroutine.yield(i) end
end)
end
-- filter the numbers generated by `g', removing multiples of `p'
function filter (p, g)
return coroutine.wrap(function ()
while 1 do
local n = g()
if n == nil then return end
if math.fmod(n, p) ~= 0 then coroutine.yield(n) end
end
end)
end
N=1000 -- from command line
x = gen(N) -- generate primes up to N
local j = 1
while 1 do
local n = x() -- pick a number until done
if n == nil then break end
print(n) -- must be a prime number
assert (n == siev [j])
x = filter(n, x) -- now remove its multiples
j = j + 1
end
-- two implementations of a sort function
-- this is an example only. Lua has now a built-in function "sort"
quicksort_x = {"Apr","Aug","Dec","Feb","Jan","Jul","Jun","Mar","May","Nov","Oct","Sep"}
reverse_x = {"Sep","Oct","Nov","May","Mar","Jun","Jul","Jan","Feb","Dec","Aug","Apr"}
-- extracted from Programming Pearls, page 110
function qsort(x,l,u,f)
if l<u then
local m=math.random(u-(l-1))+l-1 -- choose a random pivot in range l..u
x[l],x[m]=x[m],x[l] -- swap pivot to first position
local t=x[l] -- pivot value
m=l
local i=l+1
while i<=u do
-- invariant: x[l+1..m] < t <= x[m+1..i-1]
if f(x[i],t) then
m=m+1
x[m],x[i]=x[i],x[m] -- swap x[i] and x[m]
end
i=i+1
end
x[l],x[m]=x[m],x[l] -- swap pivot to a valid place
-- x[l+1..m-1] < x[m] <= x[m+1..u]
qsort(x,l,m-1,f)
qsort(x,m+1,u,f)
end
end
function selectionsort(x,n,f)
local i=1
while i<=n do
local m,j=i,i+1
while j<=n do
if f(x[j],x[m]) then m=j end
j=j+1
end
x[i],x[m]=x[m],x[i] -- swap x[i] and x[m]
i=i+1
end
end
function show(m,x, t)
print(m.." ".."\n\t")
local i=1
while x[i] do
assert (x[i] == t[i])
print(x[i])
i=i+1
if x[i] then print(",") end
end
print("\n")
end
function testsorts(x)
local n=1
while x[n] do n=n+1 end; n=n-1 -- count elements
show("original",x , x)
qsort(x,1,n,function (x,y) return x<y end)
show("after quicksort",x, quicksort_x)
selectionsort(x,n,function (x,y) return x>y end)
show("after reverse selection sort",x, reverse_x)
qsort(x,1,n,function (x,y) return x<y end)
show("after quicksort again",x, quicksort_x)
end
-- array to be sorted
x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}
testsorts(x)
width=100 width=100
height=200 height=200
message="Hello World!" message="Hello World!"
color={r=100,g=20,b=50} color={r=100,g=20,b=50}
tree={branch1={leaf1=10,leaf2="leaf2"},leaf3="leaf3"} tree={branch1={leaf1=10,leaf2="leaf2"},leaf3="leaf3"}
function func(x,y) function func(x,y)
return x,x+y return x,x+y
end end
using System;
using System.Text;
using System.Collections.Generic;
using NUnit.Framework;
using NLuaTest.Mock;
using System.Reflection;
using System.Threading;
using NLua;
using NLua.Exceptions;
namespace NLuaTest
{
[TestFixture]
public class AAACodeGenTests
{
/*
* Tests passing a Lua function to a delegate
* with value-type arguments
*/
[Test]
public void LuaDelegateValueTypes()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x,y) return x+y; end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callDelegate1(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with value-type arguments and out params
*/
[Test]
public void LuaDelegateValueTypesOutParam()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x) return x,x*2; end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callDelegate2(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(6, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with value-type arguments and ref params
*/
[Test]
public void LuaDelegateValueTypesByRefParam()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x,y) return x+y; end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callDelegate3(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with value-type arguments that returns a reference type
*/
[Test]
public void LuaDelegateValueTypesReturnReferenceType()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x,y) return TestClass(x+y); end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callDelegate4(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with reference type arguments
*/
[Test]
public void LuaDelegateReferenceTypes()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x,y) return x.testval+y.testval; end");
lua.DoString("a=test:callDelegate5(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with reference type arguments and an out param
*/
[Test]
public void LuaDelegateReferenceTypesOutParam()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x) return x,TestClass(x*2); end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callDelegate6(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(6, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua function to a delegate
* with reference type arguments and a ref param
*/
[Test]
public void LuaDelegateReferenceTypesByRefParam()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("function func(x,y) return TestClass(x+y.testval); end");
lua.DoString("a=test:callDelegate7(func)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("delegate returned: "+a);
}
}
/*
* Tests passing a Lua table as an interface and
* calling one of its methods with value-type params
*/
[Test]
public void NLuaAAValueTypes()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test=TestClass()");
lua.DoString("itest={}");
lua.DoString("function itest:test1(x,y) return x+y; end");
lua.DoString("test=TestClass()");
lua.DoString("a=test:callInterface1(itest)");
int a = (int)lua.GetNumber("a");
Assert.AreEqual(5, a);
//Console.WriteLine("interface returned: "+a);
}
}
/*
* Tests making an object from a Lua table and calling the base
* class version of one of the methods the table overrides.
*/
[Test]
public void LuaTableBaseMethod()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('NLuaTest')");
lua.DoString("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString("test={}");
lua.DoString("function test:overridableMethod(x,y) print(self[base]); return 6 end");
lua.DoString("luanet.make_object(test,'NLuaTest.Mock.TestClass')");
lua.DoString("a=TestClass.callOverridable(test,2,3)");
int a = (int)lua.GetNumber("a");
lua.DoString("luanet.free_object(test)");
Assert.AreEqual(6, a);
}
}
}
}
using System;
using NLua;
using System.IO;
using LoadFileTests;
using NUnit.Framework;
#if __IOS__ || __TVOS__ || __WATCHOS__
using Foundation;
#endif
namespace NLuaTest
{
[TestFixture]
#if __IOS__ || __TVOS__ || __WATCHOS__
[Preserve (AllMembers = true)]
#endif
public class Core
{
Lua lua = null;
string GetTestPath(string name)
{
string core = LoadLuaFile.GetScriptsPath("core");
return Path.Combine(core, name + ".lua");
}
void AssertFile(string path)
{
lua.DoFile(path);
}
void TestLuaFile(string name)
{
string path = GetTestPath(name);
AssertFile(path);
}
[SetUp]
public void Setup()
{
lua = new Lua();
lua.RegisterFunction("WriteLineString", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }));
lua.DoString(@"
function print (param)
WriteLineString (tostring(param))
end
");
}
[TearDown]
public void TearDown()
{
lua.Dispose();
lua = null;
}
[Test]
public void Bisect()
{
TestLuaFile("bisect");
}
[Test]
public void CF()
{
TestLuaFile("cf");
}
[Test]
public void Factorial()
{
TestLuaFile("factorial");
}
[Test]
public void FibFor()
{
TestLuaFile("fibfor");
}
[Test]
public void Life()
{
TestLuaFile("life");
}
[Test]
public void Printf()
{
TestLuaFile("printf");
}
[Test]
public void Sieve()
{
TestLuaFile("sieve");
}
[Test]
public void Sort()
{
TestLuaFile("sort");
}
}
}
using System;
namespace NLuaTest.Mock
{
public class Entity
{
public event EventHandler<EventArgs> Clicked;
protected virtual void OnEntityClicked(EventArgs e)
{
EventHandler<EventArgs> handler = Clicked;
if (handler != null)
{
handler(this, e);
}
}
public string Property
{
get;
set;
}
// default ctor
public Entity()
{
Property = "Default";
}
// string ctor
public Entity(string param)
{
Property = "String";
}
public Entity(int param)
{
Property = "Int";
}
public void Click()
{
OnEntityClicked(new EventArgs());
}
}
}
using System;
using System.IO;
using NLua;
using NLuaTest.Mock;
using NUnit.Framework;
#if __IOS__ || __TVOS__ || __WATCHOS__
using Foundation;
#endif
namespace LoadFileTests
{
[TestFixture]
#if __IOS__ || __TVOS__ || __WATCHOS__
[Preserve (AllMembers = true)]
#endif
public class LoadLuaFile
{
public static string GetScriptsPath(string name)
{
string path = new Uri(typeof(LoadLuaFile).Assembly.CodeBase).AbsolutePath;
path = Path.GetDirectoryName(path);
path = Path.Combine(path, "scripts");
path = Path.Combine (path, name);
return path;
}
/*
* Tests capturing an exception
*/
[Test]
public void TestLoadFile()
{
using (Lua lua = new Lua())
{
lua.LoadCLRPackage();
string file = GetScriptsPath("test.lua");
lua.DoFile(file);
int width = (int)(double)lua["width"];
int height = (int)(double)lua["height"];
string message = (string)lua["message"];
int color_g = (int)(double)lua["color.g"];
LuaFunction func = (LuaFunction)lua["func"];
object[] res = func.Call(12, 34);
int x = (int)(double)res[0];
int y = (int)(double)res[1];
//function func(x,y)
// return x,x+y
//end
Assert.AreEqual(100, width);
Assert.AreEqual(200, height);
Assert.AreEqual("Hello World!", message);
Assert.AreEqual(20, color_g);
Assert.AreEqual(12, x);
Assert.AreEqual(46, y);
}
}
[Test]
public void TestBinaryLoadFile()
{
using (Lua lua = new Lua())
{
string file;
lua.LoadCLRPackage();
if (IntPtr.Size == 4)
file = GetScriptsPath("test_32.luac");
else
file = GetScriptsPath("test_64.luac");
lua.DoFile(file);
int width = (int)(double)lua["width"];
int height = (int)(double)lua["height"];
string message = (string)lua["message"];
int color_g = (int)(double)lua["color.g"];
LuaFunction func = (LuaFunction)lua["func"];
object[] res = func.Call(12, 34);
int x = (int)(double)res[0];
int y = (int)(double)res[1];
Assert.AreEqual(100, width);
Assert.AreEqual(200, height);
Assert.AreEqual("Hello World!", message);
Assert.AreEqual(20, color_g);
Assert.AreEqual(12, x);
Assert.AreEqual(46, y);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment