-
Notifications
You must be signed in to change notification settings - Fork 31
/
proxy_test.cc
217 lines (185 loc) · 9.92 KB
/
proxy_test.cc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <gtest/gtest.h>
#include "jni_bind.h"
#include "jni_test.h"
using ::jni::AsDecl_t;
using ::jni::Class;
using ::jni::ClassLoader;
using ::jni::GlobalObject;
using ::jni::LocalObject;
using ::jni::Proxy_t;
using ::jni::Return_t;
using ::jni::metaprogramming::TypeToTypeMapQuery_t;
using ::jni::test::JniTest;
namespace jni {
static constexpr Class kClass{
"kClass", jni::Method{"Bar", jni::Return<void>{}, jni::Params{}}};
static constexpr Class kClass2{
"kClass2",
jni::Method{"ReturnsObj", jni::Return{kClass}, jni::Params{}},
};
static constexpr ClassLoader kClassLoader{kDefaultClassLoader,
SupportedClassSet{kClass}};
////////////////////////////////////////////////////////////////////////////////
// Primitive Tests.
////////////////////////////////////////////////////////////////////////////////
static_assert(std::is_same_v<Proxy_t<void>::CDecl, void>);
static_assert(std::is_same_v<Proxy_t<jint>::CDecl, jint>);
static_assert(std::is_same_v<Proxy_t<jfloat>::CDecl, jfloat>);
static_assert(std::is_same_v<Proxy_t<jbyte>::CDecl, jbyte>);
static_assert(std::is_same_v<Proxy_t<jchar>::CDecl, jchar>);
static_assert(std::is_same_v<Proxy_t<jshort>::CDecl, jshort>);
static_assert(std::is_same_v<Proxy_t<jlong>::CDecl, jlong>);
static_assert(std::is_same_v<Proxy_t<jdouble>::CDecl, jdouble>);
static_assert(std::is_same_v<Proxy_t<const char*>::CDecl, jstring>);
static_assert(std::is_same_v<Proxy_t<std::string_view>::CDecl, jstring>);
static_assert(std::is_same_v<Proxy_t<std::string>::CDecl, jstring>);
////////////////////////////////////////////////////////////////////////////////
// Object Tests.
////////////////////////////////////////////////////////////////////////////////
void ReturnsObj() {
LocalObject<kClass> obj;
static_assert(std::is_same_v<Proxy_t<decltype(kClass)>::CDecl, jobject>);
static_assert(std::is_same_v<Proxy_t<decltype(obj)>::CDecl, jobject>);
static_assert(
std::is_same_v<Proxy_t<LocalObject<kClass, kClassLoader>>::CDecl,
jobject>);
}
////////////////////////////////////////////////////////////////////////////////
// Array Index Lookup Tests.
// For compile time argument viability see |method_selection_test.cc|.
// For runtime testing see |array_test.cc| and |local_array_test.cc|.
////////////////////////////////////////////////////////////////////////////////
// Rank 1 undecorated types.
static_assert(std::is_same_v<Index_t<jbyteArray>, jbyteArray>);
static_assert(std::is_same_v<Index_t<jcharArray>, jcharArray>);
static_assert(std::is_same_v<Index_t<jshortArray>, jshortArray>);
static_assert(std::is_same_v<Index_t<jintArray>, jintArray>);
static_assert(std::is_same_v<Index_t<jfloatArray>, jfloatArray>);
static_assert(std::is_same_v<Index_t<jdoubleArray>, jdoubleArray>);
static_assert(std::is_same_v<Index_t<jlongArray>, jlongArray>);
static_assert(std::is_same_v<Index_t<jbooleanArray>, jbooleanArray>);
static_assert(std::is_same_v<Index_t<jobjectArray>, jobjectArray>);
static_assert(std::is_same_v<Index_t<jarray>, jarray>);
// Rank 1 decorated declarations.
static_assert(std::is_same_v<Index_t<Array<jbyte>>, jbyteArray>);
static_assert(std::is_same_v<Index_t<Array<jchar>>, jcharArray>);
static_assert(std::is_same_v<Index_t<Array<jshort>>, jshortArray>);
static_assert(std::is_same_v<Index_t<Array<jint>>, jintArray>);
static_assert(std::is_same_v<Index_t<Array<jfloat>>, jfloatArray>);
static_assert(std::is_same_v<Index_t<Array<jdouble>>, jdoubleArray>);
static_assert(std::is_same_v<Index_t<Array<jlong>>, jlongArray>);
static_assert(std::is_same_v<Index_t<Array<jboolean>>, jbooleanArray>);
static_assert(std::is_same_v<Index_t<Array<jarray>>, jarray>);
static_assert(std::is_same_v<Index_t<Array<std::decay_t<decltype(kClass)>>>,
jobjectArray>);
// Rank 1 decorated arguments.
static_assert(std::is_same_v<Index_t<RefBase<jbooleanArray>>, jbooleanArray>);
static_assert(std::is_same_v<Index_t<RefBase<jbyteArray>>, jbyteArray>);
static_assert(std::is_same_v<Index_t<RefBase<jcharArray>>, jcharArray>);
static_assert(std::is_same_v<Index_t<RefBase<jshortArray>>, jshortArray>);
static_assert(std::is_same_v<Index_t<RefBase<jintArray>>, jintArray>);
static_assert(std::is_same_v<Index_t<RefBase<jfloatArray>>, jfloatArray>);
static_assert(std::is_same_v<Index_t<RefBase<jdoubleArray>>, jdoubleArray>);
static_assert(std::is_same_v<Index_t<RefBase<jlongArray>>, jlongArray>);
static_assert(std::is_same_v<Index_t<RefBase<jobjectArray>>, jobjectArray>);
static_assert(std::is_same_v<Index_t<RefBase<jarray>>, jarray>);
// Rank 2 decorated declarations.
static_assert(std::is_same_v<Index_t<Array<jbyte, 2>>, jbyteArray>);
static_assert(std::is_same_v<Index_t<Array<jchar, 2>>, jcharArray>);
static_assert(std::is_same_v<Index_t<Array<jshort, 2>>, jshortArray>);
static_assert(std::is_same_v<Index_t<Array<jint, 2>>, jintArray>);
static_assert(std::is_same_v<Index_t<Array<jfloat, 2>>, jfloatArray>);
static_assert(std::is_same_v<Index_t<Array<jdouble, 2>>, jdoubleArray>);
static_assert(std::is_same_v<Index_t<Array<jlong, 2>>, jlongArray>);
static_assert(std::is_same_v<Index_t<Array<jboolean, 2>>, jbooleanArray>);
static_assert(std::is_same_v<Index_t<Array<jarray, 2>>, jarray>);
static_assert(std::is_same_v<Index_t<Array<jobject, 2>>, jobjectArray>);
// Rank 3 decorated declarations.
static_assert(std::is_same_v<Index_t<Array<jbyte, 3>>, jbyteArray>);
static_assert(std::is_same_v<Index_t<Array<jchar, 3>>, jcharArray>);
static_assert(std::is_same_v<Index_t<Array<jshort, 3>>, jshortArray>);
static_assert(std::is_same_v<Index_t<Array<jint, 3>>, jintArray>);
static_assert(std::is_same_v<Index_t<Array<jfloat, 3>>, jfloatArray>);
static_assert(std::is_same_v<Index_t<Array<jdouble, 3>>, jdoubleArray>);
static_assert(std::is_same_v<Index_t<Array<jlong, 3>>, jlongArray>);
static_assert(std::is_same_v<Index_t<Array<jboolean, 3>>, jbooleanArray>);
static_assert(std::is_same_v<Index_t<Array<jarray, 3>>, jarray>);
static_assert(std::is_same_v<Index_t<Array<jobject, 3>>, jobjectArray>);
////////////////////////////////////////////////////////////////////////////////
// Types as declarations.
////////////////////////////////////////////////////////////////////////////////
static_assert(std::is_same_v<AsDecl_t<void>, std::tuple<void>>);
static_assert(std::is_same_v<AsDecl_t<jint>, std::tuple<int>>);
static_assert(std::is_same_v<AsDecl_t<jfloat>, std::tuple<float>>);
static_assert(std::is_same_v<AsDecl_t<jbyte>, std::tuple<jbyte>>);
static_assert(std::is_same_v<AsDecl_t<jchar>, std::tuple<char, jchar>>);
static_assert(std::is_same_v<AsDecl_t<jshort>, std::tuple<jshort>>);
static_assert(std::is_same_v<AsDecl_t<jlong>, std::tuple<long, jlong>>);
static_assert(std::is_same_v<AsDecl_t<jdouble>, std::tuple<double>>);
// Note, sizeof these types are _not_ equal on x86.
static_assert(!std::is_same_v<AsDecl_t<jboolean>, std::tuple<bool>>);
static_assert(std::is_same_v<AsDecl_t<jboolean>, std::tuple<jboolean, bool>>);
// AsDecl of non-trivial types with multiple keys.
static_assert(std::is_same_v<AsDecl_t<const char*>, std::tuple<jstring>>);
static_assert(std::is_same_v<AsDecl_t<std::string_view>, std::tuple<jstring>>);
static_assert(std::is_same_v<AsDecl_t<std::string>, std::tuple<jstring>>);
////////////////////////////////////////////////////////////////////////////////
// Types as return values.
////////////////////////////////////////////////////////////////////////////////
struct DummyOverload {};
static_assert(std::is_same_v<Return_t<void, DummyOverload>, void>);
static_assert(std::is_same_v<Return_t<jint, DummyOverload>, jint>);
static_assert(std::is_same_v<Return_t<jfloat, DummyOverload>, jfloat>);
static_assert(std::is_same_v<Return_t<jbyte, DummyOverload>, jbyte>);
static_assert(std::is_same_v<Return_t<jchar, DummyOverload>, jchar>);
static_assert(std::is_same_v<Return_t<jshort, DummyOverload>, jshort>);
static_assert(std::is_same_v<Return_t<jlong, DummyOverload>, jlong>);
static_assert(std::is_same_v<Return_t<jdouble, DummyOverload>, jdouble>);
// Objects are better tested through the convertability of their output.
TEST_F(JniTest, MaterializationTests) {
// Objects passed in can be from plain old jobject.
LocalObject<kClass2> obj0{jobject{nullptr}};
// Objects passed in can be lvalues.
LocalObject<kClass2> obj1{LocalObject<kClass2>{}};
// Objects can be rvalues materialized in place.
LocalObject<kClass2> local_0{LocalObject<kClass2>{}};
// Objects can be rvalues (here an x-value).
LocalObject<kClass2> generator_obj{};
LocalObject<kClass> local_1{generator_obj("ReturnsObj")};
// Objects can also be described with no actual class definition.
// These objects won't be usable, but they are "name-safe".
LocalObject local_2{generator_obj("ReturnsObj")};
// doesn't compile because of invalid class (good).
// LocalObject<kClass2> local_3 { generator_obj("ReturnsObj") }; }
// Globals can be built from locals.
GlobalObject<kClass> global_1{generator_obj("ReturnsObj")};
global_1("Bar");
// Globals can be built from expiring locals without a full type.
GlobalObject global_2{generator_obj("ReturnsObj")};
// But they lack sufficient definition to have any invocation!
// global_2("Bar");
// They can be promoted however, and thus restore their type info.
GlobalObject<kClass> global_3{std::move(global_2)};
global_3("Bar");
}
} // namespace jni