1#!/bin/sh
2
3# Copyright 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18set -e
19
20export LANG=C
21export LC_ALL=C
22
23PROGDIR=$(dirname "$0")
24PROGNAME=$(basename "$0")
25
26fatal () {
27    echo "ERROR: $@"
28    exit 1
29}
30
31OPT_EMUGEN=
32OPT_HELP=
33OPT_OUT_DIR=
34OPT_TOOL=
35
36for OPT; do
37    OPTARG=$(expr "x$OPT" : "x[^=]*=\\(.*\\)" || true)
38    case $OPT in
39    --help|-h|-?)
40        OPT_HELP=true
41        ;;
42    --emugen=*)
43        OPT_EMUGEN=$OPTARG
44        ;;
45    --out-dir=*)
46        OPT_OUT_DIR=$OPTARG
47        ;;
48    --tool=*)
49        OPT_TOOL=$OPTARG
50        ;;
51    -*)
52        fatal "Invalid option '$OPT', see --help."
53        ;;
54    *)
55        fatal "This script doesn't take arguments, see --help."
56        ;;
57    esac
58done
59
60if [ "$OPT_HELP" ]; then
61    cat <<EOF
62Usage: $PROGNAME [options]
63
64Run the emugen test suite. This scripts looks for sub-directories
65named t.<number>/input, and uses them as input to 'emugen'. It then
66compares the output to t.<number>/expected/ content.
67
68Valid options:
69    --help|-h|-?         Print this help.
70    --out-dir=<dir>      Generate outputs into <dir>.
71    --emugen=<program>   Emugen program path, if not in path.
72    --tool=<tool>        Launch visual diff tool in case of differences.
73EOF
74    exit 0
75fi
76
77# Find emugen program
78EMUGEN=
79if [ "$OPT_EMUGEN" ]; then
80    EMUGEN=$OPT_EMUGEN
81else
82    EMUGEN=$(which emugen 2>/dev/null || true)
83    if [ -z "$EMUGEN" ]; then
84        fatal "Cannot find 'emugen' program in PATH, use --emugen=<program> option."
85    fi
86    echo "Auto-config: --emugen=$EMUGEN"
87fi
88if [ ! -f "$EMUGEN" ]; then
89    fatal "Emugen program doesn't exist: $EMUGEN"
90fi
91
92# Create output directory.
93OUT_DIR=
94if [ "$OPT_OUT_DIR" ]; then
95    OUT_DIR=$OPT_OUT_DIR
96else
97    OUT_DIR=/tmp/$USER-emugen-testing
98    echo "Auto-config: --out-dir=$OUT_DIR"
99fi
100mkdir -p "$OUT_DIR" && rm -rf "$OUT_DIR/emugen"
101
102OUT_DIR=$OUT_DIR/emugen
103
104# Find test directories
105TEST_DIRS=$(cd "$PROGDIR" && find . -name "t.*" | sed -e 's|^\./||')
106for TEST_DIR in $TEST_DIRS; do
107    IN=$PROGDIR/$TEST_DIR/input
108    PREFIXES=$(cd $IN && find . -name "*.in" | sed -e 's|^\./||g' -e 's|\.in$||g')
109    OUT=$OUT_DIR/$TEST_DIR
110    mkdir -p "$OUT/encoder"
111    mkdir -p "$OUT/decoder"
112    mkdir -p "$OUT/wrapper"
113    for PREFIX in $PREFIXES; do
114        echo "Processing $IN/foo.*"
115        $EMUGEN -i "$PROGDIR/$TEST_DIR/input" -D "$OUT/decoder" -E "$OUT/encoder" -W "$OUT/wrapper" $PREFIX
116    done
117    if ! diff -qr "$PROGDIR/$TEST_DIR/expected" "$OUT"; then
118        if [ "$OPT_TOOL" ]; then
119            $OPT_TOOL "$PROGDIR/$TEST_DIR/expected" "$OUT"
120        else
121            echo "ERROR: Invalid differences between actual and expected output!"
122            diff -burN "$PROGDIR/$TEST_DIR/expected" "$OUT"
123            exit 1
124        fi
125    fi
126done
127
128echo "All good!"
129exit 0
130