CMakeLists.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # a simple C only test case
  2. cmake_minimum_required (VERSION 2.6)
  3. PROJECT (FunctionTest)
  4. FUNCTION(FAILED testname)
  5. MESSAGE(SEND_ERROR "${testname} failed ${ARGN}")
  6. ENDFUNCTION(FAILED)
  7. FUNCTION(PASS testname)
  8. MESSAGE("${testname} passed ${ARGN}")
  9. ENDFUNCTION(PASS)
  10. # test scope
  11. SET(COUNT 3)
  12. FUNCTION(scope_test)
  13. SET(COUNT 4)
  14. ENDFUNCTION(scope_test)
  15. scope_test()
  16. IF(COUNT EQUAL "3")
  17. PASS("scope")
  18. ELSE(COUNT EQUAL "3")
  19. FAILED("COUNT Got: ${COUNT}")
  20. ENDIF(COUNT EQUAL "3")
  21. # test ARGC
  22. FUNCTION(weird_name)
  23. IF("${ARGC}" EQUAL "3")
  24. PASS("ARGC")
  25. ELSE("${ARGC}" EQUAL "3")
  26. FAILED("ARGC" "Got: ${ARGC}")
  27. ENDIF("${ARGC}" EQUAL "3")
  28. ENDFUNCTION(weird_name)
  29. WeIrD_nAmE(a1 a2 a3)
  30. # test ARGN
  31. FUNCTION(test_argn_function argument)
  32. IF("${ARGN}" EQUAL "3")
  33. PASS("ARGN")
  34. ELSE("${ARGN}" EQUAL "3")
  35. FAILED("ARGN" "Got: ${ARGN}")
  36. ENDIF("${ARGN}" EQUAL "3")
  37. ENDFUNCTION(test_argn_function)
  38. Test_Argn_Function(ignored 3)
  39. # test argument naming and raise scope
  40. function(track_find_variable cache_variable is_changed)
  41. set("${is_changed}" changed PARENT_SCOPE)
  42. endfunction(track_find_variable)
  43. track_find_variable(testvar is_changed)
  44. if ("${is_changed}" STREQUAL changed)
  45. pass("same argument name test")
  46. else ("${is_changed}" STREQUAL changed)
  47. pass("same argument name test")
  48. endif ("${is_changed}" STREQUAL changed)
  49. include("Util.cmake")
  50. tester()
  51. if (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  52. pass("CMAKE_CURRENT_LIST_FILE test")
  53. else (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  54. pass("CMAKE_CURRENT_LIST_FILE test")
  55. endif (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  56. # test recursion and return via set(... PARENT_SCOPE)
  57. function (factorial argument result)
  58. if (argument LESS 2)
  59. set (lresult 1)
  60. else (argument LESS 2)
  61. math (EXPR temp "${argument} - 1")
  62. factorial (${temp} tresult)
  63. math (EXPR lresult "${argument}*${tresult}")
  64. endif (argument LESS 2)
  65. set ("${result}" "${lresult}" PARENT_SCOPE)
  66. endfunction (factorial)
  67. factorial (5 fresult)
  68. if (fresult EQUAL 120)
  69. pass("factorial")
  70. else (fresult EQUAL 120)
  71. failed ("factorial, computed ${fresult} instead of 120")
  72. endif (fresult EQUAL 120)
  73. # case test
  74. FUNCTION(strange_function m)
  75. SET("${m}" strange_function PARENT_SCOPE)
  76. ENDFUNCTION(strange_function m)
  77. STRANGE_FUNCTION(var)
  78. set(second_var "second_var")
  79. IF("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  80. PASS("Case Test" "(${var} ${second_var})")
  81. ELSE("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  82. FAILED("Case test" "(${var} ${second_var})")
  83. ENDIF("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  84. # test backing up command
  85. FUNCTION(ADD_EXECUTABLE exec)
  86. _ADD_EXECUTABLE(mini${exec} ${ARGN})
  87. ENDFUNCTION(ADD_EXECUTABLE)
  88. # var undef case
  89. FUNCTION(undef_var m)
  90. SET("${m}" PARENT_SCOPE)
  91. ENDFUNCTION(undef_var)
  92. SET(FUNCTION_UNDEFINED 1)
  93. undef_var(FUNCTION_UNDEFINED)
  94. IF(DEFINED FUNCTION_UNDEFINED)
  95. FAILED("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  96. ELSE(DEFINED FUNCTION_UNDEFINED)
  97. PASS("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  98. ENDIF(DEFINED FUNCTION_UNDEFINED)
  99. # Subdirectory scope raise.
  100. SET(SUBDIR_UNDEFINED 1)
  101. ADD_SUBDIRECTORY(SubDirScope)
  102. IF(DEFINED SUBDIR_UNDEFINED)
  103. FAILED("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
  104. ELSE(DEFINED SUBDIR_UNDEFINED)
  105. PASS("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
  106. ENDIF(DEFINED SUBDIR_UNDEFINED)
  107. IF(DEFINED SUBDIR_DEFINED)
  108. PASS("Subdir Define Test" "(${SUBDIR_DEFINED})")
  109. ELSE(DEFINED SUBDIR_DEFINED)
  110. FAILED("Subdir Define Test" "(${SUBDIR_DEFINED})")
  111. ENDIF(DEFINED SUBDIR_DEFINED)
  112. # Test function-scoped directory.
  113. FUNCTION(ADD_SUBDIR2 dir)
  114. ADD_SUBDIRECTORY("${dir}" "${dir}2")
  115. # The parent scope sets in the subdir should be visible here.
  116. IF(DEFINED SUBDIR_UNDEFINED)
  117. FAILED("Subdir Function Undefine Test 1" "(${SUBDIR_UNDEFINED})")
  118. ELSE(DEFINED SUBDIR_UNDEFINED)
  119. PASS("Subdir Function Undefine Test 1" "(${SUBDIR_UNDEFINED})")
  120. ENDIF(DEFINED SUBDIR_UNDEFINED)
  121. IF(DEFINED SUBDIR_DEFINED)
  122. PASS("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
  123. ELSE(DEFINED SUBDIR_DEFINED)
  124. FAILED("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
  125. ENDIF(DEFINED SUBDIR_DEFINED)
  126. ENDFUNCTION(ADD_SUBDIR2)
  127. # Reset test variables.
  128. SET(SUBDIR_UNDEFINED 1)
  129. SET(SUBDIR_DEFINED)
  130. # Run test function.
  131. ADD_SUBDIR2(SubDirScope)
  132. # The parent scope sets in the subdir should not be visible here.
  133. IF(DEFINED SUBDIR_UNDEFINED)
  134. PASS("Subdir Function Undefine Test 2" "(${SUBDIR_UNDEFINED})")
  135. ELSE(DEFINED SUBDIR_UNDEFINED)
  136. FAILED("Subdir Function Undefine Test 2" "(${SUBDIR_UNDEFINED})")
  137. ENDIF(DEFINED SUBDIR_UNDEFINED)
  138. IF(DEFINED SUBDIR_DEFINED)
  139. FAILED("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
  140. ELSE(DEFINED SUBDIR_DEFINED)
  141. PASS("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
  142. ENDIF(DEFINED SUBDIR_DEFINED)
  143. ADD_EXECUTABLE(FunctionTest functionTest.c)