1
0
forked from gmp/jni

tests: add tests for calling new method types

This commit is contained in:
Chris Waldon 2020-06-26 15:40:17 -04:00
parent b5d97bc020
commit b74a17279b
No known key found for this signature in database
GPG Key ID: 35BBC7C073F5A5D3
3 changed files with 190 additions and 1 deletions

View File

@ -9,7 +9,7 @@ import (
)
var (
vm JVM
vm JVM
vmOnce sync.Once
)
@ -145,6 +145,167 @@ func TestIntMethod(t *testing.T) {
}
}
func TestBooleanMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetBoolean", "()Z")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallBooleanMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if !res {
t.Errorf("Method returned %v, not expected value of %v.", res, true)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestByteMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetByte", "()B")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallByteMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 127 {
t.Errorf("Method returned %d, not expected value of %d.", res, 127)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestCharMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetChar", "()C")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallCharMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 65432 {
t.Errorf("Method returned %d, not expected value of %d.", res, 65432)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestShortMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetShort", "()S")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallShortMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 512 {
t.Errorf("Method returned %d, not expected value of %d.", res, 512)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestLongMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetLong", "()J")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallLongMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 1<<33 {
t.Errorf("Method returned %d, not expected value of %d.", res, 1<<33)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestFloatMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetFloat", "()F")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallFloatMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 4.321 {
t.Errorf("Method returned %f, not expected value of %f.", res, 4.321)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestDoubleMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetDouble", "()D")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallDoubleMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 5.4321 {
t.Errorf("Method returned %f, not expected value of %f.", res, 5.4321)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestVoidMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")

Binary file not shown.

View File

@ -21,6 +21,34 @@ public class AClass {
return intval;
}
public boolean GetBoolean() {
return true;
}
public byte GetByte() {
return 127;
}
public char GetChar() {
return 65432;
}
public short GetShort() {
return 512;
}
public long GetLong() {
return 1L<<33;
}
public float GetFloat() {
return 4.321f;
}
public double GetDouble() {
return 5.4321;
}
public void SetInt(int val) {
intval = val;
}