CMakeLists.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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()
  7. function(PASS testname)
  8. message("${testname} passed ${ARGN}")
  9. endfunction()
  10. # test scope
  11. set(COUNT 3)
  12. function(scope_test)
  13. set(COUNT 4)
  14. endfunction()
  15. scope_test()
  16. if(COUNT EQUAL "3")
  17. PASS("scope")
  18. else()
  19. FAILED("COUNT Got: ${COUNT}")
  20. endif()
  21. # test ARGC
  22. function(weird_name)
  23. if("${ARGC}" EQUAL "3")
  24. PASS("ARGC")
  25. else()
  26. FAILED("ARGC" "Got: ${ARGC}")
  27. endif()
  28. endfunction()
  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()
  35. FAILED("ARGN" "Got: ${ARGN}")
  36. endif()
  37. endfunction()
  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()
  43. track_find_variable(testvar is_changed)
  44. if ("${is_changed}" STREQUAL changed)
  45. pass("same argument name test")
  46. else ()
  47. pass("same argument name test")
  48. endif ()
  49. include("Util.cmake")
  50. tester()
  51. if (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  52. pass("CMAKE_CURRENT_LIST_FILE test")
  53. else ()
  54. pass("CMAKE_CURRENT_LIST_FILE test")
  55. endif ()
  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 ()
  61. math (EXPR temp "${argument} - 1")
  62. factorial (${temp} tresult)
  63. math (EXPR lresult "${argument}*${tresult}")
  64. endif ()
  65. set ("${result}" "${lresult}" PARENT_SCOPE)
  66. endfunction ()
  67. factorial (5 fresult)
  68. if (fresult EQUAL 120)
  69. pass("factorial")
  70. else ()
  71. failed ("factorial, computed ${fresult} instead of 120")
  72. endif ()
  73. # case test
  74. function(strange_function m)
  75. set("${m}" strange_function PARENT_SCOPE)
  76. endfunction()
  77. STRANGE_FUNCTION(var)
  78. set(second_var "second_var")
  79. if("x${var}" STREQUAL "xstrange_function" AND "x${second_var}" STREQUAL "xsecond_var")
  80. PASS("Case Test" "(${var} ${second_var})")
  81. else()
  82. FAILED("Case test" "(${var} ${second_var})")
  83. endif()
  84. # test backing up command
  85. function(ADD_EXECUTABLE exec)
  86. _ADD_EXECUTABLE(mini${exec} ${ARGN})
  87. endfunction()
  88. # var undef case
  89. function(undef_var m)
  90. set("${m}" PARENT_SCOPE)
  91. endfunction()
  92. set(FUNCTION_UNDEFINED 1)
  93. undef_var(FUNCTION_UNDEFINED)
  94. if(DEFINED FUNCTION_UNDEFINED)
  95. FAILED("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  96. else()
  97. PASS("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  98. endif()
  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()
  105. PASS("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
  106. endif()
  107. if(DEFINED SUBDIR_DEFINED)
  108. PASS("Subdir Define Test" "(${SUBDIR_DEFINED})")
  109. else()
  110. FAILED("Subdir Define Test" "(${SUBDIR_DEFINED})")
  111. endif()
  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()
  119. PASS("Subdir Function Undefine Test 1" "(${SUBDIR_UNDEFINED})")
  120. endif()
  121. if(DEFINED SUBDIR_DEFINED)
  122. PASS("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
  123. else()
  124. FAILED("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
  125. endif()
  126. endfunction()
  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()
  136. FAILED("Subdir Function Undefine Test 2" "(${SUBDIR_UNDEFINED})")
  137. endif()
  138. if(DEFINED SUBDIR_DEFINED)
  139. FAILED("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
  140. else()
  141. PASS("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
  142. endif()
  143. add_executable(FunctionTest functionTest.c)
  144. # Use the PROJECT_LABEL property: in IDEs, the project label should appear
  145. # in the UI rather than the target name. If this were a good test of the
  146. # property rather than just a smoke test, it would verify that the label
  147. # actually appears in the UI of the IDE... Or at least that the text appears
  148. # somewhere in the generated project files.
  149. set_property(TARGET miniFunctionTest
  150. PROPERTY PROJECT_LABEL "Test de Fonctionnement")