summaryrefslogtreecommitdiffstats
path: root/libcxx/test/libcxx/algorithms/half_positive.pass.cpp
blob: 178055cbbd017cf03c54922c9c48f361771709de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <algorithm>

// template <typename _Tp> _Tp __half_positive(const _Tp&);

// __half_positive divide integer number by 2 as unsigned number
// if it's safe to do so. It can be an important optimization for lower bound,
// for example.

#include <algorithm>
#include <cassert>
#include <limits>
#include <type_traits>

#include "test_macros.h"
#include "user_defined_integral.hpp"

namespace {

template <class IntType, class UnderlyingType = IntType>
TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) {
    return std::__half_positive(max_v) == max_v / 2;
}

}  // namespace

int main()
{
    {
        assert(test<char>());
        assert(test<int>());
        assert(test<long>());
        assert((test<UserDefinedIntegral<int>, int>()));
        assert(test<size_t>());
#if !defined(_LIBCPP_HAS_NO_INT128)
        assert(test<__int128_t>());
#endif  // !defined(_LIBCPP_HAS_NO_INT128)
    }

#if TEST_STD_VER >= 11
    {
        static_assert(test<char>(), "");
        static_assert(test<int>(), "");
        static_assert(test<long>(), "");
        static_assert(test<size_t>(), "");
#if !defined(_LIBCPP_HAS_NO_INT128)
        static_assert(test<__int128_t>(), "");
#endif  // !defined(_LIBCPP_HAS_NO_INT128)
    }
#endif // TEST_STD_VER >= 11
}
OpenPOWER on IntegriCloud