kwsysPlatformTestsCXX.cxx 10 KB

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