1 PROJECT(StringFileTest)
2 INCLUDE_DIRECTORIES(${StringFileTest_BINARY_DIR})
5 FILE(READ "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile)
8 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile_strings
9 LIMIT_COUNT 1 LIMIT_INPUT 1024 LIMIT_OUTPUT 1024
10 LENGTH_MINIMUM 10 LENGTH_MAXIMUM 23 REGEX include NEWLINE_CONSUME)
11 SET(infile_strings_goal "#include \"includefile\"\n")
12 IF("${infile_strings}" STREQUAL "${infile_strings_goal}")
13 MESSAGE("FILE(STRINGS) correctly read [${infile_strings}]")
14 ELSE("${infile_strings}" STREQUAL "${infile_strings_goal}")
16 "FILE(STRINGS) incorrectly read [${infile_strings}]")
17 ENDIF("${infile_strings}" STREQUAL "${infile_strings_goal}")
19 # test reading a file and getting its binary data as hex string
20 FILE(READ "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infilehex LIMIT 4 HEX)
21 IF(NOT "${infilehex}" STREQUAL "53313036")
23 "FILE(READ ... HEX) error, read: \"${infilehex}\", expected \"53313036\"")
24 ENDIF(NOT "${infilehex}" STREQUAL "53313036")
27 # test that FILE(STRINGS) also work with Intel hex and Motorola S-record files
28 # this file has been created with "sdcc main.c"
29 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.ihx" infile_strings REGEX INFO)
30 SET(infile_strings_goal "INFO:compiler\\[SDCC-HEX\\]")
31 IF("${infile_strings}" MATCHES "${infile_strings_goal}")
32 MESSAGE("FILE(STRINGS) correctly read from hex file [${infile_strings}]")
33 ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
35 "FILE(STRINGS) incorrectly read from hex file [${infile_strings}]")
36 ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
38 # this file has been created with "sdcc main.c --out-fmt-s19"
39 FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infile_strings REGEX INFO)
40 SET(infile_strings_goal "INFO:compiler\\[SDCC-SREC\\]")
41 IF("${infile_strings}" MATCHES "${infile_strings_goal}")
42 MESSAGE("FILE(STRINGS) correctly read from srec file [${infile_strings}]")
43 ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
45 "FILE(STRINGS) incorrectly read from srec file [${infile_strings}]")
46 ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
49 STRING(REGEX MATCH "[cC][mM][aA][kK][eE]" rmvar "CMake is great")
50 STRING(REGEX MATCHALL "[cC][mM][aA][kK][eE]" rmallvar "CMake is better than cmake or CMake")
51 STRING(REGEX REPLACE "[Aa][uU][tT][oO]([cC][oO][nN][fF]|[mM][aA][kK][eE])"
52 "CMake" rrepvar "People should use Autoconf and Automake")
53 STRING(COMPARE EQUAL "CMake" "Autoconf" nceqvar)
54 STRING(COMPARE EQUAL "CMake" "CMake" ceqvar)
55 STRING(COMPARE NOTEQUAL "CMake" "Autoconf" cneqvar)
56 STRING(COMPARE NOTEQUAL "CMake" "CMake" ncneqvar)
57 STRING(COMPARE LESS "before" "after" nclvar)
58 STRING(COMPARE LESS "max" "min" clvar)
59 STRING(COMPARE GREATER "before" "after" cgvar)
60 STRING(COMPARE GREATER "max" "min" ncgvar)
61 STRING(ASCII 67 109 97 107 101 savar)
62 STRING(TOUPPER "CMake" tuvar)
63 STRING(TOLOWER "CMake" tlvar)
64 STRING(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
66 IF("abc" STREQUAL "xyz")
67 MESSAGE(SEND_ERROR "Problem with the IF(STREQUAL), \"abc\" and \"xyz\" considered equal")
68 ENDIF("abc" STREQUAL "xyz")
70 IF("CMake is cool" MATCHES "(CMake) (is).+")
71 IF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
72 MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"CMake is cool\"")
73 ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
74 IF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
75 MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"CMake\"")
76 ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
77 IF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
78 MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected \"is\"")
79 ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
80 ELSE("CMake is cool" MATCHES "(CMake) (is).+")
81 MESSAGE(SEND_ERROR "Problem with the IF(MATCHES), no match found")
82 ENDIF("CMake is cool" MATCHES "(CMake) (is).+")
84 STRING(REGEX MATCH "(People).+CMake" matchResultVar "People should use CMake")
85 IF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
86 MESSAGE(SEND_ERROR "STRING(REGEX MATCH) problem: \"${matchResultVar}\", expected \"People should use CMake\"")
87 ENDIF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
88 IF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
89 MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"People should use CMake\"")
90 ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
91 IF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
92 MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"People\"")
93 ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
94 IF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
95 MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected empty string")
96 ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
102 STRING(STRIP "ST2 " ST2)
103 STRING(STRIP " ST3" ST3)
105 FOREACH(var ST1 ST2 ST3)
106 IF("${var}" STREQUAL "${${var}}")
107 MESSAGE("[${var}] == [${${var}}]")
108 ELSE("${var}" STREQUAL "${${var}}")
109 MESSAGE(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
110 ENDIF("${var}" STREQUAL "${${var}}")
113 STRING(SUBSTRING "People should use Autoconf" 7 10 substringres)
114 SET(substringres "Everybody ${substringres} CMake")
116 STRING(LENGTH ${substringres} lengthres)
118 FILE(RELATIVE_PATH relpath "/usr/local/bin" "/usr/X11R6/bin/xnest")
121 SET(var "\\ \" \ \t \n \r \# \( \) \0")
122 MESSAGE("Output: [${var}]")
123 SET(var \\ \" \ \t \n \r \# \( \) \0)
124 MESSAGE("Output: [${var}]")
126 # Make-style unquoted argument test
127 SET(var $(VAR1)$(VAR2)/$(VAR3))
128 MESSAGE("Output: [${var}]")
129 STRING(COMPARE EQUAL "${var}" "$(VAR1)$(VAR2)/$(VAR3)" result)
131 MESSAGE(SEND_ERROR "Unquoted $(VAR) syntax is broken.")
134 # Make directories test
136 "${CMAKE_CURRENT_BINARY_DIR}/Includes"
137 "${CMAKE_CURRENT_BINARY_DIR}/Directory1"
138 "${CMAKE_CURRENT_BINARY_DIR}/Directory2"
141 # Write results to the file (test write file)
142 SET(file "${CMAKE_CURRENT_BINARY_DIR}/Includes/Values.h")
143 FILE(WRITE "${file}" "/* this file is generated */\n")
163 FILE(APPEND "${file}" "#define ${var} \"${${var}}\"\n")
166 # Verify that the file was created recently.
167 IF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
168 MESSAGE(FATAL_ERROR "IF(FILE_IS_NEWER) does not seem to work.")
169 ENDIF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
171 # Test configuration of the string
172 SET(TEST_DEFINED 123)
173 SET(TEST_NOT_DEFINED)
174 STRING(CONFIGURE "${infile}" infile+-/out @ONLY)
175 SET(infile "${infile+-/out}")
177 # Write include file to a file
178 STRING(REGEX REPLACE "includefile" "${file}" outfile "${infile}")
179 FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h" "${outfile}")
182 FILE(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/*")
183 MESSAGE("Files in ${CMAKE_CURRENT_SOURCE_DIR} are ${src_files}")
184 SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/[sS][!a-su-zA-Z0-9][^a-qs-zA-Z0-9]ing?ile*.cxx")
185 MESSAGE("Glob expression is [${expr}].")
186 FILE(GLOB src_files "${expr}")
187 MESSAGE("Globbed files [${src_files}].")
188 ADD_EXECUTABLE(StringFileTest ${src_files})
190 SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/../*.cxx")
191 FILE(GLOB_RECURSE rel_src_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "${expr}")
192 MESSAGE("Globbed files [${rel_src_files}].")
195 MESSAGE("Cheack if FOREACH with RANGE works")
196 MACRO(TEST_RANGE ARGS CHECK)
198 FOREACH(a RANGE ${ARGS})
201 MESSAGE("FOREACH with RANGE ${ARGS} produces ${r}")
202 IF("x${r}x" MATCHES "^x${CHECK}x$")
203 ELSE("x${r}x" MATCHES "^x${CHECK}x$")
204 MESSAGE(SEND_ERROR "The range resulted in: ${r} should be ${CHECK}")
205 ENDIF("x${r}x" MATCHES "^x${CHECK}x$")
207 TEST_RANGE("5" "0;1;2;3;4;5")
208 TEST_RANGE("3;5" "3;4;5")
209 TEST_RANGE("5;3" "5;4;3")
210 TEST_RANGE("3;10;2" "3;5;7;9")
211 TEST_RANGE("10;0;-3" "10;7;4;1")