1# Generic test where success is expected and standard output is
2# compared to a reference file.
3#
4# Expected variables (required unless otherwise noted):
5# SOURCEDIR: the location of the reference file
6# TARGETDIR: the directory to store stdout to compare with the reference output
7# TESTBASE: the name of the test, used to create output file and find reference
8#
9# TEST_PROG: the program under test
10# ARGS: a space-separated argument list (optional)
11# SPARG1: a single escaped argument (optional)
12# SPARG2: a single escaped argument (optional)
13# SPARG3: a single escaped argument (optional)
14# SPARG4: a single escaped argument (optional)
15# EXP_RC: expected rc from the program (optional)
16
17# DIFF_PROG: if defined, use this for differenceing the outputs
18# DIFF_ARGS: arguments to pass to ${DIFF_PROG}
19#
20# Flags:
21# OWNOUTPUT: if unset, stdout will be captured to ${TESTBASE}.out.  If
22#    set, it is expected that the application itself will produce
23#    ${TESTBASE}.out.
24# NODIFF: if set, do not compare the output to a reference file as
25#    part of the test.
26
27if(NOT EXP_RC)
28    set(EXP_RC 0)
29endif()
30
31# Convert ARGS into a cmake list
32IF(DEFINED ARGS)
33   string(REPLACE " " ";" ARG_LIST ${ARGS})
34ENDIF(DEFINED ARGS)
35
36
37IF(NOT DEFINED OWNOUTPUT)
38    message(STATUS "${TEST_PROG} ${ARGS} ${SPARG1} ${SPARG2} ${SPARG3} ${SPARG4} >${TARGETDIR}/${TESTBASE}.out")
39    execute_process(COMMAND ${TEST_PROG} ${ARG_LIST} ${SPARG1} ${SPARG2} ${SPARG3} ${SPARG4}
40        OUTPUT_FILE ${TARGETDIR}/${TESTBASE}.out
41        RESULT_VARIABLE RC)
42ELSE(NOT DEFINED OWNOUTPUT)
43    message(STATUS "${TEST_PROG} ${ARGS} ${SPARG1} ${SPARG2} ${SPARG3} ${SPARG4}")
44    execute_process(COMMAND ${TEST_PROG} ${ARG_LIST} ${SPARG1} ${SPARG2} ${SPARG3} ${SPARG4}
45        OUTPUT_QUIET
46        RESULT_VARIABLE RC)
47ENDIF(NOT DEFINED OWNOUTPUT)
48
49if(NOT EXP_RC EQUAL RC)
50    message(FATAL_ERROR "Test failed, ${EXP_RC} != ${RC}")
51endif()
52
53
54IF(NOT DEFINED NODIFF)
55    set(exp "${SOURCEDIR}/${TESTBASE}.exp")
56    set(out "${TARGETDIR}/${TESTBASE}.out")
57
58    if(DEFINED DIFF_PROG)
59        message(STATUS         "${DIFF_PROG} ${DIFF_ARGS} -1 ${out} -2 ${exp}")
60        execute_process(COMMAND ${DIFF_PROG} ${DIFF_ARGS} -1 ${out} -2 ${exp}
61            RESULT_VARIABLE DIFFERENT)
62    else()
63        message(STATUS "diff ${out} ${exp}")
64        execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files ${out} ${exp}
65            RESULT_VARIABLE DIFFERENT)
66    endif()
67
68   if(DIFFERENT)
69       message(FATAL_ERROR "Test failed - files differ: ${DIFFERENT}")
70   else()
71       message(STATUS "Test passed")
72   endif(DIFFERENT)
73ENDIF(NOT DEFINED NODIFF)
74