kwsysPlatformTestsCXX.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. // Setup for tests that use result of stl namespace test.
  11. #if defined(KWSYS_STL_HAVE_STD)
  12. # if KWSYS_STL_HAVE_STD
  13. # define kwsys_stl std
  14. # else
  15. # define kwsys_stl
  16. # endif
  17. #endif
  18. // Setup for tests that use iostreams.
  19. #if defined(KWSYS_IOS_USE_ANSI) && defined(KWSYS_IOS_HAVE_STD)
  20. # if defined(_MSC_VER)
  21. # pragma warning (push,1)
  22. # endif
  23. # if KWSYS_IOS_USE_ANSI
  24. # include <iostream>
  25. # else
  26. # include <iostream.h>
  27. # endif
  28. # if defined(_MSC_VER)
  29. # pragma warning (pop)
  30. # endif
  31. # if KWSYS_IOS_HAVE_STD
  32. # define kwsys_ios std
  33. # else
  34. # define kwsys_ios
  35. # endif
  36. #endif
  37. #ifdef TEST_KWSYS_STL_HAVE_STD
  38. #include <list>
  39. void f(std ::list<int>*) {}
  40. int main() { return 0; }
  41. #endif
  42. #ifdef TEST_KWSYS_IOS_USE_ANSI
  43. #include <iosfwd>
  44. int main() { return 0; }
  45. #endif
  46. #ifdef TEST_KWSYS_IOS_HAVE_STD
  47. #include <iosfwd>
  48. void f(std ::ostream*) {}
  49. int main() { return 0; }
  50. #endif
  51. #ifdef TEST_KWSYS_IOS_USE_SSTREAM
  52. #include <sstream>
  53. #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ == 96
  54. # error "GCC 2.96 stringstream is buggy"
  55. #endif
  56. int main()
  57. {
  58. std ::ostringstream ostr;
  59. ostr << "hello";
  60. if(ostr.str().size() == 5)
  61. {
  62. return 0;
  63. }
  64. return -1;
  65. }
  66. #endif
  67. #ifdef TEST_KWSYS_IOS_USE_STRSTREAM_H
  68. #include <strstream.h>
  69. int main() { return 0; }
  70. #endif
  71. #ifdef TEST_KWSYS_IOS_USE_STRSTREA_H
  72. #include <strstrea.h>
  73. int main() { return 0; }
  74. #endif
  75. #ifdef TEST_KWSYS_STL_STRING_HAVE_OSTREAM
  76. # include <iostream.h>
  77. # include <string>
  78. void f(ostream& os, const kwsys_stl::string& s) { os << s; }
  79. int main() { return 0; }
  80. #endif
  81. #ifdef TEST_KWSYS_STL_STRING_HAVE_ISTREAM
  82. # include <iostream.h>
  83. # include <string>
  84. void f(istream& is, kwsys_stl::string& s) { is >> s; }
  85. int main() { return 0; }
  86. #endif
  87. #ifdef TEST_KWSYS_STL_STRING_HAVE_NEQ_CHAR
  88. # include <string>
  89. bool f(const kwsys_stl::string& s) { return s != ""; }
  90. int main() { return 0; }
  91. #endif
  92. #ifdef TEST_KWSYS_CXX_HAS_CSTDIO
  93. #include <cstdio>
  94. int main() { return 0; }
  95. #endif
  96. #ifdef TEST_KWSYS_CXX_HAS_CSTDDEF
  97. #include <cstddef>
  98. void f(size_t) {}
  99. int main() { return 0; }
  100. #endif
  101. #ifdef TEST_KWSYS_CXX_HAS_NULL_TEMPLATE_ARGS
  102. template <class T> class A;
  103. template <class T> int f(A<T>&);
  104. template <class T> class A
  105. {
  106. public:
  107. // "friend int f<>(A<T>&)" would conform
  108. friend int f(A<T>&);
  109. private:
  110. int x;
  111. };
  112. template <class T> int f(A<T>& a) { return a.x = 0; }
  113. template int f(A<int>&);
  114. int main()
  115. {
  116. A<int> a;
  117. return f(a);
  118. }
  119. #endif
  120. #ifdef TEST_KWSYS_CXX_HAS_MEMBER_TEMPLATES
  121. template <class U>
  122. class A
  123. {
  124. public:
  125. U u;
  126. A(): u(0) {}
  127. template <class V> V m(V* p) { return *p = u; }
  128. };
  129. int main()
  130. {
  131. A<short> a;
  132. int s = 1;
  133. return a.m(&s);
  134. }
  135. #endif
  136. #ifdef TEST_KWSYS_CXX_HAS_FULL_SPECIALIZATION
  137. template <class T> struct A {};
  138. template <> struct A<int*>
  139. {
  140. static int f() { return 0; }
  141. };
  142. int main() { return A<int*>::f(); }
  143. #endif
  144. #ifdef TEST_KWSYS_CXX_HAS_ARGUMENT_DEPENDENT_LOOKUP
  145. namespace N
  146. {
  147. class A {};
  148. int f(A*) { return 0; }
  149. }
  150. void f(void*);
  151. int main()
  152. {
  153. N::A* a = 0;
  154. return f(a);
  155. }
  156. #endif
  157. #ifdef TEST_KWSYS_STL_HAS_ITERATOR_TRAITS
  158. #include <iterator>
  159. #include <list>
  160. void f(kwsys_stl::iterator_traits<kwsys_stl::list<int>::iterator>::iterator_category const&) {}
  161. int main() { return 0; }
  162. #endif
  163. #ifdef TEST_KWSYS_STL_HAS_ITERATOR_CATEGORY
  164. #include <iterator>
  165. #include <list>
  166. void f(kwsys_stl::list<int>::iterator x) { kwsys_stl::iterator_category(x); }
  167. int main() { return 0; }
  168. #endif
  169. #ifdef TEST_KWSYS_STL_HAS___ITERATOR_CATEGORY
  170. #include <iterator>
  171. #include <list>
  172. void f(kwsys_stl::list<int>::iterator x) { kwsys_stl::__iterator_category(x); }
  173. int main() { return 0; }
  174. #endif
  175. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_TEMPLATE
  176. #include <memory>
  177. template <class Alloc>
  178. void f(const Alloc&)
  179. {
  180. typedef typename Alloc::size_type alloc_size_type;
  181. }
  182. int main()
  183. {
  184. f(kwsys_stl::allocator<char>());
  185. return 0;
  186. }
  187. #endif
  188. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_NONTEMPLATE
  189. #include <memory>
  190. void f(kwsys_stl::allocator::size_type const&) {}
  191. int main() { return 0; }
  192. #endif
  193. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_REBIND
  194. #include <memory>
  195. template <class T, class Alloc>
  196. void f(const T&, const Alloc&)
  197. {
  198. typedef typename Alloc::template rebind<T>::other alloc_type;
  199. }
  200. int main()
  201. {
  202. f(0, kwsys_stl::allocator<char>());
  203. return 0;
  204. }
  205. #endif
  206. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_MAX_SIZE_ARGUMENT
  207. #include <memory>
  208. void f(kwsys_stl::allocator<char> const& a)
  209. {
  210. a.max_size(sizeof(int));
  211. }
  212. int main()
  213. {
  214. f(kwsys_stl::allocator<char>());
  215. return 0;
  216. }
  217. #endif
  218. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_OBJECTS
  219. #include <vector>
  220. void f(kwsys_stl::vector<int> const& v1)
  221. {
  222. kwsys_stl::vector<int>(1, 1, v1.get_allocator());
  223. }
  224. int main()
  225. {
  226. f(kwsys_stl::vector<int>());
  227. return 0;
  228. }
  229. #endif
  230. #ifdef TEST_KWSYS_STAT_HAS_ST_MTIM
  231. #include <sys/types.h>
  232. #include <sys/stat.h>
  233. #include <unistd.h>
  234. int main()
  235. {
  236. struct stat stat1;
  237. (void)stat1.st_mtim.tv_sec;
  238. (void)stat1.st_mtim.tv_nsec;
  239. return 0;
  240. }
  241. #endif
  242. #ifdef TEST_KWSYS_CXX_SAME_LONG_AND___INT64
  243. void function(long**) {}
  244. int main()
  245. {
  246. __int64** p = 0;
  247. function(p);
  248. return 0;
  249. }
  250. #endif
  251. #ifdef TEST_KWSYS_CXX_SAME_LONG_LONG_AND___INT64
  252. void function(long long**) {}
  253. int main()
  254. {
  255. __int64** p = 0;
  256. function(p);
  257. return 0;
  258. }
  259. #endif
  260. #ifdef TEST_KWSYS_CAN_CONVERT_UI64_TO_DOUBLE
  261. void function(double& l, unsigned __int64 const& r)
  262. {
  263. l = static_cast<double>(r);
  264. }
  265. int main()
  266. {
  267. double tTo = 0.0;
  268. unsigned __int64 tFrom = 0;
  269. function(tTo, tFrom);
  270. return 0;
  271. }
  272. #endif
  273. #ifdef TEST_KWSYS_IOS_HAVE_BINARY
  274. int test_binary(int, ...)
  275. {
  276. return 0;
  277. }
  278. int main()
  279. {
  280. return test_binary(1, kwsys_ios::ios::binary);
  281. }
  282. #endif
  283. #ifdef TEST_KWSYS_IOS_HAS_ISTREAM_LONG_LONG
  284. int test_istream(kwsys_ios::istream& is, long long& x)
  285. {
  286. return (is >> x)? 1:0;
  287. }
  288. int main()
  289. {
  290. long long x = 0;
  291. return test_istream(kwsys_ios::cin, x);
  292. }
  293. #endif
  294. #ifdef TEST_KWSYS_IOS_HAS_OSTREAM_LONG_LONG
  295. int test_ostream(kwsys_ios::ostream& os, long long x)
  296. {
  297. return (os << x)? 1:0;
  298. }
  299. int main()
  300. {
  301. long long x = 0;
  302. return test_ostream(kwsys_ios::cout, x);
  303. }
  304. #endif
  305. #ifdef TEST_KWSYS_CHAR_IS_SIGNED
  306. /* Return 0 for char signed and 1 for char unsigned. */
  307. int main()
  308. {
  309. unsigned char uc = 255;
  310. return (*reinterpret_cast<char*>(&uc) < 0)?0:1;
  311. }
  312. #endif
  313. #ifdef TEST_KWSYS_LFS_WORKS
  314. /* Return 0 when LFS is available and 1 otherwise. */
  315. #define _LARGEFILE_SOURCE
  316. #define _LARGEFILE64_SOURCE
  317. #define _LARGE_FILES
  318. #define _FILE_OFFSET_BITS 64
  319. #include <sys/types.h>
  320. #include <sys/stat.h>
  321. #include <assert.h>
  322. #if KWSYS_CXX_HAS_CSTDIO
  323. # include <cstdio>
  324. #endif
  325. #include <stdio.h>
  326. int main(int, char **argv)
  327. {
  328. /* check that off_t can hold 2^63 - 1 and perform basic operations... */
  329. #define OFF_T_64 (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  330. if (OFF_T_64 % 2147483647 != 1)
  331. return 1;
  332. // stat breaks on SCO OpenServer
  333. struct stat buf;
  334. stat( argv[0], &buf );
  335. if (!S_ISREG(buf.st_mode))
  336. return 2;
  337. FILE *file = fopen( argv[0], "r" );
  338. off_t offset = ftello( file );
  339. fseek( file, offset, SEEK_CUR );
  340. fclose( file );
  341. return 0;
  342. }
  343. #endif
  344. #ifdef TEST_KWSYS_CXX_TYPE_INFO
  345. /* Collect fundamental type information and save it to a CMake script. */
  346. /* Include limits.h to get macros indicating long long and __int64.
  347. Note that certain compilers need special macros to define these
  348. macros in limits.h. */
  349. #if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS)
  350. # define _MSC_EXTENSIONS
  351. #endif
  352. #if defined(__GNUC__) && __GNUC__ < 3
  353. # define _GNU_SOURCE
  354. #endif
  355. #include <limits.h>
  356. #include <stdio.h>
  357. #include <string.h>
  358. /* Due to shell differences and limitations of ADD_DEFINITIONS the
  359. KWSYS_CXX_TYPE_INFO_FILE macro will sometimes have double quotes
  360. and sometimes not. This macro will make sure the value is treated
  361. as a double-quoted string. */
  362. #define TO_STRING(x) TO_STRING0(x)
  363. #define TO_STRING0(x) TO_STRING1(x)
  364. #define TO_STRING1(x) #x
  365. void f() {}
  366. int main()
  367. {
  368. /* Construct the output file name. Some preprocessors will add an
  369. extra level of double quotes, so strip them. */
  370. char fbuf[] = TO_STRING(KWSYS_CXX_TYPE_INFO_FILE);
  371. char* fname = fbuf;
  372. if(fname[0] == '"')
  373. {
  374. ++fname;
  375. int len = static_cast<int>(strlen(fname));
  376. if(len > 0 && fname[len-1] == '"')
  377. {
  378. fname[len-1] = 0;
  379. }
  380. }
  381. /* Try to open the output file. */
  382. if(FILE* fout = fopen(fname, "w"))
  383. {
  384. /* Set the size of standard types. */
  385. fprintf(fout, "SET(KWSYS_SIZEOF_CHAR %d)\n", static_cast<int>(sizeof(char)));
  386. fprintf(fout, "SET(KWSYS_SIZEOF_SHORT %d)\n", static_cast<int>(sizeof(short)));
  387. fprintf(fout, "SET(KWSYS_SIZEOF_INT %d)\n", static_cast<int>(sizeof(int)));
  388. fprintf(fout, "SET(KWSYS_SIZEOF_LONG %d)\n", static_cast<int>(sizeof(long)));
  389. /* Set the size of some non-standard but common types. */
  390. /* Check for a limits.h macro for long long to see if the type exists. */
  391. #if defined(LLONG_MAX) || defined(LONG_LONG_MAX) || defined(LONGLONG_MAX)
  392. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG %d)\n", static_cast<int>(sizeof(long long)));
  393. #else
  394. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG 0) # No long long available.\n");
  395. #endif
  396. /* Check for a limits.h macro for __int64 to see if the type exists. */
  397. #if defined(_I64_MIN)
  398. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 %d)\n", static_cast<int>(sizeof(__int64)));
  399. #else
  400. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 0) # No __int64 available.\n");
  401. #endif
  402. /* Set the size of some pointer types. */
  403. fprintf(fout, "SET(KWSYS_SIZEOF_PDATA %d)\n", static_cast<int>(sizeof(void*)));
  404. fprintf(fout, "SET(KWSYS_SIZEOF_PFUNC %d)\n", static_cast<int>(sizeof(&f)));
  405. /* Set whether the native type "char" is signed or unsigned. */
  406. unsigned char uc = 255;
  407. fprintf(fout, "SET(KWSYS_CHAR_IS_SIGNED %d)\n",
  408. (*reinterpret_cast<char*>(&uc) < 0)?1:0);
  409. fclose(fout);
  410. return 0;
  411. }
  412. else
  413. {
  414. fprintf(stderr, "Failed to write fundamental type info to \"%s\".\n",
  415. fname);
  416. return 1;
  417. }
  418. }
  419. #endif