1#! /bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2018-2021 Gavin D. Howard and contributors. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions are met: 9# 10# * Redistributions of source code must retain the above copyright notice, this 11# list of conditions and the following disclaimer. 12# 13# * Redistributions in binary form must reproduce the above copyright notice, 14# this list of conditions and the following disclaimer in the documentation 15# and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29 30set -e 31 32script="$0" 33testdir=$(dirname "$script") 34 35. "$testdir/../scripts/functions.sh" 36 37# Command-line processing. 38if [ "$#" -ge 2 ]; then 39 40 d="$1" 41 shift 42 43 extra_math="$1" 44 shift 45 46else 47 err_exit "usage: $script dir extra_math [exec args...]" 1 48fi 49 50if [ "$#" -lt 1 ]; then 51 exe="$testdir/../bin/$d" 52else 53 exe="$1" 54 shift 55fi 56 57if [ "$d" = "bc" ]; then 58 halt="quit" 59else 60 halt="q" 61fi 62 63# For tests later. 64num=100000000000000000000000000000000000000000000000000000000000000000000000000000 65numres="$num" 66num70="10000000000000000000000000000000000000000000000000000000000000000000\\ 670000000000" 68 69# Set stuff for the correct calculator. 70if [ "$d" = "bc" ]; then 71 halt="halt" 72 opt="x" 73 lopt="extended-register" 74 line_var="BC_LINE_LENGTH" 75else 76 halt="q" 77 opt="l" 78 lopt="mathlib" 79 line_var="DC_LINE_LENGTH" 80 num="$num pR" 81fi 82 83# I use these, so unset them to make the tests work. 84unset BC_ENV_ARGS 85unset BC_LINE_LENGTH 86unset DC_ENV_ARGS 87unset DC_LINE_LENGTH 88 89set +e 90 91printf '\nRunning %s quit test...' "$d" 92 93printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1 94 95checktest_retcode "$d" "$?" "quit" 96 97# bc has two halt or quit commands, so test the second as well. 98if [ "$d" = bc ]; then 99 100 printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1 101 102 checktest_retcode "$d" "$?" quit 103 104 two=$("$exe" "$@" -e 1+1 -e quit) 105 106 checktest_retcode "$d" "$?" quit 107 108 if [ "$two" != "2" ]; then 109 err_exit "$d failed test quit" 1 110 fi 111fi 112 113printf 'pass\n' 114 115base=$(basename "$exe") 116 117printf 'Running %s environment var tests...' "$d" 118 119if [ "$d" = "bc" ]; then 120 121 export BC_ENV_ARGS=" '-l' '' -q" 122 123 printf 's(.02893)\n' | "$exe" "$@" > /dev/null 124 125 checktest_retcode "$d" "$?" "environment var" 126 127 "$exe" "$@" -e 4 > /dev/null 128 129 err="$?" 130 checktest_retcode "$d" "$?" "environment var" 131 132 printf 'pass\n' 133 134 printf 'Running keyword redefinition test...' 135 136 unset BC_ENV_ARGS 137 138 redefine_res="$testdir/bc_outputs/redefine.txt" 139 redefine_out="$testdir/bc_outputs/redefine_results.txt" 140 141 outdir=$(dirname "$easter_out") 142 143 if [ ! -d "$outdir" ]; then 144 mkdir -p "$outdir" 145 fi 146 147 printf '5\n0\n' > "$redefine_res" 148 149 "$exe" "$@" --redefine=print -e 'define print(x) { x }' -e 'print(5)' > "$redefine_out" 150 151 checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out" 152 153 "$exe" "$@" -r "abs" -r "else" -e 'abs = 5;else = 0' -e 'abs;else' > "$redefine_out" 154 155 checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out" 156 157 if [ "$extra_math" -ne 0 ]; then 158 159 "$exe" "$@" -lr abs -e "perm(5, 1)" -e "0" > "$redefine_out" 160 161 checktest "$d" "$err" "keyword not redefined in builtin library" "$redefine_res" "$redefine_out" 162 163 fi 164 165 "$exe" "$@" -r "break" -e 'define break(x) { x }' 2> "$redefine_out" 166 err="$?" 167 168 checkerrtest "$d" "$err" "keyword redefinition error" "$redefine_out" "$d" 169 170 "$exe" "$@" -e 'define read(x) { x }' 2> "$redefine_out" 171 err="$?" 172 173 checkerrtest "$d" "$err" "Keyword redefinition error without BC_REDEFINE_KEYWORDS" "$redefine_out" "$d" 174 175 printf 'pass\n' 176 177else 178 179 export DC_ENV_ARGS="'-x'" 180 export DC_EXPR_EXIT="1" 181 182 printf '4s stuff\n' | "$exe" "$@" > /dev/null 183 184 checktest_retcode "$d" "$?" "environment var" 185 186 "$exe" "$@" -e 4pR > /dev/null 187 188 checktest_retcode "$d" "$?" "environment var" 189 190 printf 'pass\n' 191 192 set +e 193 194 # dc has an extra test for a case that someone found running this easter.dc 195 # script. It went into an infinite loop, so we want to check that we did not 196 # regress. 197 printf 'three\n' | cut -c1-3 > /dev/null 198 err=$? 199 200 if [ "$err" -eq 0 ]; then 201 202 printf 'Running dc Easter script...' 203 204 easter_res="$testdir/dc_outputs/easter.txt" 205 easter_out="$testdir/dc_outputs/easter_results.txt" 206 207 outdir=$(dirname "$easter_out") 208 209 if [ ! -d "$outdir" ]; then 210 mkdir -p "$outdir" 211 fi 212 213 printf '4 April 2021\n' > "$easter_res" 214 215 "$testdir/dc/scripts/easter.sh" "$exe" 2021 "$@" | cut -c1-12 > "$easter_out" 216 err="$?" 217 218 checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out" 219 220 printf 'pass\n' 221 fi 222 223fi 224 225out1="$testdir/../.log_$d.txt" 226out2="$testdir/../.log_${d}_test.txt" 227 228printf 'Running %s line length tests...' "$d" 229 230printf '%s\n' "$numres" > "$out1" 231 232export "$line_var"=80 233printf '%s\n' "$num" | "$exe" "$@" > "$out2" 234 235checktest "$d" "$?" "environment var" "$out1" "$out2" 236 237printf '%s\n' "$num70" > "$out1" 238 239export "$line_var"=2147483647 240printf '%s\n' "$num" | "$exe" "$@" > "$out2" 241 242checktest "$d" "$?" "environment var" "$out1" "$out2" 243 244printf 'pass\n' 245 246printf 'Running %s arg tests...' "$d" 247 248f="$testdir/$d/add.txt" 249exprs=$(cat "$f") 250results=$(cat "$testdir/$d/add_results.txt") 251 252printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1" 253 254"$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2" 255 256checktest "$d" "$?" "arg" "$out1" "$out2" 257 258printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2" 259 260checktest "$d" "$?" "arg" "$out1" "$out2" 261 262if [ "$d" = "bc" ]; then 263 printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1 264fi 265 266printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null 267checktest_retcode "$d" "$?" "arg" 268printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null 269checktest_retcode "$d" "$?" "arg" 270printf '%s\n' "$halt" | "$exe" "$@" -R > /dev/null 271checktest_retcode "$d" "$?" "arg" 272printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null 273checktest_retcode "$d" "$?" "arg" 274printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null 275checktest_retcode "$d" "$?" "arg" 276 277"$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2" 278err="$?" 279 280checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d" 281 282"$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2" 283err="$?" 284 285checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d" 286 287"$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2" 288err="$?" 289 290checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d" 291 292"$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2" 293err="$?" 294 295checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d" 296 297"$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2" 298err="$?" 299 300checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d" 301 302"$exe" "$@" -f > /dev/null 2> "$out2" 303err="$?" 304 305checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d" 306 307"$exe" "$@" --file > /dev/null 2> "$out2" 308err="$?" 309 310checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d" 311 312"$exe" "$@" --version=5 > /dev/null 2> "$out2" 313err="$?" 314 315checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d" 316 317"$exe" "$@" -: > /dev/null 2> "$out2" 318err="$?" 319 320checkerrtest "$d" "$err" "colon short option" "$out2" "$d" 321 322"$exe" "$@" --: > /dev/null 2> "$out2" 323err="$?" 324 325checkerrtest "$d" "$err" "colon long option" "$out2" "$d" 326 327printf 'pass\n' 328 329printf 'Running %s directory test...' "$d" 330 331"$exe" "$@" "$testdir" > /dev/null 2> "$out2" 332err="$?" 333 334checkerrtest "$d" "$err" "directory" "$out2" "$d" 335 336printf 'pass\n' 337 338printf 'Running %s binary file test...' "$d" 339 340bin="/bin/sh" 341 342"$exe" "$@" "$bin" > /dev/null 2> "$out2" 343err="$?" 344 345checkerrtest "$d" "$err" "binary file" "$out2" "$d" 346 347printf 'pass\n' 348 349printf 'Running %s binary stdin test...' "$d" 350 351cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2" 352err="$?" 353 354checkerrtest "$d" "$err" "binary stdin" "$out2" "$d" 355 356printf 'pass\n' 357 358if [ "$d" = "bc" ]; then 359 360 printf 'Running %s limits tests...' "$d" 361 printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1 362 363 checktest_retcode "$d" "$?" "limits" 364 365 if [ ! -s "$out2" ]; then 366 err_exit "$d did not produce output on the limits test" 1 367 fi 368 369 exec printf 'pass\n' 370 371fi 372