64504470d42804fe2bb8da26c50d626f7e84fe66
[platform/upstream/cmake.git] / Tests / FunctionTest / CMakeLists.txt
1 # a simple C only test case
2 cmake_minimum_required (VERSION 2.8.12)
3 project (FunctionTest)
4
5 function(FAILED testname)
6   message(SEND_ERROR "${testname} failed ${ARGN}")
7 endfunction()
8
9 function(PASS testname)
10   message("${testname} passed ${ARGN}")
11 endfunction()
12
13
14 # test scope
15 set(COUNT 3)
16 function(scope_test)
17   set(COUNT 4)
18 endfunction()
19 scope_test()
20 if(COUNT EQUAL "3")
21   PASS("scope")
22 else()
23   FAILED("COUNT Got: ${COUNT}")
24 endif()
25
26 # test ARGC
27 function(weird_name)
28   if("${ARGC}" EQUAL "3")
29     PASS("ARGC")
30   else()
31     FAILED("ARGC" "Got: ${ARGC}")
32   endif()
33 endfunction()
34 WeIrD_nAmE(a1 a2 a3)
35
36 # test ARGN
37 function(test_argn_function argument)
38   if("${ARGN}" EQUAL "3")
39     PASS("ARGN")
40   else()
41     FAILED("ARGN" "Got: ${ARGN}")
42   endif()
43 endfunction()
44 Test_Argn_Function(ignored 3)
45
46 # test argument naming and raise scope
47 function(track_find_variable cache_variable is_changed)
48  set("${is_changed}" changed PARENT_SCOPE)
49 endfunction()
50 track_find_variable(testvar is_changed)
51 if ("${is_changed}" STREQUAL changed)
52   pass("same argument name test")
53 else ()
54   pass("same argument name test")
55 endif ()
56
57 include("Util.cmake")
58 tester()
59 if (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}")
60   pass("CMAKE_CURRENT_LIST_FILE test")
61 else ()
62   pass("CMAKE_CURRENT_LIST_FILE test")
63 endif ()
64
65
66
67 # test recursion and return via set(... PARENT_SCOPE)
68 function (factorial argument result)
69   if (argument LESS 2)
70     set (lresult 1)
71   else ()
72     math (EXPR temp "${argument} - 1")
73     factorial (${temp} tresult)
74     math (EXPR lresult "${argument}*${tresult}")
75   endif ()
76   set ("${result}" "${lresult}" PARENT_SCOPE)
77 endfunction ()
78
79 factorial (5 fresult)
80 if (fresult EQUAL 120)
81   pass("factorial")
82 else ()
83   failed ("factorial, computed ${fresult} instead of 120")
84 endif ()
85
86
87
88 # case test
89 function(strange_function m)
90   set("${m}" strange_function PARENT_SCOPE)
91 endfunction()
92
93 STRANGE_FUNCTION(var)
94 set(second_var "second_var")
95 if("x${var}" STREQUAL "xstrange_function" AND "x${second_var}" STREQUAL "xsecond_var")
96   PASS("Case Test" "(${var} ${second_var})")
97 else()
98   FAILED("Case test" "(${var} ${second_var})")
99 endif()
100
101 # test backing up command
102 function(ADD_EXECUTABLE exec)
103   _ADD_EXECUTABLE(mini${exec} ${ARGN})
104 endfunction()
105
106 # var undef case
107 function(undef_var m)
108   set("${m}" PARENT_SCOPE)
109 endfunction()
110
111 set(FUNCTION_UNDEFINED 1)
112 undef_var(FUNCTION_UNDEFINED)
113 if(DEFINED FUNCTION_UNDEFINED)
114   FAILED("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
115 else()
116   PASS("Function Undefine Test" "(${FUNCTION_UNDEFINED})")
117 endif()
118
119 # Subdirectory scope raise.
120 set(SUBDIR_UNDEFINED 1)
121 add_subdirectory(SubDirScope)
122 if(DEFINED SUBDIR_UNDEFINED)
123   FAILED("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
124 else()
125   PASS("Subdir Undefine Test" "(${SUBDIR_UNDEFINED})")
126 endif()
127 if(DEFINED SUBDIR_DEFINED)
128   PASS("Subdir Define Test" "(${SUBDIR_DEFINED})")
129 else()
130   FAILED("Subdir Define Test" "(${SUBDIR_DEFINED})")
131 endif()
132
133 # Test function-scoped directory.
134 function(ADD_SUBDIR2 dir)
135   add_subdirectory("${dir}" "${dir}2")
136   # The parent scope sets in the subdir should be visible here.
137   if(DEFINED SUBDIR_UNDEFINED)
138     FAILED("Subdir Function Undefine Test 1" "(${SUBDIR_UNDEFINED})")
139   else()
140     PASS("Subdir Function Undefine Test 1" "(${SUBDIR_UNDEFINED})")
141   endif()
142   if(DEFINED SUBDIR_DEFINED)
143     PASS("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
144   else()
145     FAILED("Subdir Function Define Test 1" "(${SUBDIR_DEFINED})")
146   endif()
147 endfunction()
148
149 # Reset test variables.
150 set(SUBDIR_UNDEFINED 1)
151 set(SUBDIR_DEFINED)
152
153 # Run test function.
154 ADD_SUBDIR2(SubDirScope)
155
156 # The parent scope sets in the subdir should not be visible here.
157 if(DEFINED SUBDIR_UNDEFINED)
158   PASS("Subdir Function Undefine Test 2" "(${SUBDIR_UNDEFINED})")
159 else()
160   FAILED("Subdir Function Undefine Test 2" "(${SUBDIR_UNDEFINED})")
161 endif()
162 if(DEFINED SUBDIR_DEFINED)
163   FAILED("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
164 else()
165   PASS("Subdir Function Define Test 2" "(${SUBDIR_DEFINED})")
166 endif()
167
168 add_executable(FunctionTest functionTest.c)
169
170 # Use the PROJECT_LABEL property: in IDEs, the project label should appear
171 # in the UI rather than the target name. If this were a good test of the
172 # property rather than just a smoke test, it would verify that the label
173 # actually appears in the UI of the IDE... Or at least that the text appears
174 # somewhere in the generated project files.
175 set_property(TARGET miniFunctionTest
176   PROPERTY PROJECT_LABEL "Test de Fonctionnement")