From 0934bf917d89f796ca6acb1e02130e358fdec203 Mon Sep 17 00:00:00 2001 From: CismonX Date: Sun, 15 Mar 2020 04:31:20 +0800 Subject: [PATCH] add test cases --- .gitignore | 4 ++++ .travis.yml | 8 +++++++ README.md | 2 ++ src/Makefile.am | 7 +++++++ tests/default/cat.exp | 41 ++++++++++++++++++++++++++++++++++++ tests/default/square.exp | 43 ++++++++++++++++++++++++++++++++++++++ tests/init.exp | 45 ++++++++++++++++++++++++++++++++++++++++ tests/u6a_run | 25 ++++++++++++++++++++++ 8 files changed, 175 insertions(+) create mode 100644 tests/default/cat.exp create mode 100644 tests/default/square.exp create mode 100644 tests/init.exp create mode 100755 tests/u6a_run diff --git a/.gitignore b/.gitignore index 9848a0c..2d441e4 100644 --- a/.gitignore +++ b/.gitignore @@ -32,5 +32,9 @@ aclocal.m4 u6ac u6a +# DejaGnu +*.sum +site.* + # others *.log diff --git a/.travis.yml b/.travis.yml index ff32d6a..d5594f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,14 @@ compiler: - gcc - clang +addons: + apt: + packages: + - dejagnu + homebrew: + packages: + - dejagnu + branches: only: - master diff --git a/README.md b/README.md index f10f81c..7190212 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ Building: ```bash # (If not already) Install the required build tools. sudo apt install build-essential automake +# (If you want to run tests) Install DejaGnu. +sudo apt install dejagnu # Generate configuration script. autoreconf --install # Execute configuration script with desired options. diff --git a/src/Makefile.am b/src/Makefile.am index f609009..ab278fd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,11 @@ +AUTOMAKE_OPTIONS = dejagnu + bin_PROGRAMS = u6ac u6a u6ac_SOURCES = logging.c lexer.c parser.c codegen.c u6ac.c u6a_SOURCES = logging.c vm_stack.c vm_pool.c runtime.c u6a.c + +TEST_DIR = ${srcdir}/../tests +DEJAGNU_GLOBALS = U6A_BIN=${srcdir}/u6a U6AC_BIN=${srcdir}/u6ac U6A_RUN=${TEST_DIR}/u6a_run +RUNTESTDEFAULTFLAGS = --srcdir=${TEST_DIR} --tool default ${DEJAGNU_GLOBALS} +EXTRA_DEJAGNU_SITE_CONFIG = ../tests/init.exp diff --git a/tests/default/cat.exp b/tests/default/cat.exp new file mode 100644 index 0000000..841208c --- /dev/null +++ b/tests/default/cat.exp @@ -0,0 +1,41 @@ +## +## cat.exp - Test whether a "cat" program works on this u6a build +## +## Copyright (C) 2020 CismonX +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## + +set tool "default" +set timeout 1 + +set rand_str_list { } +for { set i 100 } { $i < 200 } { incr i } { + lappend rand_str_list [ exec head -c $i /dev/urandom | base64 -w 0 ] +} + +u6a_run "```s`d`@|i`ci" - - 1 +for { set i 0 } { $i < [ llength $rand_str_list ] } { incr i } { + set rand_str [ lindex $rand_str_list $i ] + send -- "$rand_str\r" + expect { + $rand_str { + pass "case $i ok!" + } + default { + fail "case $i fails!" + } + } +} +u6a_stop 1 diff --git a/tests/default/square.exp b/tests/default/square.exp new file mode 100644 index 0000000..ba8d2f3 --- /dev/null +++ b/tests/default/square.exp @@ -0,0 +1,43 @@ +## +## square.exp - Test whether a "square" program works on this u6a build +## +## Copyright (C) 2020 CismonX +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## + +set tool "default" +set timeout 1 + +# The following code is a modified version of ftp://ftp.madore.org/pub/madore/unlambda/CUAN/Square.unl +# Written by Panu Kalliokoski +set src_stub "`r```si`k``s``s`kk`si``s``si`k``s`k`s`k``sk``sr`k.oir``si%s`k`ki" +set src_segment "" +set expected "\n" +for { set i 0 } { $i < 20 } { incr i } { + set src_segment "$src_segment``si" + set line [ string repeat "o" [ expr $i + 1 ] ] + set square [ string repeat "$line\n" [ expr $i + 1 ] ] + set expected "$expected\n$square" + u6a_run [ format $src_stub $src_segment ] - - 0 + expect { + $expected { + pass "case $i ok!" + } + default { + fail "case $i fails!" + } + } + u6a_stop 0 +} diff --git a/tests/init.exp b/tests/init.exp new file mode 100644 index 0000000..fba5f2e --- /dev/null +++ b/tests/init.exp @@ -0,0 +1,45 @@ +## +## init.exp - u6a test init script +## +## Copyright (C) 2020 CismonX +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## + +proc u6a_run { src_code u6ac_opts u6a_opts has_input } { + global U6A_BIN U6AC_BIN U6A_RUN + set u6ac "$U6AC_BIN $u6ac_opts" + set u6a "$U6A_BIN $u6a_opts" + set run_cmd [ list $U6A_RUN $u6ac $u6a $src_code ] + if $has_input { + lappend run_cmd - + set stty_init -echo + } else { + set stty_init { raw -echo } + } + global spawn_id + spawn {*}$run_cmd +} + +proc u6a_stop { send_eof } { + global spawn_id + if $send_eof { + send \x04 + expect eof + } + catch wait result + if [ set exit_code [ lindex $result 3 ] ] { + fail "program exited with code $exit_code" + } +} diff --git a/tests/u6a_run b/tests/u6a_run new file mode 100755 index 0000000..de3d7b9 --- /dev/null +++ b/tests/u6a_run @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +## +## u6a_run - helper script for running tests for u6a +## +## Copyright (C) 2020 CismonX +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## + +U6A_BYTECODE=`echo $3 | $1 - | base64 -w 0` +if [ -z $U6A_BYTECODE ]; then exit -1; fi +cat <(echo $U6A_BYTECODE | base64 -d) <(test $4 && cat $4) | $2 - + +# Usage: ./u6a_run /path/to/u6ac/bin /path/to/u6a/bin source_code [input]