CMakeLists.txt 3.7 KB

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