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