From ccd3087fed4ea3e91386be9e0603bd9946e95ef7 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Wed, 15 Nov 2023 13:50:41 -0600 Subject: [PATCH 01/22] Basic test suite complete --- lua/autorun/cpu_load.lua | 6 +- lua/wire/cpulib.lua | 2 +- lua/wire/zvm/tests/example.lua | 28 ++ .../zvm/tests/intentional_compile_error.lua | 17 + .../zvm/tests/intentional_failed_test.lua | 28 ++ lua/wire/zvm/tests/virtualiobus.lua | 41 +++ lua/wire/zvm/tests/virtualmembus.lua | 36 +++ lua/wire/zvm/zvm_tests.lua | 291 ++++++++++++++++++ 8 files changed, 447 insertions(+), 2 deletions(-) create mode 100644 lua/wire/zvm/tests/example.lua create mode 100644 lua/wire/zvm/tests/intentional_compile_error.lua create mode 100644 lua/wire/zvm/tests/intentional_failed_test.lua create mode 100644 lua/wire/zvm/tests/virtualiobus.lua create mode 100644 lua/wire/zvm/tests/virtualmembus.lua create mode 100644 lua/wire/zvm/zvm_tests.lua diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index 77480eb..3305cb9 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -17,6 +17,7 @@ AddCSLuaFile("wire/zvm/zvm_features.lua") AddCSLuaFile("wire/zvm/zvm_opcodes.lua") AddCSLuaFile("wire/zvm/zvm_data.lua") + AddCSLuaFile("wire/cpulib.lua") include("wire/cpulib.lua") @@ -30,4 +31,7 @@ AddCSLuaFile("wire/cpu_default_data_files.lua") if CLIENT then include("wire/client/hlzasm/hc_compiler.lua") -end \ No newline at end of file +end + +AddCSLuaFile("wire/zvm/zvm_tests.lua") +include("wire/zvm/zvm_tests.lua") diff --git a/lua/wire/cpulib.lua b/lua/wire/cpulib.lua index 2e8690d..b317ddd 100644 --- a/lua/wire/cpulib.lua +++ b/lua/wire/cpulib.lua @@ -4,7 +4,7 @@ local INVALID_BREAKPOINT_IP = 2e7 CPULib = CPULib or {} -if CLIENT then +if CLIENT or TESTING then -- Sourcecode available as compiled binary CPULib.Source = "" -- Compiled binary diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua new file mode 100644 index 0000000..e9620bd --- /dev/null +++ b/lua/wire/zvm/tests/example.lua @@ -0,0 +1,28 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("x: INC R0 JMP x",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 + diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua new file mode 100644 index 0000000..ddcedf2 --- /dev/null +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -0,0 +1,17 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("MOV R0,",nil,CPUTest.RunCPU,CPUTest.CompileError) +end + +function CPUTest.RunCPU() + print('Compiler did not error when it should have!') + CPUTest.TestSuite.FinishTest(true) +end + +function CPUTest.CompileError() + CPUTest.TestSuite.FinishTest(false) +end + diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua new file mode 100644 index 0000000..ca54c38 --- /dev/null +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -0,0 +1,28 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("x: INC R0 JMP x",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 == 4095 then + CPUTest.TestSuite.FinishTest(false) + else + print("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.FinishTest(true) +end + diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua new file mode 100644 index 0000000..27d11fa --- /dev/null +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -0,0 +1,41 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0",nil,CPUTest.RunCPU,CPUTest.CompileError) +end + +function CPUTest.RunCPU() + CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(4) -- get external IO device of size 4 + CPUTest.TestSuite.Initialize(CPUTest.VM,nil,IOBus) -- reinitialize the CPU with the IOBus + IOBus.InPorts[0] = 24 + CPUTest.VM.Clk = 1 + for i=0,16 do + CPUTest.VM:RunStep() + end + + -- False = no error, True = error + if IOBus:ReadCell(0) == 24 then + if IOBus.OutPorts[0] == 1 then + if CPUTest.VM.R0 == 24 then + CPUTest.TestSuite.FinishTest(false) + else + print("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]) + end + else + print("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.FinishTest(true) +end + diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua new file mode 100644 index 0000000..f845c5b --- /dev/null +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -0,0 +1,36 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]",nil,CPUTest.RunCPU,CPUTest.CompileError) +end + +function CPUTest.RunCPU() + CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + local bus = CPUTest.TestSuite.CreateVirtualMemBus(4) -- get external ram device of size 4 + CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus + CPUTest.VM.Clk = 1 + for i=0,16 do + CPUTest.VM:RunStep() + end + + -- False = no error, True = error + if bus:ReadCell(0) == 1 then + 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.FinishTest(true) + end + else + print("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.FinishTest(true) +end + diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua new file mode 100644 index 0000000..ca70eb3 --- /dev/null +++ b/lua/wire/zvm/zvm_tests.lua @@ -0,0 +1,291 @@ +-------------------------------------------------------------------------------- +-- Library for making and running automated tests for Zyelios Virtual Machine Programs +-- +-- +-------------------------------------------------------------------------------- + +TESTING = true +include("wire/cpulib.lua") +include("wire/client/hlzasm/hc_compiler.lua") + + +ZVMTestSuite = { + TestFiles = {}, + 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 {} + ZVMTestSuite.TestQueue = {} + ZVMTestSuite.TestStatuses = {} + for ind,i in ipairs(ZVMTestSuite.TestFiles) do -- copy with reversed indexes so we can use cheap popping + ZVMTestSuite.TestQueue[(#ZVMTestSuite.TestFiles)+1-ind] = i + end + print(#ZVMTestSuite.TestFiles.." tests loaded") + PrintTable(ZVMTestSuite.TestFiles) + ZVMTestSuite.RunNextTest() + print("Game needs to be unpaused in order to run the tests (estimated time to completion "..((#ZVMTestSuite.TestQueue)*0.25).." seconds)") +end + +function ZVMTestSuite.FinishTest(fail) + local finalFail = false + if fail == nil then + finalFail = true + else + finalFail = fail + end + ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil + ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] = nil + if #ZVMTestSuite.TestQueue > 0 then + timer.Simple(0.25,ZVMTestSuite.RunNextTest) + else + for ind,i in ipairs(ZVMTestSuite.TestFiles) do + if ZVMTestSuite.TestStatuses[ind] then + print("Error in "..i) + else + print(i.." passed tests") + end + end + end +end + +function ZVMTestSuite.RunNextTest() + local curVM = CPULib.VirtualMachine() + curVM.Frequency = 2000 + print("Running "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + ZVMTestSuite.AddVirtualFunctions(curVM) + include(testDirectory..'/'..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + CPUTest:RunTest(curVM,ZVMTestSuite) +end + +function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform) + CPULib.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform) +end + +function ZVMTestSuite.GetCompileBuffer() + return CPULib.Buffer +end + +function ZVMTestSuite.CreateVirtualMemBus(MembusSize) + local virtualMemBus = {Size = MembusSize} + function virtualMemBus:ReadCell(Address) + if Address < self.Size and Address > -1 then + return virtualMemBus[Address] + end + end + function virtualMemBus:WriteCell(Address,Value) + if Address < self.Size and Address > -1 then + virtualMemBus[Address] = Value + return true + end + return false + end + return virtualMemBus +end + +function ZVMTestSuite.CreateVirtualIOBus(IOBusSize) + local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize} + function virtualIOBus:ReadCell(Address) + if Address < self.Size and Address > -1 then + return self.InPorts[Address] + end + end + function virtualIOBus:WriteCell(Address,Value) + if Address < self.Size and Address > -1 then + self.OutPorts[Address] = Value + return true + end + return false + end + return virtualIOBus +end + +function ZVMTestSuite.AddVirtualFunctions(VM) + function VM:FlashData(data) + ZVMTestSuite:FlashData(self,data) + end + function VM:RunStep() + ZVMTestSuite:Run(self) + end + function VM:TriggerInput(iname,name) + ZVMTestSuite(self,iname,name) + end + function VM:SignalError(errorcode) + self.Error = errorcode + end +end + +function ZVMTestSuite.FlashData(VM,data) + VM:Reset() + for k,v in pairs(data) do + VM:WriteCell(k,tonumber(v) or 0) + if (k >= 0) and (k < VM.ROMSize) then + VM.ROM[k] = tonumber(v) or 0 + end + end +end + +-- Execute ZCPU virtual machine +function ZVMTestSuite:Run(VM) + -- Calculate time-related variables + local CurrentTime = CurTime() + local DeltaTime = math.min(1/30,CurrentTime - (VM.PreviousTime or 0)) + VM.PreviousTime = CurrentTime + + -- Check if need to run till specific instruction + if VM.BreakpointInstructions then + VM.TimerDT = DeltaTime + VM.CPUIF = VM + VM:Step(8,function(VM) + VM:Dyn_Emit("if (VM.CPUIF.Clk and not VM.CPUIF.VMStopped) and (VM.CPUIF.OnVMStep) then") + VM:Dyn_EmitState() + VM:Emit("VM.CPUIF.OnVMStep()") + VM:Emit("end") + VM:Emit("if VM.CPUIF.BreakpointInstructions[VM.IP] then") + VM:Dyn_EmitState() + VM:Emit("VM.CPUIF.OnBreakpointInstruction(VM.IP)") + VM:Emit("VM.CPUIF.VMStopped = true") + VM:Emit("VM.TMR = VM.TMR + "..VM.PrecompileInstruction) + VM:Emit("VM.CODEBYTES = VM.CODEBYTES + "..VM.PrecompileBytes) + VM:Emit("if true then return end") + VM:Emit("end") + VM:Emit("if VM.CPUIF.LastInstruction and ((VM.IP > VM.CPUIF.LastInstruction) or VM.CPUIF.ForceLastInstruction) then") + VM:Dyn_EmitState() + VM:Emit("VM.CPUIF.ForceLastInstruction = nil") + VM:Emit("VM.CPUIF.OnLastInstruction()") + VM:Emit("VM.CPUIF.VMStopped = true") + VM:Emit("VM.TMR = VM.TMR + "..VM.PrecompileInstruction) + VM:Emit("VM.CODEBYTES = VM.CODEBYTES + "..VM.PrecompileBytes) + VM:Emit("if true then return end") + VM:Emit("end") + end) + VM.CPUIF = nil + else + -- How many steps VM must make to keep up with execution + local Cycles = math.max(1,math.floor(VM.Frequency*DeltaTime*0.5)) + VM.TimerDT = (DeltaTime/Cycles) + + while (Cycles > 0) and (VM.Clk) and (not VMStopped) and (VM.Idle == 0) do + -- Run VM step + local previousTMR = VM.TMR + VM:Step() + Cycles = Cycles - math.max(1, VM.TMR - previousTMR) + end + end + + -- Update VM timer + VM.TIMER = VM.TIMER + DeltaTime + + -- Reset idle register + VM.Idle = 0 +end + +function ZVMTestSuite.TriggerInput(VM, iname, value) + if iname == "Clk" then + VM.Clk = (value >= 1) + if VM.Clk then + VM.VMStopped = false + end + elseif iname == "Frequency" then + if value > 0 then VM.Frequency = math.floor(value) end + elseif iname == "Reset" then --VM may be nil + if VM.HWDEBUG ~= 0 then + VM.DBGSTATE = math.floor(value) + if (value > 0) and (value <= 1.0) then VM:Reset() end + else + if value >= 1.0 then VM:Reset() end + end + -- Wire_TriggerOutput(VM, "Error", 0) + elseif iname == "Interrupt" then + if (value >= 32) and (value < 256) then + if (VM.Clk and not VM.VMStopped) then VM:ExternalInterrupt(math.floor(value)) end + end + end +end + +function ZVMTestSuite.Initialize(VM,Membus,IOBus) + -- CPU platform settings + VM.Clk = false -- whether the Clk input is on + VM.VMStopped = false -- whether the VM has halted itself (e.g. by running off the end of the program) + VM.Frequency = 2000 + -- Create virtual machine + VM.VM = CPULib.VirtualMachine() + VM.SerialNo = CPULib.GenerateSN("CPU") + VM:Reset() + + VM.SignalError = function(VM,errorCode) + Wire_TriggerOutput(VM, "Error", errorCode) + end + VM.SignalShutdown = function(VM) + VM.VMStopped = true + end + VM.ExternalWrite = function(VM,Address,Value) + if Address >= 0 then -- Use MemBus + local MemBusSource = Membus + if MemBusSource then + if MemBusSource.ReadCell then + local result = MemBusSource:WriteCell(Address-VM.RAMSize,Value) + if result then return true + else VM:Interrupt(7,Address) return false + end + else VM:Interrupt(8,Address) return false + end + else VM:Interrupt(7,Address) return false + end + else -- Use IOBus + local IOBusSource = IOBus + if IOBusSource then + if IOBusSource.ReadCell then + local result = IOBusSource:WriteCell(-Address-1,Value) + if result then return true + else VM:Interrupt(10,-Address-1) return false + end + else VM:Interrupt(8,Address+1) return false + end + else return true + end + end + end + VM.ExternalRead = function(VM,Address) + if Address >= 0 then -- Use MemBus + local MemBusSource = Membus + if MemBusSource then + if MemBusSource.ReadCell then + local result = MemBusSource:ReadCell(Address-VM.RAMSize) + if isnumber(result) then return result + else VM:Interrupt(7,Address) return + end + else VM:Interrupt(8,Address) return + end + else VM:Interrupt(7,Address) return + end + else -- Use IOBus + local IOBusSource = IOBus + if IOBusSource then + if IOBusSource.ReadCell then + local result = IOBusSource:ReadCell(-Address-1) + if isnumber(result) then return result + else VM:Interrupt(10,-Address-1) return + end + else VM:Interrupt(8,Address+1) return + end + else return 0 + end + end + end + + local oldReset = VM.Reset + VM.Reset = function(...) + if VM.Clk and VM.VMStopped then + VM:NextThink(CurTime()) + end + VM.VMStopped = false + return oldReset(...) + end +end + + +concommand.Add("ZCPU_RUN_AUTO_TESTS",ZVMTestSuite.RunAll,nil,"runs zcpu tests") From 2338ac25c027e89c84aeb89f70652f8bb9e5efce Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Wed, 15 Nov 2023 21:55:08 -0600 Subject: [PATCH 02/22] IOBus execution test + Fixed IOBus/Membus readcell --- lua/wire/zvm/tests/execute_from_iobus.lua | 54 +++++++++++++++++++++++ lua/wire/zvm/zvm_tests.lua | 27 +++++++++--- 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 lua/wire/zvm/tests/execute_from_iobus.lua diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua new file mode 100644 index 0000000..aca74ad --- /dev/null +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -0,0 +1,54 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2",nil,CPUTest.CompileNext,CPUTest.CompileError) + -- end result of the above code should be R0 = 24 +end + +function CPUTest.CompileNext() + local buff = CPUTest.TestSuite.GetCompileBuffer() + local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff+1) -- create an IOBus large enough to hold this code + PrintTable(buff) + CPUTest.IOBus = IOBus + -- reverse the compiled code, the CPU will read them in reverse if it's in the IOBus + -- because CS will be negative, and IP only increments + -- ipairs won't index 0 and the cpu compile buffer uses 0 + for i=0,#buff do + IOBus.InPorts[#buff-i] = buff[i] + end + IOBus.OldReadCell = IOBus.ReadCell + IOBus.AccessLog = {} + function IOBus:ReadCell(address) + IOBus.AccessLog[#IOBus.AccessLog+1] = {"read address"..tostring(address),self:OldReadCell(address) or "no value"} + return self:OldReadCell(address) + end + -- JMPF jumps to 0 IP, CS = (code length+1)*-1 because first index of IOBus is "cell -1" of extern read/write + local generatedcode = "CMP R0,0 JNER -3 JMPF 0,"..(#buff+1)*-1 + CPUTest.TestSuite.Compile(generatedcode,nil,CPUTest.RunCPU,CPUTest.CompileError) +end + +function CPUTest.RunCPU() + CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + CPUTest.TestSuite.Initialize(CPUTest.VM,nil,CPUTest.IOBus) -- reinitialize the CPU with the IOBus + CPUTest.VM.Clk = 1 + for i=0,32 do + CPUTest.VM:RunStep() + end + + -- False = no error, True = error + if CPUTest.VM.R0 == 24 then + CPUTest.TestSuite.FinishTest(false) + else + PrintTable(CPUTest.IOBus) + print("R0 != 24, R0 = "..tostring(CPUTest.VM.R0)) + CPUTest.TestSuite.FinishTest(true) + end +end + +function CPUTest.CompileError() + print('hit a compile time error') + CPUTest.TestSuite.FinishTest(true) +end + diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index ca70eb3..c265d1d 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -62,6 +62,23 @@ function ZVMTestSuite.RunNextTest() end function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform) + ZVMTestSuite.CompileArgs = { + SourceCode = SourceCode, + FileName = FileName, + SuccessCallback = SuccessCallback, + ErrorCallback = ErrorCallback, + TargetPlatform = TargetPlatform + } + -- Needs to delay next compile otherwise it'll halt, this will make it easier. + timer.Simple(0.125,ZVMTestSuite.StartCompileInternal) +end + +function ZVMTestSuite.StartCompileInternal() + 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) end @@ -72,12 +89,12 @@ end function ZVMTestSuite.CreateVirtualMemBus(MembusSize) local virtualMemBus = {Size = MembusSize} function virtualMemBus:ReadCell(Address) - if Address < self.Size and Address > -1 then + if Address <= self.Size and Address > -1 then return virtualMemBus[Address] end end function virtualMemBus:WriteCell(Address,Value) - if Address < self.Size and Address > -1 then + if Address <= self.Size and Address > -1 then virtualMemBus[Address] = Value return true end @@ -87,14 +104,14 @@ function ZVMTestSuite.CreateVirtualMemBus(MembusSize) end function ZVMTestSuite.CreateVirtualIOBus(IOBusSize) - local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize} + local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize-1} function virtualIOBus:ReadCell(Address) - if Address < self.Size and Address > -1 then + if Address <= self.Size and Address > -1 then return self.InPorts[Address] end end function virtualIOBus:WriteCell(Address,Value) - if Address < self.Size and Address > -1 then + if Address <= self.Size and Address > -1 then self.OutPorts[Address] = Value return true end From ab77d1ea1ea92dc8ed8ddd9aee04669207a85b98 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Wed, 15 Nov 2023 22:27:33 -0600 Subject: [PATCH 03/22] No internal ram/rom external test + fixes --- lua/wire/zvm/tests/execute_from_iobus.lua | 1 - lua/wire/zvm/tests/no_internal_mem.lua | 34 +++++++++++++++++++++++ lua/wire/zvm/zvm_tests.lua | 29 ++++++++++++++----- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 lua/wire/zvm/tests/no_internal_mem.lua diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index aca74ad..1391d5a 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -10,7 +10,6 @@ end function CPUTest.CompileNext() local buff = CPUTest.TestSuite.GetCompileBuffer() local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff+1) -- create an IOBus large enough to hold this code - PrintTable(buff) CPUTest.IOBus = IOBus -- reverse the compiled code, the CPU will read them in reverse if it's in the IOBus -- because CS will be negative, and IP only increments diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua new file mode 100644 index 0000000..6750f42 --- /dev/null +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -0,0 +1,34 @@ +CPUTest = {} + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + TestSuite.Compile("MOV R0,1",nil,CPUTest.RunCPU,CPUTest.CompileError) +end + +function CPUTest.RunCPU() + local buff = CPUTest.TestSuite.GetCompileBuffer() + local bus = CPUTest.TestSuite.CreateVirtualMemBus(#buff) -- get external ram device large enough to hold program + CPUTest.TestSuite.FlashData(bus,buff) -- upload compiled to membus + CPUTest.VM.RAMSize = 0 + CPUTest.VM.ROMSize = 0 + CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus + CPUTest.VM.Clk = 1 + for i=0,16 do + CPUTest.VM:RunStep() + end + + -- False = no error, True = error + 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.FinishTest(true) + end +end + +function CPUTest.CompileError() + print('hit a compile time error') + CPUTest.TestSuite.FinishTest(true) +end + diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index c265d1d..2ef75ac 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -39,16 +39,27 @@ function ZVMTestSuite.FinishTest(fail) end ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] = nil + local passed, failed = 0, 0 if #ZVMTestSuite.TestQueue > 0 then - timer.Simple(0.25,ZVMTestSuite.RunNextTest) + timer.Simple(0.125,ZVMTestSuite.RunNextTest) else for ind,i in ipairs(ZVMTestSuite.TestFiles) do if ZVMTestSuite.TestStatuses[ind] then + failed = failed + 1 print("Error in "..i) else + passed = passed + 1 print(i.." passed tests") end end + local passmod, errormod = "","" + if passed > 1 then + passmod = "s" + end + if failed > 1 then + errormod = "s" + end + print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod) end end @@ -69,7 +80,7 @@ function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback, ErrorCallback = ErrorCallback, TargetPlatform = TargetPlatform } - -- Needs to delay next compile otherwise it'll halt, this will make it easier. + -- Needs to delay next compile otherwise it'll halt if a test compiles two or more programs. timer.Simple(0.125,ZVMTestSuite.StartCompileInternal) end @@ -128,7 +139,7 @@ function ZVMTestSuite.AddVirtualFunctions(VM) ZVMTestSuite:Run(self) end function VM:TriggerInput(iname,name) - ZVMTestSuite(self,iname,name) + ZVMTestSuite.TriggerInput(self,iname,name) end function VM:SignalError(errorcode) self.Error = errorcode @@ -136,11 +147,15 @@ function ZVMTestSuite.AddVirtualFunctions(VM) end function ZVMTestSuite.FlashData(VM,data) - VM:Reset() + if VM.Reset then + VM:Reset() + end for k,v in pairs(data) do VM:WriteCell(k,tonumber(v) or 0) - if (k >= 0) and (k < VM.ROMSize) then - VM.ROM[k] = tonumber(v) or 0 + if VM.ROMSize then + if (k >= 0) and (k < VM.ROMSize) then + VM.ROM[k] = tonumber(v) or 0 + end end end end @@ -297,7 +312,7 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) local oldReset = VM.Reset VM.Reset = function(...) if VM.Clk and VM.VMStopped then - VM:NextThink(CurTime()) + --VM:NextThink(CurTime()) end VM.VMStopped = false return oldReset(...) From c2e89b52d88c50a95eac4ed30874fd955a6b5fac Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Wed, 15 Nov 2023 23:59:52 -0600 Subject: [PATCH 04/22] Simple file read, color print, include interception --- lua/wire/zvm/tests/example.lua | 4 +- lua/wire/zvm/tests/execute_from_iobus.lua | 4 +- lua/wire/zvm/tests/file_example.lua | 30 ++++++++++++++ lua/wire/zvm/tests/file_example.txt | 3 ++ lua/wire/zvm/tests/includes.lua | 30 ++++++++++++++ lua/wire/zvm/tests/includes_1.txt | 2 + lua/wire/zvm/tests/includes_2.txt | 1 + .../zvm/tests/intentional_compile_error.lua | 2 +- .../zvm/tests/intentional_failed_test.lua | 4 +- lua/wire/zvm/tests/no_internal_mem.lua | 4 +- lua/wire/zvm/tests/virtualiobus.lua | 8 ++-- lua/wire/zvm/tests/virtualmembus.lua | 6 +-- lua/wire/zvm/zvm_tests.lua | 41 +++++++++++++++++-- 13 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 lua/wire/zvm/tests/file_example.lua create mode 100644 lua/wire/zvm/tests/file_example.txt create mode 100644 lua/wire/zvm/tests/includes.lua create mode 100644 lua/wire/zvm/tests/includes_1.txt create mode 100644 lua/wire/zvm/tests/includes_2.txt diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index e9620bd..a6391a5 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -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 diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index 1391d5a..da4defd 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -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 diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua new file mode 100644 index 0000000..9e539ba --- /dev/null +++ b/lua/wire/zvm/tests/file_example.lua @@ -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 + diff --git a/lua/wire/zvm/tests/file_example.txt b/lua/wire/zvm/tests/file_example.txt new file mode 100644 index 0000000..b0a5999 --- /dev/null +++ b/lua/wire/zvm/tests/file_example.txt @@ -0,0 +1,3 @@ +x: +INC R0 +JMP x \ No newline at end of file diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua new file mode 100644 index 0000000..4ba52a8 --- /dev/null +++ b/lua/wire/zvm/tests/includes.lua @@ -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 + diff --git a/lua/wire/zvm/tests/includes_1.txt b/lua/wire/zvm/tests/includes_1.txt new file mode 100644 index 0000000..c419b54 --- /dev/null +++ b/lua/wire/zvm/tests/includes_1.txt @@ -0,0 +1,2 @@ +#include +ADD R0,1 \ No newline at end of file diff --git a/lua/wire/zvm/tests/includes_2.txt b/lua/wire/zvm/tests/includes_2.txt new file mode 100644 index 0000000..1c24af2 --- /dev/null +++ b/lua/wire/zvm/tests/includes_2.txt @@ -0,0 +1 @@ +MOV R0,1 \ No newline at end of file diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index ddcedf2..4bbd740 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -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 diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index ca54c38..af014a9 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -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 diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index 6750f42..6257a58 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -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 diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index 27d11fa..64de885 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -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 diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index f845c5b..3bf1102 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -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 diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 2ef75ac..90c6c26 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -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 {} @@ -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 = "","" @@ -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 @@ -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, @@ -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() From 779ea6eac64047b5002239fd69cf1f1a48dd3b6c Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 01:21:37 -0600 Subject: [PATCH 05/22] Ifdef test, Access CPUName + reformatting --- lua/wire/zvm/tests/example.lua | 5 +- lua/wire/zvm/tests/execute_from_iobus.lua | 5 +- lua/wire/zvm/tests/file_example.lua | 5 +- lua/wire/zvm/tests/ifdefs.lua | 86 +++++++++++++++++++ lua/wire/zvm/tests/ifdefs.txt | 38 ++++++++ lua/wire/zvm/tests/includes.lua | 1 - .../zvm/tests/intentional_compile_error.lua | 1 - .../zvm/tests/intentional_failed_test.lua | 5 +- lua/wire/zvm/tests/no_internal_mem.lua | 5 +- lua/wire/zvm/tests/virtualiobus.lua | 5 +- lua/wire/zvm/tests/virtualmembus.lua | 5 +- lua/wire/zvm/zvm_tests.lua | 4 + 12 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 lua/wire/zvm/tests/ifdefs.lua create mode 100644 lua/wire/zvm/tests/ifdefs.txt diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index a6391a5..3413ca7 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -21,8 +21,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index da4defd..885b78b 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -46,8 +46,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua index 9e539ba..09f48c8 100644 --- a/lua/wire/zvm/tests/file_example.lua +++ b/lua/wire/zvm/tests/file_example.lua @@ -23,8 +23,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - print('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua new file mode 100644 index 0000000..2edcdf1 --- /dev/null +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -0,0 +1,86 @@ +CPUTest = {} + +--not x and y = 14, name "Test Y" +-- x and y = 11, name "Test Y" +-- x and not y = 1, name "Test Y" + +-- culling update +-- not x and y = 12, name "Test Y" +-- x and y = 11, name "Test X and Y" +-- x and not y = 1, name "Test X" + + +CPUTest.ExpectedVariations1 = {"X","Y","X and Y","Y","Y","Y"} -- CPU Name vars +CPUTest.ExpectedVariations2 = {1,12,11,1,14,11} +CPUTest.ResultVariations1 = {} +CPUTest.ResultVariations2 = {} + +CPUTest.Variations1 = {"true","false"} +CPUTest.Variations2 = {"#define x\n","#define y\n","#define x\n#define y\n"} +CPUTest.Variation1Index = 1 +CPUTest.Variation2Index = 1 + +function CPUTest:RunTest(VM,TestSuite) + CPUTest.VM = VM + CPUTest.TestSuite = TestSuite + -- Loads a file from the testing directory and returns it as a str + CPUTest.Src = TestSuite:LoadFile("ifdefs.txt") + CPUTest.CompileNext() +end + +function CPUTest.CompileNext() + local cursrc + if CPUTest.Variation1Index <= #CPUTest.Variations1 then + cursrc = "#pragma set NewIfDefs "..CPUTest.Variations1[CPUTest.Variation1Index].."\n" + else + return CPUTest.CompareResults() + end + if CPUTest.Variation2Index <= #CPUTest.Variations2 then + cursrc = cursrc..CPUTest.Variations2[CPUTest.Variation2Index].."\n"..CPUTest.Src + CPUTest.TestSuite.Compile(cursrc,nil,CPUTest.LogResults,CPUTest.CompileError) + else + CPUTest.Variation1Index = CPUTest.Variation1Index + 1 + CPUTest.Variation2Index = 1 + CPUTest.CompileNext() + end +end + +function CPUTest.LogResults() + CPUTest.ResultVariations1[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = CPUTest.TestSuite.GetCPUName() or "ERROR" + CPUTest.ResultVariations2[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = #CPUTest.TestSuite.GetCompileBuffer()+1 or "ERROR" + CPUTest.Variation2Index = CPUTest.Variation2Index + 1 + CPUTest.CompileNext() +end + +function CPUTest.CompareResults() + local fail,results1,results2 = false,{},{} + for ind,i in ipairs(CPUTest.ExpectedVariations1) do + if CPUTest.ResultVariations1[ind] == "Test "..i then + results1[ind] = true + else + fail = true + results1[ind] = false + end + end + for ind,i in ipairs(CPUTest.ExpectedVariations2) do + if CPUTest.ResultVariations2[ind] == i then + results2[ind] = true + else + fail = true + results2[ind] = false + end + end + if fail then + CPUTest.TestSuite.Error('Unexpected test results!') + PrintTable({CPUTest.ResultVariations1,results1,CPUTest.ResultVariations2,results2}) + CPUTest.TestSuite.FinishTest(true) + else + CPUTest.TestSuite.FinishTest(false) + end +end + +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.FinishTest(true) +end + diff --git a/lua/wire/zvm/tests/ifdefs.txt b/lua/wire/zvm/tests/ifdefs.txt new file mode 100644 index 0000000..c0cd75c --- /dev/null +++ b/lua/wire/zvm/tests/ifdefs.txt @@ -0,0 +1,38 @@ +/*defs will be added programatically*/ + +#ifdef x +#define z +#pragma cpuname Test X + ALLOC 1 +#ifdef y +#ifdef x +#pragma cpuname Test X and Y +#endif + ALLOC 2 +#endif +//not x +#else + ALLOC 4 +#endif + +#ifdef y +#ifndef x +#pragma cpuname Test Y +#endif +ALLOC 8 +#endif +/* +#ifdef y + ALLOC 8 +#endif +*/ +//above comment intentional for making sure ifdef handler doesn't skip into the middle of a comment + +// not x and y = 14, name "Test Y" +// x and y = 11, name "Test Y" +// x and not y = 1, name "Test Y" + +// culling update +// not x and y = 12, name "Test Y" +// x and y = 11, name "Test X and Y" +// x and not y = 1, name "Test X" diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index 4ba52a8..09550fd 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -27,4 +27,3 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index 4bbd740..082e2fd 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -14,4 +14,3 @@ end function CPUTest.CompileError() CPUTest.TestSuite.FinishTest(false) end - diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index af014a9..c293ffd 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -21,8 +21,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index 6257a58..05cc47c 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -27,8 +27,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index 64de885..98636d9 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -34,8 +34,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index 3bf1102..db783bb 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -29,8 +29,7 @@ function CPUTest.RunCPU() end end -function CPUTest.CompileError() - CPUTest.TestSuite.Error('hit a compile time error') +function CPUTest.CompileError(msg) + CPUTest.TestSuite.Error('hit a compile time error '..msg) CPUTest.TestSuite.FinishTest(true) end - diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 90c6c26..5ab9143 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -130,6 +130,10 @@ function ZVMTestSuite.GetCompileBuffer() return CPULib.Buffer end +function ZVMTestSuite.GetCPUName() + return CPULib.CPUName +end + function ZVMTestSuite.CreateVirtualMemBus(MembusSize) local virtualMemBus = {Size = MembusSize} function virtualMemBus:ReadCell(Address) From 92ef735279ed20f9f52db4ccc4331f7a10f521d4 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 01:42:34 -0600 Subject: [PATCH 06/22] Smarter ConCMD, Run tests by name(with wildcards) --- lua/wire/zvm/zvm_tests.lua | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 5ab9143..2ecb5d7 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -17,9 +17,34 @@ ZVMTestSuite = { local testDirectory = "wire/zvm/tests" +function ZVMTestSuite.CMDRun(_,_,_,names) + ZVMTestSuite.TestFiles = {} + for filename in string.gmatch(names, '[^,]+') do + local files = file.Find('lua/'..testDirectory..'/'..filename..'.lua',"GAME") + for _,i in ipairs(files) do + ZVMTestSuite.TestFiles[#ZVMTestSuite.TestFiles+1] = i + end + end + if #ZVMTestSuite.TestFiles == 0 then + if names ~= nil then + if names ~= "" then + print("Didn't find any tests with name(s): "..names) + return + end + end + ZVMTestSuite.RunAll() + else + ZVMTestSuite.StartTesting() + end +end + function ZVMTestSuite.RunAll() local files,directories = file.Find(testDirectory..'/*.lua',"LUA","nameasc") ZVMTestSuite.TestFiles = files or {} + ZVMTestSuite.StartTesting() +end + +function ZVMTestSuite.StartTesting() ZVMTestSuite.TestQueue = {} ZVMTestSuite.TestStatuses = {} for ind,i in ipairs(ZVMTestSuite.TestFiles) do -- copy with reversed indexes so we can use cheap popping @@ -357,4 +382,4 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) end -concommand.Add("ZCPU_RUN_AUTO_TESTS",ZVMTestSuite.RunAll,nil,"runs zcpu tests") +concommand.Add("ZCPU_RUN_TESTS",ZVMTestSuite.CMDRun,nil,"Runs ZCPU Tests, pass a comma delimited list to only run tests with those names\nExample: ZCPU_RUN_TESTS example,file_example\n\nRun without args to run all tests") From 8b25c6b49c6a5c27e1770032e7dff4b1a6b2de59 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 01:46:10 -0600 Subject: [PATCH 07/22] Reorganize cpu_load.lua --- lua/autorun/cpu_load.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index 3305cb9..cbb179b 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -16,7 +16,8 @@ AddCSLuaFile("wire/zvm/zvm_core.lua") AddCSLuaFile("wire/zvm/zvm_features.lua") AddCSLuaFile("wire/zvm/zvm_opcodes.lua") AddCSLuaFile("wire/zvm/zvm_data.lua") - +AddCSLuaFile("wire/zvm/zvm_tests.lua") +include("wire/zvm/zvm_tests.lua") AddCSLuaFile("wire/cpulib.lua") include("wire/cpulib.lua") @@ -33,5 +34,3 @@ if CLIENT then include("wire/client/hlzasm/hc_compiler.lua") end -AddCSLuaFile("wire/zvm/zvm_tests.lua") -include("wire/zvm/zvm_tests.lua") From 004913b602dcb538a496989dba7be4bdb93699d9 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 02:22:37 -0600 Subject: [PATCH 08/22] Don't append a VM to your VM + Init VM before test --- lua/wire/zvm/zvm_tests.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 2ecb5d7..fade091 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -106,7 +106,7 @@ end function ZVMTestSuite.RunNextTest() local curVM = CPULib.VirtualMachine() - curVM.Frequency = 2000 + ZVMTestSuite.Initialize(curVM) print("Running "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) ZVMTestSuite.AddVirtualFunctions(curVM) include(testDirectory..'/'..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) @@ -306,7 +306,6 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) VM.VMStopped = false -- whether the VM has halted itself (e.g. by running off the end of the program) VM.Frequency = 2000 -- Create virtual machine - VM.VM = CPULib.VirtualMachine() VM.SerialNo = CPULib.GenerateSN("CPU") VM:Reset() From e07660b8e44a02ba73dd65d4a7fc4e39c0649e01 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 12:50:35 -0600 Subject: [PATCH 09/22] Suggestions pass 1 + Make command SP Only --- lua/autorun/cpu_load.lua | 7 +++++-- lua/wire/cpulib.lua | 7 ++++--- lua/wire/zvm/zvm_tests.lua | 37 ++++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index cbb179b..c48c441 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -16,8 +16,11 @@ AddCSLuaFile("wire/zvm/zvm_core.lua") AddCSLuaFile("wire/zvm/zvm_features.lua") AddCSLuaFile("wire/zvm/zvm_opcodes.lua") AddCSLuaFile("wire/zvm/zvm_data.lua") -AddCSLuaFile("wire/zvm/zvm_tests.lua") -include("wire/zvm/zvm_tests.lua") + +if game.Singleplayer() then + AddCSLuaFile("wire/zvm/zvm_tests.lua") + include("wire/zvm/zvm_tests.lua") +end AddCSLuaFile("wire/cpulib.lua") include("wire/cpulib.lua") diff --git a/lua/wire/cpulib.lua b/lua/wire/cpulib.lua index b317ddd..d0cfb69 100644 --- a/lua/wire/cpulib.lua +++ b/lua/wire/cpulib.lua @@ -34,6 +34,7 @@ if CLIENT or TESTING then CPULib.Debugger.PointersByLine = {} CPULib.Debugger.Breakpoint = {} + CPULib.print = print -- Convars to control CPULib local wire_cpu_upload_speed = CreateClientConVar("wire_cpu_upload_speed",1000,false,false) @@ -69,7 +70,7 @@ if CLIENT or TESTING then -- Start compiling the sourcecode HCOMP:StartCompile(source,fileName or "source",CPULib.OnWriteByte,nil) HCOMP.Settings.CurrentPlatform = targetPlatform or "CPU" - print("=== HL-ZASM High Level Assembly Compiler Output ==") + CPULib.print("=== HL-ZASM High Level Assembly Compiler Output ==") -- Initialize callbacks CPULib.SuccessCallback = successCallback @@ -147,14 +148,14 @@ if CLIENT or TESTING then for _ = 1, compile_speed do local status,result = pcall(HCOMP.Compile,HCOMP) if not status then - print("==================================================") + CPULib.print("==================================================") if CPULib.ErrorCallback then CPULib.ErrorCallback(HCOMP.ErrorMessage or ("Internal error: "..result),HCOMP.ErrorPosition) end timer.Remove("cpulib_compile") CPULib.Compiling = false return elseif not result then - print("==================================================") + CPULib.print("==================================================") CPULib.Source = CPULib.CurrentSource CPULib.Buffer = CPULib.CurrentBuffer diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index fade091..e40a1e0 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -12,7 +12,9 @@ include("wire/client/hlzasm/hc_compiler.lua") ZVMTestSuite = { TestFiles = {}, TestQueue = {}, - TestStatuses = {} + TestStatuses = {}, + Warnings = 0, + CurrentWarnings = 0 } local testDirectory = "wire/zvm/tests" @@ -63,19 +65,23 @@ function ZVMTestSuite.FinishTest(fail) else finalFail = fail end + if ZVMTestSuite.CurrentWarnings > 0 then + print("Compiler Warnings from "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue].." "..ZVMTestSuite.CurrentWarnings) + ZVMTestSuite.CurrentWarnings = 0 + end ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] = nil - local passed, failed = 0, 0 if #ZVMTestSuite.TestQueue > 0 then timer.Simple(0.125,ZVMTestSuite.RunNextTest) else + local passed, failed = 0, 0 for ind,i in ipairs(ZVMTestSuite.TestFiles) do if ZVMTestSuite.TestStatuses[ind] then failed = failed + 1 MsgC(Color(255,0,0),"Error ",Color(255,255,255),"in "..i.."\n") else passed = passed + 1 - MsgC(Color(0,255,0),i.." passed tests".."\n") + --MsgC(Color(0,255,0),i.." passed tests".."\n") end end local passmod, errormod = "","" @@ -85,7 +91,7 @@ function ZVMTestSuite.FinishTest(fail) if failed > 1 then errormod = "s" end - print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod) + print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod.." Compiler Warnings: "..ZVMTestSuite.Warnings) end end @@ -108,7 +114,6 @@ function ZVMTestSuite.RunNextTest() local curVM = CPULib.VirtualMachine() ZVMTestSuite.Initialize(curVM) print("Running "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) - ZVMTestSuite.AddVirtualFunctions(curVM) include(testDirectory..'/'..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) CPUTest:RunTest(curVM,ZVMTestSuite) end @@ -131,11 +136,15 @@ end function ZVMTestSuite.InternalSuccessCallback() HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile + CPULib.print = ZVMTestSuite.oldCPUprint + HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning ZVMTestSuite.CompileArgs.SuccessCallback() end function ZVMTestSuite.InternalErrorCallback() HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile + CPULib.print = ZVMTestSuite.oldCPUprint + HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning ZVMTestSuite.CompileArgs.ErrorCallback() end @@ -143,6 +152,13 @@ function ZVMTestSuite.StartCompileInternal() -- Swap loadfile function to load files from test folder ZVMTestSuite.HCOMPLoadFile = HCOMP.LoadFile HCOMP.LoadFile = ZVMTestSuite.LoadFile + ZVMTestSuite.oldCPUprint = CPULib.print + CPULib.print = function (...) end + ZVMTestSuite.OldHCOMPWarning = HCOMP.Warning + function HCOMP:Warning() + ZVMTestSuite.Warnings = ZVMTestSuite.Warnings + 1 + ZVMTestSuite.CurrentWarnings = ZVMTestSuite.CurrentWarnings + 1 + end local SourceCode = ZVMTestSuite.CompileArgs.SourceCode local FileName = ZVMTestSuite.CompileArgs.FileName local SuccessCallback = ZVMTestSuite.CompileArgs.SuccessCallback @@ -194,6 +210,9 @@ function ZVMTestSuite.CreateVirtualIOBus(IOBusSize) end function ZVMTestSuite.AddVirtualFunctions(VM) + function VM:ErrorCallback(errorcode) + return + end function VM:FlashData(data) ZVMTestSuite:FlashData(self,data) end @@ -205,6 +224,7 @@ function ZVMTestSuite.AddVirtualFunctions(VM) end function VM:SignalError(errorcode) self.Error = errorcode + self.ErrorCallback(errorcode) end end @@ -307,14 +327,9 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) VM.Frequency = 2000 -- Create virtual machine VM.SerialNo = CPULib.GenerateSN("CPU") + ZVMTestSuite.AddVirtualFunctions(VM) VM:Reset() - VM.SignalError = function(VM,errorCode) - Wire_TriggerOutput(VM, "Error", errorCode) - end - VM.SignalShutdown = function(VM) - VM.VMStopped = true - end VM.ExternalWrite = function(VM,Address,Value) if Address >= 0 then -- Use MemBus local MemBusSource = Membus From 8ebc3922d7b679044dd8b29374caa1df21ef5135 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 12:56:01 -0600 Subject: [PATCH 10/22] Corrected error function, now prints all varargs --- lua/wire/zvm/tests/includes.lua | 2 +- lua/wire/zvm/zvm_tests.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index 09550fd..a517815 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -18,7 +18,7 @@ function CPUTest.RunCPU() if CPUTest.VM.R0 == 2 then CPUTest.TestSuite.FinishTest(false) else - print("R0 is not 2! R0 is "..tostring(CPUTest.VM.R0)) + CPUTest.TestSuite.Error("R0 is not 2! R0 is "..tostring(CPUTest.VM.R0)) CPUTest.TestSuite.FinishTest(true) end end diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index e40a1e0..578f672 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -96,7 +96,7 @@ function ZVMTestSuite.FinishTest(fail) end function ZVMTestSuite.Error(...) - local args = ... + local args = table.Pack(...) 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 From 299bc81e5e451a9f0cc14a1444d036f63da20ab6 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 13:11:49 -0600 Subject: [PATCH 11/22] Spelling fixes for test framework --- lua/wire/zvm/zvm_tests.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 578f672..826f4b6 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -66,7 +66,7 @@ function ZVMTestSuite.FinishTest(fail) finalFail = fail end if ZVMTestSuite.CurrentWarnings > 0 then - print("Compiler Warnings from "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue].." "..ZVMTestSuite.CurrentWarnings) + print("Compiler Warnings from "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]..": "..ZVMTestSuite.CurrentWarnings) ZVMTestSuite.CurrentWarnings = 0 end ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil @@ -91,7 +91,7 @@ function ZVMTestSuite.FinishTest(fail) if failed > 1 then errormod = "s" end - print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod.." Compiler Warnings: "..ZVMTestSuite.Warnings) + print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod..", Compiler Warnings: "..ZVMTestSuite.Warnings) end end From 184f293a9a114651bf7b5749c7a749ff308cbcd6 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Thu, 16 Nov 2023 17:29:13 -0600 Subject: [PATCH 12/22] Reset warning count, don't print test list on runall --- lua/wire/zvm/zvm_tests.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 826f4b6..b94f800 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -20,6 +20,7 @@ ZVMTestSuite = { local testDirectory = "wire/zvm/tests" function ZVMTestSuite.CMDRun(_,_,_,names) + ZVMTestSuite.Warnings = 0 ZVMTestSuite.TestFiles = {} for filename in string.gmatch(names, '[^,]+') do local files = file.Find('lua/'..testDirectory..'/'..filename..'.lua',"GAME") @@ -36,6 +37,7 @@ function ZVMTestSuite.CMDRun(_,_,_,names) end ZVMTestSuite.RunAll() else + PrintTable(ZVMTestSuite.TestFiles) ZVMTestSuite.StartTesting() end end @@ -53,7 +55,6 @@ function ZVMTestSuite.StartTesting() ZVMTestSuite.TestQueue[(#ZVMTestSuite.TestFiles)+1-ind] = i end print(#ZVMTestSuite.TestFiles.." tests loaded") - PrintTable(ZVMTestSuite.TestFiles) ZVMTestSuite.RunNextTest() print("Game needs to be unpaused in order to run the tests (estimated time to completion "..((#ZVMTestSuite.TestQueue)*0.25).." seconds)") end From 295daa5371340b868f3e4b4a985f9c88d263818c Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 21 Nov 2023 23:31:39 -0600 Subject: [PATCH 13/22] Spelling error corrected, now runs in singleplayer --- lua/autorun/cpu_load.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index c48c441..8924a39 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -17,7 +17,7 @@ AddCSLuaFile("wire/zvm/zvm_features.lua") AddCSLuaFile("wire/zvm/zvm_opcodes.lua") AddCSLuaFile("wire/zvm/zvm_data.lua") -if game.Singleplayer() then +if game.SinglePlayer() then AddCSLuaFile("wire/zvm/zvm_tests.lua") include("wire/zvm/zvm_tests.lua") end From c6b60f9ee3ae3fb7bbd83d6f73b5a40aa58717df Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Sun, 26 Nov 2023 12:06:12 -0600 Subject: [PATCH 14/22] Test compilation now runs on dedicated servers + instant compilation --- lua/autorun/cpu_load.lua | 3 +-- lua/wire/zvm/zvm_tests.lua | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index 8924a39..bfbc7ca 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -17,8 +17,7 @@ AddCSLuaFile("wire/zvm/zvm_features.lua") AddCSLuaFile("wire/zvm/zvm_opcodes.lua") AddCSLuaFile("wire/zvm/zvm_data.lua") -if game.SinglePlayer() then - AddCSLuaFile("wire/zvm/zvm_tests.lua") +if SERVER then include("wire/zvm/zvm_tests.lua") end diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index b94f800..f70e7bc 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -56,7 +56,6 @@ function ZVMTestSuite.StartTesting() end print(#ZVMTestSuite.TestFiles.." tests loaded") ZVMTestSuite.RunNextTest() - print("Game needs to be unpaused in order to run the tests (estimated time to completion "..((#ZVMTestSuite.TestQueue)*0.25).." seconds)") end function ZVMTestSuite.FinishTest(fail) @@ -73,7 +72,7 @@ function ZVMTestSuite.FinishTest(fail) ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] = nil if #ZVMTestSuite.TestQueue > 0 then - timer.Simple(0.125,ZVMTestSuite.RunNextTest) + ZVMTestSuite.RunNextTest() else local passed, failed = 0, 0 for ind,i in ipairs(ZVMTestSuite.TestFiles) do @@ -131,30 +130,29 @@ function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback, ErrorCallback = ErrorCallback, TargetPlatform = TargetPlatform } - -- Needs to delay next compile otherwise it'll halt if a test compiles two or more programs. - timer.Simple(0.125,ZVMTestSuite.StartCompileInternal) + ZVMTestSuite.StartCompileInternal() end function ZVMTestSuite.InternalSuccessCallback() HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile - CPULib.print = ZVMTestSuite.oldCPUprint HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning ZVMTestSuite.CompileArgs.SuccessCallback() end function ZVMTestSuite.InternalErrorCallback() HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile - CPULib.print = ZVMTestSuite.oldCPUprint HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning ZVMTestSuite.CompileArgs.ErrorCallback() end +function ZVMTestSuite.OnWriteByte(caller,address,data) + ZVMTestSuite.Buffer[address] = data +end + function ZVMTestSuite.StartCompileInternal() -- Swap loadfile function to load files from test folder ZVMTestSuite.HCOMPLoadFile = HCOMP.LoadFile HCOMP.LoadFile = ZVMTestSuite.LoadFile - ZVMTestSuite.oldCPUprint = CPULib.print - CPULib.print = function (...) end ZVMTestSuite.OldHCOMPWarning = HCOMP.Warning function HCOMP:Warning() ZVMTestSuite.Warnings = ZVMTestSuite.Warnings + 1 @@ -162,14 +160,27 @@ function ZVMTestSuite.StartCompileInternal() end local SourceCode = ZVMTestSuite.CompileArgs.SourceCode local FileName = ZVMTestSuite.CompileArgs.FileName - local SuccessCallback = ZVMTestSuite.CompileArgs.SuccessCallback - local ErrorCallback = ZVMTestSuite.CompileArgs.ErrorCallback + local SuccessCallback = ZVMTestSuite.InternalSuccessCallback + local ErrorCallback = ZVMTestSuite.InternalErrorCallback local TargetPlatform = ZVMTestSuite.CompileArgs.TargetPlatform - CPULib.Compile(SourceCode,FileName,ZVMTestSuite.InternalSuccessCallback,ErrorCallback,TargetPlatform) + ZVMTestSuite.Buffer = {} + HCOMP:StartCompile(SourceCode,FileName or "source",ZVMTestSuite.OnWriteByte,nil) + HCOMP.Settings.CurrentPlatform = "CPU" + local noError, anotherStep = true,true + local steps = 0 + while noError and anotherStep do + noError,anotherStep = pcall(HCOMP.Compile,HCOMP) + end + if not noError then + return ErrorCallback(HCOMP.ErrorMessage or ("Internal error: "..result),HCOMP.ErrorPosition) + end + if not anotherStep then + return SuccessCallback() + end end function ZVMTestSuite.GetCompileBuffer() - return CPULib.Buffer + return ZVMTestSuite.Buffer end function ZVMTestSuite.GetCPUName() From 26801ddd1a2d5e741e051956a040a348178f5469 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 03:17:09 -0600 Subject: [PATCH 15/22] Revert CPULib.print back to print --- lua/wire/cpulib.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/wire/cpulib.lua b/lua/wire/cpulib.lua index d0cfb69..b317ddd 100644 --- a/lua/wire/cpulib.lua +++ b/lua/wire/cpulib.lua @@ -34,7 +34,6 @@ if CLIENT or TESTING then CPULib.Debugger.PointersByLine = {} CPULib.Debugger.Breakpoint = {} - CPULib.print = print -- Convars to control CPULib local wire_cpu_upload_speed = CreateClientConVar("wire_cpu_upload_speed",1000,false,false) @@ -70,7 +69,7 @@ if CLIENT or TESTING then -- Start compiling the sourcecode HCOMP:StartCompile(source,fileName or "source",CPULib.OnWriteByte,nil) HCOMP.Settings.CurrentPlatform = targetPlatform or "CPU" - CPULib.print("=== HL-ZASM High Level Assembly Compiler Output ==") + print("=== HL-ZASM High Level Assembly Compiler Output ==") -- Initialize callbacks CPULib.SuccessCallback = successCallback @@ -148,14 +147,14 @@ if CLIENT or TESTING then for _ = 1, compile_speed do local status,result = pcall(HCOMP.Compile,HCOMP) if not status then - CPULib.print("==================================================") + print("==================================================") if CPULib.ErrorCallback then CPULib.ErrorCallback(HCOMP.ErrorMessage or ("Internal error: "..result),HCOMP.ErrorPosition) end timer.Remove("cpulib_compile") CPULib.Compiling = false return elseif not result then - CPULib.print("==================================================") + print("==================================================") CPULib.Source = CPULib.CurrentSource CPULib.Buffer = CPULib.CurrentBuffer From c203bb24fbc4b51c2f3eb05d1bfbd2557fe60efd Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 03:28:04 -0600 Subject: [PATCH 16/22] Switches uses of ' out for " for strings for consistency --- lua/wire/zvm/tests/example.lua | 2 +- lua/wire/zvm/tests/execute_from_iobus.lua | 2 +- lua/wire/zvm/tests/file_example.lua | 2 +- lua/wire/zvm/tests/ifdefs.lua | 4 ++-- lua/wire/zvm/tests/includes.lua | 2 +- lua/wire/zvm/tests/intentional_compile_error.lua | 2 +- lua/wire/zvm/tests/intentional_failed_test.lua | 2 +- lua/wire/zvm/tests/no_internal_mem.lua | 2 +- lua/wire/zvm/tests/virtualiobus.lua | 2 +- lua/wire/zvm/tests/virtualmembus.lua | 4 ++-- lua/wire/zvm/zvm_tests.lua | 12 ++++++------ 11 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index 3413ca7..a682b87 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -22,6 +22,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index 885b78b..6bc6bd0 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -47,6 +47,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua index 09f48c8..906cc23 100644 --- a/lua/wire/zvm/tests/file_example.lua +++ b/lua/wire/zvm/tests/file_example.lua @@ -24,6 +24,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua index 2edcdf1..fca0817 100644 --- a/lua/wire/zvm/tests/ifdefs.lua +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -71,7 +71,7 @@ function CPUTest.CompareResults() end end if fail then - CPUTest.TestSuite.Error('Unexpected test results!') + CPUTest.TestSuite.Error("Unexpected test results!") PrintTable({CPUTest.ResultVariations1,results1,CPUTest.ResultVariations2,results2}) CPUTest.TestSuite.FinishTest(true) else @@ -80,7 +80,7 @@ function CPUTest.CompareResults() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index a517815..88881fb 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -24,6 +24,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index 082e2fd..657909d 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -7,7 +7,7 @@ function CPUTest:RunTest(VM,TestSuite) end function CPUTest.RunCPU() - CPUTest.TestSuite.Error('Compiler did not error when it should have!') + CPUTest.TestSuite.Error("Compiler did not error when it should have!") CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index c293ffd..3d3607c 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -22,6 +22,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index 05cc47c..a2aad15 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -28,6 +28,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index 98636d9..2005cca 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -35,6 +35,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index db783bb..e1a8e6a 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -20,7 +20,7 @@ function CPUTest.RunCPU() if CPUTest.VM.R1 == 1 then CPUTest.TestSuite.FinishTest(false) else - CPUTest.TestSuite.Error('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 @@ -30,6 +30,6 @@ function CPUTest.RunCPU() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error('hit a compile time error '..msg) + CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index f70e7bc..f8c7e3e 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -22,8 +22,8 @@ local testDirectory = "wire/zvm/tests" function ZVMTestSuite.CMDRun(_,_,_,names) ZVMTestSuite.Warnings = 0 ZVMTestSuite.TestFiles = {} - for filename in string.gmatch(names, '[^,]+') do - local files = file.Find('lua/'..testDirectory..'/'..filename..'.lua',"GAME") + for filename in string.gmatch(names, "[^,]+") do + local files = file.Find("lua/"..testDirectory.."/"..filename..".lua","GAME") for _,i in ipairs(files) do ZVMTestSuite.TestFiles[#ZVMTestSuite.TestFiles+1] = i end @@ -43,7 +43,7 @@ function ZVMTestSuite.CMDRun(_,_,_,names) end function ZVMTestSuite.RunAll() - local files,directories = file.Find(testDirectory..'/*.lua',"LUA","nameasc") + local files,directories = file.Find(testDirectory.."/*.lua","LUA","nameasc") ZVMTestSuite.TestFiles = files or {} ZVMTestSuite.StartTesting() end @@ -107,19 +107,19 @@ function ZVMTestSuite.Error(...) MsgC(Color(255,255,255),tostring(args)) end end - MsgC(Color(0,0,255),'\n') + MsgC(Color(0,0,255),"\n") end function ZVMTestSuite.RunNextTest() local curVM = CPULib.VirtualMachine() ZVMTestSuite.Initialize(curVM) print("Running "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) - include(testDirectory..'/'..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + include(testDirectory.."/"..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) CPUTest:RunTest(curVM,ZVMTestSuite) end function ZVMTestSuite:LoadFile(FileName) - return file.Read('lua/'..testDirectory..'/'..FileName,"GAME") + return file.Read("lua/"..testDirectory.."/"..FileName,"GAME") end function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform) From 31de8fe86e6fb5219a11f471075ce68e4319c77c Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 04:00:48 -0600 Subject: [PATCH 17/22] Space operators / minor formatting changes --- lua/autorun/cpu_load.lua | 3 +- lua/wire/zvm/tests/example.lua | 8 +-- lua/wire/zvm/tests/execute_from_iobus.lua | 23 ++++---- lua/wire/zvm/tests/file_example.lua | 6 +- lua/wire/zvm/tests/ifdefs.lua | 28 ++++----- lua/wire/zvm/tests/includes.lua | 10 ++-- .../zvm/tests/intentional_compile_error.lua | 2 +- .../zvm/tests/intentional_failed_test.lua | 10 ++-- lua/wire/zvm/tests/no_internal_mem.lua | 12 ++-- lua/wire/zvm/tests/virtualiobus.lua | 16 +++--- lua/wire/zvm/tests/virtualmembus.lua | 8 +-- lua/wire/zvm/zvm_tests.lua | 57 ++++++++++--------- 12 files changed, 93 insertions(+), 90 deletions(-) diff --git a/lua/autorun/cpu_load.lua b/lua/autorun/cpu_load.lua index bfbc7ca..e09af0b 100644 --- a/lua/autorun/cpu_load.lua +++ b/lua/autorun/cpu_load.lua @@ -34,5 +34,4 @@ AddCSLuaFile("wire/cpu_default_data_files.lua") if CLIENT then include("wire/client/hlzasm/hc_compiler.lua") -end - +end \ No newline at end of file diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index a682b87..3b29b6d 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -3,11 +3,11 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("x: INC R0 JMP x", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + 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() @@ -16,12 +16,12 @@ function CPUTest.RunCPU() if CPUTest.VM.R0 == 4096 then CPUTest.TestSuite.FinishTest(false) else - CPUTest.TestSuite.Error("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(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index 6bc6bd0..cf51872 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -3,36 +3,37 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2",nil,CPUTest.CompileNext,CPUTest.CompileError) + TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2", nil, CPUTest.CompileNext, CPUTest.CompileError) -- end result of the above code should be R0 = 24 end function CPUTest.CompileNext() local buff = CPUTest.TestSuite.GetCompileBuffer() - local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff+1) -- create an IOBus large enough to hold this code + local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff + 1) -- create an IOBus large enough to hold this code CPUTest.IOBus = IOBus -- reverse the compiled code, the CPU will read them in reverse if it's in the IOBus -- because CS will be negative, and IP only increments + -- ipairs won't index 0 and the cpu compile buffer uses 0 - for i=0,#buff do + for i = 0, #buff do IOBus.InPorts[#buff-i] = buff[i] end IOBus.OldReadCell = IOBus.ReadCell IOBus.AccessLog = {} function IOBus:ReadCell(address) - IOBus.AccessLog[#IOBus.AccessLog+1] = {"read address"..tostring(address),self:OldReadCell(address) or "no value"} + IOBus.AccessLog[#IOBus.AccessLog + 1] = {"read address" .. tostring(address), self:OldReadCell(address) or "no value"} return self:OldReadCell(address) end -- JMPF jumps to 0 IP, CS = (code length+1)*-1 because first index of IOBus is "cell -1" of extern read/write - local generatedcode = "CMP R0,0 JNER -3 JMPF 0,"..(#buff+1)*-1 - CPUTest.TestSuite.Compile(generatedcode,nil,CPUTest.RunCPU,CPUTest.CompileError) + local generatedcode = "CMP R0,0 JNER -3 JMPF 0," .. (#buff + 1) * -1 + CPUTest.TestSuite.Compile(generatedcode, nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - CPUTest.TestSuite.Initialize(CPUTest.VM,nil,CPUTest.IOBus) -- reinitialize the CPU with the IOBus + CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + CPUTest.TestSuite.Initialize(CPUTest.VM, nil, CPUTest.IOBus) -- reinitialize the CPU with the IOBus CPUTest.VM.Clk = 1 - for i=0,32 do + for i = 0, 32 do CPUTest.VM:RunStep() end @@ -41,12 +42,12 @@ function CPUTest.RunCPU() CPUTest.TestSuite.FinishTest(false) else PrintTable(CPUTest.IOBus) - CPUTest.TestSuite.Error("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(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua index 906cc23..c8c0f1b 100644 --- a/lua/wire/zvm/tests/file_example.lua +++ b/lua/wire/zvm/tests/file_example.lua @@ -11,19 +11,19 @@ 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 + 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.Error("R0 is not 4096! 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.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua index fca0817..3caa211 100644 --- a/lua/wire/zvm/tests/ifdefs.lua +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -10,13 +10,13 @@ CPUTest = {} -- x and not y = 1, name "Test X" -CPUTest.ExpectedVariations1 = {"X","Y","X and Y","Y","Y","Y"} -- CPU Name vars -CPUTest.ExpectedVariations2 = {1,12,11,1,14,11} +CPUTest.ExpectedVariations1 = {"X", "Y", "X and Y", "Y", "Y", "Y"} -- CPU Name vars +CPUTest.ExpectedVariations2 = {1, 12, 11, 1, 14, 11} CPUTest.ResultVariations1 = {} CPUTest.ResultVariations2 = {} -CPUTest.Variations1 = {"true","false"} -CPUTest.Variations2 = {"#define x\n","#define y\n","#define x\n#define y\n"} +CPUTest.Variations1 = {"true", "false"} +CPUTest.Variations2 = {"#define x\n", "#define y\n", "#define x\n#define y\n"} CPUTest.Variation1Index = 1 CPUTest.Variation2Index = 1 @@ -31,12 +31,12 @@ end function CPUTest.CompileNext() local cursrc if CPUTest.Variation1Index <= #CPUTest.Variations1 then - cursrc = "#pragma set NewIfDefs "..CPUTest.Variations1[CPUTest.Variation1Index].."\n" + cursrc = "#pragma set NewIfDefs " .. CPUTest.Variations1[CPUTest.Variation1Index] .. "\n" else return CPUTest.CompareResults() end if CPUTest.Variation2Index <= #CPUTest.Variations2 then - cursrc = cursrc..CPUTest.Variations2[CPUTest.Variation2Index].."\n"..CPUTest.Src + cursrc = cursrc .. CPUTest.Variations2[CPUTest.Variation2Index] .. "\n" .. CPUTest.Src CPUTest.TestSuite.Compile(cursrc,nil,CPUTest.LogResults,CPUTest.CompileError) else CPUTest.Variation1Index = CPUTest.Variation1Index + 1 @@ -46,23 +46,23 @@ function CPUTest.CompileNext() end function CPUTest.LogResults() - CPUTest.ResultVariations1[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = CPUTest.TestSuite.GetCPUName() or "ERROR" - CPUTest.ResultVariations2[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = #CPUTest.TestSuite.GetCompileBuffer()+1 or "ERROR" + CPUTest.ResultVariations1[CPUTest.Variation2Index + #CPUTest.Variations2*(CPUTest.Variation1Index-1)] = CPUTest.TestSuite.GetCPUName() or "ERROR" + CPUTest.ResultVariations2[CPUTest.Variation2Index + #CPUTest.Variations2*(CPUTest.Variation1Index-1)] = #CPUTest.TestSuite.GetCompileBuffer() + 1 or "ERROR" CPUTest.Variation2Index = CPUTest.Variation2Index + 1 CPUTest.CompileNext() end function CPUTest.CompareResults() - local fail,results1,results2 = false,{},{} - for ind,i in ipairs(CPUTest.ExpectedVariations1) do - if CPUTest.ResultVariations1[ind] == "Test "..i then + local fail,results1,results2 = false, {}, {} + for ind, i in ipairs(CPUTest.ExpectedVariations1) do + if CPUTest.ResultVariations1[ind] == "Test " .. i then results1[ind] = true else fail = true results1[ind] = false end end - for ind,i in ipairs(CPUTest.ExpectedVariations2) do + for ind, i in ipairs(CPUTest.ExpectedVariations2) do if CPUTest.ResultVariations2[ind] == i then results2[ind] = true else @@ -72,7 +72,7 @@ function CPUTest.CompareResults() end if fail then CPUTest.TestSuite.Error("Unexpected test results!") - PrintTable({CPUTest.ResultVariations1,results1,CPUTest.ResultVariations2,results2}) + PrintTable({CPUTest.ResultVariations1, results1, CPUTest.ResultVariations2, results2}) CPUTest.TestSuite.FinishTest(true) else CPUTest.TestSuite.FinishTest(false) @@ -80,7 +80,7 @@ function CPUTest.CompareResults() end function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index 88881fb..527a1fb 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -5,25 +5,25 @@ function CPUTest:RunTest(VM,TestSuite) 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) + 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.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu CPUTest.VM.Clk = 1 - for i=0,16 do + 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 - CPUTest.TestSuite.Error("R0 is not 2! R0 is "..tostring(CPUTest.VM.R0)) + CPUTest.TestSuite.Error("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.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index 657909d..7a4dfa6 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -3,7 +3,7 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("MOV R0,", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index 3d3607c..bfd285a 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -3,25 +3,25 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("x: INC R0 JMP x", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu CPUTest.VM.Clk = 1 - for i=0,4096 do + for i = 0, 4096 do CPUTest.VM:RunStep() end -- False = no error, True = error if CPUTest.VM.R0 == 4095 then CPUTest.TestSuite.FinishTest(false) else - CPUTest.TestSuite.Error("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(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index a2aad15..cb91197 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -3,18 +3,18 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,1",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("MOV R0,1", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() local buff = CPUTest.TestSuite.GetCompileBuffer() local bus = CPUTest.TestSuite.CreateVirtualMemBus(#buff) -- get external ram device large enough to hold program - CPUTest.TestSuite.FlashData(bus,buff) -- upload compiled to membus + CPUTest.TestSuite.FlashData(bus, buff) -- upload compiled to membus CPUTest.VM.RAMSize = 0 CPUTest.VM.ROMSize = 0 - CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus + CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus CPUTest.VM.Clk = 1 - for i=0,16 do + for i = 0, 16 do CPUTest.VM:RunStep() end @@ -22,12 +22,12 @@ function CPUTest.RunCPU() if CPUTest.VM.R0 == 1 then CPUTest.TestSuite.FinishTest(false) else - CPUTest.TestSuite.Error("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(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index 2005cca..e2251c3 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -3,16 +3,16 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(4) -- get external IO device of size 4 - CPUTest.TestSuite.Initialize(CPUTest.VM,nil,IOBus) -- reinitialize the CPU with the IOBus + CPUTest.TestSuite.Initialize(CPUTest.VM, nil, IOBus) -- reinitialize the CPU with the IOBus IOBus.InPorts[0] = 24 CPUTest.VM.Clk = 1 - for i=0,16 do + for i = 0, 16 do CPUTest.VM:RunStep() end @@ -22,19 +22,19 @@ function CPUTest.RunCPU() if CPUTest.VM.R0 == 24 then CPUTest.TestSuite.FinishTest(false) else - CPUTest.TestSuite.Error("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 - CPUTest.TestSuite.Error("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 - CPUTest.TestSuite.Error("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(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) + CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index e1a8e6a..5dd3a36 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -3,15 +3,15 @@ CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM CPUTest.TestSuite = TestSuite - TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]",nil,CPUTest.RunCPU,CPUTest.CompileError) + TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]", nil, CPUTest.RunCPU, CPUTest.CompileError) end function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu + CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu local bus = CPUTest.TestSuite.CreateVirtualMemBus(4) -- get external ram device of size 4 - CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus + CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus CPUTest.VM.Clk = 1 - for i=0,16 do + for i = 0, 16 do CPUTest.VM:RunStep() end diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index f8c7e3e..549b006 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -19,19 +19,19 @@ ZVMTestSuite = { local testDirectory = "wire/zvm/tests" -function ZVMTestSuite.CMDRun(_,_,_,names) +function ZVMTestSuite.CMDRun(_, _, _, names) ZVMTestSuite.Warnings = 0 ZVMTestSuite.TestFiles = {} for filename in string.gmatch(names, "[^,]+") do - local files = file.Find("lua/"..testDirectory.."/"..filename..".lua","GAME") - for _,i in ipairs(files) do + local files = file.Find("lua/" .. testDirectory .. "/" .. filename .. ".lua", "GAME") + for _, i in ipairs(files) do ZVMTestSuite.TestFiles[#ZVMTestSuite.TestFiles+1] = i end end if #ZVMTestSuite.TestFiles == 0 then if names ~= nil then if names ~= "" then - print("Didn't find any tests with name(s): "..names) + print("Didn't find any tests with name(s): " .. names) return end end @@ -43,7 +43,7 @@ function ZVMTestSuite.CMDRun(_,_,_,names) end function ZVMTestSuite.RunAll() - local files,directories = file.Find(testDirectory.."/*.lua","LUA","nameasc") + local files,directories = file.Find(testDirectory .. "/*.lua", "LUA", "nameasc") ZVMTestSuite.TestFiles = files or {} ZVMTestSuite.StartTesting() end @@ -51,10 +51,10 @@ end function ZVMTestSuite.StartTesting() ZVMTestSuite.TestQueue = {} ZVMTestSuite.TestStatuses = {} - for ind,i in ipairs(ZVMTestSuite.TestFiles) do -- copy with reversed indexes so we can use cheap popping + for ind, i in ipairs(ZVMTestSuite.TestFiles) do -- copy with reversed indexes so we can use cheap popping ZVMTestSuite.TestQueue[(#ZVMTestSuite.TestFiles)+1-ind] = i end - print(#ZVMTestSuite.TestFiles.." tests loaded") + print(#ZVMTestSuite.TestFiles .. " tests loaded") ZVMTestSuite.RunNextTest() end @@ -66,10 +66,10 @@ function ZVMTestSuite.FinishTest(fail) finalFail = fail end if ZVMTestSuite.CurrentWarnings > 0 then - print("Compiler Warnings from "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]..": "..ZVMTestSuite.CurrentWarnings) + print("Compiler Warnings from " .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] .. ": " .. ZVMTestSuite.CurrentWarnings) ZVMTestSuite.CurrentWarnings = 0 end - ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses+1] = finalFail -- auto fail on return nil + ZVMTestSuite.TestStatuses[#ZVMTestSuite.TestStatuses + 1] = finalFail -- auto fail on return nil ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue] = nil if #ZVMTestSuite.TestQueue > 0 then ZVMTestSuite.RunNextTest() @@ -78,10 +78,9 @@ function ZVMTestSuite.FinishTest(fail) for ind,i in ipairs(ZVMTestSuite.TestFiles) do if ZVMTestSuite.TestStatuses[ind] then failed = failed + 1 - MsgC(Color(255,0,0),"Error ",Color(255,255,255),"in "..i.."\n") + MsgC(Color(255,0,0), "Error ", Color(255,255,255), "in " .. i .. "\n") else passed = passed + 1 - --MsgC(Color(0,255,0),i.." passed tests".."\n") end end local passmod, errormod = "","" @@ -91,38 +90,38 @@ function ZVMTestSuite.FinishTest(fail) if failed > 1 then errormod = "s" end - print(failed.." Failed test"..errormod..", "..passed.." Passed test"..passmod..", Compiler Warnings: "..ZVMTestSuite.Warnings) + print(failed .. " Failed test" .. errormod .. ", " ..passed.. " Passed test" ..passmod.. ", " .. ZVMTestSuite.Warnings .. "Compiler Warnings were generated") end end function ZVMTestSuite.Error(...) local args = table.Pack(...) - MsgC(Color(255,0,0),"in file ",Color(255,255,255),ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue],Color(255,0,0)," Error: ") + 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) + for _, i in pairs(args) do + MsgC(Color(255,255,255), i) end else - MsgC(Color(255,255,255),tostring(args)) + MsgC(Color(255,255,255), tostring(args)) end end - MsgC(Color(0,0,255),"\n") + MsgC(Color(0,0,255), "\n") end function ZVMTestSuite.RunNextTest() local curVM = CPULib.VirtualMachine() ZVMTestSuite.Initialize(curVM) - print("Running "..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) - include(testDirectory.."/"..ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + print("Running " .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + include(testDirectory .. "/" .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) CPUTest:RunTest(curVM,ZVMTestSuite) end function ZVMTestSuite:LoadFile(FileName) - return file.Read("lua/"..testDirectory.."/"..FileName,"GAME") + return file.Read("lua/" .. testDirectory .. "/" .. FileName, "GAME") end -function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform) +function ZVMTestSuite.Compile(SourceCode, FileName, SuccessCallback, ErrorCallback, TargetPlatform) ZVMTestSuite.CompileArgs = { SourceCode = SourceCode, FileName = FileName, @@ -164,15 +163,15 @@ function ZVMTestSuite.StartCompileInternal() local ErrorCallback = ZVMTestSuite.InternalErrorCallback local TargetPlatform = ZVMTestSuite.CompileArgs.TargetPlatform ZVMTestSuite.Buffer = {} - HCOMP:StartCompile(SourceCode,FileName or "source",ZVMTestSuite.OnWriteByte,nil) + HCOMP:StartCompile(SourceCode, FileName or "source", ZVMTestSuite.OnWriteByte, nil) HCOMP.Settings.CurrentPlatform = "CPU" - local noError, anotherStep = true,true + local noError, anotherStep = true, true local steps = 0 while noError and anotherStep do - noError,anotherStep = pcall(HCOMP.Compile,HCOMP) + noError,anotherStep = pcall(HCOMP.Compile, HCOMP) end if not noError then - return ErrorCallback(HCOMP.ErrorMessage or ("Internal error: "..result),HCOMP.ErrorPosition) + return ErrorCallback(HCOMP.ErrorMessage or ("Internal error: " .. result), HCOMP.ErrorPosition) end if not anotherStep then return SuccessCallback() @@ -205,7 +204,11 @@ function ZVMTestSuite.CreateVirtualMemBus(MembusSize) end function ZVMTestSuite.CreateVirtualIOBus(IOBusSize) - local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize-1} + local virtualIOBus = { + InPorts = {}, + OutPorts = {}, + Size = IOBusSize-1 + } function virtualIOBus:ReadCell(Address) if Address <= self.Size and Address > -1 then return self.InPorts[Address] @@ -408,4 +411,4 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) end -concommand.Add("ZCPU_RUN_TESTS",ZVMTestSuite.CMDRun,nil,"Runs ZCPU Tests, pass a comma delimited list to only run tests with those names\nExample: ZCPU_RUN_TESTS example,file_example\n\nRun without args to run all tests") +concommand.Add("ZCPU_RUN_TESTS", ZVMTestSuite.CMDRun, nil, "Runs ZCPU Tests, pass a comma delimited list to only run tests with those names\nExample: ZCPU_RUN_TESTS example,file_example\n\nRun without args to run all tests") From e6b3431cd925f4ba23a137b2330e0060512fadbf Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 04:20:01 -0600 Subject: [PATCH 18/22] Create color constants & table.pack(...) to {...} --- lua/wire/zvm/zvm_tests.lua | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 549b006..a315469 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -8,6 +8,8 @@ TESTING = true include("wire/cpulib.lua") include("wire/client/hlzasm/hc_compiler.lua") +local color_white = Color(255,255,255) +local color_red = Color(255,0,0) ZVMTestSuite = { TestFiles = {}, @@ -78,32 +80,35 @@ function ZVMTestSuite.FinishTest(fail) for ind,i in ipairs(ZVMTestSuite.TestFiles) do if ZVMTestSuite.TestStatuses[ind] then failed = failed + 1 - MsgC(Color(255,0,0), "Error ", Color(255,255,255), "in " .. i .. "\n") + MsgC(color_red, "Error ", color_white, "in " .. i .. "\n") else passed = passed + 1 end end - local passmod, errormod = "","" - if passed > 1 then + local passmod, errormod, warnstring = "","","" + if passed ~= 1 then passmod = "s" end - if failed > 1 then + if failed ~= 1 then errormod = "s" end - print(failed .. " Failed test" .. errormod .. ", " ..passed.. " Passed test" ..passmod.. ", " .. ZVMTestSuite.Warnings .. "Compiler Warnings were generated") + if ZVMTestSuite.Warnings > 0 then + warnstring = ZVMTestSuite.Warnings .. " Compiler Warnings were generated" + end + print(failed .. " Failed test" .. errormod .. ", " ..passed.. " Passed test" ..passmod.. ", " .. warnstring) end end function ZVMTestSuite.Error(...) - local args = table.Pack(...) - MsgC(Color(255,0,0), "in file ", Color(255,255,255), ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue], Color(255,0,0), " Error: ") + local args = { ... } + MsgC(color_red, "in file ", color_white, ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue], color_red, " Error: ") if args ~= nil then if istable(args) then for _, i in pairs(args) do - MsgC(Color(255,255,255), i) + MsgC(color_white, i) end else - MsgC(Color(255,255,255), tostring(args)) + MsgC(color_white, tostring(args)) end end MsgC(Color(0,0,255), "\n") From 77b75b17a4ed36e90047b9553b5ac2dbc4bcf524 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 04:31:10 -0600 Subject: [PATCH 19/22] Collapse if statements, missed spots from other commits --- lua/wire/zvm/tests/example.lua | 2 +- lua/wire/zvm/tests/ifdefs.lua | 2 +- lua/wire/zvm/zvm_tests.lua | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index 3b29b6d..82c89d0 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -9,7 +9,7 @@ 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 + for i = 0, 4096 do CPUTest.VM:RunStep() end -- False = no error, True = error diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua index 3caa211..43ef484 100644 --- a/lua/wire/zvm/tests/ifdefs.lua +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -53,7 +53,7 @@ function CPUTest.LogResults() end function CPUTest.CompareResults() - local fail,results1,results2 = false, {}, {} + local fail, results1, results2 = false, {}, {} for ind, i in ipairs(CPUTest.ExpectedVariations1) do if CPUTest.ResultVariations1[ind] == "Test " .. i then results1[ind] = true diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index a315469..4a85af0 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -10,6 +10,7 @@ include("wire/client/hlzasm/hc_compiler.lua") local color_white = Color(255,255,255) local color_red = Color(255,0,0) +local color_blue = Color(0,0,255) ZVMTestSuite = { TestFiles = {}, @@ -30,13 +31,11 @@ function ZVMTestSuite.CMDRun(_, _, _, names) ZVMTestSuite.TestFiles[#ZVMTestSuite.TestFiles+1] = i end end - if #ZVMTestSuite.TestFiles == 0 then - if names ~= nil then + if #ZVMTestSuite.TestFiles == 0 and names ~= nil then if names ~= "" then print("Didn't find any tests with name(s): " .. names) return end - end ZVMTestSuite.RunAll() else PrintTable(ZVMTestSuite.TestFiles) @@ -85,7 +84,7 @@ function ZVMTestSuite.FinishTest(fail) passed = passed + 1 end end - local passmod, errormod, warnstring = "","","" + local passmod, errormod, warnstring = "", "", "" if passed ~= 1 then passmod = "s" end @@ -111,7 +110,7 @@ function ZVMTestSuite.Error(...) MsgC(color_white, tostring(args)) end end - MsgC(Color(0,0,255), "\n") + MsgC(color_blue, "\n") end function ZVMTestSuite.RunNextTest() @@ -138,7 +137,7 @@ function ZVMTestSuite.Compile(SourceCode, FileName, SuccessCallback, ErrorCallba end function ZVMTestSuite.InternalSuccessCallback() - HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile + HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning ZVMTestSuite.CompileArgs.SuccessCallback() end From e741e1f564cea7fdcad3e33a81dc4e8fb78cc572 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 04:58:18 -0600 Subject: [PATCH 20/22] More commented code removal --- lua/wire/zvm/zvm_tests.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index 4a85af0..c4d37d1 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -331,7 +331,6 @@ function ZVMTestSuite.TriggerInput(VM, iname, value) else if value >= 1.0 then VM:Reset() end end - -- Wire_TriggerOutput(VM, "Error", 0) elseif iname == "Interrupt" then if (value >= 32) and (value < 256) then if (VM.Clk and not VM.VMStopped) then VM:ExternalInterrupt(math.floor(value)) end @@ -406,9 +405,6 @@ function ZVMTestSuite.Initialize(VM,Membus,IOBus) local oldReset = VM.Reset VM.Reset = function(...) - if VM.Clk and VM.VMStopped then - --VM:NextThink(CurTime()) - end VM.VMStopped = false return oldReset(...) end From d4cdcc8915720742aa61bfafa72533602be1b38d Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 12 Dec 2023 05:04:30 -0600 Subject: [PATCH 21/22] Return a local table from tests instead of global. --- lua/wire/zvm/tests/example.lua | 4 +++- lua/wire/zvm/tests/execute_from_iobus.lua | 4 +++- lua/wire/zvm/tests/file_example.lua | 4 +++- lua/wire/zvm/tests/ifdefs.lua | 3 ++- lua/wire/zvm/tests/includes.lua | 4 +++- lua/wire/zvm/tests/intentional_compile_error.lua | 4 +++- lua/wire/zvm/tests/intentional_failed_test.lua | 4 +++- lua/wire/zvm/tests/no_internal_mem.lua | 4 +++- lua/wire/zvm/tests/virtualiobus.lua | 4 +++- lua/wire/zvm/tests/virtualmembus.lua | 4 +++- lua/wire/zvm/zvm_tests.lua | 2 +- 11 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index 82c89d0..4dcef79 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -25,3 +25,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index cf51872..c77b2af 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -51,3 +51,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua index c8c0f1b..d9b57c6 100644 --- a/lua/wire/zvm/tests/file_example.lua +++ b/lua/wire/zvm/tests/file_example.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -27,3 +27,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua index 43ef484..a182aca 100644 --- a/lua/wire/zvm/tests/ifdefs.lua +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} --not x and y = 14, name "Test Y" -- x and y = 11, name "Test Y" @@ -84,3 +84,4 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.FinishTest(true) end +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index 527a1fb..8a502f3 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -27,3 +27,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index 7a4dfa6..bab5044 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -14,3 +14,5 @@ end function CPUTest.CompileError() CPUTest.TestSuite.FinishTest(false) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index bfd285a..b852cd4 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -25,3 +25,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index cb91197..cf90c6b 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -31,3 +31,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index e2251c3..fc7b13a 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -38,3 +38,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error " .. msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index 5dd3a36..7298c5a 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -1,4 +1,4 @@ -CPUTest = {} +local CPUTest = {} function CPUTest:RunTest(VM,TestSuite) CPUTest.VM = VM @@ -33,3 +33,5 @@ function CPUTest.CompileError(msg) CPUTest.TestSuite.Error("hit a compile time error "..msg) CPUTest.TestSuite.FinishTest(true) end + +return CPUTest \ No newline at end of file diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index c4d37d1..ccc13d2 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -117,7 +117,7 @@ function ZVMTestSuite.RunNextTest() local curVM = CPULib.VirtualMachine() ZVMTestSuite.Initialize(curVM) print("Running " .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) - include(testDirectory .. "/" .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) + local CPUTest = include(testDirectory .. "/" .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) CPUTest:RunTest(curVM,ZVMTestSuite) end From 544494ae9487776eba7ac29c745fc78af5a92c6a Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 2 Jan 2024 04:33:02 -0600 Subject: [PATCH 22/22] Tests revised Tests can have a virtual filesystem(still falls back to actually using the folder if you don't use it) Easier to use "Deploy" function, for compiling and flashing straight to a device Revises all tests to use assert inside of a pcalled context --- lua/wire/zvm/tests/example.lua | 34 ++--- lua/wire/zvm/tests/execute_from_iobus.lua | 52 +++----- lua/wire/zvm/tests/file_example.lua | 45 ++++--- lua/wire/zvm/tests/file_example.txt | 3 - lua/wire/zvm/tests/ifdefs.lua | 118 +++++++++++------- lua/wire/zvm/tests/ifdefs.txt | 38 ------ lua/wire/zvm/tests/includes.lua | 44 ++++--- lua/wire/zvm/tests/includes_1.txt | 2 - lua/wire/zvm/tests/includes_2.txt | 1 - .../zvm/tests/intentional_compile_error.lua | 19 +-- .../zvm/tests/intentional_failed_test.lua | 34 ++--- lua/wire/zvm/tests/no_internal_mem.lua | 41 +++--- lua/wire/zvm/tests/virtualiobus.lua | 46 +++---- lua/wire/zvm/tests/virtualmembus.lua | 41 ++---- lua/wire/zvm/zvm_tests.lua | 97 ++++++-------- 15 files changed, 244 insertions(+), 371 deletions(-) delete mode 100644 lua/wire/zvm/tests/file_example.txt delete mode 100644 lua/wire/zvm/tests/ifdefs.txt delete mode 100644 lua/wire/zvm/tests/includes_1.txt delete mode 100644 lua/wire/zvm/tests/includes_2.txt diff --git a/lua/wire/zvm/tests/example.lua b/lua/wire/zvm/tests/example.lua index 4dcef79..139c130 100644 --- a/lua/wire/zvm/tests/example.lua +++ b/lua/wire/zvm/tests/example.lua @@ -1,29 +1,19 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("x: INC R0 JMP x", 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 +function Test.Run(CPU,TestSuite) + TestSuite:Deploy(CPU,"x: INC R0 JMP x",Test.CompileError) + CPU.Clk = 1 + -- Run the VM for 4096 cycles 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 - CPUTest.TestSuite.Error("R0 is not 4096! R0 is " .. tostring(CPUTest.VM.R0)) - CPUTest.TestSuite.FinishTest(true) + CPU:RunStep() end + + -- On false, will cause test to fail with message + assert(CPU.R0 == 4096,"R0 is not 4096! R0 is " .. tostring(CPU.R0)) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false,"compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/execute_from_iobus.lua b/lua/wire/zvm/tests/execute_from_iobus.lua index c77b2af..bb5eaf6 100644 --- a/lua/wire/zvm/tests/execute_from_iobus.lua +++ b/lua/wire/zvm/tests/execute_from_iobus.lua @@ -1,16 +1,10 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2", nil, CPUTest.CompileNext, CPUTest.CompileError) +function Test.Run(CPU,TestSuite) + TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2", "internal", nil, Test.CompileError) -- end result of the above code should be R0 = 24 -end - -function CPUTest.CompileNext() - local buff = CPUTest.TestSuite.GetCompileBuffer() - local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff + 1) -- create an IOBus large enough to hold this code - CPUTest.IOBus = IOBus + local buff = TestSuite.GetCompileBuffer() + local IOBus = TestSuite.CreateVirtualIOBus(#buff + 1) -- create an IOBus large enough to hold this code -- reverse the compiled code, the CPU will read them in reverse if it's in the IOBus -- because CS will be negative, and IP only increments @@ -18,38 +12,24 @@ function CPUTest.CompileNext() for i = 0, #buff do IOBus.InPorts[#buff-i] = buff[i] end - IOBus.OldReadCell = IOBus.ReadCell - IOBus.AccessLog = {} - function IOBus:ReadCell(address) - IOBus.AccessLog[#IOBus.AccessLog + 1] = {"read address" .. tostring(address), self:OldReadCell(address) or "no value"} - return self:OldReadCell(address) - end + -- JMPF jumps to 0 IP, CS = (code length+1)*-1 because first index of IOBus is "cell -1" of extern read/write local generatedcode = "CMP R0,0 JNER -3 JMPF 0," .. (#buff + 1) * -1 - CPUTest.TestSuite.Compile(generatedcode, nil, CPUTest.RunCPU, CPUTest.CompileError) -end -function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - CPUTest.TestSuite.Initialize(CPUTest.VM, nil, CPUTest.IOBus) -- reinitialize the CPU with the IOBus - CPUTest.VM.Clk = 1 + TestSuite:Deploy(CPU, generatedcode, Test.CompileError) + TestSuite.Initialize(CPU, nil, IOBus) -- reinitialize the CPU with the IOBus + CPU.Clk = 1 for i = 0, 32 do - CPUTest.VM:RunStep() + CPU:RunStep() end - -- False = no error, True = error - if CPUTest.VM.R0 == 24 then - CPUTest.TestSuite.FinishTest(false) - else - PrintTable(CPUTest.IOBus) - CPUTest.TestSuite.Error("R0 != 24, R0 = " .. tostring(CPUTest.VM.R0)) - CPUTest.TestSuite.FinishTest(true) - end + -- On false, will cause test to fail with message + assert(CPU.R0 == 24, "R0 != 24, R0 = " .. tostring(CPU.R0)) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) + +function Test.CompileError(msg) + assert(false, "compile time error " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/file_example.lua b/lua/wire/zvm/tests/file_example.lua index d9b57c6..d80e3a9 100644 --- a/lua/wire/zvm/tests/file_example.lua +++ b/lua/wire/zvm/tests/file_example.lua @@ -1,31 +1,30 @@ -local CPUTest = {} +local Test = {} -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 +-- If Test.Files is present, the compiler and suite will read files from it instead of the test directory +Test.Files = { + ["file_example.txt"] = [[ +x: +INC R0 +JMP x + ]] +} -function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - CPUTest.VM.Clk = 1 +function Test.Run(CPU,TestSuite) + -- Use the suite to load the file from our provided files table + local code = TestSuite:LoadFile("file_example.txt") + TestSuite:Deploy(CPU,code,Test.CompileError) + CPU.Clk = 1 + -- Run the VM for 4096 cycles 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 - CPUTest.TestSuite.Error("R0 is not 4096! R0 is " .. tostring(CPUTest.VM.R0)) - CPUTest.TestSuite.FinishTest(true) + CPU:RunStep() end + + -- On false, will cause test to fail with message + assert(CPU.R0 == 4096, "R0 is not 4096! R0 is " .. tostring(CPU.R0)) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/file_example.txt b/lua/wire/zvm/tests/file_example.txt deleted file mode 100644 index b0a5999..0000000 --- a/lua/wire/zvm/tests/file_example.txt +++ /dev/null @@ -1,3 +0,0 @@ -x: -INC R0 -JMP x \ No newline at end of file diff --git a/lua/wire/zvm/tests/ifdefs.lua b/lua/wire/zvm/tests/ifdefs.lua index a182aca..5095d00 100644 --- a/lua/wire/zvm/tests/ifdefs.lua +++ b/lua/wire/zvm/tests/ifdefs.lua @@ -1,4 +1,38 @@ -local CPUTest = {} +local Test = {} + +Test.Files = { + ["ifdefs.txt"] = [[ +/*defs will be added programatically*/ + +#ifdef x +#define z +#pragma cpuname Test X + ALLOC 1 +#ifdef y +#ifdef x +#pragma cpuname Test X and Y +#endif + ALLOC 2 +#endif +//not x +#else + ALLOC 4 +#endif + +#ifdef y +#ifndef x +#pragma cpuname Test Y +#endif +ALLOC 8 +#endif +/* +#ifdef y + ALLOC 8 +#endif +*/ +//above comment intentional for making sure ifdef handler doesn't skip into the middle of a comment +]] +} --not x and y = 14, name "Test Y" -- x and y = 11, name "Test Y" @@ -9,61 +43,58 @@ local CPUTest = {} -- x and y = 11, name "Test X and Y" -- x and not y = 1, name "Test X" +Test.ExpectedVariations1 = {"X", "Y", "X and Y", "Y", "Y", "Y"} -- CPU Name vars +Test.ExpectedVariations2 = {1, 12, 11, 1, 14, 11} +Test.ResultVariations1 = {} +Test.ResultVariations2 = {} -CPUTest.ExpectedVariations1 = {"X", "Y", "X and Y", "Y", "Y", "Y"} -- CPU Name vars -CPUTest.ExpectedVariations2 = {1, 12, 11, 1, 14, 11} -CPUTest.ResultVariations1 = {} -CPUTest.ResultVariations2 = {} +Test.Variations1 = {"true", "false"} +Test.Variations2 = {"#define x\n", "#define y\n", "#define x\n#define y\n"} +Test.Variation1Index = 1 +Test.Variation2Index = 1 -CPUTest.Variations1 = {"true", "false"} -CPUTest.Variations2 = {"#define x\n", "#define y\n", "#define x\n#define y\n"} -CPUTest.Variation1Index = 1 -CPUTest.Variation2Index = 1 - -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - -- Loads a file from the testing directory and returns it as a str - CPUTest.Src = TestSuite:LoadFile("ifdefs.txt") - CPUTest.CompileNext() +function Test.Run(CPU,TestSuite) + Test.TestSuite = TestSuite + Test.Src = TestSuite:LoadFile("ifdefs.txt") + Test.CompileNext() end -function CPUTest.CompileNext() +function Test.CompileNext() local cursrc - if CPUTest.Variation1Index <= #CPUTest.Variations1 then - cursrc = "#pragma set NewIfDefs " .. CPUTest.Variations1[CPUTest.Variation1Index] .. "\n" + if Test.Variation1Index <= #Test.Variations1 then + cursrc = "#pragma set NewIfDefs " .. Test.Variations1[Test.Variation1Index] .. "\n" else - return CPUTest.CompareResults() + return Test.CompareResults() end - if CPUTest.Variation2Index <= #CPUTest.Variations2 then - cursrc = cursrc .. CPUTest.Variations2[CPUTest.Variation2Index] .. "\n" .. CPUTest.Src - CPUTest.TestSuite.Compile(cursrc,nil,CPUTest.LogResults,CPUTest.CompileError) + if Test.Variation2Index <= #Test.Variations2 then + cursrc = cursrc .. Test.Variations2[Test.Variation2Index] .. "\n" .. Test.Src + Test.TestSuite.Compile(cursrc,nil,Test.LogResults,Test.CompileError) else - CPUTest.Variation1Index = CPUTest.Variation1Index + 1 - CPUTest.Variation2Index = 1 - CPUTest.CompileNext() + Test.Variation1Index = Test.Variation1Index + 1 + Test.Variation2Index = 1 + Test.CompileNext() end end -function CPUTest.LogResults() - CPUTest.ResultVariations1[CPUTest.Variation2Index + #CPUTest.Variations2*(CPUTest.Variation1Index-1)] = CPUTest.TestSuite.GetCPUName() or "ERROR" - CPUTest.ResultVariations2[CPUTest.Variation2Index + #CPUTest.Variations2*(CPUTest.Variation1Index-1)] = #CPUTest.TestSuite.GetCompileBuffer() + 1 or "ERROR" - CPUTest.Variation2Index = CPUTest.Variation2Index + 1 - CPUTest.CompileNext() +function Test.LogResults() + Test.ResultVariations1[Test.Variation2Index + #Test.Variations2*(Test.Variation1Index-1)] = Test.TestSuite.GetCPUName() or "ERROR" + Test.ResultVariations2[Test.Variation2Index + #Test.Variations2*(Test.Variation1Index-1)] = #Test.TestSuite.GetCompileBuffer() + 1 or "ERROR" + Test.Variation2Index = Test.Variation2Index + 1 + Test.CompileNext() end -function CPUTest.CompareResults() +function Test.CompareResults() local fail, results1, results2 = false, {}, {} - for ind, i in ipairs(CPUTest.ExpectedVariations1) do - if CPUTest.ResultVariations1[ind] == "Test " .. i then + for ind, i in ipairs(Test.ExpectedVariations1) do + if Test.ResultVariations1[ind] == "Test " .. i then results1[ind] = true else fail = true results1[ind] = false end end - for ind, i in ipairs(CPUTest.ExpectedVariations2) do - if CPUTest.ResultVariations2[ind] == i then + for ind, i in ipairs(Test.ExpectedVariations2) do + if Test.ResultVariations2[ind] == i then results2[ind] = true else fail = true @@ -71,17 +102,14 @@ function CPUTest.CompareResults() end end if fail then - CPUTest.TestSuite.Error("Unexpected test results!") - PrintTable({CPUTest.ResultVariations1, results1, CPUTest.ResultVariations2, results2}) - CPUTest.TestSuite.FinishTest(true) - else - CPUTest.TestSuite.FinishTest(false) + PrintTable({Test.ResultVariations1, results1, Test.ResultVariations2, results2}) + assert(false, "Unexpected Test Results!") end + return end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/ifdefs.txt b/lua/wire/zvm/tests/ifdefs.txt deleted file mode 100644 index c0cd75c..0000000 --- a/lua/wire/zvm/tests/ifdefs.txt +++ /dev/null @@ -1,38 +0,0 @@ -/*defs will be added programatically*/ - -#ifdef x -#define z -#pragma cpuname Test X - ALLOC 1 -#ifdef y -#ifdef x -#pragma cpuname Test X and Y -#endif - ALLOC 2 -#endif -//not x -#else - ALLOC 4 -#endif - -#ifdef y -#ifndef x -#pragma cpuname Test Y -#endif -ALLOC 8 -#endif -/* -#ifdef y - ALLOC 8 -#endif -*/ -//above comment intentional for making sure ifdef handler doesn't skip into the middle of a comment - -// not x and y = 14, name "Test Y" -// x and y = 11, name "Test Y" -// x and not y = 1, name "Test Y" - -// culling update -// not x and y = 12, name "Test Y" -// x and y = 11, name "Test X and Y" -// x and not y = 1, name "Test X" diff --git a/lua/wire/zvm/tests/includes.lua b/lua/wire/zvm/tests/includes.lua index 8a502f3..b07a169 100644 --- a/lua/wire/zvm/tests/includes.lua +++ b/lua/wire/zvm/tests/includes.lua @@ -1,31 +1,29 @@ -local CPUTest = {} +local Test = {} -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 +Test.Files = { + ["includes_1.txt"] = [[ +#include +ADD R0,1 +]], + + ["includes_2.txt"] = [[ +MOV R0,1 +]] +} -function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - CPUTest.VM.Clk = 1 +function Test.Run(CPU,TestSuite) + local src = TestSuite:LoadFile("includes_1.txt") + TestSuite:Deploy(CPU, src, Test.CompileError) + CPU.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 - CPUTest.TestSuite.Error("R0 is not 2! R0 is " .. tostring(CPUTest.VM.R0)) - CPUTest.TestSuite.FinishTest(true) + CPU:RunStep() end + + assert(CPU.R0 == 2, "R0 is not 2! R0 is " .. tostring(CPU.R0)) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/includes_1.txt b/lua/wire/zvm/tests/includes_1.txt deleted file mode 100644 index c419b54..0000000 --- a/lua/wire/zvm/tests/includes_1.txt +++ /dev/null @@ -1,2 +0,0 @@ -#include -ADD R0,1 \ No newline at end of file diff --git a/lua/wire/zvm/tests/includes_2.txt b/lua/wire/zvm/tests/includes_2.txt deleted file mode 100644 index 1c24af2..0000000 --- a/lua/wire/zvm/tests/includes_2.txt +++ /dev/null @@ -1 +0,0 @@ -MOV R0,1 \ No newline at end of file diff --git a/lua/wire/zvm/tests/intentional_compile_error.lua b/lua/wire/zvm/tests/intentional_compile_error.lua index bab5044..996869b 100644 --- a/lua/wire/zvm/tests/intentional_compile_error.lua +++ b/lua/wire/zvm/tests/intentional_compile_error.lua @@ -1,18 +1,11 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,", nil, CPUTest.RunCPU, CPUTest.CompileError) +function Test.Run(CPU,TestSuite) + return TestSuite.Compile("MOV R0,", nil, Test.CompileSucceed) end -function CPUTest.RunCPU() - CPUTest.TestSuite.Error("Compiler did not error when it should have!") - CPUTest.TestSuite.FinishTest(true) +function Test.CompileSucceed() + assert(false, "Compiler should have errored!") end -function CPUTest.CompileError() - CPUTest.TestSuite.FinishTest(false) -end - -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/intentional_failed_test.lua b/lua/wire/zvm/tests/intentional_failed_test.lua index b852cd4..ea52283 100644 --- a/lua/wire/zvm/tests/intentional_failed_test.lua +++ b/lua/wire/zvm/tests/intentional_failed_test.lua @@ -1,29 +1,19 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("x: INC R0 JMP x", 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 +function Test.Run(CPU,TestSuite) + TestSuite:Deploy(CPU,"x: INC R0 JMP x",Test.CompileError) + CPU.Clk = 1 + -- Run the VM for 4096 cycles for i = 0, 4096 do - CPUTest.VM:RunStep() - end - -- False = no error, True = error - if CPUTest.VM.R0 == 4095 then - CPUTest.TestSuite.FinishTest(false) - else - CPUTest.TestSuite.Error("R0 is not 4095! R0 is " .. tostring(CPUTest.VM.R0)) - CPUTest.TestSuite.FinishTest(true) + CPU:RunStep() end + + -- On false, will cause test to fail with message + assert(CPU.R0 == 4095, "[INTENTIONAL ERROR] R0 is not 4095! R0 is " .. tostring(CPU.R0)) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/no_internal_mem.lua b/lua/wire/zvm/tests/no_internal_mem.lua index cf90c6b..c8a417c 100644 --- a/lua/wire/zvm/tests/no_internal_mem.lua +++ b/lua/wire/zvm/tests/no_internal_mem.lua @@ -1,35 +1,24 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV R0,1", nil, CPUTest.RunCPU, CPUTest.CompileError) -end -function CPUTest.RunCPU() - local buff = CPUTest.TestSuite.GetCompileBuffer() - local bus = CPUTest.TestSuite.CreateVirtualMemBus(#buff) -- get external ram device large enough to hold program - CPUTest.TestSuite.FlashData(bus, buff) -- upload compiled to membus - CPUTest.VM.RAMSize = 0 - CPUTest.VM.ROMSize = 0 - CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus - CPUTest.VM.Clk = 1 +function Test.Run(CPU,TestSuite) + TestSuite.Compile("MOV R0,1", "internal", nil, Test.CompileError) + local buff = TestSuite.GetCompileBuffer() + local bus = TestSuite.CreateVirtualMemBus(#buff) -- get external ram device large enough to hold program + TestSuite.FlashData(bus, buff) -- upload compiled to membus + CPU.RAMSize = 0 + CPU.ROMSize = 0 + TestSuite.Initialize(CPU, bus, nil) -- reinitialize the CPU with the membus + CPU.Clk = 1 for i = 0, 16 do - CPUTest.VM:RunStep() + CPU:RunStep() end - -- False = no error, True = error - if CPUTest.VM.R0 == 1 then - CPUTest.TestSuite.FinishTest(false) - else - CPUTest.TestSuite.Error("CPU with no ram/rom failed to execute code from bus! R0 = " .. CPUTest.VM.R0) - CPUTest.TestSuite.FinishTest(true) - end + assert(CPU.R0 == 1, "CPU with no ram/rom failed to execute code from bus! R0 = " .. CPU.R0) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/virtualiobus.lua b/lua/wire/zvm/tests/virtualiobus.lua index fc7b13a..97b3834 100644 --- a/lua/wire/zvm/tests/virtualiobus.lua +++ b/lua/wire/zvm/tests/virtualiobus.lua @@ -1,42 +1,24 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0", nil, CPUTest.RunCPU, CPUTest.CompileError) -end - -function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(4) -- get external IO device of size 4 - CPUTest.TestSuite.Initialize(CPUTest.VM, nil, IOBus) -- reinitialize the CPU with the IOBus +function Test.Run(CPU,TestSuite) + TestSuite:Deploy(CPU, "MOV PORT0,1 MOV R0,PORT0", Test.CompileError) + local IOBus = TestSuite.CreateVirtualIOBus(4) -- get external IO device of size 4 + TestSuite.Initialize(CPU, nil, IOBus) -- reinitialize the CPU with the IOBus + IOBus.InPorts[0] = 24 - CPUTest.VM.Clk = 1 + CPU.Clk = 1 for i = 0, 16 do - CPUTest.VM:RunStep() + CPU:RunStep() end -- False = no error, True = error - if IOBus:ReadCell(0) == 24 then - if IOBus.OutPorts[0] == 1 then - if CPUTest.VM.R0 == 24 then - CPUTest.TestSuite.FinishTest(false) - else - CPUTest.TestSuite.Error("CPU failed to read input port! R0 = " .. CPUTest.VM.R0) - CPUTest.TestSuite.FinishTest(true) - end - else - CPUTest.TestSuite.Error("CPU failed to write to output port! Port0 = " .. IOBus.OutPorts[0]) - end - else - CPUTest.TestSuite.Error("CPU wrote to input ports! " .. tostring(IOBus:ReadCell(0))) - CPUTest.TestSuite.FinishTest(true) - end + assert(IOBus:ReadCell(0) == 24, "IOBus InPort 0 != 24! Possibly CPU's fault? InPort 0 = " .. IOBus:ReadCell(0)) + assert(IOBus.OutPorts[0] == 1, "CPU failed to write to output port! Port0 = " .. tostring(IOBus.OutPorts[0])) + assert(CPU.R0 == 24, "CPU failed to read input port! R0 = " .. CPU.R0) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error " .. msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/tests/virtualmembus.lua b/lua/wire/zvm/tests/virtualmembus.lua index 7298c5a..de1d4a0 100644 --- a/lua/wire/zvm/tests/virtualmembus.lua +++ b/lua/wire/zvm/tests/virtualmembus.lua @@ -1,37 +1,20 @@ -local CPUTest = {} +local Test = {} -function CPUTest:RunTest(VM,TestSuite) - CPUTest.VM = VM - CPUTest.TestSuite = TestSuite - TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]", nil, CPUTest.RunCPU, CPUTest.CompileError) -end - -function CPUTest.RunCPU() - CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu - local bus = CPUTest.TestSuite.CreateVirtualMemBus(4) -- get external ram device of size 4 - CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus - CPUTest.VM.Clk = 1 +function Test.Run(CPU,TestSuite) + TestSuite:Deploy(CPU,"CPUGET R0,43 MOV [R0],1 MOV R1,[R0]",Test.CompileError) + local bus = TestSuite.CreateVirtualMemBus(4) -- get external ram device of size 4 + TestSuite.Initialize(CPU, bus, nil) -- reinitialize the CPU with the membus + CPU.Clk = 1 for i = 0, 16 do - CPUTest.VM:RunStep() + CPU:RunStep() end - -- False = no error, True = error - if bus:ReadCell(0) == 1 then - if CPUTest.VM.R1 == 1 then - CPUTest.TestSuite.FinishTest(false) - else - CPUTest.TestSuite.Error("CPU failed to read the bus! R1 was "..tostring(CPUTest.VM.R1)) - CPUTest.TestSuite.FinishTest(true) - end - else - CPUTest.TestSuite.Error("CPU failed to write to bus! "..tostring(bus:ReadCell(0))) - CPUTest.TestSuite.FinishTest(true) - end + assert(bus:ReadCell(0) == 1, "CPU failed to write to bus! " .. tostring(bus:ReadCell(0))) + assert(CPU.R1 == 1, "CPU failed to read the bus! R1 was " .. CPU.R1) end -function CPUTest.CompileError(msg) - CPUTest.TestSuite.Error("hit a compile time error "..msg) - CPUTest.TestSuite.FinishTest(true) +function Test.CompileError(msg) + assert(false, "compile time error: " .. msg) end -return CPUTest \ No newline at end of file +return Test \ No newline at end of file diff --git a/lua/wire/zvm/zvm_tests.lua b/lua/wire/zvm/zvm_tests.lua index ccc13d2..d9b81db 100644 --- a/lua/wire/zvm/zvm_tests.lua +++ b/lua/wire/zvm/zvm_tests.lua @@ -92,7 +92,7 @@ function ZVMTestSuite.FinishTest(fail) errormod = "s" end if ZVMTestSuite.Warnings > 0 then - warnstring = ZVMTestSuite.Warnings .. " Compiler Warnings were generated" + warnstring = ZVMTestSuite.Warnings .. " Compiler Warnings" end print(failed .. " Failed test" .. errormod .. ", " ..passed.. " Passed test" ..passmod.. ", " .. warnstring) end @@ -118,10 +118,22 @@ function ZVMTestSuite.RunNextTest() ZVMTestSuite.Initialize(curVM) print("Running " .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) local CPUTest = include(testDirectory .. "/" .. ZVMTestSuite.TestQueue[#ZVMTestSuite.TestQueue]) - CPUTest:RunTest(curVM,ZVMTestSuite) + -- if the test provides a file table, we should use the table when grabbing files for the compiler/test + -- instead of the directory containing the tests + ZVMTestSuite.VirtualFiles = CPUTest.Files + local success, msg = pcall(CPUTest.Run,curVM,ZVMTestSuite) + if success ~= nil and success then + return ZVMTestSuite.FinishTest(false) + else + ZVMTestSuite.Error(tostring(msg)) + return ZVMTestSuite.FinishTest(true) + end end function ZVMTestSuite:LoadFile(FileName) + if ZVMTestSuite.VirtualFiles then + return ZVMTestSuite.VirtualFiles[FileName] + end return file.Read("lua/" .. testDirectory .. "/" .. FileName, "GAME") end @@ -139,13 +151,17 @@ end function ZVMTestSuite.InternalSuccessCallback() HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning - ZVMTestSuite.CompileArgs.SuccessCallback() + if ZVMTestSuite.CompileArgs.SuccessCallback then + ZVMTestSuite.CompileArgs.SuccessCallback() + end end -function ZVMTestSuite.InternalErrorCallback() +function ZVMTestSuite.InternalErrorCallback(msg) HCOMP.LoadFile = ZVMTestSuite.HCOMPLoadFile HCOMP.Warning = ZVMTestSuite.OldHCOMPWarning - ZVMTestSuite.CompileArgs.ErrorCallback() + if ZVMTestSuite.CompileArgs.ErrorCallback then + ZVMTestSuite.CompileArgs.ErrorCallback(msg) + end end function ZVMTestSuite.OnWriteByte(caller,address,data) @@ -169,15 +185,15 @@ function ZVMTestSuite.StartCompileInternal() ZVMTestSuite.Buffer = {} HCOMP:StartCompile(SourceCode, FileName or "source", ZVMTestSuite.OnWriteByte, nil) HCOMP.Settings.CurrentPlatform = "CPU" - local noError, anotherStep = true, true + local noError, returnedValue = true, true local steps = 0 - while noError and anotherStep do - noError,anotherStep = pcall(HCOMP.Compile, HCOMP) + while noError and returnedValue do + noError, returnedValue = pcall(HCOMP.Compile, HCOMP) end if not noError then - return ErrorCallback(HCOMP.ErrorMessage or ("Internal error: " .. result), HCOMP.ErrorPosition) + return ErrorCallback(HCOMP.ErrorMessage or ("Internal error: " .. returnedValue), HCOMP.ErrorPosition) end - if not anotherStep then + if not returnedValue then return SuccessCallback() end end @@ -236,7 +252,7 @@ function ZVMTestSuite.AddVirtualFunctions(VM) ZVMTestSuite:FlashData(self,data) end function VM:RunStep() - ZVMTestSuite:Run(self) + ZVMTestSuite.Run(self) end function VM:TriggerInput(iname,name) ZVMTestSuite.TriggerInput(self,iname,name) @@ -261,52 +277,26 @@ function ZVMTestSuite.FlashData(VM,data) end end +function ZVMTestSuite:Deploy(device,code,errcallback) + self.Compile(code,"internal", nil, errcallback) + self.FlashData(device, self.GetCompileBuffer()) +end + -- Execute ZCPU virtual machine -function ZVMTestSuite:Run(VM) +function ZVMTestSuite.Run(VM) -- Calculate time-related variables local CurrentTime = CurTime() local DeltaTime = math.min(1/30,CurrentTime - (VM.PreviousTime or 0)) VM.PreviousTime = CurrentTime - -- Check if need to run till specific instruction - if VM.BreakpointInstructions then - VM.TimerDT = DeltaTime - VM.CPUIF = VM - VM:Step(8,function(VM) - VM:Dyn_Emit("if (VM.CPUIF.Clk and not VM.CPUIF.VMStopped) and (VM.CPUIF.OnVMStep) then") - VM:Dyn_EmitState() - VM:Emit("VM.CPUIF.OnVMStep()") - VM:Emit("end") - VM:Emit("if VM.CPUIF.BreakpointInstructions[VM.IP] then") - VM:Dyn_EmitState() - VM:Emit("VM.CPUIF.OnBreakpointInstruction(VM.IP)") - VM:Emit("VM.CPUIF.VMStopped = true") - VM:Emit("VM.TMR = VM.TMR + "..VM.PrecompileInstruction) - VM:Emit("VM.CODEBYTES = VM.CODEBYTES + "..VM.PrecompileBytes) - VM:Emit("if true then return end") - VM:Emit("end") - VM:Emit("if VM.CPUIF.LastInstruction and ((VM.IP > VM.CPUIF.LastInstruction) or VM.CPUIF.ForceLastInstruction) then") - VM:Dyn_EmitState() - VM:Emit("VM.CPUIF.ForceLastInstruction = nil") - VM:Emit("VM.CPUIF.OnLastInstruction()") - VM:Emit("VM.CPUIF.VMStopped = true") - VM:Emit("VM.TMR = VM.TMR + "..VM.PrecompileInstruction) - VM:Emit("VM.CODEBYTES = VM.CODEBYTES + "..VM.PrecompileBytes) - VM:Emit("if true then return end") - VM:Emit("end") - end) - VM.CPUIF = nil - else - -- How many steps VM must make to keep up with execution - local Cycles = math.max(1,math.floor(VM.Frequency*DeltaTime*0.5)) - VM.TimerDT = (DeltaTime/Cycles) - - while (Cycles > 0) and (VM.Clk) and (not VMStopped) and (VM.Idle == 0) do - -- Run VM step - local previousTMR = VM.TMR - VM:Step() - Cycles = Cycles - math.max(1, VM.TMR - previousTMR) - end + local Cycles = math.max(1,math.floor(VM.Frequency*DeltaTime*0.5)) + VM.TimerDT = (DeltaTime/Cycles) + + while (Cycles > 0) and (VM.Clk) and (not VMStopped) and (VM.Idle == 0) do + -- Run VM step + local previousTMR = VM.TMR + VM:Step() + Cycles = Cycles - math.max(1, VM.TMR - previousTMR) end -- Update VM timer @@ -325,12 +315,7 @@ function ZVMTestSuite.TriggerInput(VM, iname, value) elseif iname == "Frequency" then if value > 0 then VM.Frequency = math.floor(value) end elseif iname == "Reset" then --VM may be nil - if VM.HWDEBUG ~= 0 then - VM.DBGSTATE = math.floor(value) - if (value > 0) and (value <= 1.0) then VM:Reset() end - else if value >= 1.0 then VM:Reset() end - end elseif iname == "Interrupt" then if (value >= 32) and (value < 256) then if (VM.Clk and not VM.VMStopped) then VM:ExternalInterrupt(math.floor(value)) end