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 long to_ullong() const;
11
12 #include <bitset>
13 #include <algorithm>
14 #include <type_traits>
15 #include <climits>
16 #include <cassert>
17
18 template <std::size_t N>
test_to_ullong()19 void test_to_ullong()
20 {
21 const std::size_t M = sizeof(unsigned long long) * CHAR_BIT < N ? sizeof(unsigned long long) * CHAR_BIT : N;
22 const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
23 const std::size_t X = is_M_zero ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
24 const unsigned long long max = is_M_zero ? 0 : (unsigned long long)(-1) >> X;
25 unsigned long long tests[] = {0,
26 std::min<unsigned long long>(1, max),
27 std::min<unsigned long long>(2, max),
28 std::min<unsigned long long>(3, max),
29 std::min(max, max-3),
30 std::min(max, max-2),
31 std::min(max, max-1),
32 max};
33 for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
34 {
35 unsigned long long j = tests[i];
36 std::bitset<N> v(j);
37 assert(j == v.to_ullong());
38 }
39 }
40
main()41 int main()
42 {
43 test_to_ullong<0>();
44 test_to_ullong<1>();
45 test_to_ullong<31>();
46 test_to_ullong<32>();
47 test_to_ullong<33>();
48 test_to_ullong<63>();
49 test_to_ullong<64>();
50 test_to_ullong<65>();
51 test_to_ullong<1000>();
52 }
53