1 /* boost integer_traits.hpp tests
2 *
3 * Copyright Jens Maurer 2000
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * $Id$
9 *
10 * Revision history
11 * 2000-02-22 Small improvements by Beman Dawes
12 * 2000-06-27 Rework for better MSVC and BCC co-operation
13 */
14
15 #include <iostream>
16 #include <boost/integer_traits.hpp>
17 // use int64_t instead of long long for better portability
18 #include <boost/cstdint.hpp>
19
20 #include <boost/detail/lightweight_test.hpp>
21
22 /*
23 * General portability note:
24 * MSVC mis-compiles explicit function template instantiations.
25 * For example, f<A>() and f<B>() are both compiled to call f<A>().
26 * BCC is unable to implicitly convert a "const char *" to a std::string
27 * when using explicit function template instantiations.
28 *
29 * Therefore, avoid explicit function template instantiations.
30 */
31
32 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
make_char_numeric_for_streaming(T x)33 template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
34 namespace fix{
make_char_numeric_for_streaming(char c)35 inline int make_char_numeric_for_streaming(char c) { return c; }
make_char_numeric_for_streaming(signed char c)36 inline int make_char_numeric_for_streaming(signed char c) { return c; }
make_char_numeric_for_streaming(unsigned char c)37 inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
38 }
39 using namespace fix;
40 #else
make_char_numeric_for_streaming(T x)41 template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
make_char_numeric_for_streaming(char c)42 inline int make_char_numeric_for_streaming(char c) { return c; }
make_char_numeric_for_streaming(signed char c)43 inline int make_char_numeric_for_streaming(signed char c) { return c; }
make_char_numeric_for_streaming(unsigned char c)44 inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
45 #endif
46
47 template<class T>
runtest(const char * type,T)48 void runtest(const char * type, T)
49 {
50 typedef boost::integer_traits<T> traits;
51 std::cout << "Checking " << type
52 << "; min is " << make_char_numeric_for_streaming((traits::min)())
53 << ", max is " << make_char_numeric_for_streaming((traits::max)())
54 << std::endl;
55 BOOST_TEST(traits::is_specialized);
56 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
57 // MSVC++ 6.0 issues a LNK1179 error (duplicate comdat) when the compiler
58 // generates different symbol names with a very long common prefix:
59 // the dummy "&& true" disambiguates between the symbols generated by this
60 // BOOST_TEST instantiation and the preceding one.
61 BOOST_TEST(traits::is_integer && true);
62 #else
63 BOOST_TEST(traits::is_integer);
64 #endif
65 BOOST_TEST(traits::is_integral == true);
66 BOOST_TEST(traits::const_min == (traits::min)());
67 BOOST_TEST(traits::const_max == (traits::max)());
68 }
69
main(int,char * [])70 int main(int, char*[])
71 {
72 runtest("bool", bool());
73 runtest("char", char());
74 typedef signed char signed_char;
75 runtest("signed char", signed_char());
76 typedef unsigned char unsigned_char;
77 runtest("unsigned char", unsigned_char());
78 runtest("wchar_t", wchar_t());
79 runtest("short", short());
80 typedef unsigned short unsigned_short;
81 runtest("unsigned short", unsigned_short());
82 runtest("int", int());
83 typedef unsigned int unsigned_int;
84 runtest("unsigned int", unsigned_int());
85 runtest("long", long());
86 typedef unsigned long unsigned_long;
87 runtest("unsigned long", unsigned_long());
88 #ifndef BOOST_NO_INTEGRAL_INT64_T
89 //
90 // MS/Borland compilers can't support 64-bit member constants
91 // BeOS doesn't have specialisations for long long in SGI's <limits> header.
92 runtest("int64_t (possibly long long)", boost::int64_t());
93 runtest("uint64_t (possibly unsigned long long)", boost::uint64_t());
94 #else
95 std::cout << "Skipped int64_t and uint64_t" << std::endl;
96 #endif
97 // Some compilers don't pay attention to std:3.6.1/5 and issue a
98 // warning here if "return 0;" is omitted.
99 return boost::report_errors();
100 }
101
102