CMakeLists.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # a simple C only test case
  2. cmake_minimum_required (VERSION 2.6)
  3. PROJECT (FunctionTest)
  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 scope
  12. SET(COUNT 3)
  13. FUNCTION(scope_test)
  14. SET(COUNT 4)
  15. ENDFUNCTION(scope_test)
  16. scope_test()
  17. IF(COUNT EQUAL "3")
  18. PASS("scope")
  19. ELSE(COUNT EQUAL "3")
  20. FAILED("COUNT Got: ${COUNT}")
  21. ENDIF(COUNT EQUAL "3")
  22. # test ARGC
  23. FUNCTION(weird_name)
  24. IF("${ARGC}" EQUAL "3")
  25. PASS("ARGC")
  26. ELSE("${ARGC}" EQUAL "3")
  27. FAILED("ARGC" "Got: ${ARGC}")
  28. ENDIF("${ARGC}" EQUAL "3")
  29. ENDFUNCTION(weird_name)
  30. WeIrD_nAmE(a1 a2 a3)
  31. # test ARGN
  32. FUNCTION(test_argn_function argument)
  33. IF("${ARGN}" EQUAL "3")
  34. PASS("ARGN")
  35. ELSE("${ARGN}" EQUAL "3")
  36. FAILED("ARGN" "Got: ${ARGN}")
  37. ENDIF("${ARGN}" EQUAL "3")
  38. ENDFUNCTION(test_argn_function)
  39. Test_Argn_Function(ignored 3)
  40. # test argument naming and raise scope
  41. function(track_find_variable cache_variable is_changed)
  42. set("${is_changed}" changed PARENT_SCOPE)
  43. endfunction(track_find_variable)
  44. track_find_variable(testvar is_changed)
  45. if ("${is_changed}" STREQUAL changed)
  46. pass("same argument name test")
  47. else ("${is_changed}" STREQUAL changed)
  48. pass("same argument name test")
  49. endif ("${is_changed}" STREQUAL changed)
  50. include("Util.cmake")
  51. tester()
  52. if (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  53. pass("CMAKE_CURRENT_LIST_FILE test")
  54. else (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  55. pass("CMAKE_CURRENT_LIST_FILE test")
  56. endif (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
  57. # test recursion and return via set(... PARENT_SCOPE)
  58. function (factorial argument result)
  59. if (argument LESS 2)
  60. set (lresult 1)
  61. else (argument LESS 2)
  62. math (EXPR temp "${argument} - 1")
  63. factorial (${temp} tresult)
  64. math (EXPR lresult "${argument}*${tresult}")
  65. endif (argument LESS 2)
  66. set ("${result}" "${lresult}" PARENT_SCOPE)
  67. endfunction (factorial)
  68. factorial (5 fresult)
  69. if (fresult EQUAL 120)
  70. pass("factorial")
  71. else (fresult EQUAL 120)
  72. failed ("factorial, computed ${fresult} instead of 120")
  73. endif (fresult EQUAL 120)
  74. # case test
  75. FUNCTION(strange_function m)
  76. SET("${m}" strange_function PARENT_SCOPE)
  77. ENDFUNCTION(strange_function m)
  78. STRANGE_FUNCTION(var)
  79. set(second_var "second_var")
  80. IF("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  81. PASS("Case Test" "(${var} ${second_var})")
  82. ELSE("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  83. FAILED("Case test" "(${var} ${second_var})")
  84. ENDIF("${var}" STREQUAL "strange_function" AND "${second_var}" STREQUAL "second_var")
  85. # test backing up command
  86. FUNCTION(ADD_EXECUTABLE exec)
  87. _ADD_EXECUTABLE(mini${exec} ${ARGN})
  88. ENDFUNCTION(ADD_EXECUTABLE)
  89. # var undef case
  90. FUNCTION(undef_var m)
  91. SET("${m}" PARENT_SCOPE)
  92. ENDFUNCTION(undef_var)
  93. SET(FUNCTION_UNDEFINED 1)
  94. undef_var(FUNCTION_UNDEFINED)
  95. IF(DEFINED FUNCTION_UNDEFINED)
  96. FAILED("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  97. ELSE(DEFINED FUNCTION_UNDEFINED)
  98. PASS("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
  99. ENDIF(DEFINED FUNCTION_UNDEFINED)
  100. # Subdirectory scope raise.
  101. SET(SUBDIR_UNDEFINED 1)
  102. ADD_SUBDIRECTORY(SubDirScope)
  103. IF(DEFINED SUBDIR_UNDEFINED)
  104. FAILED("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
  105. ELSE(DEFINED SUBDIR_UNDEFINED)
  106. PASS("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
  107. ENDIF(DEFINED SUBDIR_UNDEFINED)
  108. IF(DEFINED SUBDIR_DEFINED)
  109. PASS("Subdir Define Test" "(${SUBDIR_DEFINED})")
  110. ELSE(DEFINED SUBDIR_DEFINED)
  111. FAILED("Subdir Define Test" "(${SUBDIR_DEFINED})")
  112. ENDIF(DEFINED SUBDIR_DEFINED)
  113. ADD_EXECUTABLE(FunctionTest functionTest.c)