aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.0.lua
blob: 53a3ae371a99f411e44e3a9b0c0aed2ff030de95 (plain)
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
--[[--------------------------------------------------------------------

  test_scripts-5.0.lua
  Compile and compare Lua files
  This file is part of Yueliang.

  Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- NOTE
-- * use the argument ALL to pull in additional personal Lua scripts
--   for testing, e.g. lua test_scripts.lua ALL
----------------------------------------------------------------------]]

------------------------------------------------------------------------
-- reads in a list of files to crunch from a text file
------------------------------------------------------------------------

local function loadlist(fname, flist)
  local inf = io.open(fname, "r")
  if not inf then error("cannot open "..fname.." for reading") end
  while true do
    local d = inf:read("*l")
    if not d then break end
    if string.find(d, "^%s*$") or string.find(d, "^#") then
      -- comments and empty lines are ignored
    else
      table.insert(flist, d)
    end
  end
  inf:close()
end

------------------------------------------------------------------------
-- read in list of files to test
------------------------------------------------------------------------

local files = {}

loadlist("files-lua-5.0.txt", files)
loadlist("files-yueliang-5.0.txt", files)

-- pull in personal scripts to test if user specifies "ALL"
if arg[1] == "ALL" then
  loadlist("files-other-5.0.txt", files)
end

-- if you want to specify files in this script itself (not recommended)
-- you can add them using the following
--[[
for v in string.gfind([[
]], "[^%s]+") do
  table.insert(files, v)
end
--]]

total = 0  -- sum of sizes
fsize = 0  -- current file size

------------------------------------------------------------------------
-- initialize
------------------------------------------------------------------------

require("../orig-5.0.3/lzio")
require("../orig-5.0.3/llex")
require("../orig-5.0.3/lopcodes")
require("../orig-5.0.3/ldump")
require("../orig-5.0.3/lcode")
require("../orig-5.0.3/lparser")

function lua_assert(test)
  if not test then error("assertion failed!") end
end

luaX:init()

io.stdout:write("\n\n")

------------------------------------------------------------------------
-- * basic comparison for now; do it properly later
------------------------------------------------------------------------

local LuaState = {}

------------------------------------------------------------------------
-- dump binary chunks to a file if something goes wrong
------------------------------------------------------------------------
local function Dump(data, filename)
  h = io.open(filename, "wb")
  if not h then error("failed to open "..filename.." for writing") end
  h:write(data)
  h:close()
end

------------------------------------------------------------------------
-- create custom chunk reader (sums up file sizes)
------------------------------------------------------------------------
function make_getF(filename)
  local h = io.open(filename, "r")
  if not h then return nil end
  fsize = h:seek("end")
  h:seek("set")
  total = total + fsize
  return function() -- chunk reader anonymous function here
    if not h then return nil end
    local buff = h:read(512)
    if not buff then h:close() end
    return buff
  end
end

------------------------------------------------------------------------
-- attempt to compile Lua source files
------------------------------------------------------------------------
for i, filename in ipairs(files) do
  -- compile a source file
  local zio = luaZ:init(make_getF(filename), nil, "@"..filename)
  if not zio then
    error("could not initialize zio stream for "..filename)
  end
  io.stdout:write(filename.."("..fsize.."): ")
  local Func = luaY:parser(LuaState, zio, nil)
  local Writer, Buff = luaU:make_setS()
  luaU:dump(LuaState, Func, Writer, Buff)
  local bc1 = Buff.data  -- Yueliang's output

  local f = loadfile(filename)
  local bc2 = string.dump(f)  -- Lua's output

  -- compare outputs
  if string.len(bc1) ~= string.len(bc2) then
    Dump(bc1, "bc1.out")
    Dump(bc2, "bc2.out")
    error("binary chunk sizes different")
  elseif bc1 ~= bc2 then
    Dump(bc1, "bc1.out")
    Dump(bc2, "bc2.out")
    error("binary chunks different")
  else
    io.stdout:write("CORRECT\n")
  end
  local x, y = gcinfo()
  -- memory usage isn't really a problem for the straight port
  -- string handling in Lua that follows the original C closely is more
  -- of a problem, but this will be fixed elsewhere, not in this tree
  --io.stdout:write("gcinfo: "..x.." "..y.."\n")
end

-- summaries here
io.stdout:write("\nTotal file sizes: "..total.."\n")

-- end