Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/metaprogramming/test_enableIf.cpp
41145 views
1
/*
2
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "memory/allStatic.hpp"
27
#include "metaprogramming/enableIf.hpp"
28
#include "utilities/debug.hpp"
29
#include <type_traits>
30
#include "unittest.hpp"
31
32
class EnableIfTest: AllStatic {
33
class A: AllStatic {
34
public:
35
template <bool condition>
36
static typename EnableIf<condition, char>::type test();
37
template <bool condition>
38
static typename EnableIf<!condition, long>::type test();
39
};
40
41
static const bool A_test_true_is_char = sizeof(A::test<true>()) == sizeof(char);
42
STATIC_ASSERT(A_test_true_is_char);
43
44
static const bool A_test_false_is_long = sizeof(A::test<false>()) == sizeof(long);
45
STATIC_ASSERT(A_test_false_is_long);
46
};
47
48
template<typename T, ENABLE_IF(std::is_integral<T>::value)>
49
static T sub1(T x) { return x - 1; }
50
51
TEST(TestEnableIf, one_decl_and_def) {
52
EXPECT_EQ(15, sub1(16));
53
}
54
55
template<typename T, ENABLE_IF(std::is_integral<T>::value)>
56
static T sub2(T x);
57
58
template<typename T, ENABLE_IF_SDEFN(std::is_integral<T>::value)>
59
T sub2(T x) { return x - 2; }
60
61
TEST(TestEnableIf, separate_decl_and_def) {
62
EXPECT_EQ(14, sub2(16));
63
}
64
65
template<typename T>
66
struct TestEnableIfNested {
67
template<typename U, ENABLE_IF(std::is_integral<U>::value)>
68
static U sub1(U x);
69
};
70
71
template<typename T>
72
template<typename U, ENABLE_IF_SDEFN(std::is_integral<U>::value)>
73
U TestEnableIfNested<T>::sub1(U x) { return x - 1; }
74
75
TEST(TestEnableIf, nested_separate_decl_and_def) {
76
EXPECT_EQ(15, TestEnableIfNested<void>::sub1(16));
77
}
78
79
// Demonstrate workaround for non-dependent condition.
80
template<typename T>
81
struct TestEnableIfNonDependent {
82
// Dependent is used to make the ENABLE_IF condition dependent on
83
// the type parameters for this function.
84
template<typename Dependent = T, ENABLE_IF(std::is_same<int, Dependent>::value)>
85
static T value() { return T{}; }
86
static int instantiate() { return 5; }
87
};
88
89
TEST(TestEnableIf, non_dependent) {
90
EXPECT_EQ(int{}, TestEnableIfNonDependent<int>::value());
91
// This fails to compile if the ENABLE_IF for value() directly uses
92
// T rather than indirectly via Dependent.
93
EXPECT_EQ(5, TestEnableIfNonDependent<void>::instantiate());
94
}
95
96