Removed unused/dead code + improved test suite code quality

This commit is contained in:
nocturn9x 2021-01-05 16:10:28 +01:00
parent 97b7c5d91e
commit a58b8a9ef0
4 changed files with 119 additions and 73 deletions

View File

@ -1,7 +1,28 @@
# Copyright 2020 Mattia Giambirtone
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Makes our short integers platform-independent (big vs little endian)
proc toDouble*(input: int | uint | uint16): array[2, uint8] = proc toDouble*(input: int | uint | uint16): array[2, uint8] =
cast[array[2, uint8]](uint16(input)) ## Converts an int (either int, uint or uint16)
## to an array[2, uint8]
result = cast[array[2, uint8]](uint16(input))
proc fromDouble*(input: array[2, uint8]): uint16 = proc fromDouble*(input: array[2, uint8]): uint16 =
## Rebuilds the output of toDouble into
## an uint16
copyMem(result.addr, unsafeAddr(input), sizeof(uint16)) copyMem(result.addr, unsafeAddr(input), sizeof(uint16))

View File

@ -599,13 +599,8 @@ proc run(self: var VM, repl: bool): InterpretResult =
if self.frameCount == 0: if self.frameCount == 0:
discard self.pop() discard self.pop()
return OK return OK
self.stackTop -= frame.clear() self.stackTop -= frame.clear()
# frame.clear() clears the stack and returns the amount cleared
self.push(retResult) self.push(retResult)
# self.stackTop = len(frame.getView()) - 1 # TODO
frame = self.frames[self.frameCount - 1] frame = self.frames[self.frameCount - 1]

View File

@ -1,3 +1,20 @@
# Copyright 2020 Mattia Giambirtone
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Tests that our multibyte module works
import ../src/multibyte import ../src/multibyte

View File

@ -1,3 +1,19 @@
# Copyright 2020 Mattia Giambirtone
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Common entry point to run JAPL's tests # Common entry point to run JAPL's tests
# #
# - Assumes "japl" binary in ../src/japl built with all debugging off # - Assumes "japl" binary in ../src/japl built with all debugging off
@ -5,19 +21,16 @@
# - Runs all tests in (/tests/)japl/ and checks their output (marked by `//output:{output}`) # - Runs all tests in (/tests/)japl/ and checks their output (marked by `//output:{output}`)
# #
# go through all nim tests # Imports nim tests as well
import multibyte import multibyte, os, strformat, times, re
testMultibyte()
# go through all japl tests
import os, strformat, times, re
proc compileExpectedOutput(path: string): string = proc compileExpectedOutput(path: string): string =
for line in path.lines(): for line in path.lines():
if line =~ re"^.*//output:(.*)$": if line =~ re"^.*//output:(.*)$":
result &= matches[0] & "\n" result &= matches[0] & "\n"
proc deepComp(left, right: string): tuple[same: bool, place: int] = proc deepComp(left, right: string): tuple[same: bool, place: int] =
result.same = true result.same = true
if left.high() != right.high(): if left.high() != right.high():
@ -33,70 +46,70 @@ proc deepComp(left, right: string): tuple[same: bool, place: int] =
return return
var testsDir = "tests" / "japl" # Quick logging levels using procs
var japlExec = "src" / "japl"
# support running from both the japl root and the tests dir where it proc log(file: File, msg: string) =
# resides file.writeLine(&"[LOG] {msg}")
var currentDir = getCurrentDir()
if currentDir.lastPathPart() == "tests":
testsDir = "japl"
japlExec = ".." / japlExec
let testResultsFile = open("testresults.txt", fmAppend)
testResultsFile.writeLine(&"Executing tests at {$getTime()}")
# quick logging levels using procs
proc log(msg: string) =
testResultsFile.File.writeLine(&"[LOG] {msg}")
echo msg echo msg
proc detail(msg: string) =
testResultsFile.writeLine(&"[DETAIL] {msg}")
# Exceptions for tests that represent not-yet implemented behaviour proc detail(file: File, msg: string) =
var exceptions = @["all.jpl"] file.writeLine(&"[DETAIL] {msg}")
log &"Running JAPL tests"
log &"Looking for JAPL tests in {testsDir}"
log &"Looking for JAPL executable at {japlExec}"
if not fileExists(japlExec):
log &"JAPL executable not found"
quit(1)
if not dirExists(testsDir):
log &"Tests dir not found"
quit(1)
for file in walkDir(testsDir): when isMainModule:
block singularTest: try:
for exc in exceptions: testMultibyte()
if exc == file.path.extractFilename: var testsDir = "tests" / "japl"
log &"Skipping {file.path} because it's on the exceptions list" var japlExec = "src" / "japl"
break singularTest # support running from both the japl root and the tests dir where it
# resides
log &"Running test {file.path}" var currentDir = getCurrentDir()
if fileExists("testoutput.txt"): if currentDir.lastPathPart() == "tests":
removeFile("testoutput.txt") # in case this crashed testsDir = "japl"
discard execShellCmd(&"{japlExec} {file.path} >>testoutput.txt") japlExec = ".." / japlExec
let expectedOutput = compileExpectedOutput(file.path).replace(re"(\n*)$", "") let testResultsFile = open("testresults.txt", fmAppend)
let realOutputFile = open("testoutput.txt", fmRead) testResultsFile.writeLine(&"Executing tests at {$getTime()}")
let realOutput = realOutputFile.readAll().replace(re"([\n\r]*)$", "") # Exceptions for tests that represent not-yet implemented behaviour
realOutputFile.close() var exceptions = @["all.jpl"]
removeFile("testoutput.txt") log(testResultsFile, "Running JAPL tests")
let comparison = deepComp(expectedOutput, realOutput) log(testResultsFile, &"Looking for JAPL tests in {testsDir}")
if comparison.same: log(testResultsFile, &"Looking for JAPL executable at {japlExec}")
log &"Successful test {file.path}" if not fileExists(japlExec):
else: log(testResultsFile, "JAPL executable not found")
detail &"Expected output:\n{expectedOutput}\n" quit(1)
detail &"Received output:\n{realOutput}\n" if not dirExists(testsDir):
detail &"Mismatch at pos {comparison.place}" log(testResultsFile, "Tests dir not found")
if comparison.place > expectedOutput.high() or quit(1)
comparison.place > realOutput.high(): for file in walkDir(testsDir):
detail &"Length mismatch" block singleTest:
else: for exc in exceptions:
detail &"Expected is '{expectedOutput[comparison.place]}' while received '{realOutput[comparison.place]}'" if exc == file.path.extractFilename:
log &"Test failed {file.path}, check 'testresults.txt' for details" log(testResultsFile, &"Skipping {file.path} because it's on the exceptions list")
break singleTest
testResultsFile.close() log(testResultsFile, &"Running test {file.path}")
if fileExists("testoutput.txt"):
removeFile("testoutput.txt") # in case this crashed
discard execShellCmd(&"{japlExec} {file.path} >>testoutput.txt")
let expectedOutput = compileExpectedOutput(file.path).replace(re"(\n*)$", "")
let realOutputFile = open("testoutput.txt", fmRead)
let realOutput = realOutputFile.readAll().replace(re"([\n\r]*)$", "")
realOutputFile.close()
removeFile("testoutput.txt")
let comparison = deepComp(expectedOutput, realOutput)
if comparison.same:
log(testResultsFile, &"Successful test {file.path}")
else:
detail(testResultsFile, &"Expected output:\n{expectedOutput}\n")
detail(testResultsFile, &"Received output:\n{realOutput}\n")
detail(testResultsFile, &"Mismatch at pos {comparison.place}")
if comparison.place > expectedOutput.high() or
comparison.place > realOutput.high():
detail(testResultsFile, &"Length mismatch")
else:
detail(testResultsFile, &"Expected is '{expectedOutput[comparison.place]}' while received '{realOutput[comparison.place]}'")
log(testResultsFile, &"Test failed {file.path}, check 'testresults.txt' for details")
testResultsFile.close()
except IOError:
stderr.write(&"Fatal IO error encountered while running tesrs -> {getCurrentExceptionMsg()}")