test.lua.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. require("strict")
  2. local _, socket = pcall(require, "socket")
  3. local now = 0
  4. local timeoutQueue
  5. local conf = {
  6. time = socket and socket.gettime or os.time,
  7. setTimeout = function (f, delay)
  8. if not timeoutQueue then
  9. timeoutQueue = System.TimeoutQueue()
  10. end
  11. return timeoutQueue:Add(now, delay, f)
  12. end,
  13. clearTimeout = function (t)
  14. timeoutQueue:Erase(t)
  15. end
  16. }
  17. local function runTimeout()
  18. if timeoutQueue then
  19. while true do
  20. local nextExpiration = timeoutQueue:getNextExpiration()
  21. if nextExpiration then
  22. now = nextExpiration
  23. timeoutQueue:RunLoop(now)
  24. else
  25. break
  26. end
  27. end
  28. end
  29. end
  30. package.path = package.path .. ";CSharp.lua/Coresystem.lua/?.lua"
  31. require("All")("", conf)
  32. collectgarbage("collect")
  33. print(collectgarbage("count"))
  34. local function test(f, name)
  35. print("-----------------------------", name)
  36. f()
  37. print("\n")
  38. end
  39. local function printList(list)
  40. assert(list)
  41. local t = {}
  42. for _, i in System.each(list) do
  43. table.insert(t, i:ToString())
  44. end
  45. print(table.concat(t, " "))
  46. end
  47. local function testDateTimeAndTimeSpan()
  48. local date = System.DateTime.getNow()
  49. print(date:getTicks())
  50. print(date:ToString(), date:getYear(), date:getMonth(), date:getDay(), date:getHour(), date:getMinute(), date:getSecond())
  51. local ts = System.TimeSpan.FromSeconds(20)
  52. print(ts:ToString())
  53. date = date + System.TimeSpan.FromDays(2)
  54. print(date:ToString())
  55. date = date:AddMonths(2);
  56. print(date:ToString())
  57. local baseTime = System.DateTime(1970, 1, 1)
  58. print(baseTime:ToString())
  59. print(baseTime:AddMilliseconds(1458032204643):ToString())
  60. end
  61. local function testArray()
  62. local arr = System.Array(System.Int32):new(10)
  63. print(arr:ToString(), #arr)
  64. printList(arr)
  65. arr:set(0, 2)
  66. arr:set(6, 4)
  67. printList(arr)
  68. print(arr:get(0), arr:get(6), arr:get(9))
  69. end
  70. local function testList()
  71. local list = System.List(System.Int)()
  72. list:Add(20)
  73. list:Add(15)
  74. list:Add(6)
  75. print(list:ToString(), #list)
  76. printList(list)
  77. local subList = list:GetRange(1, 2)
  78. printList(subList)
  79. list:set(1, 8)
  80. list:Sort()
  81. printList(list)
  82. print(list:Contains(10), list:Contains(15), list:IndexOf(20))
  83. list:RemoveAll(function(i) return i >= 10 end)
  84. print(#list, list:get(1))
  85. printList(list)
  86. end
  87. local function testDictionary()
  88. local dict = System.Dictionary(System.String, System.Int)()
  89. dict:Add("a", 1)
  90. dict:Add("b", 12)
  91. dict:Add("c", 25)
  92. dict:Add("d", 30)
  93. for _, pair in System.each(dict) do
  94. print(pair.Key, pair.Value)
  95. end
  96. print("-------------")
  97. for k, v in System.pairs(dict) do
  98. print(k, v)
  99. end
  100. end
  101. local function testYeild()
  102. local enumerable = function (begin, _end)
  103. return System.yieldIEnumerable(function (begin, _end)
  104. while begin < _end do
  105. System.yield(begin)
  106. begin = begin + 1
  107. end
  108. end, System.Int, begin, _end)
  109. end
  110. local e = enumerable(1, 10)
  111. printList(e)
  112. printList(e)
  113. end
  114. local function testDelegate()
  115. local prints = ""
  116. local function printExt(s)
  117. prints = prints .. s
  118. print(s)
  119. end
  120. local function assertExt(s)
  121. assert(prints == s, s)
  122. prints = ""
  123. end
  124. local d1 = function() printExt("d1") end
  125. local d2 = function() printExt("d2") end
  126. local d3 = function() printExt("d3") end
  127. local f = nil + d1
  128. f()
  129. assertExt("d1")
  130. print("--")
  131. f = d1 + nil
  132. f()
  133. assertExt("d1")
  134. print("--")
  135. f = d1 + d2
  136. f()
  137. assertExt("d1d2")
  138. print("--")
  139. f = d1 + (d2 + d3)
  140. f()
  141. assertExt("d1d2d3")
  142. print("--")
  143. f = (d1 + d2) + (d2 + d3)
  144. f()
  145. assertExt("d1d2d2d3")
  146. print("--")
  147. f = d1 + d2 - d1
  148. f()
  149. assertExt("d2")
  150. print("--")
  151. f = d1 + d2 - d2
  152. f()
  153. assertExt("d1")
  154. print("--")
  155. f = d1 + d2 + d1 - d1
  156. f()
  157. assertExt("d1d2")
  158. print("--")
  159. f = d1 + d2 + d3 - (d1 + d2)
  160. f()
  161. assertExt("d3")
  162. print("--")
  163. f = d1 + d2 + d3 - (d2 + d1)
  164. f()
  165. assertExt("d1d2d3")
  166. print("--")
  167. f = (d1 + d2) + (d3 + d1 + d2)
  168. local f1 = d1 + d2
  169. f = f - f1
  170. f()
  171. assertExt("d1d2d3")
  172. print("--")
  173. f = (d1 + d2) - (d1 + d2)
  174. print(f == nil)
  175. end
  176. local function testLinq()
  177. local Linq = System.Linq.Enumerable
  178. local list = System.List(System.Int)()
  179. list:Add(10) list:Add(2) list:Add(30) list:Add(4) list:Add(5) list:Add(6) list:Add(7) list:Add(8)
  180. printList(Linq.Where(list, function(i) return i >= 4 end))
  181. printList(Linq.Take(list, 4))
  182. printList(Linq.Select(list, function(i) return i + 2 end, System.Int))
  183. print(Linq.Min(list), Linq.Max(list))
  184. print(Linq.ElementAtOrDefault(Linq.Where(list, function(i) return i <= 4 end), 5))
  185. local ll = Linq.Where(list, function(i) return i <= 4 end)
  186. print(Linq.Count(ll))
  187. Linq.Any(ll)
  188. print(Linq.Count(ll))
  189. printList(Linq.OrderByDescending(list, function(i) return i end, nil, System.Int))
  190. list = System.List(System.Object)()
  191. local super = {
  192. ToString = function(t)
  193. return t[1] .. ',' .. t[2] .. ',' .. t[3] .. '|'
  194. end
  195. }
  196. super.__index = super
  197. list:Add(setmetatable({ 4, 2, 3 }, super))
  198. list:Add(setmetatable({ 3, 1, 3 }, super))
  199. list:Add(setmetatable({ 1, 2, 3 }, super))
  200. list:Add(setmetatable({ 3, 2, 4 }, super))
  201. list:Add(setmetatable({ 3, 2, 3 }, super))
  202. local t1 = Linq.OrderBy(list, function(i) return i[1] end, nil, System.Int)
  203. printList(t1)
  204. t1 = Linq.ThenBy(t1, function(i) return i[2] end, nil, System.Int)
  205. t1 = Linq.ThenBy(t1, function(i) return i[3] end, nil, System.Int)
  206. printList(t1)
  207. end
  208. local function testGroupBy()
  209. local Linq = System.Linq.Enumerable
  210. local list = System.List(System.Object)()
  211. list:Add({ id = 5, Template = 30 })
  212. list:Add({ id = 6, Template = 30 })
  213. list:Add({ id = 1, Template = 1 })
  214. list:Add({ id = 2, Template = 2 })
  215. list:Add({ id = 3, Template = 1 })
  216. list:Add({ id = 4, Template = 2 })
  217. local groups = Linq.GroupBy(list, function (i) return i.Template end, System.Int)
  218. local s = ""
  219. for _, group in System.each(groups) do
  220. for _, item in System.each(group) do
  221. s = s .. item.id
  222. end
  223. end
  224. print(s)
  225. assert(s == "561324");
  226. end
  227. local function testType()
  228. local ins = 2
  229. print(System.is(ins, System.Double))
  230. local t = ins:GetType()
  231. print(t:getName())
  232. print(System.is("ddd", System.String))
  233. print(System.as("ddd", System.String))
  234. print(System.cast(System.String, "ddd"))
  235. end
  236. local function testNumCast()
  237. assert(System.toInt32(-2147483659) == 2147483637)
  238. assert(System.toUInt32(-2147483659) == 2147483637)
  239. assert(System.toUInt64(-1) == 18446744073709551615)
  240. end
  241. local function testSplit()
  242. local a = "a, b"
  243. local aa = a:Split(44 --[[',']])
  244. printList(aa)
  245. end
  246. local function testConsole()
  247. print("enter your name")
  248. local name = System.Console.ReadLine()
  249. print("enter your age")
  250. local age = System.Console.ReadLine()
  251. System.Console.WriteLine("name {0}, age {1}", name, age)
  252. end
  253. local function testIO()
  254. local path = "iotest.txt"
  255. local s = "hero, CSharp.lua\nIO"
  256. local File = System.IO.File
  257. File.WriteAllText(path, s)
  258. local text = File.ReadAllText(path)
  259. assert(text == s)
  260. File.Delete(path)
  261. end
  262. local function testStringBuilder()
  263. local sb = System.StringBuilder()
  264. sb:Append("aa")
  265. sb:Append("bbcc")
  266. sb:setLength(5)
  267. print(sb, sb:getLength())
  268. end
  269. local function testAsync()
  270. -- Generated by CSharp.lua Compiler
  271. local System = System
  272. local Test
  273. System.import(function (global)
  274. Test = global.Test
  275. end)
  276. System.namespace("Test", function (namespace)
  277. namespace.class("TestAsync", function (namespace)
  278. local f, __ctor__
  279. __ctor__ = function (this)
  280. local t = f(this)
  281. System.Console.WriteLine(("{0}, {1}"):Format(t:getStatus():EnumToString(System.TaskStatus), t:getException()))
  282. end
  283. f = function (this)
  284. return System.async(function (async, this)
  285. local t = System.Task.Delay(2000)
  286. async:await(t)
  287. async:await(t)
  288. System.Console.WriteLine(("Delay {0}"):Format(t:getStatus():ToEnumString(System.TaskStatus)))
  289. end, nil, this)
  290. end
  291. return {
  292. f = f,
  293. __ctor__ = __ctor__
  294. }
  295. end)
  296. namespace.class("Program", function (namespace)
  297. local Main
  298. Main = function ()
  299. Test.TestAsync()
  300. end
  301. return {
  302. Main = Main
  303. }
  304. end)
  305. end)
  306. System.init({
  307. types = {
  308. "Test.Program",
  309. "Test.TestAsync"
  310. },
  311. Main = "Test.Program.Main"
  312. })
  313. Test.Program.Main()
  314. runTimeout()
  315. end
  316. local function testAsyncForeach()
  317. local System = System
  318. local ListInt32 = System.List(System.Int32)
  319. System.namespace("Test", function (namespace)
  320. namespace.class("Program", function (namespace)
  321. local GenerateSequence, Main, Test
  322. GenerateSequence = function (n)
  323. return System.yieldIAsyncEnumerable(function (async, n)
  324. for i = 0, n - 1 do
  325. async:yield(i)
  326. end
  327. end, System.Int32, n)
  328. end
  329. Main = function (args)
  330. Test()
  331. end
  332. Test = function ()
  333. System.async(function (async)
  334. local l = ListInt32()
  335. for _, number in System.asynceach(async, GenerateSequence(10)) do
  336. System.Console.WriteLine(number)
  337. l:Add(number)
  338. end
  339. async:await(System.Task.Delay(200))
  340. System.Console.WriteLine(System.String.JoinEnumerable(",", l))
  341. end, true)
  342. end
  343. return {
  344. GenerateSequence = GenerateSequence,
  345. Main = Main
  346. }
  347. end)
  348. end)
  349. System.init({
  350. "Test.Program"
  351. }, {
  352. Main = "Test.Program.Main"
  353. })
  354. Test.Program.Main()
  355. runTimeout()
  356. end
  357. test(testDateTimeAndTimeSpan, "DateTime & TimeSpan")
  358. test(testArray, "Array")
  359. test(testList, "List")
  360. test(testDictionary, "Dictionary")
  361. test(testYeild, "Yeild")
  362. test(testDelegate, "Delegate")
  363. test(testLinq, "Linq")
  364. test(testGroupBy, "GroupBy")
  365. test(testType, "Type")
  366. test(testNumCast, "NumCast")
  367. test(testSplit, "testSplit")
  368. test(testStringBuilder, "StringBuilder")
  369. test(testIO, "IO")
  370. --test(testConsole, "Console")
  371. --test(testAsync, "Async")
  372. --test(testAsyncForeach, "testAsyncForeach")