to_ullong.pass.cpp revision c52f43e72dfcea03037729649da84c23b3beb04a
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. 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 <climits>
15#include <cassert>
16
17template <std::size_t N>
18void test_to_ullong()
19{
20    const std::size_t M = sizeof(unsigned long long) * CHAR_BIT < N ? sizeof(unsigned long long) * CHAR_BIT : N;
21    const std::size_t X = M == 0 ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
22    const unsigned long long max = M == 0 ? 0 : (unsigned long long)(-1) >> X;
23    unsigned long long tests[] = {0,
24                           std::min<unsigned long long>(1, max),
25                           std::min<unsigned long long>(2, max),
26                           std::min<unsigned long long>(3, max),
27                           std::min(max, max-3),
28                           std::min(max, max-2),
29                           std::min(max, max-1),
30                           max};
31    for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
32    {
33        unsigned long long j = tests[i];
34        std::bitset<N> v(j);
35        assert(j == v.to_ullong());
36    }
37}
38
39int main()
40{
41    test_to_ullong<0>();
42    test_to_ullong<1>();
43    test_to_ullong<31>();
44    test_to_ullong<32>();
45    test_to_ullong<33>();
46    test_to_ullong<63>();
47    test_to_ullong<64>();
48    test_to_ullong<65>();
49    test_to_ullong<1000>();
50}
51