1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# a simple C only test case
cmake_minimum_required(VERSION 3.10)
project (ReturnTest)
function (FAILED testname)
message (SEND_ERROR "${testname} failed ${ARGN}")
endfunction ()
function (PASS testname)
message ("${testname} passed ${ARGN}")
endfunction ()
# test simple return
function (simple)
set(simpleResult 1 PARENT_SCOPE)
return()
set(simpleResult 0 PARENT_SCOPE)
endfunction ()
simple()
if ("${simpleResult}")
pass ("simple")
else ()
failed ("simple got: ${simpleResult}")
endif ()
#test return in an if statement
set (simple2IF 1)
function (simple2)
set(simple2Result 1 PARENT_SCOPE)
if (simple2IF)
return()
endif ()
set(simple2Result 0 PARENT_SCOPE)
endfunction ()
simple2()
if ("${simple2Result}")
pass ("simple2")
else ()
failed ("simple2 got: ${simple2Result}")
endif ()
#test return in a foreach loop
function (looptest)
foreach (iter RANGE 1 5)
set (looptestResult "${iter}" PARENT_SCOPE)
if ("${iter}" EQUAL 3)
return ()
endif ()
endforeach ()
endfunction ()
looptest()
if ("${looptestResult}" EQUAL 3)
pass ("looptest")
else ()
failed ("looptest got: ${looptestResult}")
endif ()
#test return in a while loop
function (whiletest)
set (iter "a")
while(NOT "${iter}" STREQUAL "aaaaa")
set (whiletestResult "${iter}" PARENT_SCOPE)
if ("${iter}" STREQUAL "aaa")
return ()
endif ()
set (iter "${iter}a")
endwhile()
endfunction ()
whiletest()
if ("${whiletestResult}" STREQUAL "aaa")
pass ("whiletest")
else ()
failed ("whiletest got: ${whiletestResult}")
endif ()
# check subdir return
add_subdirectory(subdir)
get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
if ("${subdirResult}" EQUAL 1)
pass ("subdir")
else ()
failed ("subdir got: ${subdirResult}")
endif ()
# check return from a file
include(include_return.cmake)
if ("${include_returnResult}" EQUAL 1)
pass ("include_return")
else ()
failed ("include_return got: ${include_returnResult}")
endif ()
# check return from within a macro
macro (mymacro)
set (foo 1)
if (foo)
return()
endif ()
endmacro()
# test simple return
function (simple3)
set (bar 0)
set(simple3Result 1 PARENT_SCOPE)
if (bar)
else ()
mymacro()
endif()
set(simple3Result 0 PARENT_SCOPE)
endfunction ()
simple3()
if ("${simple3Result}")
pass ("macrotest")
else ()
failed ("macrotest got: ${simple3Result}")
endif ()
# test break command now in a foreach
foreach (iter RANGE 1 5)
set (break1 "${iter}")
if ("${iter}" EQUAL 3)
break ()
endif ()
endforeach ()
if ("${break1}" EQUAL 3)
pass ("break in foreach")
else ()
failed ("break in foreach got: ${break1}")
endif ()
# test break in a while loop
set (iter "a")
while(NOT "${iter}" STREQUAL "aaaaa")
if ("${iter}" STREQUAL "aaa")
break ()
endif ()
set (iter "${iter}a")
endwhile()
if ("${iter}" STREQUAL "aaa")
pass ("break in a while")
else ()
failed ("break in a while got: ${whiletestResult}")
endif ()
add_executable (ReturnTest returnTest.c)
|