test/run 3.5 KiB raw
1
#!/bin/sh
2
# Run binary tests.
3
# Usage: test/run [<test.rad|test.ras>...]
4
#
5
# If no arguments are provided, runs all `.rad` and `.ras` tests in
6
# `test/tests/`.
7
#
8
# For each test:
9
#   - If a `.ril` file exists alongside it, the IL output is checked
10
#     against it via the runner binary.
11
#   - If `//! session-support` appears, the IL check uses the fixture allocator
12
#     package required by session allocation.
13
#   - If `//! returns: N` appears in the file, the test is compiled to
14
#     a binary and executed; the exit code must match N.
15
#   - If `//! rw-data-size: N` appears in the file, the emitted image header's
16
#     rwdata size must match N bytes.
17
#   - If `//! rejects: TEXT` appears, the compiler must reject the source with
18
#     that diagnostic. Crashes and timeouts are failures.
19
20
RUNNER="test/runner.rv64"
21
TEST_DIR="test/tests"
22
EMU="${RAD_EMULATOR:-emulator} -stack-size=1024 -run"
23
EMU_RUN="${RAD_EMULATOR:-emulator} -no-jit -run"
24
25
if [ ! -f "$RUNNER" ]; then
26
  echo "error: runner binary not found: $RUNNER" >&2
27
  echo "hint: run 'make test' first" >&2
28
  exit 1
29
fi
30
31
# Disable core dumps for tests.
32
ulimit -c 0
33
34
# Collect tests.
35
if [ $# -eq 0 ]; then
36
  tests=$(find "$TEST_DIR" \( -name '*.rad' -o -name '*.ras' \) | sort)
37
else
38
  tests="$*"
39
fi
40
41
if [ -z "$tests" ]; then
42
  echo "error: no tests found" >&2
43
  exit 1
44
fi
45
46
passed=0
47
failed=0
48
49
for test in $tests; do
50
  case "$test" in
51
    *.rad) base="${test%.rad}" ;;
52
    *.ras) base="${test%.ras}" ;;
53
    *) base="$test" ;;
54
  esac
55
56
  ril="${base}.ril"
57
  bin="${base}.rv64"
58
  if grep -q '^//! rejects: ' "$test"; then
59
    echo -n "test $test rejection ... "
60
    if sh test/reject "$test"; then
61
      echo "ok"
62
      passed=$((passed + 1))
63
    else
64
      echo "FAILED"
65
      failed=$((failed + 1))
66
    fi
67
    continue
68
  fi
69
  # IL check: run the runner if a .ril file exists.
70
  if [ -f "$ril" ]; then
71
    if grep -q '^//! session-support$' "$test"; then
72
      echo -n "test $test ... "
73
      if test/package-golden "$test"; then
74
        echo "ok"
75
        passed=$((passed + 1))
76
      else
77
        echo "FAILED"
78
        failed=$((failed + 1))
79
      fi
80
    elif $EMU "$RUNNER" -- "$test"; then
81
      passed=$((passed + 1))
82
    else
83
      failed=$((failed + 1))
84
    fi
85
  fi
86
87
  # Execution check: run the binary if //! returns: is present.
88
  returns=$(grep -m1 '^//! returns:' "$test" | sed 's/^\/\/! returns: *//')
89
  if [ -n "$returns" ]; then
90
    echo -n "test $test ... "
91
92
    if [ ! -f "$bin" ]; then
93
      echo "FAILED (binary not found: $bin)"
94
      failed=$((failed + 1))
95
      continue
96
    fi
97
98
    $EMU_RUN "$bin" >/dev/null 2>&1
99
    ret=$?
100
101
    if [ "$ret" -eq "$returns" ]; then
102
      echo "ok"
103
      passed=$((passed + 1))
104
    else
105
      echo "FAILED (expected: $returns, got: $ret)"
106
      failed=$((failed + 1))
107
    fi
108
  fi
109
110
  rw_data_size=$(grep -m1 '^//! rw-data-size:' "$test" | sed 's/^\/\/! rw-data-size: *//')
111
  if [ -n "$rw_data_size" ]; then
112
    echo -n "test $test rw.data ... "
113
114
    if [ ! -f "$bin" ]; then
115
      echo "FAILED (binary not found: $bin)"
116
      failed=$((failed + 1))
117
      continue
118
    fi
119
120
    actual_rw_data_size=$(od -An -tu4 -j16 -N4 "$bin" | tr -d ' ')
121
122
    if [ "$actual_rw_data_size" -eq "$rw_data_size" ]; then
123
      echo "ok"
124
      passed=$((passed + 1))
125
    else
126
      echo "FAILED (expected: $rw_data_size, got: $actual_rw_data_size)"
127
      failed=$((failed + 1))
128
    fi
129
  fi
130
done
131
echo
132
133
if [ "$failed" -eq 0 ]; then
134
  echo "test result: ok. $passed passed; $failed failed"
135
  exit 0
136
else
137
  echo "test result: FAILED. $passed passed; $failed failed"
138
  exit 1
139
fi