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

363 lines, 158 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 # TODO: spec/nul-bytes passes when it should probably fail -- we get 1
84 # failure instead of 2.
85
86 # $suite $compare_mode
87 test/spec-runner.sh all-parallel \
88 osh compare-cpp $spec_subdir "$@"
89
90 write-compare-html $spec_subdir
91}
92
93ysh-all() {
94 ninja _bin/cxx-asan/{osh,ysh}
95
96 local spec_subdir=ysh-cpp
97
98 # $suite $compare_mode
99 test/spec-runner.sh all-parallel \
100 ysh compare-cpp $spec_subdir "$@"
101
102 write-compare-html $spec_subdir
103}
104
105console-row() {
106 ### Print out a histogram of results
107
108 awk '
109FNR == 1 {
110 #print FILENAME > "/dev/stderr"
111}
112FNR != 1 {
113 case_num = $1
114 sh = $2
115 result = $3
116
117 if (sh == "osh") {
118 osh[result] += 1
119 } else if (sh == "osh_cpp") { # bin/osh_cpp
120 oe_py[result] += 1
121 } else if (sh == "osh_ALT") { # _bin/*/osh
122 oe_cpp[result] += 1
123 }
124}
125
126function print_hist(sh, hist) {
127 printf("%s\t", sh)
128
129 k = "pass"
130 printf("%s %4d\t", k, hist[k])
131 k = "FAIL"
132 printf("%s %4d\t", k, hist[k])
133
134 print ""
135
136 # This prints N-I, ok, bug, etc.
137 #for (k in hist) {
138 # printf("%s %s\t", k, hist[k])
139 #}
140
141}
142
143END {
144 print_hist("osh", osh)
145 print_hist("osh_cpp", oe_py)
146 print_hist("osh_ALT", oe_cpp)
147}
148 ' "$@"
149}
150
151console-summary() {
152 ### Report on our progress translating
153
154 local spec_subdir=$1
155
156 # Can't go at the top level because files won't exist!
157 readonly TSV=(_tmp/spec/$spec_subdir/*.tsv)
158
159 wc -l "${TSV[@]}"
160
161 for file in "${TSV[@]}"; do
162 echo
163 echo "$file"
164 console-row $file
165 done
166
167 echo
168 echo "TOTAL"
169 console-row "${TSV[@]}"
170}
171
172#
173# HTML
174#
175
176summary-csv-row() {
177 ### Print one row or the last total row
178
179 local spec_subdir=$1
180 shift
181
182 if test $# -eq 1; then
183 local spec_name=$1
184 local -a tsv_files=( _tmp/spec/$spec_subdir/$spec_name.tsv )
185 else
186 local spec_name='TOTAL'
187 local -a tsv_files=( "$@" )
188 fi
189
190 awk -v spec_name=$spec_name '
191# skip the first row
192FNR != 1 {
193 case_num = $1
194 sh = $2
195 result = $3
196
197 if (sh == "osh" || sh == "ysh") {
198 osh[result] += 1
199 } else if (sh == "osh-cpp" || sh == "ysh-cpp") { # bin/osh
200 osh_native[result] += 1
201 }
202}
203
204END {
205 num_py = osh["pass"]
206 num_cpp = osh_native["pass"]
207 if (spec_name == "TOTAL") {
208 href = ""
209 } else {
210 href = sprintf("%s.html", spec_name)
211 }
212
213 if (num_py == num_cpp) {
214 row_css_class = "cpp-good" # green
215 }
216
217 printf("%s,%s,%s,%d,%d,%d\n",
218 row_css_class,
219 spec_name, href,
220 num_py,
221 num_cpp,
222 num_py - num_cpp)
223}
224' "${tsv_files[@]}"
225}
226
227summary-csv() {
228 local spec_subdir=$1
229
230 local sh_label
231 local manifest
232
233 case $spec_subdir in
234 osh-cpp)
235 sh_label=osh
236 manifest=_tmp/spec/SUITE-osh.txt
237 ;;
238 ysh-cpp)
239 sh_label=ysh
240 manifest=_tmp/spec/SUITE-ysh.txt
241 ;;
242 *)
243 die "Invalid dir $spec_subdir"
244 ;;
245 esac
246
247 # Can't go at the top level because files might not exist!
248 cat <<EOF
249ROW_CSS_CLASS,name,name_HREF,${sh_label}_py,${sh_label}_cpp,delta
250EOF
251
252 # total row rows goes at the TOP, so it's in <thead> and not sorted.
253 summary-csv-row $spec_subdir _tmp/spec/$spec_subdir/*.tsv
254
255 head -n $NUM_SPEC_TASKS $manifest | sort |
256 while read spec_name; do
257 summary-csv-row $spec_subdir $spec_name
258 done
259}
260
261html-summary-header() {
262 local prefix=../../..
263 html-head --title 'Passing Spec Tests in C++' \
264 $prefix/web/ajax.js \
265 $prefix/web/table/table-sort.js $prefix/web/table/table-sort.css \
266 $prefix/web/base.css \
267 $prefix/web/spec-cpp.css
268
269 table-sort-begin "width50"
270
271 cat <<EOF
272<p id="home-link">
273 <!-- The release index is two dirs up -->
274 <a href="../..">Up</a> |
275 <a href="/">oilshell.org</a>
276</p>
277
278<h1>Python vs C++</h1>
279
280<p>These numbers measure the progress of the C++ translation.
281Compare with <a href=".">index.html</a>.
282</p>
283
284EOF
285}
286
287html-summary-footer() {
288 cat <<EOF
289<p>Generated by <code>test/spec-cpp.sh</code>.</p>
290EOF
291 table-sort-end 'summary' # The table name
292}
293
294# TODO: Use here-schema-tsv in test/tsv-lib.sh
295here-schema() {
296 ### Read a legible text format on stdin, and write CSV on stdout
297
298 # This is a little like: https://wiki.xxiivv.com/site/tablatal.html
299 # TODO: generalize this in stdlib/here.sh
300 while read one two; do
301 echo "$one,$two"
302 done
303}
304
305write-compare-html() {
306 local spec_subdir=$1
307
308 local sh_label
309 case $spec_subdir in
310 osh-cpp)
311 sh_label=osh
312 ;;
313 ysh-cpp)
314 sh_label=ysh
315 ;;
316 *)
317 die "Invalid dir $spec_subdir"
318 ;;
319 esac
320
321 local dir=_tmp/spec/$spec_subdir
322 local out=$dir/compare.html
323
324 summary-csv $spec_subdir >$dir/summary.csv
325
326 # The underscores are stripped when we don't want them to be!
327 # Note: we could also put "pretty_heading" in the schema
328
329 here-schema >$dir/summary.schema.csv <<EOF
330column_name type
331ROW_CSS_CLASS string
332name string
333name_HREF string
334${sh_label}_py integer
335${sh_label}_cpp integer
336delta integer
337EOF
338
339 { html-summary-header
340 # total row isn't sorted
341 web/table/csv2html.py --thead-offset 1 $dir/summary.csv
342 html-summary-footer
343 } > $out
344
345 log "Comparison: file://$REPO_ROOT/$out"
346}
347
348tsv-demo() {
349 sh-spec spec/arith.test.sh --tsv-output _tmp/arith.tsv dash bash "$@"
350 cat _tmp/arith.tsv
351}
352
353repro() {
354 test/spec.sh alias -r 0 -p > _tmp/a
355 ninja _bin/clang-dbg/osh
356 _bin/clang-dbg/osh _tmp/a
357}
358
359repro-all() {
360 OSH_CC=$REPO_ROOT/_bin/clang-dbg/osh $0 all
361}
362
363task-five "$@"