OILS / test / spec-cpp.sh View on Github | oilshell.org

367 lines, 163 significant
1#!/usr/bin/env bash
2#
3# Test the C++ translation of Oils.
4#
5# Usage:
6# test/spec-cpp.sh <function name>
7#
8# Examples:
9# test/spec-cpp.sh run-file smoke -r 0 -v
10# NUM_SPEC_TASKS=2 test/spec-cpp.sh osh-all
11
12: ${LIB_OSH=stdlib/osh}
13source $LIB_OSH/bash-strict.sh
14source $LIB_OSH/task-five.sh
15
16REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
17
18source build/dev-shell.sh # PYTHONPATH
19source test/common.sh # html-head
20source test/spec-common.sh
21source web/table/html.sh
22
23shopt -s failglob # to debug TSV expansion failure below
24
25OSH_PY=$REPO_ROOT/bin/osh
26YSH_PY=$REPO_ROOT/bin/ysh
27
28# Run with ASAN binary by default. Release overrides this
29OSH_CC=${OSH_CC:-$REPO_ROOT/_bin/cxx-asan/osh}
30YSH_CC=${YSH_CC:-$REPO_ROOT/_bin/cxx-asan/ysh}
31
32# Same variable in test/spec-runner.sh
33NUM_SPEC_TASKS=${NUM_SPEC_TASKS:-400}
34
35# So we can pass ASAN. Note that test/spec-common.sh has to pass this to
36# sh_spec.py.
37export OILS_GC_ON_EXIT=1
38
39#
40# For translation
41#
42
43run-file() {
44 local spec_name=$1
45 shift
46
47 local spec_file=spec/$spec_name.test.sh
48
49 local suite
50 suite=$(test/sh_spec.py --print-spec-suite $spec_file)
51
52 local spec_subdir
53 case $suite in
54 osh) spec_subdir='osh-cpp' ;;
55 ysh) spec_subdir='ysh-cpp' ;;
56 *) die "Invalid suite $suite" ;;
57 esac
58
59 local base_dir=_tmp/spec/$spec_subdir
60 mkdir -v -p $base_dir
61
62 # Compare Python and C++ shells by passing --oils-cpp-bin-dir
63 sh-spec $spec_file \
64 --timeout 10 \
65 --oils-bin-dir $PWD/bin \
66 --oils-cpp-bin-dir $REPO_ROOT/_bin/cxx-asan \
67 --tsv-output $base_dir/${spec_name}.tsv \
68 "$@"
69}
70
71osh-all() {
72 # Like test/spec.sh {osh,ysh}-all, but it compares against different binaries
73
74 # For debugging hangs
75 #export MAX_PROCS=1
76
77 ninja _bin/cxx-asan/{osh,ysh}
78
79 test/spec-runner.sh shell-sanity-check $OSH_PY $OSH_CC
80
81 local spec_subdir=osh-cpp
82
83 local status
84 set +o errexit
85 # $suite $compare_mode
86 test/spec-runner.sh all-parallel \
87 osh compare-cpp $spec_subdir "$@"
88 status=$?
89 set -o errexit
90
91 # Write comparison even if we failed
92 write-compare-html $spec_subdir
93
94 return $status
95}
96
97ysh-all() {
98 ninja _bin/cxx-asan/{osh,ysh}
99
100 local spec_subdir=ysh-cpp
101
102 # $suite $compare_mode
103 test/spec-runner.sh all-parallel \
104 ysh compare-cpp $spec_subdir "$@"
105
106 write-compare-html $spec_subdir
107}
108
109console-row() {
110 ### Print out a histogram of results
111
112 awk '
113FNR == 1 {
114 #print FILENAME > "/dev/stderr"
115}
116FNR != 1 {
117 case_num = $1
118 sh = $2
119 result = $3
120
121 if (sh == "osh") {
122 osh[result] += 1
123 } else if (sh == "osh_cpp") { # bin/osh_cpp
124 oe_py[result] += 1
125 } else if (sh == "osh_ALT") { # _bin/*/osh
126 oe_cpp[result] += 1
127 }
128}
129
130function print_hist(sh, hist) {
131 printf("%s\t", sh)
132
133 k = "pass"
134 printf("%s %4d\t", k, hist[k])
135 k = "FAIL"
136 printf("%s %4d\t", k, hist[k])
137
138 print ""
139
140 # This prints N-I, ok, bug, etc.
141 #for (k in hist) {
142 # printf("%s %s\t", k, hist[k])
143 #}
144
145}
146
147END {
148 print_hist("osh", osh)
149 print_hist("osh_cpp", oe_py)
150 print_hist("osh_ALT", oe_cpp)
151}
152 ' "$@"
153}
154
155console-summary() {
156 ### Report on our progress translating
157
158 local spec_subdir=$1
159
160 # Can't go at the top level because files won't exist!
161 readonly TSV=(_tmp/spec/$spec_subdir/*.tsv)
162
163 wc -l "${TSV[@]}"
164
165 for file in "${TSV[@]}"; do
166 echo
167 echo "$file"
168 console-row $file
169 done
170
171 echo
172 echo "TOTAL"
173 console-row "${TSV[@]}"
174}
175
176#
177# HTML
178#
179
180summary-csv-row() {
181 ### Print one row or the last total row
182
183 local spec_subdir=$1
184 shift
185
186 if test $# -eq 1; then
187 local spec_name=$1
188 local -a tsv_files=( _tmp/spec/$spec_subdir/$spec_name.tsv )
189 else
190 local spec_name='TOTAL'
191 local -a tsv_files=( "$@" )
192 fi
193
194 awk -v spec_name=$spec_name '
195# skip the first row
196FNR != 1 {
197 case_num = $1
198 sh = $2
199 result = $3
200
201 if (sh == "osh" || sh == "ysh") {
202 osh[result] += 1
203 } else if (sh == "osh-cpp" || sh == "ysh-cpp") { # bin/osh
204 osh_native[result] += 1
205 }
206}
207
208END {
209 num_py = osh["pass"]
210 num_cpp = osh_native["pass"]
211 if (spec_name == "TOTAL") {
212 href = ""
213 } else {
214 href = sprintf("%s.html", spec_name)
215 }
216
217 if (num_py == num_cpp) {
218 row_css_class = "cpp-good" # green
219 }
220
221 printf("%s,%s,%s,%d,%d,%d\n",
222 row_css_class,
223 spec_name, href,
224 num_py,
225 num_cpp,
226 num_py - num_cpp)
227}
228' "${tsv_files[@]}"
229}
230
231summary-csv() {
232 local spec_subdir=$1
233
234 local sh_label
235 local manifest
236
237 case $spec_subdir in
238 osh-cpp)
239 sh_label=osh
240 manifest=_tmp/spec/SUITE-osh.txt
241 ;;
242 ysh-cpp)
243 sh_label=ysh
244 manifest=_tmp/spec/SUITE-ysh.txt
245 ;;
246 *)
247 die "Invalid dir $spec_subdir"
248 ;;
249 esac
250
251 # Can't go at the top level because files might not exist!
252 cat <<EOF
253ROW_CSS_CLASS,name,name_HREF,${sh_label}_py,${sh_label}_cpp,delta
254EOF
255
256 # total row rows goes at the TOP, so it's in <thead> and not sorted.
257 summary-csv-row $spec_subdir _tmp/spec/$spec_subdir/*.tsv
258
259 head -n $NUM_SPEC_TASKS $manifest | sort |
260 while read spec_name; do
261 summary-csv-row $spec_subdir $spec_name
262 done
263}
264
265html-summary-header() {
266 local prefix=../../..
267 html-head --title 'Passing Spec Tests in C++' \
268 $prefix/web/ajax.js \
269 $prefix/web/table/table-sort.js $prefix/web/table/table-sort.css \
270 $prefix/web/base.css \
271 $prefix/web/spec-cpp.css
272
273 table-sort-begin "width50"
274
275 cat <<EOF
276<p id="home-link">
277 <!-- The release index is two dirs up -->
278 <a href="../..">Up</a> |
279 <a href="/">oilshell.org</a>
280</p>
281
282<h1>Python vs C++</h1>
283
284<p>These numbers measure the progress of the C++ translation.
285Compare with <a href=".">index.html</a>.
286</p>
287
288EOF
289}
290
291html-summary-footer() {
292 cat <<EOF
293<p>Generated by <code>test/spec-cpp.sh</code>.</p>
294EOF
295 table-sort-end 'summary' # The table name
296}
297
298# TODO: Use here-schema-tsv in test/tsv-lib.sh
299here-schema() {
300 ### Read a legible text format on stdin, and write CSV on stdout
301
302 # This is a little like: https://wiki.xxiivv.com/site/tablatal.html
303 # TODO: generalize this in stdlib/here.sh
304 while read one two; do
305 echo "$one,$two"
306 done
307}
308
309write-compare-html() {
310 local spec_subdir=$1
311
312 local sh_label
313 case $spec_subdir in
314 osh-cpp)
315 sh_label=osh
316 ;;
317 ysh-cpp)
318 sh_label=ysh
319 ;;
320 *)
321 die "Invalid dir $spec_subdir"
322 ;;
323 esac
324
325 local dir=_tmp/spec/$spec_subdir
326 local out=$dir/compare.html
327
328 summary-csv $spec_subdir >$dir/summary.csv
329
330 # The underscores are stripped when we don't want them to be!
331 # Note: we could also put "pretty_heading" in the schema
332
333 here-schema >$dir/summary.schema.csv <<EOF
334column_name type
335ROW_CSS_CLASS string
336name string
337name_HREF string
338${sh_label}_py integer
339${sh_label}_cpp integer
340delta integer
341EOF
342
343 { html-summary-header
344 # total row isn't sorted
345 web/table/csv2html.py --thead-offset 1 $dir/summary.csv
346 html-summary-footer
347 } > $out
348
349 log "Comparison: file://$REPO_ROOT/$out"
350}
351
352tsv-demo() {
353 sh-spec spec/arith.test.sh --tsv-output _tmp/arith.tsv dash bash "$@"
354 cat _tmp/arith.tsv
355}
356
357repro() {
358 test/spec.sh alias -r 0 -p > _tmp/a
359 ninja _bin/clang-dbg/osh
360 _bin/clang-dbg/osh _tmp/a
361}
362
363repro-all() {
364 OSH_CC=$REPO_ROOT/_bin/clang-dbg/osh $0 all
365}
366
367task-five "$@"