Skip to content

Commit

Permalink
Simple file read, color print, include interception
Browse files Browse the repository at this point in the history
  • Loading branch information
DerelictDrone committed Nov 16, 2023
1 parent ab77d1e commit c2e89b5
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lua/wire/zvm/tests/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function CPUTest.RunCPU()
if CPUTest.VM.R0 == 4096 then
CPUTest.TestSuite.FinishTest(false)
else
print("R0 is not 4096! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.Error("R0 is not 4096! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

4 changes: 2 additions & 2 deletions lua/wire/zvm/tests/execute_from_iobus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ function CPUTest.RunCPU()
CPUTest.TestSuite.FinishTest(false)
else
PrintTable(CPUTest.IOBus)
print("R0 != 24, R0 = "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.Error("R0 != 24, R0 = "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

30 changes: 30 additions & 0 deletions lua/wire/zvm/tests/file_example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
-- Loads a file from the testing directory and returns it as a str
local src = TestSuite:LoadFile("file_example.txt")
TestSuite.Compile(src,nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 4096 then
CPUTest.TestSuite.FinishTest(false)
else
print("R0 is not 4096! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

3 changes: 3 additions & 0 deletions lua/wire/zvm/tests/file_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x:
INC R0
JMP x
30 changes: 30 additions & 0 deletions lua/wire/zvm/tests/includes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
-- Loads a file from the testing directory and returns it as a str
local src = TestSuite:LoadFile("includes_1.txt")
TestSuite.Compile(src,nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,16 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 2 then
CPUTest.TestSuite.FinishTest(false)
else
print("R0 is not 2! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error('hit a compile time error '..msg)
CPUTest.TestSuite.FinishTest(true)
end

2 changes: 2 additions & 0 deletions lua/wire/zvm/tests/includes_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <includes_2.txt>
ADD R0,1
1 change: 1 addition & 0 deletions lua/wire/zvm/tests/includes_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MOV R0,1
2 changes: 1 addition & 1 deletion lua/wire/zvm/tests/intentional_compile_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function CPUTest:RunTest(VM,TestSuite)
end

function CPUTest.RunCPU()
print('Compiler did not error when it should have!')
CPUTest.TestSuite.Error('Compiler did not error when it should have!')
CPUTest.TestSuite.FinishTest(true)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/wire/zvm/tests/intentional_failed_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function CPUTest.RunCPU()
if CPUTest.VM.R0 == 4095 then
CPUTest.TestSuite.FinishTest(false)
else
print("R0 is not 4095! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.Error("R0 is not 4095! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

4 changes: 2 additions & 2 deletions lua/wire/zvm/tests/no_internal_mem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ function CPUTest.RunCPU()
if CPUTest.VM.R0 == 1 then
CPUTest.TestSuite.FinishTest(false)
else
print("CPU with no ram/rom failed to execute code from bus! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.Error("CPU with no ram/rom failed to execute code from bus! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

8 changes: 4 additions & 4 deletions lua/wire/zvm/tests/virtualiobus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ function CPUTest.RunCPU()
if CPUTest.VM.R0 == 24 then
CPUTest.TestSuite.FinishTest(false)
else
print("CPU failed to read input port! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.Error("CPU failed to read input port! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.FinishTest(true)
end
else
print("CPU failed to write to output port! Port0 = "..IOBus.OutPorts[0])
CPUTest.TestSuite.Error("CPU failed to write to output port! Port0 = "..IOBus.OutPorts[0])
end
else
print("CPU wrote to input ports! "..tostring(IOBus:ReadCell(0)))
CPUTest.TestSuite.Error("CPU wrote to input ports! "..tostring(IOBus:ReadCell(0)))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

6 changes: 3 additions & 3 deletions lua/wire/zvm/tests/virtualmembus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ function CPUTest.RunCPU()
if CPUTest.VM.R1 == 1 then
CPUTest.TestSuite.FinishTest(false)
else
print('CPU failed to read the bus! R1 was '..tostring(CPUTest.VM.R1))
CPUTest.TestSuite.Error('CPU failed to read the bus! R1 was '..tostring(CPUTest.VM.R1))
CPUTest.TestSuite.FinishTest(true)
end
else
print("CPU failed to write to bus! "..tostring(bus:ReadCell(0)))
CPUTest.TestSuite.Error("CPU failed to write to bus! "..tostring(bus:ReadCell(0)))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.Error('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

41 changes: 37 additions & 4 deletions lua/wire/zvm/zvm_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ ZVMTestSuite = {
TestQueue = {},
TestStatuses = {}
}

local testDirectory = "wire/zvm/tests"
-- Make this dynamic, search directory under zvm/tests/ and run every lua in there

function ZVMTestSuite.RunAll()
local files,directories = file.Find(testDirectory..'/*.lua',"LUA","nameasc")
ZVMTestSuite.TestFiles = files or {}
Expand Down Expand Up @@ -46,10 +47,10 @@ function ZVMTestSuite.FinishTest(fail)
for ind,i in ipairs(ZVMTestSuite.TestFiles) do
if ZVMTestSuite.TestStatuses[ind] then
failed = failed + 1
print("Error in "..i)
MsgC(Color(255,0,0),"Error ",Color(255,255,255),"in "..i.."\n")
else
passed = passed + 1
print(i.." passed tests")
MsgC(Color(0,255,0),i.." passed tests".."\n")
end
end
local passmod, errormod = "",""
Expand All @@ -63,6 +64,21 @@ function ZVMTestSuite.FinishTest(fail)
end
end

function ZVMTestSuite.Error(...)
local args = ...
MsgC(Color(255,0,0),"in file ",Color(255,255,255),ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue],Color(255,0,0)," Error: ")
if args ~= nil then
if istable(args) then
for _,i in pairs(args) do
MsgC(Color(255,255,255),i)
end
else
MsgC(Color(255,255,255),tostring(args))
end
end
MsgC(Color(0,0,255),'\n')
end

function ZVMTestSuite.RunNextTest()
local curVM = CPULib.VirtualMachine()
curVM.Frequency = 2000
Expand All @@ -72,6 +88,10 @@ function ZVMTestSuite.RunNextTest()
CPUTest:RunTest(curVM,ZVMTestSuite)
end

function ZVMTestSuite:LoadFile(FileName)
return file.Read('lua/'..testDirectory..'/'..FileName,"GAME")
end

function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform)
ZVMTestSuite.CompileArgs = {
SourceCode = SourceCode,
Expand All @@ -84,13 +104,26 @@ function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,
timer.Simple(0.125,ZVMTestSuite.StartCompileInternal)
end

function ZVMTestSuite.InternalSuccessCallback()
HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile
ZVMTestSuite.CompileArgs.SuccessCallback()
end

function ZVMTestSuite.InternalErrorCallback()
HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile
ZVMTestSuite.CompileArgs.ErrorCallback()
end

function ZVMTestSuite.StartCompileInternal()
-- Swap loadfile function to load files from test folder
ZVMTestSuite.HCOMPLoadFile = HCOMP.LoadFile
HCOMP.LoadFile = ZVMTestSuite.LoadFile
local SourceCode = ZVMTestSuite.CompileArgs.SourceCode
local FileName = ZVMTestSuite.CompileArgs.FileName
local SuccessCallback = ZVMTestSuite.CompileArgs.SuccessCallback
local ErrorCallback = ZVMTestSuite.CompileArgs.ErrorCallback
local TargetPlatform = ZVMTestSuite.CompileArgs.TargetPlatform
CPULib.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform)
CPULib.Compile(SourceCode,FileName,ZVMTestSuite.InternalSuccessCallback,ErrorCallback,TargetPlatform)
end

function ZVMTestSuite.GetCompileBuffer()
Expand Down

0 comments on commit c2e89b5

Please sign in to comment.