From 569a294fc2a47c59df50d1fdca2918e0f0ecfc62 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 23 Feb 2020 23:13:51 +1100 Subject: [PATCH] Add script/run-json-test-suite.sh --- example/jsonptr/jsonptr.cc | 3 + script/run-json-test-suite.sh | 103 ++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100755 script/run-json-test-suite.sh diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index ce24ed9f..2d725a78 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -23,6 +23,9 @@ is written in C++, not C. $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out for a C++ compiler $CXX, such as clang++ or g++. + +After modifying this program, run "build-example.sh example/jsonptr/" and then +"script/run-json-test-suite.sh" to catch correctness regressions. */ #include diff --git a/script/run-json-test-suite.sh b/script/run-json-test-suite.sh new file mode 100755 index 00000000..c7d86566 --- /dev/null +++ b/script/run-json-test-suite.sh @@ -0,0 +1,103 @@ +#!/bin/bash -eu +# Copyright 2020 The Wuffs Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# ---------------- + +# This script runs a JSON parsing program (e.g. example/jsonptr) over a JSON +# test suite (e.g. https://github.com/nst/JSONTestSuite checked out on local +# disk as $HOME/JSONTestSuite). +# +# It runs that program over every command line argument: if a file then itself, +# if a directory then every [iny]_*.json child. It checks for a 0 exit code for +# y_*.json files, 1 for n_*.json files and either 0 or 1 for i_*.json files. + +jsonparser=${JSONPARSER:-gen/bin/example-jsonptr} +if [ ! "$(command -v $jsonparser)" ]; then + if [ ${JSONPARSER:-} ]; then + echo "Could not run \$JSONPARSER ($jsonparser)." + else + echo "Could not run the default JSON parser (gen/bin/example-jsonptr)." + echo "Run \"./build-example.sh example/jsonptr\" from the Wuffs root directory." + fi + exit 1 +fi + +sources=$@ +if [ $# -eq 0 ]; then + sources=$HOME/JSONTestSuite/test_parsing +fi + +num_fail=0 +num_ok=0 +num_total=0 + +# ---- + +test1() { + set +e + + $jsonparser < $1 >/dev/null 2>/dev/null + local x=$? + + local basename=${1##*/} + local ok= + if [[ $basename = i_*.json ]]; then + if [[ ($x == 0 || $x == 1) ]]; then + ok=1 + fi + elif [[ $basename = n_*.json ]]; then + if [[ ($x == 1) ]]; then + ok=1 + fi + elif [[ $basename = y_*.json ]]; then + if [[ ($x == 0) ]]; then + ok=1 + fi + fi + + if [ $ok ]; then + echo "ok $1" + ((num_ok++)) + else + echo "fail $1" + ((num_fail++)) + fi + ((num_total++)) + + set -e +} + +# ---- + +for f in $sources; do + if [ -f $f ]; then + test1 $f + elif [ -d $f ]; then + for g in ${f%/}/[iny]_*.json; do + test1 $g + done + else + echo "Could not open $f" + if [ $# -eq 0 ]; then + echo "Run \"git clone https://github.com/nst/JSONTestSuite.git\" from your home dir." + fi + exit 1 + fi +done + +echo "$num_fail fail, $num_ok ok, $num_total total" +if [ $num_fail -ne 0 ]; then + exit 1 +fi