| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Run tests in this directory.
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # mycpp/TEST.sh <function name>
|
| 7 |
|
| 8 | : ${LIB_OSH=stdlib/osh}
|
| 9 | source $LIB_OSH/bash-strict.sh
|
| 10 | source $LIB_OSH/task-five.sh
|
| 11 |
|
| 12 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
| 13 |
|
| 14 | source build/common.sh
|
| 15 | source build/ninja-rules-cpp.sh
|
| 16 | source devtools/common.sh
|
| 17 | source test/common.sh # run-test-bin, can-compile-32-bit
|
| 18 |
|
| 19 | # in case binaries weren't built
|
| 20 | shopt -s failglob
|
| 21 |
|
| 22 | # Will be needed to pass ASAN leak detector? Or only do this for the main binary?
|
| 23 | # export OILS_GC_ON_EXIT=1
|
| 24 |
|
| 25 | examples-variant() {
|
| 26 | ### Run all examples using a variant -- STATS only
|
| 27 |
|
| 28 | local compiler=${1:-cxx}
|
| 29 | local variant=${2:-asan+gcalways}
|
| 30 | local do_benchmark=${3:-}
|
| 31 |
|
| 32 | banner "$0 examples-variant $compiler $variant"
|
| 33 |
|
| 34 | ninja mycpp-examples-$compiler-$variant
|
| 35 |
|
| 36 | local num_tests=0
|
| 37 | local num_failed=0
|
| 38 | local status=0
|
| 39 |
|
| 40 | local log_dir=_test/$compiler-$variant/mycpp/examples
|
| 41 | mkdir -p $log_dir
|
| 42 |
|
| 43 | for b in _bin/$compiler-$variant/mycpp/examples/*; do
|
| 44 | case $b in
|
| 45 | *.pea) # for now, don't run pea_hello.pea, it fails on purpose
|
| 46 | continue
|
| 47 | ;;
|
| 48 | *.stripped) # just run the unstripped binary
|
| 49 | continue
|
| 50 | ;;
|
| 51 | esac
|
| 52 |
|
| 53 | local prefix="$log_dir/$(basename $b)"
|
| 54 |
|
| 55 | case $variant in
|
| 56 | coverage)
|
| 57 | export LLVM_PROFILE_FILE=$prefix.profraw
|
| 58 | ;;
|
| 59 | esac
|
| 60 |
|
| 61 | local log="${prefix}${do_benchmark}.log"
|
| 62 |
|
| 63 | log "RUN $b > $log"
|
| 64 |
|
| 65 | local test_name=$(basename $b)
|
| 66 | if test -n "$do_benchmark" && [[ $test_name == test_* ]]; then
|
| 67 | log "Skipping $test_name in benchmark mode"
|
| 68 | continue
|
| 69 | fi
|
| 70 |
|
| 71 | set +o errexit
|
| 72 | BENCHMARK="$do_benchmark" $b >$log 2>&1
|
| 73 | status=$?
|
| 74 | set -o errexit
|
| 75 |
|
| 76 | if test "$status" -eq 0; then
|
| 77 | log 'OK'
|
| 78 | else
|
| 79 | log "FAIL with status $?"
|
| 80 | log ''
|
| 81 | #return $status
|
| 82 | num_failed=$((num_failed + 1))
|
| 83 | fi
|
| 84 |
|
| 85 | num_tests=$((num_tests + 1))
|
| 86 | done
|
| 87 |
|
| 88 | log ''
|
| 89 | log "$num_failed of $num_tests tests failed"
|
| 90 | log ''
|
| 91 |
|
| 92 | if test $num_failed -ne 0; then
|
| 93 | echo "FAIL: Expected no failures, got $num_failed"
|
| 94 | return 1
|
| 95 | fi
|
| 96 |
|
| 97 | return 0
|
| 98 | }
|
| 99 |
|
| 100 | #
|
| 101 | # 3 Variants x {test, benchmark}
|
| 102 | #
|
| 103 |
|
| 104 | ex-gcalways() {
|
| 105 | local compiler=${1:-}
|
| 106 | examples-variant "$compiler" asan+gcalways
|
| 107 | }
|
| 108 |
|
| 109 | # TOO SLOW to run. It's garbage collecting all the time.
|
| 110 | ex-gcalways-bench() {
|
| 111 | local compiler=${1:-}
|
| 112 | examples-variant "$compiler" asan+gcalways '.BENCHMARK'
|
| 113 | }
|
| 114 |
|
| 115 | ex-asan() {
|
| 116 | local compiler=${1:-}
|
| 117 | examples-variant "$compiler" asan
|
| 118 | }
|
| 119 |
|
| 120 | # 2 of 18 tests failed: cartesian, parse
|
| 121 | # So it does not catch the 10 segfaults that 'asan+gcalways' catches with a few
|
| 122 | # iterations!
|
| 123 | ex-asan-bench() {
|
| 124 | local compiler=${1:-}
|
| 125 | examples-variant "$compiler" asan '.BENCHMARK'
|
| 126 | }
|
| 127 |
|
| 128 | # PASS! Under both clang and GCC.
|
| 129 | ex-ubsan() {
|
| 130 | local compiler=${1:-}
|
| 131 | examples-variant "$compiler" ubsan
|
| 132 | }
|
| 133 |
|
| 134 | # same as ASAN: 2 of 18
|
| 135 | ex-ubsan-bench() {
|
| 136 | local compiler=${1:-}
|
| 137 | examples-variant "$compiler" ubsan '.BENCHMARK'
|
| 138 | }
|
| 139 |
|
| 140 | # PASS!
|
| 141 | ex-opt() {
|
| 142 | local compiler=${1:-}
|
| 143 | examples-variant "$compiler" opt
|
| 144 | }
|
| 145 |
|
| 146 | # 2 of 18 tests failed
|
| 147 | ex-opt-bench() {
|
| 148 | local compiler=${1:-}
|
| 149 | examples-variant "$compiler" opt '.BENCHMARK'
|
| 150 | }
|
| 151 |
|
| 152 | #
|
| 153 | # Unit Tests
|
| 154 | #
|
| 155 |
|
| 156 | run-unit-tests() {
|
| 157 |
|
| 158 | local compiler=${1:-cxx}
|
| 159 | local variant=${2:-asan+gcalways}
|
| 160 |
|
| 161 | log ''
|
| 162 | log "$0 run-unit-tests $compiler $variant"
|
| 163 | log ''
|
| 164 |
|
| 165 | ninja mycpp-unit-$compiler-$variant
|
| 166 |
|
| 167 | local -a binaries=(_bin/$compiler-$variant/mycpp/*)
|
| 168 |
|
| 169 | # Add these files if they exist in the variant
|
| 170 | if test -d _bin/$compiler-$variant/mycpp/demo; then
|
| 171 | binaries+=(_bin/$compiler-$variant/mycpp/demo/*)
|
| 172 | fi
|
| 173 |
|
| 174 | for b in "${binaries[@]}"; do
|
| 175 | if ! test -f $b; then
|
| 176 | continue
|
| 177 | fi
|
| 178 |
|
| 179 | local prefix=${b//_bin/_test/}
|
| 180 | local log=$prefix.log
|
| 181 | mkdir -p $(dirname $log)
|
| 182 |
|
| 183 | local asan_options=''
|
| 184 | case $b in
|
| 185 | # leaks with malloc
|
| 186 | */demo/hash_table|*/demo/target_lang|*/demo/gc_header|*/small_str_test)
|
| 187 | asan_options='detect_leaks=0'
|
| 188 | ;;
|
| 189 | esac
|
| 190 |
|
| 191 | ASAN_OPTIONS="$asan_options" run-test-bin $b
|
| 192 |
|
| 193 | done
|
| 194 | }
|
| 195 |
|
| 196 | #
|
| 197 | # Test failures
|
| 198 | #
|
| 199 |
|
| 200 | translate-example() {
|
| 201 | local ex=$1
|
| 202 |
|
| 203 | local mycpp=_bin/shwrap/mycpp_main
|
| 204 | $mycpp '.:pyext' '' _tmp/mycpp-invalid $ex
|
| 205 | }
|
| 206 |
|
| 207 | test-invalid-examples() {
|
| 208 | local mycpp=_bin/shwrap/mycpp_main
|
| 209 | ninja $mycpp
|
| 210 | for ex in mycpp/examples/invalid_*; do
|
| 211 |
|
| 212 | banner "$ex"
|
| 213 |
|
| 214 | set +o errexit
|
| 215 | translate-example $ex
|
| 216 | local status=$?
|
| 217 | set -o errexit
|
| 218 |
|
| 219 | local expected_status=1
|
| 220 |
|
| 221 | case $ex in
|
| 222 | */invalid_condition.py)
|
| 223 | expected_status=8
|
| 224 | ;;
|
| 225 | */invalid_other.py)
|
| 226 | expected_status=6
|
| 227 | ;;
|
| 228 | */invalid_default_args.py)
|
| 229 | expected_status=5
|
| 230 | ;;
|
| 231 | */invalid_try_else.py)
|
| 232 | expected_status=3
|
| 233 | ;;
|
| 234 | */invalid_except.py)
|
| 235 | expected_status=4
|
| 236 | ;;
|
| 237 | */invalid_global.py)
|
| 238 | expected_status=2
|
| 239 | ;;
|
| 240 | */invalid_python.py)
|
| 241 | expected_status=5
|
| 242 | ;;
|
| 243 | */invalid_switch.py)
|
| 244 | expected_status=5
|
| 245 | ;;
|
| 246 | */invalid_ctx_raise.py)
|
| 247 | expected_status=2
|
| 248 | ;;
|
| 249 | esac
|
| 250 |
|
| 251 | if test $status -ne $expected_status; then
|
| 252 | die "mycpp $ex: expected status $expected_status, got $status"
|
| 253 | fi
|
| 254 |
|
| 255 | done
|
| 256 | }
|
| 257 |
|
| 258 | test-control-flow-graph() {
|
| 259 | local mycpp=_bin/shwrap/mycpp_main
|
| 260 | ninja $mycpp
|
| 261 | for ex in mycpp/examples/*.py; do
|
| 262 | local data_dir=testdata/control-flow-graph/$(basename -s .py $ex)
|
| 263 | if ! test -d $data_dir; then
|
| 264 | continue
|
| 265 | fi
|
| 266 | banner "$ex"
|
| 267 |
|
| 268 | translate-example $ex
|
| 269 | for fact_path in $data_dir/*.facts; do
|
| 270 | local fact_file=$(basename $fact_path)
|
| 271 | diff -u $data_dir/$fact_file _tmp/mycpp-facts/$fact_file
|
| 272 | done
|
| 273 | done
|
| 274 | }
|
| 275 |
|
| 276 | # TODO: Run with Clang UBSAN in CI as well
|
| 277 | readonly UBSAN_COMPILER=cxx
|
| 278 |
|
| 279 | unit() {
|
| 280 | ### Run by test/cpp-unit.sh
|
| 281 |
|
| 282 | # Run other unit tests, e.g. the GC tests
|
| 283 |
|
| 284 | if can-compile-32-bit; then
|
| 285 | run-unit-tests '' asan32+gcalways # ASAN on 32-bit
|
| 286 | else
|
| 287 | log ''
|
| 288 | log "*** Can't compile 32-bit binaries (gcc-multilib g++-multilib needed on Debian)"
|
| 289 | log ''
|
| 290 | fi
|
| 291 |
|
| 292 | # Run other tests with all variants
|
| 293 |
|
| 294 | run-unit-tests $UBSAN_COMPILER ubsan
|
| 295 |
|
| 296 | run-unit-tests '' asan
|
| 297 | run-unit-tests '' asan+gcalways
|
| 298 | run-unit-tests '' opt
|
| 299 | run-unit-tests '' asan+bigint
|
| 300 |
|
| 301 | bump-leak-heap-test
|
| 302 | }
|
| 303 |
|
| 304 | bump-leak-heap-test() {
|
| 305 | for config in cxx-asan+bumpleak $UBSAN_COMPILER-ubsan+bumpleak; do
|
| 306 | local bin=_bin/$config/mycpp/bump_leak_heap_test
|
| 307 | ninja $bin
|
| 308 | run-test-bin $bin
|
| 309 | done
|
| 310 | }
|
| 311 |
|
| 312 | #
|
| 313 | # Translator
|
| 314 | #
|
| 315 |
|
| 316 | test-translator() {
|
| 317 | ### Invoked by soil/worker.sh
|
| 318 |
|
| 319 | examples-variant '' asan
|
| 320 |
|
| 321 | # Test with more collections
|
| 322 | examples-variant '' asan+gcalways
|
| 323 |
|
| 324 | run-test-func test-invalid-examples _test/mycpp/test-invalid-examples.log
|
| 325 |
|
| 326 | run-test-func test-control-flow-graph _test/mycpp/test-cfg-examples.log
|
| 327 |
|
| 328 | # Runs tests in cxx-asan variant, and benchmarks in cxx-opt variant
|
| 329 | if ! ninja mycpp-logs-equal; then
|
| 330 | log 'FAIL mycpp-logs-equal'
|
| 331 | return 1
|
| 332 | fi
|
| 333 | }
|
| 334 |
|
| 335 | soil-run() {
|
| 336 | set +o errexit
|
| 337 | $0 test-translator
|
| 338 | local status=$?
|
| 339 | set -o errexit
|
| 340 |
|
| 341 | return $status
|
| 342 | }
|
| 343 |
|
| 344 | unit-test-coverage() {
|
| 345 | ### Invoked by Soil
|
| 346 |
|
| 347 | local bin=_bin/clang-coverage+bumpleak/mycpp/bump_leak_heap_test
|
| 348 | ninja $bin
|
| 349 | run-test-bin $bin
|
| 350 |
|
| 351 | run-unit-tests clang coverage
|
| 352 |
|
| 353 | local out_dir=_test/clang-coverage/mycpp
|
| 354 | test/coverage.sh html-report $out_dir \
|
| 355 | clang-coverage/mycpp clang-coverage+bumpleak/mycpp
|
| 356 | }
|
| 357 |
|
| 358 | examples-coverage() {
|
| 359 | ### Invoked by Soil
|
| 360 |
|
| 361 | examples-variant clang coverage
|
| 362 |
|
| 363 | local out_dir=_test/clang-coverage/mycpp/examples
|
| 364 | test/coverage.sh html-report $out_dir clang-coverage/mycpp/examples
|
| 365 | }
|
| 366 |
|
| 367 | files() {
|
| 368 | wc -l mycpp/*.py | sort -n
|
| 369 | }
|
| 370 |
|
| 371 | copy-golden() {
|
| 372 | local dir=testdata/mycpp
|
| 373 | mkdir -p $dir
|
| 374 | cp -v _gen/bin/oils_for_unix.mycpp.cc $dir
|
| 375 | }
|
| 376 |
|
| 377 | compare-golden() {
|
| 378 | local -a files=(
|
| 379 | testdata/mycpp/oils_for_unix.mycpp.cc _gen/bin/oils_for_unix.mycpp.cc
|
| 380 | )
|
| 381 |
|
| 382 | wc -l "${files[@]}"
|
| 383 | echo
|
| 384 |
|
| 385 | if diff -u "${files[@]}"; then
|
| 386 | echo EQUAL
|
| 387 | else
|
| 388 | echo 'NOT EQUAL'
|
| 389 | fi
|
| 390 | }
|
| 391 |
|
| 392 | compare-souffle() {
|
| 393 | # Show less rooting in examples
|
| 394 | ninja _bin/cxx-asan/mycpp/examples/test_iterators.mycpp{,-souffle}
|
| 395 |
|
| 396 | local -a files=(
|
| 397 | _gen/mycpp/examples/test_iterators.mycpp{,-souffle}.cc
|
| 398 | )
|
| 399 | if diff -u "${files[@]}"; then
|
| 400 | die 'Should not be equal'
|
| 401 | fi
|
| 402 |
|
| 403 | ninja _bin/cxx-asan/mycpp-souffle/osh
|
| 404 | local -a files=(
|
| 405 | _gen/bin/oils_for_unix.mycpp{,-souffle}.cc
|
| 406 | )
|
| 407 | if diff -u "${files[@]}"; then
|
| 408 | die 'Should not be equal'
|
| 409 | fi
|
| 410 | }
|
| 411 |
|
| 412 | const-pass() {
|
| 413 | python3 mycpp/const_pass.py "$@"
|
| 414 | }
|
| 415 |
|
| 416 | str-hash-demo() {
|
| 417 | local file=benchmarks/testdata/configure-coreutils
|
| 418 |
|
| 419 | # We have ~1600 strings - let's say it doubles
|
| 420 | #
|
| 421 | # 1613 unique strings -> 34 collisions of length 2, 1 of length 3
|
| 422 | # 2618 unique strings -> 108 collisions of length 2, 6 of length 3
|
| 423 | #
|
| 424 | # So yes 3 is good
|
| 425 |
|
| 426 | for n in 180 1800 3600 18000; do
|
| 427 | echo "=== Number of strings $n ==="
|
| 428 | head -n $n $file | const-pass
|
| 429 | echo
|
| 430 | done
|
| 431 | }
|
| 432 |
|
| 433 | task-five "$@"
|