1 cmake_minimum_required (VERSION 2.6)
2 PROJECT(StringFileTest)
3 INCLUDE_DIRECTORIES(${StringFileTest_BINARY_DIR})
6 FILE(READ "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile)
9 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile_strings
10 LIMIT_COUNT 1 LIMIT_INPUT 1024 LIMIT_OUTPUT 1024
11 LENGTH_MINIMUM 10 LENGTH_MAXIMUM 23 REGEX include NEWLINE_CONSUME)
12 SET(infile_strings_goal "#include \"includefile\"\n")
13 IF("${infile_strings}" STREQUAL "${infile_strings_goal}")
14 MESSAGE("FILE(STRINGS) correctly read [${infile_strings}]")
15 ELSE("${infile_strings}" STREQUAL "${infile_strings_goal}")
17 "FILE(STRINGS) incorrectly read [${infile_strings}]")
18 ENDIF("${infile_strings}" STREQUAL "${infile_strings_goal}")
20 # test reading a file and getting its binary data as hex string
21 FILE(READ "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infilehex LIMIT 4 HEX)
22 IF(NOT "${infilehex}" STREQUAL "53313036")
24 "FILE(READ ... HEX) error, read: \"${infilehex}\", expected \"53313036\"")
25 ENDIF(NOT "${infilehex}" STREQUAL "53313036")
28 # test that FILE(STRINGS) also work with Intel hex and Motorola S-record files
29 # this file has been created with "sdcc main.c"
30 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.ihx" infile_strings REGEX INFO)
31 SET(infile_strings_goal "INFO:compiler\\[SDCC-HEX\\]")
32 IF("${infile_strings}" MATCHES "${infile_strings_goal}")
33 MESSAGE("FILE(STRINGS) correctly read from hex file [${infile_strings}]")
34 ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
36 "FILE(STRINGS) incorrectly read from hex file [${infile_strings}]")
37 ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
39 # this file has been created with "sdcc main.c --out-fmt-s19"
40 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infile_strings REGEX INFO)
41 SET(infile_strings_goal "INFO:compiler\\[SDCC-SREC\\]")
42 IF("${infile_strings}" MATCHES "${infile_strings_goal}")
43 MESSAGE("FILE(STRINGS) correctly read from srec file [${infile_strings}]")
44 ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
46 "FILE(STRINGS) incorrectly read from srec file [${infile_strings}]")
47 ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
50 STRING(REGEX MATCH "[cC][mM][aA][kK][eE]" rmvar "CMake is great")
51 STRING(REGEX MATCHALL "[cC][mM][aA][kK][eE]" rmallvar "CMake is better than cmake or CMake")
52 STRING(REGEX REPLACE "[Aa][uU][tT][oO]([cC][oO][nN][fF]|[mM][aA][kK][eE])"
53 "CMake" rrepvar "People should use Autoconf and Automake")
54 STRING(COMPARE EQUAL "CMake" "Autoconf" nceqvar)
55 STRING(COMPARE EQUAL "CMake" "CMake" ceqvar)
56 STRING(COMPARE NOTEQUAL "CMake" "Autoconf" cneqvar)
57 STRING(COMPARE NOTEQUAL "CMake" "CMake" ncneqvar)
58 STRING(COMPARE LESS "before" "after" nclvar)
59 STRING(COMPARE LESS "max" "min" clvar)
60 STRING(COMPARE GREATER "before" "after" cgvar)
61 STRING(COMPARE GREATER "max" "min" ncgvar)
62 STRING(ASCII 67 109 97 107 101 savar)
63 STRING(TOUPPER "CMake" tuvar)
64 STRING(TOLOWER "CMake" tlvar)
65 STRING(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
67 IF("abc" STREQUAL "xyz")
68 MESSAGE(SEND_ERROR "Problem with the IF(STREQUAL), \"abc\" and \"xyz\" considered equal")
69 ENDIF("abc" STREQUAL "xyz")
71 IF("CMake is cool" MATCHES "(CMake) (is).+")
72 IF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
73 MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"CMake is cool\"")
74 ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
75 IF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
76 MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"CMake\"")
77 ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
78 IF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
79 MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected \"is\"")
80 ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
81 ELSE("CMake is cool" MATCHES "(CMake) (is).+")
82 MESSAGE(SEND_ERROR "Problem with the IF(MATCHES), no match found")
83 ENDIF("CMake is cool" MATCHES "(CMake) (is).+")
85 STRING(REGEX MATCH "(People).+CMake" matchResultVar "People should use CMake")
86 IF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
87 MESSAGE(SEND_ERROR "STRING(REGEX MATCH) problem: \"${matchResultVar}\", expected \"People should use CMake\"")
88 ENDIF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
89 IF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
90 MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"People should use CMake\"")
91 ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
92 IF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
93 MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"People\"")
94 ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
95 IF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
96 MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected empty string")
97 ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
103 STRING(STRIP "ST2 " ST2)
104 STRING(STRIP " ST3" ST3)
106 FOREACH(var ST1 ST2 ST3)
107 IF("${var}" STREQUAL "${${var}}")
108 MESSAGE("[${var}] == [${${var}}]")
109 ELSE("${var}" STREQUAL "${${var}}")
110 MESSAGE(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
111 ENDIF("${var}" STREQUAL "${${var}}")
114 STRING(SUBSTRING "People should use Autoconf" 7 10 substringres)
115 SET(substringres "Everybody ${substringres} CMake")
117 STRING(LENGTH ${substringres} lengthres)
119 FILE(RELATIVE_PATH relpath "/usr/local/bin" "/usr/X11R6/bin/xnest")
122 SET(var "\\ \" \ \t \n \r \# \( \) \0")
123 MESSAGE("Output: [${var}]")
124 SET(var \\ \" \ \t \n \r \# \( \) \0)
125 MESSAGE("Output: [${var}]")
127 # Make-style unquoted argument test
128 SET(var $(VAR1)$(VAR2)/$(VAR3))
129 MESSAGE("Output: [${var}]")
130 STRING(COMPARE EQUAL "${var}" "$(VAR1)$(VAR2)/$(VAR3)" result)
132 MESSAGE(SEND_ERROR "Unquoted $(VAR) syntax is broken.")
135 # Obscure environment variable name
136 SET("ENV{x+(y)}" "Obscure environment variable value")
137 MESSAGE("Output: [$ENV{x+(y)}]")
138 IF(NOT "$ENV{x+(y)}" STREQUAL "Obscure environment variable value")
139 MESSAGE(SEND_ERROR "Environment variable \"ENV{x+(y)}\" does not work.")
142 # Make directories test
144 "${CMAKE_CURRENT_BINARY_DIR}/Includes"
145 "${CMAKE_CURRENT_BINARY_DIR}/Directory1"
146 "${CMAKE_CURRENT_BINARY_DIR}/Directory2"
149 # Write results to the file (test write file)
150 SET(file "${CMAKE_CURRENT_BINARY_DIR}/Includes/Values.h")
151 FILE(WRITE "${file}" "/* this file is generated */\n")
171 FILE(APPEND "${file}" "#define ${var} \"${${var}}\"\n")
174 # Verify that the file was created recently.
175 IF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
176 MESSAGE(FATAL_ERROR "IF(FILE_IS_NEWER) does not seem to work.")
177 ENDIF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
179 # Test configuration of the string
180 SET(TEST_DEFINED 123)
181 SET(TEST_NOT_DEFINED)
182 STRING(CONFIGURE "${infile}" infile+-/out @ONLY)
183 SET(infile "${infile+-/out}")
185 # Write include file to a file
186 STRING(REGEX REPLACE "includefile" "${file}" outfile "${infile}")
187 FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h" "${outfile}")
190 FILE(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/*")
191 MESSAGE("Files in ${CMAKE_CURRENT_SOURCE_DIR} are ${src_files}")
192 SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/[sS][!a-su-zA-Z0-9][^a-qs-zA-Z0-9]ing?ile*.cxx")
193 MESSAGE("Glob expression is [${expr}].")
194 FILE(GLOB src_files "${expr}")
195 MESSAGE("Globbed files [${src_files}].")
196 ADD_EXECUTABLE(StringFileTest ${src_files})
198 SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/../*.cxx")
199 FILE(GLOB_RECURSE rel_src_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "${expr}")
200 MESSAGE("Globbed files [${rel_src_files}].")
203 MESSAGE("Cheack if FOREACH with RANGE works")
204 MACRO(TEST_RANGE ARGS CHECK)
206 FOREACH(a RANGE ${ARGS})
209 MESSAGE("FOREACH with RANGE ${ARGS} produces ${r}")
210 IF("x${r}x" MATCHES "^x${CHECK}x$")
211 ELSE("x${r}x" MATCHES "^x${CHECK}x$")
212 MESSAGE(SEND_ERROR "The range resulted in: ${r} should be ${CHECK}")
213 ENDIF("x${r}x" MATCHES "^x${CHECK}x$")
215 TEST_RANGE("5" "0;1;2;3;4;5")
216 TEST_RANGE("3;5" "3;4;5")
217 TEST_RANGE("5;3" "5;4;3")
218 TEST_RANGE("3;10;2" "3;5;7;9")
219 TEST_RANGE("10;0;-3" "10;7;4;1")
221 # Test FOREACH IN signature
227 foreach(item IN LISTS list1 list2 ITEMS "" a "")
228 set(var_${item} "${var_${item}}x")
230 if(NOT "${var_}" STREQUAL "xxxxx")
231 message(FATAL_ERROR "count incorrect for \"\": [${var_}]")
233 if(NOT "${var_a}" STREQUAL "xxx")
234 message(FATAL_ERROR "count incorrect for \"a\": [${var_a}]")
236 if(NOT "${var_b}" STREQUAL "x")
237 message(FATAL_ERROR "count incorrect \"b\": [${var_b}]")