Skip to content

Commit

Permalink
Split signature test into method and field.
Browse files Browse the repository at this point in the history
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 (#40) so this ought help fix it.

PiperOrigin-RevId: 584098520
  • Loading branch information
jwhpryor authored and copybara-github committed Nov 20, 2023
1 parent db5ecf9 commit 829989e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 55 deletions.
14 changes: 12 additions & 2 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
138 changes: 138 additions & 0 deletions implementation/signature_field_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#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;

// clang-format off
static constexpr Class kClass1{
"kClass1",

// Rank 0.
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"}},

// Rank 1.
Field{"f9", Array<jboolean>{}},
Field{"f10", Array<jbyte>{}},
Field{"f11", Array<jchar>{}},
Field{"f12", Array<jshort>{}},
Field{"f13", Array<jint>{}},
Field{"f14", Array<jfloat>{}},
Field{"f15", Array<jdouble>{}},
Field{"f16", Array<jlong>{}},
Field{"f17", Array{Class{"kClass2"}}},

// Rank 2.
Field{"f18", Array{jboolean{}, Rank<2>{}}},
Field{"f19", Array{jbyte{}, Rank<2>{}}},
Field{"f20", Array{jchar{}, Rank<2>{}}},
Field{"f21", Array{jshort{}, Rank<2>{}}},
Field{"f22", Array{jint{}, Rank<2>{}}},
Field{"f23", Array{jfloat{}, Rank<2>{}}},
Field{"f24", Array{jdouble{}, Rank<2>{}}},
Field{"f25", Array{jlong{}, Rank<2>{}}},
Field{"f26", Array{Class{"kClass2"}, Rank<2>{}}}
};
// clang-format on

using JT = JniT<jobject, kClass1>;

////////////////////////////////////////////////////////////////////////////////
// Class (i.e. self).
////////////////////////////////////////////////////////////////////////////////
using kSelf1 = Id<JT, IdType::CLASS, kNoIdx, 0>;
using JniSelfT = JniTSelector<jni::LocalArray<jobject, 3, kClass1>::JniT_, -1>;
using StaticSelectorInfoSelf = SelectorStaticInfo<JniSelfT>;

////////////////////////////////////////////////////////////////////////////////
// Rank 0.
////////////////////////////////////////////////////////////////////////////////
static_assert(std::string_view{"Z"} == Signature_v<Id<JT, IdType::FIELD, 0>>);
static_assert(std::string_view{"B"} == Signature_v<Id<JT, IdType::FIELD, 1>>);
static_assert(std::string_view{"C"} == Signature_v<Id<JT, IdType::FIELD, 2>>);
static_assert(std::string_view{"S"} == Signature_v<Id<JT, IdType::FIELD, 3>>);
static_assert(std::string_view{"I"} == Signature_v<Id<JT, IdType::FIELD, 4>>);
static_assert(std::string_view{"F"} == Signature_v<Id<JT, IdType::FIELD, 5>>);
static_assert(std::string_view{"D"} == Signature_v<Id<JT, IdType::FIELD, 6>>);
static_assert(std::string_view{"J"} == Signature_v<Id<JT, IdType::FIELD, 7>>);
static_assert(std::string_view{"LkClass2;"} ==
Signature_v<Id<JT, IdType::FIELD, 8>>);

////////////////////////////////////////////////////////////////////////////////
// Rank 1.
////////////////////////////////////////////////////////////////////////////////
static_assert(std::string_view{"[Z"} == Signature_v<Id<JT, IdType::FIELD, 9>>);
static_assert(std::string_view{"[B"} == Signature_v<Id<JT, IdType::FIELD, 10>>);
static_assert(std::string_view{"[C"} == Signature_v<Id<JT, IdType::FIELD, 11>>);
static_assert(std::string_view{"[S"} == Signature_v<Id<JT, IdType::FIELD, 12>>);
static_assert(std::string_view{"[I"} == Signature_v<Id<JT, IdType::FIELD, 13>>);
static_assert(std::string_view{"[F"} == Signature_v<Id<JT, IdType::FIELD, 14>>);
static_assert(std::string_view{"[D"} == Signature_v<Id<JT, IdType::FIELD, 15>>);
static_assert(std::string_view{"[J"} == Signature_v<Id<JT, IdType::FIELD, 16>>);
static_assert(std::string_view{"[LkClass2;"} ==
Signature_v<Id<JT, IdType::FIELD, 17>>);

////////////////////////////////////////////////////////////////////////////////
// Rank 2.
////////////////////////////////////////////////////////////////////////////////
static_assert(std::string_view{"[[Z"} ==
Signature_v<Id<JT, IdType::FIELD, 18>>);
static_assert(std::string_view{"[[B"} ==
Signature_v<Id<JT, IdType::FIELD, 19>>);
static_assert(std::string_view{"[[C"} ==
Signature_v<Id<JT, IdType::FIELD, 20>>);
static_assert(std::string_view{"[[S"} ==
Signature_v<Id<JT, IdType::FIELD, 21>>);
static_assert(std::string_view{"[[I"} ==
Signature_v<Id<JT, IdType::FIELD, 22>>);
static_assert(std::string_view{"[[F"} ==
Signature_v<Id<JT, IdType::FIELD, 23>>);
static_assert(std::string_view{"[[D"} ==
Signature_v<Id<JT, IdType::FIELD, 24>>);
static_assert(std::string_view{"[[J"} ==
Signature_v<Id<JT, IdType::FIELD, 25>>);
static_assert(std::string_view{"[[LkClass2;"} ==
Signature_v<Id<JT, IdType::FIELD, 26>>);

} // namespace
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<jobject, kClass1>;

Expand All @@ -86,12 +84,6 @@ static_assert(std::string_view{"[[LkClass1;"} ==
////////////////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////////////////
using kCtor0 = Id<JT, IdType::OVERLOAD, kNoIdx, 0>;
using kCtor1 = Id<JT, IdType::OVERLOAD, kNoIdx, 1>;
using kCtor2 = Id<JT, IdType::OVERLOAD, kNoIdx, 2>;
using kCtor3 = Id<JT, IdType::OVERLOAD, kNoIdx, 3>;
using kCtor4 = Id<JT, IdType::OVERLOAD, kNoIdx, 4>;

static_assert(std::string_view{"I"} ==
Signature_v<Id<JT, IdType::OVERLOAD_PARAM, kNoIdx, 1, 0>>);
static_assert(std::string_view{"F"} ==
Expand All @@ -101,11 +93,16 @@ static_assert(std::string_view{"Z"} ==
static_assert(std::string_view{"[I"} ==
Signature_v<Id<JT, IdType::OVERLOAD_PARAM, kNoIdx, 3, 0>>);

static_assert(std::string_view{"()V"} == Signature_v<kCtor0>);
static_assert(std::string_view{"(I)V"} == Signature_v<kCtor1>);
static_assert(std::string_view{"(FZ)V"} == Signature_v<kCtor2>);
static_assert(std::string_view{"([I)V"} == Signature_v<kCtor3>);
static_assert(std::string_view{"([[I)V"} == Signature_v<kCtor4>);
static_assert(std::string_view{"()V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, kNoIdx, 0>>);
static_assert(std::string_view{"(I)V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, kNoIdx, 1>>);
static_assert(std::string_view{"(FZ)V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, kNoIdx, 2>>);
static_assert(std::string_view{"([I)V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, kNoIdx, 3>>);
static_assert(std::string_view{"([[I)V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, kNoIdx, 4>>);

////////////////////////////////////////////////////////////////////////////////
// Methods (Overload sets with only one overload).
Expand Down Expand Up @@ -134,43 +131,35 @@ static_assert(std::string_view{"F"} ==
static_assert(std::string_view{"Z"} ==
Signature_v<Id<JT, IdType::OVERLOAD_PARAM, 2, 0, 1>>);

using kMethod0Overload0 = Id<JT, IdType::OVERLOAD, 0, 0>;
using kMethod1Overload0 = Id<JT, IdType::OVERLOAD, 1, 0>;
using kMethod2Overload0 = Id<JT, IdType::OVERLOAD, 2, 0>;
using kMethod3Overload0 = Id<JT, IdType::OVERLOAD, 3, 0>;

static_assert(std::string_view{"()V"} == Signature_v<kMethod0Overload0>);
static_assert(std::string_view{"(I)S"} == Signature_v<kMethod1Overload0>);
static_assert(std::string_view{"()V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 0, 0>>);
static_assert(std::string_view{"(I)S"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 1, 0>>);
static_assert(std::string_view{"(FZ)LkClass2;"} ==
Signature_v<kMethod2Overload0>);
Signature_v<Id<JT, IdType::OVERLOAD, 2, 0>>);

////////////////////////////////////////////////////////////////////////////////
// Overloaded Method (smoke test, they should behave the same).
////////////////////////////////////////////////////////////////////////////////
using kMethod3Overload0 = Id<JT, IdType::OVERLOAD, 3, 0>;
using kMethod3Overload1 = Id<JT, IdType::OVERLOAD, 3, 1>;
using kMethod3Overload2 = Id<JT, IdType::OVERLOAD, 3, 2>;
using kMethod3Overload3 = Id<JT, IdType::OVERLOAD, 3, 3>;
using kMethod3Overload4 = Id<JT, IdType::OVERLOAD, 3, 4>;

using kMethod4Overload0 = Id<JT, IdType::OVERLOAD, 4, 0>;
using kMethod4Overload1 = Id<JT, IdType::OVERLOAD, 4, 1>;
using kMethod4Overload2 = Id<JT, IdType::OVERLOAD, 4, 2>;
using kMethod4Overload3 = Id<JT, IdType::OVERLOAD, 4, 3>;

static_assert(std::string_view{"()V"} == Signature_v<kMethod3Overload0>);
static_assert(std::string_view{"(Z)I"} == Signature_v<kMethod3Overload1>);
static_assert(std::string_view{"(SD)F"} == Signature_v<kMethod3Overload2>);
static_assert(std::string_view{"()V"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 3, 0>>);
static_assert(std::string_view{"(Z)I"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 3, 1>>);
static_assert(std::string_view{"(SD)F"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 3, 2>>);
static_assert(std::string_view{"(LkClass2;)F"} ==
Signature_v<kMethod3Overload3>);
Signature_v<Id<JT, IdType::OVERLOAD, 3, 3>>);
static_assert(std::string_view{"()LkClass3;"} ==
Signature_v<kMethod3Overload4>);

static_assert(std::string_view{"([I)[Z"} == Signature_v<kMethod4Overload0>);
static_assert(std::string_view{"([[F)[[Z"} == Signature_v<kMethod4Overload1>);
static_assert(std::string_view{"([[[S)[[[Z"} == Signature_v<kMethod4Overload2>);
Signature_v<Id<JT, IdType::OVERLOAD, 3, 4>>);

static_assert(std::string_view{"([I)[Z"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 4, 0>>);
static_assert(std::string_view{"([[F)[[Z"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 4, 1>>);
static_assert(std::string_view{"([[[S)[[[Z"} ==
Signature_v<Id<JT, IdType::OVERLOAD, 4, 2>>);
static_assert(std::string_view{"()[[LkClass2;"} ==
Signature_v<kMethod4Overload3>);
Signature_v<Id<JT, IdType::OVERLOAD, 4, 3>>);

////////////////////////////////////////////////////////////////////////////////
// Self Referencing Method (gives richly decorated self back).
Expand All @@ -189,13 +178,4 @@ using kMethod8 = Id<JT, IdType::OVERLOAD, 8, 0>;
static_assert(std::string_view{"(LkClass1;LkClass1;)LkClass1;"} ==
Signature_v<kMethod8>.data());

////////////////////////////////////////////////////////////////////////////////
// Fields (Overload sets with only one overload).
////////////////////////////////////////////////////////////////////////////////
using kField0 = Id<JT, IdType::FIELD, 0>;
using kField1 = Id<JT, IdType::FIELD, 1>;

static_assert(std::string_view{"I"} == Signature_v<kField0>);
static_assert(std::string_view{"LkClass2;"} == Signature_v<kField1>);

} // namespace

0 comments on commit 829989e

Please sign in to comment.