aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2017-08-30 21:37:53 +0200
committerKarel Kočí <cynerd@email.cz>2017-08-30 21:42:02 +0200
commit9cf92379d5fcf0076c25dae0935daab446c992cd (patch)
treedd09a2e996db1e5a8117f01bec76f1e93eaca6e1 /tests
downloadqtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.tar.gz
qtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.tar.bz2
qtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.zip
Initial commit
Adding work done so far.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/machine-unit-tests/test.sh11
-rw-r--r--tests/registers/Makefile5
-rw-r--r--tests/registers/registers.S8
-rwxr-xr-xtests/registers/test.sh15
-rwxr-xr-xtests/run-all.sh7
-rw-r--r--tests/test.mk39
-rw-r--r--tests/test.sh99
7 files changed, 184 insertions, 0 deletions
diff --git a/tests/machine-unit-tests/test.sh b/tests/machine-unit-tests/test.sh
new file mode 100755
index 0000000..c916f74
--- /dev/null
+++ b/tests/machine-unit-tests/test.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+TEST_NAME="machine-unit-test"
+
+# Load common test shell functions
+. "$(dirname "$0")/../test.sh"
+
+# Build tests binary
+qtmips_make sub-qtmips_machine-tests
+
+# Run unit tests
+qtmips_run qtmips_machine/tests/tst_machine || echo_fail "Test $TEST_NAME failed!"
diff --git a/tests/registers/Makefile b/tests/registers/Makefile
new file mode 100644
index 0000000..4b5ebdc
--- /dev/null
+++ b/tests/registers/Makefile
@@ -0,0 +1,5 @@
+include ../test.mk
+
+SRC_registers = registers.S
+
+$(eval $(call MIPS_ELF,registers))
diff --git a/tests/registers/registers.S b/tests/registers/registers.S
new file mode 100644
index 0000000..64ff5ac
--- /dev/null
+++ b/tests/registers/registers.S
@@ -0,0 +1,8 @@
+.text
+.globl _start
+
+_start:
+ j main
+
+main:
+ addi $1, $0, 6
diff --git a/tests/registers/test.sh b/tests/registers/test.sh
new file mode 100755
index 0000000..2ace7cd
--- /dev/null
+++ b/tests/registers/test.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+TEST_NAME="registers"
+
+# Load common test shell functions
+. "$(dirname "$0")/../test.sh"
+
+# Build cli binary
+qtmips_make sub-qtmips_cli
+
+# Compile mips binary
+mips_make_test
+
+# Run test
+qtmips_run qtmips_cli/qtmips_cli "$TEST_DIR/registers" \
+ || echo_fail "Test $TEST_NAME failed!"
diff --git a/tests/run-all.sh b/tests/run-all.sh
new file mode 100755
index 0000000..9c57391
--- /dev/null
+++ b/tests/run-all.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+# Run all tests
+set -e
+
+for T in $(find "$(dirname "$0")" -name test.sh -type f -executable); do
+ timeout -k 120 60 "$T"
+done
diff --git a/tests/test.mk b/tests/test.mk
new file mode 100644
index 0000000..c05a72b
--- /dev/null
+++ b/tests/test.mk
@@ -0,0 +1,39 @@
+MAKEFLAGS += --no-builtin-rules
+
+# Output path
+O ?= .
+
+#MIPS_CFLAGS += -Wall
+MIPS_LDFLAGS += -nostdlib -nodefaultlibs -nostartfiles -Wl,-Ttext,0x80020000
+
+.PHONY: all
+all::
+ @
+
+.PHONY: clean
+clean::
+ @
+
+
+define MIPS_ELF
+
+ifndef MIPS_PREFIX
+ $$(error Toolchain prefix have to be passed in MIPS_PREFIX variable)
+endif
+
+OBJ_$(1):=$$(patsubst %.S,$$(O)/%.o,$$(SRC_$(1)))
+$$(info $$(OBJ_$(1)))
+
+all:: $$(O)/$(1)
+
+$$(O)/$(1): $$(OBJ_$(1))
+ $$(MIPS_PREFIX)-gcc $$(MIPS_LDFLAGS) -o $$@ $$^
+
+$$(OBJ_$(1)): $$(O)/%.o: %.S
+ $$(MIPS_PREFIX)-gcc $$(MIPS_CFLAGS) -c -x assembler-with-cpp -o $$@ $$<
+
+clean::
+ $(RM) $$(OBJ_$(1))
+ $(RM) $$(O)/$(1)
+
+endef
diff --git a/tests/test.sh b/tests/test.sh
new file mode 100644
index 0000000..7145a0c
--- /dev/null
+++ b/tests/test.sh
@@ -0,0 +1,99 @@
+# Enable shell exit on failure
+set -e
+
+## Colored echo function ########################################################
+
+echo_head() {
+ echo -e "\e[1;34m-- $@ --\e[0m" >&2
+}
+
+echo_msg() {
+ echo -e "\e[1;33m$@\e[0m" >&2
+}
+
+echo_fail() {
+ echo -e "\e[1;31m$@\e[0m" >&2
+ exit 1
+}
+
+## Check and init test ##########################################################
+
+# Check if test name is defined
+[ -n "$TEST_NAME" ] || echo_fail "Test name is not defined! Define TEST_NAME variable in test script."
+# Report user what test is going to be executed
+echo_head "Running test $TEST_NAME"
+
+## Define some variables ########################################################
+
+# Store base directory from which we started test script
+PWD_DIR="$(pwd)"
+# Define path to test sources
+TEST_SRC="$(dirname "$(readlink -f "$0")")"
+
+# Get project root.
+# As we want to have script calling this anywhere, we have to do some more work to
+# get project root. But we know content of root and so we can use that to locate
+# it and we know that test source directory is part of tree so it's under root.
+PROJECT_ROOT="$TEST_SRC"
+while [ -n "$PROJECT_ROOT" ] && ! [ \
+ -f "$PROJECT_ROOT/LICENSE" -a \
+ -f "$PROJECT_ROOT/README.md" -a \
+ -d "$PROJECT_ROOT/qtmips_gui" -a \
+ -d "$PROJECT_ROOT/qtmips_cli" -a \
+ -d "$PROJECT_ROOT/qtmips_machine" \
+ ]; do
+ PROJECT_ROOT="${PROJECT_ROOT%/*}"
+done
+[ -n "$PROJECT_ROOT" ] || echo_fail "Project root not found!!"
+
+# Define root for all tests
+# We are little bit clever about it. If we detect that we are in project tree we
+# will use same directory every time. Otherwise we use current working directory
+# where we create test_dir directory.
+if [ -n "${PWD_DIR#$PROJECT_ROOT}" ]; then
+ TEST_DIR_ROOT="$PROJECT_ROOT/test_dir"
+else
+ TEST_DIR_ROOT="$PWD_DIR/test_dir"
+fi
+
+# Directory where qtmips will be build
+BUILD_DIR="$TEST_DIR_ROOT/build"
+
+# Directory where test should be build and executed
+TEST_DIR="$TEST_DIR_ROOT/$TEST_NAME"
+
+## Helper functions for building and running project ############################
+
+qtmips_make() {
+ mkdir -p "$BUILD_DIR"
+ pushd "$BUILD_DIR" >/dev/null
+ /usr/lib64/qt5/bin/qmake "$PROJECT_ROOT" || echo_fail "QtMips qmake failed!"
+ make "$@" || echo_fail "QtMips build failed! (target: $@)"
+ popd >/dev/null
+}
+
+qtmips_run() {
+ local BIN="$BUILD_DIR/$1"
+ shift
+ LD_LIBRARY_PATH="$BUILD_DIR/qtmips_machine" "$BIN" "$@"
+}
+
+## Mips elf binary compilation ##################################################
+
+MIPS_COMPILER="$TEST_DIR_ROOT/mips-qtmips-elf"
+
+mips_compiler() {
+ # Skip if we have compiler already
+ [ ! -d "$MIPS_COMPILER" ] || return 0
+ # Otherwise compile it
+ mkdir -p "$TEST_DIR_ROOT"
+ pushd "$TEST_DIR_ROOT" >/dev/null
+ "$PROJECT_ROOT"/compiler/compile.sh
+ popd >/dev/null
+}
+
+mips_make_test() {
+ mips_compiler
+ mkdir -p "$TEST_DIR"
+ PATH="$PATH:$MIPS_COMPILER/bin" make -C "$TEST_SRC" O="$TEST_DIR" MIPS_PREFIX="mips-qtmips-elf" "$@"
+}