1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // test unsigned long to_ulong() const;
11
12 #include <bitset>
13 #include <algorithm>
14 #include <type_traits>
15 #include <limits>
16 #include <climits>
17 #include <cassert>
18
19 template <std::size_t N>
test_to_ulong()20 void test_to_ulong()
21 {
22 const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N;
23 const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
24 const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
25 const std::size_t max = is_M_zero ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
26 std::size_t tests[] = {0,
27 std::min<std::size_t>(1, max),
28 std::min<std::size_t>(2, max),
29 std::min<std::size_t>(3, max),
30 std::min(max, max-3),
31 std::min(max, max-2),
32 std::min(max, max-1),
33 max};
34 for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
35 {
36 std::size_t j = tests[i];
37 std::bitset<N> v(j);
38 assert(j == v.to_ulong());
39 }
40 }
41
main()42 int main()
43 {
44 test_to_ulong<0>();
45 test_to_ulong<1>();
46 test_to_ulong<31>();
47 test_to_ulong<32>();
48 test_to_ulong<33>();
49 test_to_ulong<63>();
50 test_to_ulong<64>();
51 test_to_ulong<65>();
52 test_to_ulong<1000>();
53 }
54