diff --git a/IDAWin/CMakeLists.txt b/IDAWin/CMakeLists.txt index 8b72c06f..0dcf9f0a 100644 --- a/IDAWin/CMakeLists.txt +++ b/IDAWin/CMakeLists.txt @@ -37,13 +37,15 @@ enable_testing() if (MINGW) set(test_sundials_exe ${CMAKE_BINARY_DIR}/bin/${EXE_FILE}.exe) + set(python_cmd py) else (MINGW) set(test_sundials_exe ${CMAKE_BINARY_DIR}/bin/${EXE_FILE}) + set(python_cmd python3) endif (MINGW) set(test_dir ${CMAKE_CURRENT_SOURCE_DIR}/tests/smoke) -# smoke test as a bash script, for python test example, see NFsim/tests/smoke -add_test(NAME ${EXE_FILE}_smoke COMMAND bash -c "${test_dir}/smoke.sh ${test_sundials_exe}" WORKING_DIRECTORY ${test_dir}) +# smoke test as a python script, for bash test example, see NFsim/tests/smoke +add_test(NAME ${EXE_FILE}_smoke COMMAND ${python_cmd} ${test_dir}/smoke.py ${test_sundials_exe} WORKING_DIRECTORY ${test_dir}) add_executable( diff --git a/IDAWin/tests/smoke/smoke.py b/IDAWin/tests/smoke/smoke.py new file mode 100755 index 00000000..c024c55d --- /dev/null +++ b/IDAWin/tests/smoke/smoke.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import os +import posixpath +import subprocess +import sys +from pathlib import Path + + +# function to numerically compare the contents of two text files to determine approximate equality +# each line is a space delimited list of numbers, compare them to within 8 significant figures +def compare_files(file1: Path, file2: Path, tolerance: float): + with open(file1, 'r') as f1, open(file2, 'r') as f2: + for line1, line2 in zip(f1, f2): + line1 = [float(x) for x in line1.split()] + line2 = [float(x) for x in line2.split()] + for x, y in zip(line1, line2): + if x != y and abs(x - y) > tolerance * max(abs(x), abs(y)): + return False + return True + + +# get the directory of this script +test_dir = os.path.dirname(os.path.realpath(__file__)) +# in the path replace \ with /, D:\ with /d/ +test_dir = test_dir.replace("\\", "/") +# tell os.path.join to use / as the path separator +os.path.sep = "/" +exe = sys.argv[1] + +print(f"test_dir: {test_dir}") +print(f"exe: {exe}") + +input_file = posixpath.join(test_dir, "SimID_1489333437_0_.cvodeInput") +output_file = posixpath.join(test_dir, "SimID_1489333437_0_.ida") +expected_output_file = posixpath.join(test_dir, "SimID_1489333437_0_.ida.expected") + +if not posixpath.exists(exe): + print(f"SundialsSolverStandalone_x64 executable {exe} not found. Exiting...") + sys.exit(1) + +if not posixpath.exists(input_file): + print(f"Input file {input_file} not found. Exiting...") + sys.exit(1) + +if not posixpath.exists(expected_output_file): + print(f"Expected output file {expected_output_file} not found. Exiting...") + sys.exit(1) + +command = [exe, input_file, output_file] +print(" ".join(command)) + +try: + subprocess.check_call(command) +except subprocess.CalledProcessError: + print("SundialsSolverStandalone_x64 failed to run. Exiting...") + sys.exit(1) + +# verify that the output files exist +if not os.path.isfile(output_file): + print(f"Output file {output_file} not found. Exiting...") + sys.exit(1) + +# verify that the output files match the expected output files +if not compare_files(file1=Path(output_file), file2=Path(expected_output_file), tolerance=1e-8): + print(f"Output file {output_file} does not match expected output {expected_output_file}. Exiting...") + sys.exit(1) + +print("SundialsSolverStandalone_x64 solver completed and solution matched expected output. Exiting...") +sys.exit(0) diff --git a/IDAWin/tests/smoke/smoke.sh b/IDAWin/tests/smoke/smoke.sh deleted file mode 100755 index 6eeb6722..00000000 --- a/IDAWin/tests/smoke/smoke.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -set -e -EXE=$1 - -echo "Running SundialsSolverStandalone_x64 solver with smoke.sh $EXE" - -# get the directory of this script -TEST_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -INPUT="${TEST_DIR}/SimID_1489333437_0_.cvodeInput" -OUTPUT="${TEST_DIR}/SimID_1489333437_0_.ida" -EXPECTED_OUTPUT="${TEST_DIR}/SimID_1489333437_0_.ida.expected" - -if [ ! -f "$EXE" ]; then - echo "NFsim executable $EXE not found. Exiting..." - exit 1 -fi -if [ ! -f "$INPUT" ]; then - echo "Input file $INPUT not found. Exiting..." - exit 1 -fi -if [ ! -f "$EXPECTED_OUTPUT" ]; then - echo "Expected output file $EXPECTED not found. Exiting..." - exit 1 -fi - - -command="$EXE $INPUT $OUTPUT" -echo "$command" -if ! $command; then - echo "SundialsSolverStandalone_x64 failed to run. Exiting..." - exit 1 -fi - -# verify that the output files exist -if [ ! -f "$OUTPUT" ]; then - echo "Output file $OUTPUT not found. Exiting..." - exit 1 -fi - -# verify that the output files match the expected output files -if ! diff "$OUTPUT" "$EXPECTED_OUTPUT"; then - echo "Output file $OUTPUT does not match expected output $EXPECTED_OUTPUT. Exiting..." - exit 1 -fi - -echo "SundialsSolverStandalone_x64 solver completed and solution matched expected output. Exiting..." -exit 0