From 5b7b73407d9a8e2adc853e97428c191456c1af13 Mon Sep 17 00:00:00 2001 From: Jamieson Pryor Date: Sat, 18 Nov 2023 10:56:58 -0800 Subject: [PATCH] Split signature test into method and field. Field tests are under exercised for these signatures. While they're exercised indirectly elsewhere, it's helpful to have these tests for debugging. Also, multi-dimensional field arrays aren't working (https://github.com/google/jni-bind/issues/40) so this ought help fix it. PiperOrigin-RevId: 583658797 --- implementation/BUILD | 14 ++- implementation/signature_field_test.cc | 99 +++++++++++++++++++ ...ature_test.cc => signature_method_test.cc} | 86 +++++++--------- 3 files changed, 144 insertions(+), 55 deletions(-) create mode 100644 implementation/signature_field_test.cc rename implementation/{signature_test.cc => signature_method_test.cc} (69%) diff --git a/implementation/BUILD b/implementation/BUILD index aca5dee..18a8a0f 100644 --- a/implementation/BUILD +++ b/implementation/BUILD @@ -987,8 +987,18 @@ cc_library( ) cc_test( - name = "signature_test", - srcs = ["signature_test.cc"], + name = "signature_method_test", + srcs = ["signature_method_test.cc"], + deps = [ + "//:jni_bind", + "//:jni_test", + "@googletest//:gtest_main", + ], +) + +cc_test( + name = "signature_field_test", + srcs = ["signature_field_test.cc"], deps = [ "//:jni_bind", "//:jni_test", diff --git a/implementation/signature_field_test.cc b/implementation/signature_field_test.cc new file mode 100644 index 0000000..b761498 --- /dev/null +++ b/implementation/signature_field_test.cc @@ -0,0 +1,99 @@ +/* + * Copyright 2022 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 +#include +#include "jni_bind.h" + +namespace { + +using ::jni::Array; +using ::jni::Class; +using ::jni::Constructor; +using ::jni::Field; +using ::jni::Id; +using ::jni::IdType; +using ::jni::JniT; +using ::jni::JniTSelector; +using ::jni::kNoIdx; +using ::jni::Method; +using ::jni::Overload; +using ::jni::Params; +using ::jni::Rank; +using ::jni::Return; +using ::jni::SelectorStaticInfo; +using ::jni::Self; +using ::jni::Signature_v; + +static constexpr Class kClass1{"kClass1", + Field{"f0", jboolean{}}, + Field{"f1", jbyte{}}, + Field{"f2", jchar{}}, + Field{"f3", jshort{}}, + Field{"f4", jint{}}, + Field{"f5", jfloat{}}, + Field{"f6", jdouble{}}, + Field{"f7", jlong{}}, + Field{"f8", Class{"kClass2"}}, + + Field{"f9", Array{}}, + Field{"f10", Array{}}, + Field{"f11", Array{}}, + Field{"f12", Array{}}, + Field{"f13", Array{}}, + Field{"f14", Array{}}, + Field{"f15", Array{}}, + Field{"f16", Array{}}, + Field{"f17", Array{Class{"kClass2"}}} + +}; + +using JT = JniT; + +//////////////////////////////////////////////////////////////////////////////// +// Class (i.e. self). +//////////////////////////////////////////////////////////////////////////////// +using kSelf1 = Id; +using JniSelfT = JniTSelector::JniT_, -1>; +using StaticSelectorInfoSelf = SelectorStaticInfo; + +//////////////////////////////////////////////////////////////////////////////// +// Rank 0. +//////////////////////////////////////////////////////////////////////////////// +static_assert(std::string_view{"Z"} == Signature_v>); +static_assert(std::string_view{"B"} == Signature_v>); +static_assert(std::string_view{"C"} == Signature_v>); +static_assert(std::string_view{"S"} == Signature_v>); +static_assert(std::string_view{"I"} == Signature_v>); +static_assert(std::string_view{"F"} == Signature_v>); +static_assert(std::string_view{"D"} == Signature_v>); +static_assert(std::string_view{"J"} == Signature_v>); +static_assert(std::string_view{"LkClass2;"} == + Signature_v>); + +//////////////////////////////////////////////////////////////////////////////// +// Rank 1. +//////////////////////////////////////////////////////////////////////////////// +static_assert(std::string_view{"[Z"} == Signature_v>); +static_assert(std::string_view{"[B"} == Signature_v>); +static_assert(std::string_view{"[C"} == Signature_v>); +static_assert(std::string_view{"[S"} == Signature_v>); +static_assert(std::string_view{"[I"} == Signature_v>); +static_assert(std::string_view{"[F"} == Signature_v>); +static_assert(std::string_view{"[D"} == Signature_v>); +static_assert(std::string_view{"[J"} == Signature_v>); +static_assert(std::string_view{"[LkClass2;"} == + Signature_v>); +} // namespace diff --git a/implementation/signature_test.cc b/implementation/signature_method_test.cc similarity index 69% rename from implementation/signature_test.cc rename to implementation/signature_method_test.cc index d472935..1bb0a92 100644 --- a/implementation/signature_test.cc +++ b/implementation/signature_method_test.cc @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,9 +65,7 @@ static constexpr Class kClass1{ Method{"m5", jni::Return{Self{}}, Params{}}, Method{"m6", jni::Return{}, Params{Self{}}}, Method{"m7", jni::Return{}, Params{Self{}, Self{}}}, - Method{"m8", jni::Return{Self{}}, Params{Self{}, Self{}}}, - Field{"f0", int{}}, - Field{"f1", Class{"kClass2"}}}; + Method{"m8", jni::Return{Self{}}, Params{Self{}, Self{}}}}; using JT = JniT; @@ -86,12 +84,6 @@ static_assert(std::string_view{"[[LkClass1;"} == //////////////////////////////////////////////////////////////////////////////// // Constructors. //////////////////////////////////////////////////////////////////////////////// -using kCtor0 = Id; -using kCtor1 = Id; -using kCtor2 = Id; -using kCtor3 = Id; -using kCtor4 = Id; - static_assert(std::string_view{"I"} == Signature_v>); static_assert(std::string_view{"F"} == @@ -101,11 +93,16 @@ static_assert(std::string_view{"Z"} == static_assert(std::string_view{"[I"} == Signature_v>); -static_assert(std::string_view{"()V"} == Signature_v); -static_assert(std::string_view{"(I)V"} == Signature_v); -static_assert(std::string_view{"(FZ)V"} == Signature_v); -static_assert(std::string_view{"([I)V"} == Signature_v); -static_assert(std::string_view{"([[I)V"} == Signature_v); +static_assert(std::string_view{"()V"} == + Signature_v>); +static_assert(std::string_view{"(I)V"} == + Signature_v>); +static_assert(std::string_view{"(FZ)V"} == + Signature_v>); +static_assert(std::string_view{"([I)V"} == + Signature_v>); +static_assert(std::string_view{"([[I)V"} == + Signature_v>); //////////////////////////////////////////////////////////////////////////////// // Methods (Overload sets with only one overload). @@ -134,43 +131,35 @@ static_assert(std::string_view{"F"} == static_assert(std::string_view{"Z"} == Signature_v>); -using kMethod0Overload0 = Id; -using kMethod1Overload0 = Id; -using kMethod2Overload0 = Id; -using kMethod3Overload0 = Id; - -static_assert(std::string_view{"()V"} == Signature_v); -static_assert(std::string_view{"(I)S"} == Signature_v); +static_assert(std::string_view{"()V"} == + Signature_v>); +static_assert(std::string_view{"(I)S"} == + Signature_v>); static_assert(std::string_view{"(FZ)LkClass2;"} == - Signature_v); + Signature_v>); //////////////////////////////////////////////////////////////////////////////// // Overloaded Method (smoke test, they should behave the same). //////////////////////////////////////////////////////////////////////////////// -using kMethod3Overload0 = Id; -using kMethod3Overload1 = Id; -using kMethod3Overload2 = Id; -using kMethod3Overload3 = Id; -using kMethod3Overload4 = Id; - -using kMethod4Overload0 = Id; -using kMethod4Overload1 = Id; -using kMethod4Overload2 = Id; -using kMethod4Overload3 = Id; - -static_assert(std::string_view{"()V"} == Signature_v); -static_assert(std::string_view{"(Z)I"} == Signature_v); -static_assert(std::string_view{"(SD)F"} == Signature_v); +static_assert(std::string_view{"()V"} == + Signature_v>); +static_assert(std::string_view{"(Z)I"} == + Signature_v>); +static_assert(std::string_view{"(SD)F"} == + Signature_v>); static_assert(std::string_view{"(LkClass2;)F"} == - Signature_v); + Signature_v>); static_assert(std::string_view{"()LkClass3;"} == - Signature_v); - -static_assert(std::string_view{"([I)[Z"} == Signature_v); -static_assert(std::string_view{"([[F)[[Z"} == Signature_v); -static_assert(std::string_view{"([[[S)[[[Z"} == Signature_v); + Signature_v>); + +static_assert(std::string_view{"([I)[Z"} == + Signature_v>); +static_assert(std::string_view{"([[F)[[Z"} == + Signature_v>); +static_assert(std::string_view{"([[[S)[[[Z"} == + Signature_v>); static_assert(std::string_view{"()[[LkClass2;"} == - Signature_v); + Signature_v>); //////////////////////////////////////////////////////////////////////////////// // Self Referencing Method (gives richly decorated self back). @@ -189,13 +178,4 @@ using kMethod8 = Id; static_assert(std::string_view{"(LkClass1;LkClass1;)LkClass1;"} == Signature_v.data()); -//////////////////////////////////////////////////////////////////////////////// -// Fields (Overload sets with only one overload). -//////////////////////////////////////////////////////////////////////////////// -using kField0 = Id; -using kField1 = Id; - -static_assert(std::string_view{"I"} == Signature_v); -static_assert(std::string_view{"LkClass2;"} == Signature_v); - } // namespace