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