aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/test/test_lparser2.lua
blob: e9fa188d10c7b280c25e56728e7531663c2ec391 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
--[[--------------------------------------------------------------------

  test_lparser2.lua
  Test for lparser.lua, using the test case file
  This file is part of Yueliang.

  Copyright (c) 2006 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.

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

--[[--------------------------------------------------------------------
-- Notes:
-- * the test cases are in the test_lua directory (test_parser-5.1.lua)
----------------------------------------------------------------------]]

-- * true if you want an output of all failure cases in native Lua,
--   for checking whether test cases fail where you intend them to
local DEBUG_FAILS = false

------------------------------------------------------------------------
-- test the whole kaboodle
------------------------------------------------------------------------

dofile("../lzio.lua")
dofile("../llex.lua")
dofile("../lopcodes.lua")
dofile("../ldump.lua")
dofile("../lcode.lua")
dofile("../lparser.lua")

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

luaX:init()

------------------------------------------------------------------------
-- load test cases
------------------------------------------------------------------------

dofile("../../test_lua/test_parser-5.1.lua")

local test, expect, heading = {}, {}, {}
local total, total_pass, total_fail = 0, 0, 0

for ln in string.gmatch(tests_source, "([^\n]*)\n") do
  if string.find(ln, "^%s*%-%-") then
    -- comment, ignore
  else
    local m, _, head = string.find(ln, "^%s*(TESTS:%s*.*)$")
    if m then
      heading[total + 1] = head         -- informational heading
    else
      total = total + 1
      local n, _, flag = string.find(ln, "%s*%-%-%s*FAIL%s*$")
      if n then                         -- FAIL test case
        ln = string.sub(ln, 1, n - 1)   -- remove comment
        expect[total] = "FAIL"
        total_fail = total_fail + 1
      else                              -- PASS test case
        expect[total] = "PASS"
        total_pass = total_pass + 1
      end--n
      test[total] = ln
    end--m
  end--ln
end--for

print("Tests loaded: "..total.." (total), "
                      ..total_pass.." (passes), "
                      ..total_fail.." (fails)")

------------------------------------------------------------------------
-- verify test cases using native Lua
------------------------------------------------------------------------

local last_head = "TESTS: no heading yet"
for i = 1, total do
  local test_case, expected, head = test[i], expect[i], heading[i]
  -- show progress
  if head then
    last_head = head
    if DEBUG_FAILS then print("\n"..head.."\n") end
  end
  ------------------------------------------------------------------
  -- perform test
  local f, err = loadstring(test_case)
  -- look at outcome
  ------------------------------------------------------------------
  if f then-- actual PASS
    if expected == "FAIL" then
      print("\nVerified as PASS but expected to FAIL"..
            "\n-------------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      os.exit()
    end
  ------------------------------------------------------------------
  else-- actual FAIL
    if expected == "PASS" then
      print("\nVerified as FAIL but expected to PASS"..
            "\n-------------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      print("ERROR: "..err)
      os.exit()
    end
    if DEBUG_FAILS then
      print("TEST: "..test_case)
      print("ERROR: "..err.."\n")
    end
  ------------------------------------------------------------------
  end--f
end--for

print("Test cases verified using native Lua, no anomalies.")

------------------------------------------------------------------------
-- 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

------------------------------------------------------------------------
-- test using Yueliang front end
------------------------------------------------------------------------

local last_head = "TESTS: no heading yet"
for i = 1, total do
  local test_case, expected, head = test[i], expect[i], heading[i]
  -- show progress
  if head then last_head = head end
  ------------------------------------------------------------------
  -- perform test
  local LuaState = {}
  local zio = luaZ:init(luaZ:make_getS(test_case), nil)
  local status, func = pcall(luaY.parser, luaY, LuaState, zio, nil, "test")
  -- look at outcome
  ------------------------------------------------------------------
  if status then-- actual PASS
    if expected == "PASS" then
      -- actual PASS and expected PASS, so check binary chunks
      local writer, buff = luaU:make_setS()
      luaU:dump(LuaState, func, writer, buff)
      local bc1 = buff.data  -- Yueliang's output
      local f = loadstring(test_case, "test")
      local bc2 = string.dump(f)  -- Lua's output
      local die
      -- compare outputs
      if #bc1 ~= #bc2 then
        Dump(bc1, "bc1.out")
        Dump(bc2, "bc2.out")
        die = "binary chunk sizes different"
      elseif bc1 ~= bc2 then
        Dump(bc1, "bc1.out")
        Dump(bc2, "bc2.out")
        die = "binary chunks different"
      else
        -- everything checks out!
      end
      if die then
        print("\nTested PASS and expected to PASS, but chunks different"..
              "\n------------------------------------------------------")
        print("Reason: "..die)
        print("Lastest heading: "..last_head)
        print("TEST: "..test_case)
        os.exit()
      end
    else-- expected FAIL
      print("\nTested as PASS but expected to FAIL"..
            "\n-----------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      os.exit()
    end
  ------------------------------------------------------------------
  else-- actual FAIL
    if expected == "PASS" then
      print("\nTested as FAIL but expected to PASS"..
            "\n-----------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      print("ERROR: "..err)
      os.exit()
    end
  ------------------------------------------------------------------
  end--status
  io.stdout:write("\rTesting ["..i.."]...")
end--for
print(" done.")

print("Test cases run on Yueliang, no anomalies.")

-- end