-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathCore.cs
172 lines (144 loc) · 4.02 KB
/
Core.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
using ObjCRuntime;
#endif
namespace KeraLua.Tests
{
[TestFixture]
public class Core
{
internal static LuaFunction func_print = Print;
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
[MonoPInvokeCallback (typeof (LuaFunction))]
#endif
private static int Print(IntPtr p)
{
var state = Lua.FromIntPtr(p);
int n = state.GetTop(); /* number of arguments */
int i;
for (i = 1; i <= n; i++)
{
LuaType type = state.Type(i);
switch (type)
{
case LuaType.Nil:
Trace.Write("nil");
break;
case LuaType.String:
string s = state.ToString(i, false);
Trace.Write(s);
break;
case LuaType.Number:
double number = state.ToNumber(i);
Trace.Write(number);
break;
case LuaType.None:
break;
case LuaType.Boolean:
break;
case LuaType.LightUserData:
break;
case LuaType.Table:
break;
case LuaType.Function:
break;
case LuaType.UserData:
break;
case LuaType.Thread:
break;
default:
break;
}
}
Trace.WriteLine("\n");
return 0;
}
private Lua state;
private string GetTestPath(string name)
{
string path = new Uri(GetType().Assembly.Location).AbsolutePath;
path = Path.GetDirectoryName(path);
path = Path.Combine(path, "LuaTests", "core");
path = Path.Combine(path, name + ".lua");
return path;
}
private void AssertFile(string path)
{
string error = string.Empty;
LuaStatus result = state.LoadFile(path);
if (result != LuaStatus.OK)
error = state.ToString(1);
Assert.True(result == LuaStatus.OK, "Fail loading file: " + path + "ERROR:" + error);
result = state.PCall(0, -1, 0);
if (result != LuaStatus.OK)
error = state.ToString(1);
Assert.True(result == LuaStatus.OK, "Fail calling file: " + path + " ERROR: " + error);
}
private void TestLuaFile(string name)
{
string path = GetTestPath(name);
AssertFile(path);
}
[SetUp]
public void Setup()
{
state = new Lua();
state.Register("print", func_print);
}
[TearDown]
public void TearDown()
{
state.Close();
state = 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 Fib()
{
TestLuaFile("fib");
}
[Test]
public void Life()
{
TestLuaFile("life");
}
[Test]
public void Printf()
{
TestLuaFile("printf");
}
[Test]
public void Sieve()
{
TestLuaFile("sieve");
}
[Test]
public void Sort()
{
TestLuaFile("sort");
}
}
}