kwsysPlatformTestsCXX.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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_LONG_LONG
  102. long long f(long long n) { return n; }
  103. int main()
  104. {
  105. long long n = 0;
  106. return static_cast<int>(f(n));
  107. }
  108. #endif
  109. #ifdef TEST_KWSYS_CXX_HAS___INT64
  110. __int64 f(__int64 n) { return n; }
  111. int main()
  112. {
  113. __int64 n = 0;
  114. return static_cast<int>(f(n));
  115. }
  116. #endif
  117. #ifdef TEST_KWSYS_CXX_HAS_NULL_TEMPLATE_ARGS
  118. template <class T> class A;
  119. template <class T> int f(A<T>&);
  120. template <class T> class A
  121. {
  122. public:
  123. // "friend int f<>(A<T>&)" would conform
  124. friend int f(A<T>&);
  125. private:
  126. int x;
  127. };
  128. template <class T> int f(A<T>& a) { return a.x = 0; }
  129. template int f(A<int>&);
  130. int main()
  131. {
  132. A<int> a;
  133. return f(a);
  134. }
  135. #endif
  136. #ifdef TEST_KWSYS_CXX_HAS_MEMBER_TEMPLATES
  137. template <class U>
  138. class A
  139. {
  140. public:
  141. U u;
  142. A(): u(0) {}
  143. template <class V> V m(V* p) { return *p = u; }
  144. };
  145. int main()
  146. {
  147. A<short> a;
  148. int s = 1;
  149. return a.m(&s);
  150. }
  151. #endif
  152. #ifdef TEST_KWSYS_CXX_HAS_FULL_SPECIALIZATION
  153. template <class T> struct A {};
  154. template <> struct A<int*>
  155. {
  156. static int f() { return 0; }
  157. };
  158. int main() { return A<int*>::f(); }
  159. #endif
  160. #ifdef TEST_KWSYS_CXX_HAS_ARGUMENT_DEPENDENT_LOOKUP
  161. namespace N
  162. {
  163. class A {};
  164. int f(A*) { return 0; }
  165. }
  166. void f(void*);
  167. int main()
  168. {
  169. N::A* a = 0;
  170. return f(a);
  171. }
  172. #endif
  173. #ifdef TEST_KWSYS_STL_HAS_ITERATOR_TRAITS
  174. #include <iterator>
  175. #include <list>
  176. void f(kwsys_stl::iterator_traits<kwsys_stl::list<int>::iterator>::iterator_category const&) {}
  177. int main() { return 0; }
  178. #endif
  179. #ifdef TEST_KWSYS_STL_HAS_ITERATOR_CATEGORY
  180. #include <iterator>
  181. #include <list>
  182. void f(kwsys_stl::list<int>::iterator x) { kwsys_stl::iterator_category(x); }
  183. int main() { return 0; }
  184. #endif
  185. #ifdef TEST_KWSYS_STL_HAS___ITERATOR_CATEGORY
  186. #include <iterator>
  187. #include <list>
  188. void f(kwsys_stl::list<int>::iterator x) { kwsys_stl::__iterator_category(x); }
  189. int main() { return 0; }
  190. #endif
  191. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_TEMPLATE
  192. #include <memory>
  193. template <class Alloc>
  194. void f(const Alloc&)
  195. {
  196. typedef typename Alloc::size_type alloc_size_type;
  197. }
  198. int main()
  199. {
  200. f(kwsys_stl::allocator<char>());
  201. return 0;
  202. }
  203. #endif
  204. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_NONTEMPLATE
  205. #include <memory>
  206. void f(kwsys_stl::allocator::size_type const&) {}
  207. int main() { return 0; }
  208. #endif
  209. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_REBIND
  210. #include <memory>
  211. template <class T, class Alloc>
  212. void f(const T&, const Alloc&)
  213. {
  214. typedef typename Alloc::template rebind<T>::other alloc_type;
  215. }
  216. int main()
  217. {
  218. f(0, kwsys_stl::allocator<char>());
  219. return 0;
  220. }
  221. #endif
  222. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_MAX_SIZE_ARGUMENT
  223. #include <memory>
  224. void f(kwsys_stl::allocator<char> const& a)
  225. {
  226. a.max_size(sizeof(int));
  227. }
  228. int main()
  229. {
  230. f(kwsys_stl::allocator<char>());
  231. return 0;
  232. }
  233. #endif
  234. #ifdef TEST_KWSYS_STL_HAS_ALLOCATOR_OBJECTS
  235. #include <vector>
  236. void f(kwsys_stl::vector<int> const& v1)
  237. {
  238. kwsys_stl::vector<int>(1, 1, v1.get_allocator());
  239. }
  240. int main()
  241. {
  242. f(kwsys_stl::vector<int>());
  243. return 0;
  244. }
  245. #endif
  246. #ifdef TEST_KWSYS_STAT_HAS_ST_MTIM
  247. #include <sys/types.h>
  248. #include <sys/stat.h>
  249. #include <unistd.h>
  250. int main()
  251. {
  252. struct stat stat1;
  253. (void)stat1.st_mtim.tv_sec;
  254. (void)stat1.st_mtim.tv_nsec;
  255. return 0;
  256. }
  257. #endif
  258. #ifdef TEST_KWSYS_CXX_SAME_LONG_AND___INT64
  259. void function(long**) {}
  260. int main()
  261. {
  262. __int64** p = 0;
  263. function(p);
  264. return 0;
  265. }
  266. #endif
  267. #ifdef TEST_KWSYS_CXX_SAME_LONG_LONG_AND___INT64
  268. void function(long long**) {}
  269. int main()
  270. {
  271. __int64** p = 0;
  272. function(p);
  273. return 0;
  274. }
  275. #endif
  276. #ifdef TEST_KWSYS_CAN_CONVERT_UI64_TO_DOUBLE
  277. void function(double& l, unsigned __int64 const& r)
  278. {
  279. l = static_cast<double>(r);
  280. }
  281. int main()
  282. {
  283. double tTo = 0.0;
  284. unsigned __int64 tFrom = 0;
  285. function(tTo, tFrom);
  286. return 0;
  287. }
  288. #endif
  289. #ifdef TEST_KWSYS_IOS_HAVE_BINARY
  290. int test_binary(int, ...)
  291. {
  292. return 0;
  293. }
  294. int main()
  295. {
  296. return test_binary(1, kwsys_ios::ios::binary);
  297. }
  298. #endif
  299. #ifdef TEST_KWSYS_IOS_HAS_ISTREAM_LONG_LONG
  300. int test_istream(kwsys_ios::istream& is, long long& x)
  301. {
  302. return (is >> x)? 1:0;
  303. }
  304. int main()
  305. {
  306. long long x = 0;
  307. return test_istream(kwsys_ios::cin, x);
  308. }
  309. #endif
  310. #ifdef TEST_KWSYS_IOS_HAS_OSTREAM_LONG_LONG
  311. int test_ostream(kwsys_ios::ostream& os, long long x)
  312. {
  313. return (os << x)? 1:0;
  314. }
  315. int main()
  316. {
  317. long long x = 0;
  318. return test_ostream(kwsys_ios::cout, x);
  319. }
  320. #endif
  321. #ifdef TEST_KWSYS_IOS_HAS_ISTREAM___INT64
  322. int test_istream(kwsys_ios::istream& is, __int64& x)
  323. {
  324. return (is >> x)? 1:0;
  325. }
  326. int main()
  327. {
  328. __int64 x = 0;
  329. return test_istream(kwsys_ios::cin, x);
  330. }
  331. #endif
  332. #ifdef TEST_KWSYS_IOS_HAS_OSTREAM___INT64
  333. int test_ostream(kwsys_ios::ostream& os, __int64 x)
  334. {
  335. return (os << x)? 1:0;
  336. }
  337. int main()
  338. {
  339. __int64 x = 0;
  340. return test_ostream(kwsys_ios::cout, x);
  341. }
  342. #endif
  343. #ifdef TEST_KWSYS_CHAR_IS_SIGNED
  344. /* Return 0 for char signed and 1 for char unsigned. */
  345. int main()
  346. {
  347. unsigned char uc = 255;
  348. return (*reinterpret_cast<char*>(&uc) < 0)?0:1;
  349. }
  350. #endif
  351. #ifdef TEST_KWSYS_LFS_WORKS
  352. /* Return 0 when LFS is available and 1 otherwise. */
  353. #define _LARGEFILE_SOURCE
  354. #define _LARGEFILE64_SOURCE
  355. #define _LARGE_FILES
  356. #define _FILE_OFFSET_BITS 64
  357. #include <sys/types.h>
  358. #include <sys/stat.h>
  359. #include <assert.h>
  360. #if KWSYS_CXX_HAS_CSTDIO
  361. # include <cstdio>
  362. #endif
  363. #include <stdio.h>
  364. int main(int, char **argv)
  365. {
  366. /* check that off_t can hold 2^63 - 1 and perform basic operations... */
  367. #define OFF_T_64 (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  368. if (OFF_T_64 % 2147483647 != 1)
  369. return 1;
  370. // stat breaks on SCO OpenServer
  371. struct stat buf;
  372. stat( argv[0], &buf );
  373. if (!S_ISREG(buf.st_mode))
  374. return 2;
  375. FILE *file = fopen( argv[0], "r" );
  376. off_t offset = ftello( file );
  377. fseek( file, offset, SEEK_CUR );
  378. fclose( file );
  379. return 0;
  380. }
  381. #endif
  382. #ifdef TEST_KWSYS_CXX_HAS_SETENV
  383. #include <stdlib.h>
  384. int main()
  385. {
  386. return setenv("A", "B", 1);
  387. }
  388. #endif
  389. #ifdef TEST_KWSYS_CXX_HAS_UNSETENV
  390. #include <stdlib.h>
  391. int main()
  392. {
  393. unsetenv("A");
  394. return 0;
  395. }
  396. #endif
  397. #ifdef TEST_KWSYS_CXX_HAS_ENVIRON_IN_STDLIB_H
  398. #include <stdlib.h>
  399. int main()
  400. {
  401. char* e = environ[0];
  402. return e? 0:1;
  403. }
  404. #endif
  405. #ifdef TEST_KWSYS_CXX_HAS_RLIMIT64
  406. # if defined(KWSYS_HAS_LFS)
  407. # define _LARGEFILE_SOURCE
  408. # define _LARGEFILE64_SOURCE
  409. # define _LARGE_FILES
  410. # define _FILE_OFFSET_BITS 64
  411. # endif
  412. # include <sys/resource.h>
  413. int main()
  414. {
  415. struct rlimit64 rlim;
  416. return getrlimit64(0,&rlim);
  417. }
  418. #endif
  419. #ifdef TEST_KWSYS_CXX_HAS_ATOLL
  420. #include <stdlib.h>
  421. int main()
  422. {
  423. const char *str="1024";
  424. return static_cast<int>(atoll(str));
  425. }
  426. #endif
  427. #ifdef TEST_KWSYS_CXX_HAS_ATOL
  428. #include <stdlib.h>
  429. int main()
  430. {
  431. const char *str="1024";
  432. return static_cast<int>(atol(str));
  433. }
  434. #endif
  435. #ifdef TEST_KWSYS_CXX_HAS__ATOI64
  436. #include <stdlib.h>
  437. int main()
  438. {
  439. const char *str="1024";
  440. return static_cast<int>(_atoi64(str));
  441. }
  442. #endif
  443. #ifdef TEST_KWSYS_CXX_TYPE_INFO
  444. /* Collect fundamental type information and save it to a CMake script. */
  445. /* Include limits.h to get macros indicating long long and __int64.
  446. Note that certain compilers need special macros to define these
  447. macros in limits.h. */
  448. #if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS)
  449. # define _MSC_EXTENSIONS
  450. #endif
  451. #if defined(__GNUC__) && __GNUC__ < 3
  452. # define _GNU_SOURCE
  453. #endif
  454. #include <limits.h>
  455. #include <stdio.h>
  456. #include <string.h>
  457. /* Due to shell differences and limitations of ADD_DEFINITIONS the
  458. KWSYS_CXX_TYPE_INFO_FILE macro will sometimes have double quotes
  459. and sometimes not. This macro will make sure the value is treated
  460. as a double-quoted string. */
  461. #define TO_STRING(x) TO_STRING0(x)
  462. #define TO_STRING0(x) TO_STRING1(x)
  463. #define TO_STRING1(x) #x
  464. void f() {}
  465. int main()
  466. {
  467. /* Construct the output file name. Some preprocessors will add an
  468. extra level of double quotes, so strip them. */
  469. char fbuf[] = TO_STRING(KWSYS_CXX_TYPE_INFO_FILE);
  470. char* fname = fbuf;
  471. if(fname[0] == '"')
  472. {
  473. ++fname;
  474. int len = static_cast<int>(strlen(fname));
  475. if(len > 0 && fname[len-1] == '"')
  476. {
  477. fname[len-1] = 0;
  478. }
  479. }
  480. /* Try to open the output file. */
  481. if(FILE* fout = fopen(fname, "w"))
  482. {
  483. /* Set the size of standard types. */
  484. fprintf(fout, "SET(KWSYS_SIZEOF_CHAR %d)\n", static_cast<int>(sizeof(char)));
  485. fprintf(fout, "SET(KWSYS_SIZEOF_SHORT %d)\n", static_cast<int>(sizeof(short)));
  486. fprintf(fout, "SET(KWSYS_SIZEOF_INT %d)\n", static_cast<int>(sizeof(int)));
  487. fprintf(fout, "SET(KWSYS_SIZEOF_LONG %d)\n", static_cast<int>(sizeof(long)));
  488. /* Set the size of some non-standard but common types. */
  489. /* Check for a limits.h macro for long long to see if the type exists. */
  490. #if defined(LLONG_MAX) || defined(LONG_LONG_MAX) || defined(LONGLONG_MAX)
  491. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG %d)\n", static_cast<int>(sizeof(long long)));
  492. #else
  493. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG 0) # No long long available.\n");
  494. #endif
  495. /* Check for a limits.h macro for __int64 to see if the type exists. */
  496. #if defined(_I64_MIN)
  497. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 %d)\n", static_cast<int>(sizeof(__int64)));
  498. #else
  499. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 0) # No __int64 available.\n");
  500. #endif
  501. /* Set the size of some pointer types. */
  502. fprintf(fout, "SET(KWSYS_SIZEOF_PDATA %d)\n", static_cast<int>(sizeof(void*)));
  503. fprintf(fout, "SET(KWSYS_SIZEOF_PFUNC %d)\n", static_cast<int>(sizeof(&f)));
  504. /* Set whether the native type "char" is signed or unsigned. */
  505. unsigned char uc = 255;
  506. fprintf(fout, "SET(KWSYS_CHAR_IS_SIGNED %d)\n",
  507. (*reinterpret_cast<char*>(&uc) < 0)?1:0);
  508. fclose(fout);
  509. return 0;
  510. }
  511. else
  512. {
  513. fprintf(stderr, "Failed to write fundamental type info to \"%s\".\n",
  514. fname);
  515. return 1;
  516. }
  517. }
  518. #endif