CMakeLists.txt 2.96 KB
Newer Older
1
# a simple C only test case
Ken Martin's avatar
Ken Martin committed
2
cmake_minimum_required (VERSION 2.6)
3 4 5 6
project (ReturnTest)

function (FAILED testname)
  message (SEND_ERROR "${testname} failed ${ARGN}")
7
endfunction ()
8 9 10

function (PASS testname)
  message ("${testname} passed ${ARGN}")
11
endfunction ()
12 13 14 15 16 17

# test simple return
function (simple)
  set(simpleResult 1 PARENT_SCOPE)
  return()
  set(simpleResult 0 PARENT_SCOPE)
18
endfunction ()
19 20 21
simple()
if ("${simpleResult}")
  pass ("simple")
22
else ()
23
  failed ("simple got: ${simpleResult}")
24
endif ()
25 26 27 28 29 30 31

#test return in an if statement
set (simple2IF 1)
function (simple2)
  set(simple2Result 1 PARENT_SCOPE)
  if (simple2IF)
    return()
32
  endif ()
33
  set(simple2Result 0 PARENT_SCOPE)
34
endfunction ()
35 36 37
simple2()
if ("${simple2Result}")
  pass ("simple2")
38
else ()
39
  failed ("simple2 got: ${simple2Result}")
40
endif ()
41 42 43 44 45 46 47

#test return in a foreach loop
function (looptest)
  foreach (iter RANGE 1 5)
    set (looptestResult "${iter}" PARENT_SCOPE)
    if ("${iter}" EQUAL 3)
      return ()
48 49 50
    endif ()
  endforeach ()
endfunction ()
51 52 53
looptest()
if ("${looptestResult}" EQUAL 3)
  pass ("looptest")
54
else ()
55
  failed ("looptest got: ${looptestResult}")
56
endif ()
57 58 59 60 61 62 63 64

#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 ()
65
    endif ()
66
    set (iter "${iter}a")
67 68
  endwhile()
endfunction ()
69 70 71
whiletest()
if ("${whiletestResult}" STREQUAL "aaa")
  pass ("whiletest")
72
else ()
73
  failed ("whiletest got: ${whiletestResult}")
74
endif ()
75 76 77 78 79 80

# check subdir return
add_subdirectory(subdir)
get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
if ("${subdirResult}" EQUAL 1)
  pass ("subdir")
81
else ()
82
  failed ("subdir got: ${subdirResult}")
83
endif ()
84

85 86 87 88
# check return from a file
include(include_return.cmake)
if ("${include_returnResult}" EQUAL 1)
  pass ("include_return")
89
else ()
90
  failed ("include_return got: ${include_returnResult}")
91
endif ()
92

93 94 95 96 97
# check return from within a macro
macro (mymacro)
  set (foo 1)
  if (foo)
    return()
98 99
  endif ()
endmacro()
100 101 102 103 104 105

# test simple return
function (simple3)
  set (bar 0)
  set(simple3Result 1 PARENT_SCOPE)
  if (bar)
106
  else ()
107
    mymacro()
108
  endif()
109
  set(simple3Result 0 PARENT_SCOPE)
110
endfunction ()
111 112 113
simple3()
if ("${simple3Result}")
  pass ("macrotest")
114
else ()
115
  failed ("macrotest got: ${simple3Result}")
116
endif ()
117 118 119 120 121 122 123


# test break command now in a foreach
foreach (iter RANGE 1 5)
  set (break1 "${iter}")
  if ("${iter}" EQUAL 3)
    break ()
124 125
  endif ()
endforeach ()
126 127
if ("${break1}" EQUAL 3)
  pass ("break in foreach")
128
else ()
129
  failed ("break in foreach got: ${break1}")
130
endif ()
131 132 133 134 135 136

# test break in a while loop
set (iter "a")
while(NOT "${iter}" STREQUAL "aaaaa")
  if ("${iter}" STREQUAL "aaa")
    break ()
137
  endif ()
138
  set (iter "${iter}a")
139
endwhile()
140 141
if ("${iter}" STREQUAL "aaa")
  pass ("break in a while")
142
else ()
143
  failed ("break in a while got: ${whiletestResult}")
144
endif ()
145 146 147


add_executable (ReturnTest returnTest.c)