Add a regression test

This commit is contained in:
Neale Pickett 2021-02-02 20:05:46 -07:00
parent 0b38368b59
commit 7ed3258510
3 changed files with 110 additions and 1 deletions

22
.github/workflows/build+test.yml vendored Normal file
View File

@ -0,0 +1,22 @@
name: Build/Test
on:
push:
branches:
- main
tags:
- 'v*.*.*'
jobs:
test:
name: Test mothd
runs-on: ubuntu-latest
steps:
- name: Retrieve code
uses: actions/checkout@v2
- name: Build
run: make
- name: Test
run: ./test.sh

87
test.sh Executable file
View File

@ -0,0 +1,87 @@
#! /bin/sh
tests=0
successes=0
failures=0
H () {
section="$*"
printf "\n%-20s " "$*"
}
title() {
thistest="$1"
tests=$(expr $tests + 1)
}
successes=0
pass () {
printf '.'
successes=$(expr $successes + 1)
}
failures=0
fail () {
printf '(%s)' "$thistest"
failures=$(expr $failures + 1)
}
H "xor"
title "single-byte decimal"
printf 'hello' | ./xor 22 | grep -q '~szzy' && pass || fail
title "single-byte hex"
printf 'hello' | ./xor 0x16 | grep -q '~szzy' && pass || fail
title "single-byte -x"
printf 'hello' | ./xor -x 16 | grep -q '~szzy' && pass || fail
H "hex/unhex"
title "hex"
printf ' hello' | ./hex | grep -q '68 65 6c 6c 6f' && pass || fail
title "unhex"
echo '68 65 6c 6c 6f' | ./unhex | grep -q 'hello' && pass || fail
H "slice"
title "slice begin"
printf 'hello' | ./slice 2 | grep -qx 'llo' && pass || fail
title "slice begin + end"
printf 'hello' | ./slice 2 4 | grep -qx 'll' && pass || fail
title "slice begin + end + begin"
printf 'hello' | ./slice 0 2 4 | grep -qx 'heo' && pass || fail
H "hd"
title "hd short"
printf 'hello' | ./hd | grep -qx '00000000 68 65 6c 6c 6f.*hello' && pass || fail
H "Everything else"
title "entropy"
printf 'hello' | ./entropy | grep -q 1.921928 && pass || fail
title "pyesc"
printf '\0hello\n' | ./pyesc | grep -xq '.x00hello.n' && pass || fail
title "freq"
printf 'hello' | ./freq | grep -xq '2 6c l' && pass || fail
title "histogram"
printf 'hello' | ./freq | ./histogram | grep -xq '6c l ## 2' && pass || fail
echo
echo
echo "$successes of $tests tests passed ($failures failed)."
exit $failures

2
xor.c
View File

@ -14,7 +14,7 @@ int main(int argc, char* argv[]) {
int arg;
int c;
while ((c = getopt(argc, argv, "ax")) != -1) {
while ((c = getopt(argc, argv, "x")) != -1) {
switch (c) {
case 'x':
radix = 16;