From a58b8a9ef0c2349b007ba8e36bee5339cc6a462b Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Tue, 5 Jan 2021 16:10:28 +0100 Subject: [PATCH] Removed unused/dead code + improved test suite code quality --- src/multibyte.nim | 23 ++++++- src/vm.nim | 5 -- tests/multibyte.nim | 17 +++++ tests/runtests.nim | 147 ++++++++++++++++++++++++-------------------- 4 files changed, 119 insertions(+), 73 deletions(-) diff --git a/src/multibyte.nim b/src/multibyte.nim index 3b92930..d60e11c 100644 --- a/src/multibyte.nim +++ b/src/multibyte.nim @@ -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] = - 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 = + ## Rebuilds the output of toDouble into + ## an uint16 copyMem(result.addr, unsafeAddr(input), sizeof(uint16)) diff --git a/src/vm.nim b/src/vm.nim index 8c61e86..195a433 100644 --- a/src/vm.nim +++ b/src/vm.nim @@ -599,13 +599,8 @@ proc run(self: var VM, repl: bool): InterpretResult = if self.frameCount == 0: discard self.pop() return OK - self.stackTop -= frame.clear() - # frame.clear() clears the stack and returns the amount cleared - self.push(retResult) -# self.stackTop = len(frame.getView()) - 1 # TODO - frame = self.frames[self.frameCount - 1] diff --git a/tests/multibyte.nim b/tests/multibyte.nim index 80af798..2cd2b93 100644 --- a/tests/multibyte.nim +++ b/tests/multibyte.nim @@ -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 diff --git a/tests/runtests.nim b/tests/runtests.nim index 815da55..a04df58 100644 --- a/tests/runtests.nim +++ b/tests/runtests.nim @@ -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 # # - 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}`) # -# go through all nim tests -import multibyte -testMultibyte() +# Imports nim tests as well +import multibyte, os, strformat, times, re -# go through all japl tests -import os, strformat, times, re - proc compileExpectedOutput(path: string): string = for line in path.lines(): if line =~ re"^.*//output:(.*)$": result &= matches[0] & "\n" + proc deepComp(left, right: string): tuple[same: bool, place: int] = result.same = true if left.high() != right.high(): @@ -33,70 +46,70 @@ proc deepComp(left, right: string): tuple[same: bool, place: int] = return -var testsDir = "tests" / "japl" -var japlExec = "src" / "japl" +# Quick logging levels using procs -# support running from both the japl root and the tests dir where it -# resides -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}") +proc log(file: File, msg: string) = + file.writeLine(&"[LOG] {msg}") echo msg -proc detail(msg: string) = - testResultsFile.writeLine(&"[DETAIL] {msg}") -# Exceptions for tests that represent not-yet implemented behaviour -var exceptions = @["all.jpl"] - -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) +proc detail(file: File, msg: string) = + file.writeLine(&"[DETAIL] {msg}") -for file in walkDir(testsDir): - block singularTest: - for exc in exceptions: - if exc == file.path.extractFilename: - log &"Skipping {file.path} because it's on the exceptions list" - break singularTest - - log &"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 &"Successful test {file.path}" - else: - detail &"Expected output:\n{expectedOutput}\n" - detail &"Received output:\n{realOutput}\n" - detail &"Mismatch at pos {comparison.place}" - if comparison.place > expectedOutput.high() or - comparison.place > realOutput.high(): - detail &"Length mismatch" - else: - detail &"Expected is '{expectedOutput[comparison.place]}' while received '{realOutput[comparison.place]}'" - log &"Test failed {file.path}, check 'testresults.txt' for details" - -testResultsFile.close() +when isMainModule: + try: + testMultibyte() + var testsDir = "tests" / "japl" + var japlExec = "src" / "japl" + # support running from both the japl root and the tests dir where it + # resides + var currentDir = getCurrentDir() + if currentDir.lastPathPart() == "tests": + testsDir = "japl" + japlExec = ".." / japlExec + let testResultsFile = open("testresults.txt", fmAppend) + testResultsFile.writeLine(&"Executing tests at {$getTime()}") + # Exceptions for tests that represent not-yet implemented behaviour + var exceptions = @["all.jpl"] + log(testResultsFile, "Running JAPL tests") + log(testResultsFile, &"Looking for JAPL tests in {testsDir}") + log(testResultsFile, &"Looking for JAPL executable at {japlExec}") + if not fileExists(japlExec): + log(testResultsFile, "JAPL executable not found") + quit(1) + if not dirExists(testsDir): + log(testResultsFile, "Tests dir not found") + quit(1) + for file in walkDir(testsDir): + block singleTest: + for exc in exceptions: + if exc == file.path.extractFilename: + log(testResultsFile, &"Skipping {file.path} because it's on the exceptions list") + break singleTest + 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()}")