Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi committed Nov 12, 2024
1 parent 8c95af4 commit e28e6ec
Show file tree
Hide file tree
Showing 21 changed files with 591 additions and 456 deletions.
21 changes: 19 additions & 2 deletions pkgs/jni/lib/src/jarray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class JArray<E> extends JObject {
static JArrayType<E> type<E>(JArrayElementType<E> innerType) =>
JArrayType(innerType);

/// The type which includes information such as the signature of this class.
static JArrayNullableType<E> nullableType<E>(
JArrayElementType<E> innerType) =>
JArrayNullableType(innerType);

/// Construct a new [JArray] with [reference] as its underlying reference.
JArray.fromReference(this.elementType, JReference reference)
: $type = type(elementType),
Expand All @@ -104,7 +109,15 @@ class JArray<E> extends JObject {
/// Creates a [JArray] of the given length from the given [elementType].
///
/// The [length] must be a non-negative integer.
/// For objects, [elementType] must be a nullable type as this constructor
/// initializes all elements with `null`.
factory JArray(JArrayElementType<E> elementType, int length) {
RangeError.checkNotNegative(length);
if (elementType is JObjType<JObject?> &&
!(elementType as JObjType<JObject?>).isNullable) {
throw StateError('Element type of JArray must be nullable when '
'all elements with null');
}
return elementType._newArray(length);
}

Expand Down Expand Up @@ -412,8 +425,12 @@ extension DoubleArray on JArray<jdouble> {
extension ObjectArray<T extends JObject?> on JArray<T> {
T operator [](int index) {
RangeError.checkValidIndex(index, this);
return (elementType as JObjType<T>).fromReference(JGlobalReference(
Jni.env.GetObjectArrayElement(reference.pointer, index)));
final pointer = Jni.env.GetObjectArrayElement(reference.pointer, index);
if (pointer == nullptr) {
return null as T;
}
return (elementType as JObjType<T>)
.fromReference(JGlobalReference(pointer));
}

void operator []=(int index, T value) {
Expand Down
3 changes: 2 additions & 1 deletion pkgs/jni/lib/src/jvalues.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import 'dart:ffi';

import '../_internal.dart';

import 'jobject.dart';
import 'third_party/generated_bindings.dart';

void _fillJValue(Pointer<JValue> pos, dynamic arg) {
switch (arg) {
case null:
pos.ref.l = nullptr;
case JObject():
pos.ref.l = arg.reference.pointer;
case JReference():
Expand Down
2 changes: 2 additions & 0 deletions pkgs/jni/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ abstract class JObjType<T extends JObject?> extends JType<T>

JObjType<T?> get nullableType;

bool get isNullable => this == nullableType;

const JObjType();

/// Creates an object from this type using the reference.
Expand Down
11 changes: 8 additions & 3 deletions pkgs/jni/lib/src/util/jiterator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ class JIterator<$E extends JObject?> extends JObject implements Iterator<$E> {
static JIteratorType<$E> type<$E extends JObject?>(
JObjType<$E> E,
) {
return JIteratorType<$E>(
E,
);
return JIteratorType<$E>(E);
}

/// The type which includes information such as the signature of this class.
static JIteratorNullableType<$E> nullableType<$E extends JObject?>(
JObjType<$E> E,
) {
return JIteratorNullableType<$E>(E);
}

$E? _current;
Expand Down
14 changes: 9 additions & 5 deletions pkgs/jni/lib/src/util/jlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ class JList<$E extends JObject?> extends JObject with ListMixin<$E> {
static JListType<$E> type<$E extends JObject?>(
JObjType<$E> E,
) {
return JListType<$E>(
E,
);
return JListType<$E>(E);
}

/// The type which includes information such as the signature of this class.
static JListNullableType<$E> nullableType<$E extends JObject?>(
JObjType<$E> E) {
return JListNullableType<$E>(E);
}

static final _arrayListClassRef = JClass.forName(r'java/util/ArrayList');
Expand All @@ -137,7 +141,7 @@ class JList<$E extends JObject?> extends JObject with ListMixin<$E> {
set length(int newLength) {
RangeError.checkNotNegative(newLength);
while (length < newLength) {
add(E.fromReference(jNullReference));
add(null as $E);
}
while (newLength < length) {
removeAt(length - 1);
Expand All @@ -149,7 +153,7 @@ class JList<$E extends JObject?> extends JObject with ListMixin<$E> {
@override
$E operator [](int index) {
RangeError.checkValidIndex(index, this);
return _getId(this, E, [JValueInt(index)])!;
return _getId(this, E, [JValueInt(index)]);
}

static final _setId = _class.instanceMethodId(
Expand Down
15 changes: 12 additions & 3 deletions pkgs/jni/lib/src/util/jmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ class JMap<$K extends JObject?, $V extends JObject?> extends JObject
return JMapType<$K, $V>(K, V);
}

/// The type which includes information such as the signature of this class.
static JMapNullableType<$K, $V>
nullableType<$K extends JObject?, $V extends JObject?>(
JObjType<$K> K,
JObjType<$V> V,
) {
return JMapNullableType<$K, $V>(K, V);
}

static final _hashMapClass = JClass.forName(r'java/util/HashMap');
static final _ctorId = _hashMapClass.constructorId(r'()V');
JMap.hash(this.K, this.V)
Expand All @@ -149,7 +158,7 @@ class JMap<$K extends JObject?, $V extends JObject?> extends JObject
return null;
}
final keyRef = key.reference;
final value = _getId(this, V, [keyRef.pointer]);
final value = _getId(this, V.nullableType, [keyRef.pointer]);
return value;
}

Expand All @@ -159,7 +168,7 @@ class JMap<$K extends JObject?, $V extends JObject?> extends JObject
void operator []=($K key, $V value) {
final keyRef = key?.reference ?? jNullReference;
final valueRef = value?.reference ?? jNullReference;
_putId(this, V, [keyRef.pointer, valueRef.pointer]);
_putId(this, V.nullableType, [keyRef.pointer, valueRef.pointer]);
}

static final _addAllId =
Expand Down Expand Up @@ -226,7 +235,7 @@ class JMap<$K extends JObject?, $V extends JObject?> extends JObject
return null;
}
final keyRef = key.reference;
final value = _removeId(this, V, [keyRef.pointer]);
final value = _removeId(this, V.nullableType, [keyRef.pointer]);
return value;
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkgs/jni/lib/src/util/jset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class JSet<$E extends JObject?> extends JObject with SetMixin<$E> {
return JSetType<$E>(E);
}

/// The type which includes information such as the signature of this class.
static JSetNullableType<$E> nullableType<$E extends JObject?>(
JObjType<$E> E,
) {
return JSetNullableType<$E>(E);
}

static final _hashSetClass = JClass.forName(r'java/util/HashSet');
static final _ctorId = _hashSetClass.constructorId(r'()V');
JSet.hash(this.E)
Expand Down
61 changes: 32 additions & 29 deletions pkgs/jni/test/jarray_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,14 @@ void run({required TestRunnerCallback testRunner}) {
});
testRunner('Java string array', () {
using((arena) {
final array = JArray(JString.type, 3)..releasedBy(arena);
final array = JArray(JString.nullableType, 3)..releasedBy(arena);
expect(array.length, 3);
array[0] = 'حس'.toJString()..releasedBy(arena);
array[1] = '\$'.toJString()..releasedBy(arena);
array[2] = '33'.toJString()..releasedBy(arena);
expect(array[0].toDartString(releaseOriginal: true), 'حس');
expect(array[1].toDartString(releaseOriginal: true), '\$');
expect(array[2].toDartString(releaseOriginal: true), '33');
expect(array[0]!.toDartString(releaseOriginal: true), 'حس');
expect(array[1]!.toDartString(releaseOriginal: true), '\$');
expect(array[2]!.toDartString(releaseOriginal: true), '33');
array.setRange(
0,
3,
Expand All @@ -330,9 +330,9 @@ void run({required TestRunnerCallback testRunner}) {
],
1,
);
expect(array[0].toDartString(releaseOriginal: true), '55');
expect(array[1].toDartString(releaseOriginal: true), '66');
expect(array[2].toDartString(releaseOriginal: true), '77');
expect(array[0]!.toDartString(releaseOriginal: true), '55');
expect(array[1]!.toDartString(releaseOriginal: true), '66');
expect(array[2]!.toDartString(releaseOriginal: true), '77');
expect(() {
final _ = array[-1];
}, throwsRangeError);
Expand All @@ -346,11 +346,11 @@ void run({required TestRunnerCallback testRunner}) {
});
testRunner('Java object array', () {
using((arena) {
final array = JArray(JObject.type, 3)..releasedBy(arena);
final array = JArray(JObject.nullableType, 3)..releasedBy(arena);
expect(array.length, 3);
expect(array[0].reference.pointer, nullptr);
expect(array[1].reference.pointer, nullptr);
expect(array[2].reference.pointer, nullptr);
expect(array[0], isNull);
expect(array[1], isNull);
expect(array[2], isNull);
});
});
testRunner('Java 2d array', () {
Expand All @@ -359,26 +359,27 @@ void run({required TestRunnerCallback testRunner}) {
array[0] = 1;
array[1] = 2;
array[2] = 3;
final twoDimArray = JArray(JArray.type(jint.type), 3)..releasedBy(arena);
final twoDimArray = JArray(JArray.nullableType(jint.type), 3)
..releasedBy(arena);
expect(twoDimArray.length, 3);
twoDimArray[0] = array;
twoDimArray[1] = array;
twoDimArray[2] = array;
for (var i = 0; i < 3; ++i) {
expect(twoDimArray[i][0], 1);
expect(twoDimArray[i][1], 2);
expect(twoDimArray[i][2], 3);
expect(twoDimArray[i]![0], 1);
expect(twoDimArray[i]![1], 2);
expect(twoDimArray[i]![2], 3);
}
twoDimArray[2][2] = 4;
expect(twoDimArray[2][2], 4);
twoDimArray[2]![2] = 4;
expect(twoDimArray[2]![2], 4);
});
});
testRunner('JArray.filled', () {
using((arena) {
final string = 'abc'.toJString()..releasedBy(arena);
final array = JArray.filled(3, string)..releasedBy(arena);
expect(
() => JArray.filled(-3, JString.fromReference(jNullReference)),
() => JArray.filled(-3, 'abc'.toJString()..releasedBy(arena)),
throwsA(isA<RangeError>()),
);
expect(array.length, 3);
Expand All @@ -395,68 +396,70 @@ void run({required TestRunnerCallback testRunner}) {
});
testRunner('JArray of JShort', () {
using((arena) {
final arr = JArray(JShort.type, 1)..releasedBy(arena);
final arr = JArray(JShort.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JInteger', () {
using((arena) {
final arr = JArray(JInteger.type, 1)..releasedBy(arena);
final arr = JArray(JInteger.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JCharacter', () {
using((arena) {
final arr = JArray(JCharacter.type, 1)..releasedBy(arena);
final arr = JArray(JCharacter.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JLong', () {
using((arena) {
final arr = JArray(JLong.type, 1)..releasedBy(arena);
final arr = JArray(JLong.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JFloat', () {
using((arena) {
final arr = JArray(JFloat.type, 1)..releasedBy(arena);
final arr = JArray(JFloat.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JDouble', () {
using((arena) {
final arr = JArray(JDouble.type, 1)..releasedBy(arena);
final arr = JArray(JDouble.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JBoolean', () {
using((arena) {
final arr = JArray(JBoolean.type, 1)..releasedBy(arena);
final arr = JArray(JBoolean.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JSet', () {
using((arena) {
final arr = JArray(JSet.type(JString.type), 1)..releasedBy(arena);
final arr = JArray(JSet.nullableType(JString.type), 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JList', () {
using((arena) {
final arr = JArray(JList.type(JString.type), 1)..releasedBy(arena);
final arr = JArray(JList.nullableType(JString.type), 1)
..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JMap', () {
using((arena) {
final arr = JArray(JMap.type(JString.type, JString.type), 1)
final arr = JArray(JMap.nullableType(JString.type, JString.type), 1)
..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JIterator', () {
using((arena) {
final arr = JArray(JIterator.type(JString.type), 1)..releasedBy(arena);
final arr = JArray(JIterator.nullableType(JString.type), 1)
..releasedBy(arena);
expect(arr[0], isNull);
});
});
Expand Down
4 changes: 2 additions & 2 deletions pkgs/jni/test/jbyte_buffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ void run({required TestRunnerCallback testRunner}) {
final b = testDataBuffer(arena);
expect(a.$type, b.$type);
expect(a.$type.hashCode, b.$type.hashCode);
final c = JBuffer.fromReference(jNullReference);
final d = JBuffer.fromReference(jNullReference);
final c = JBuffer.fromReference(a.reference);
final d = JBuffer.fromReference(b.reference);
expect(c.$type, d.$type);
expect(c.$type.hashCode, d.$type.hashCode);

Expand Down
Loading

0 comments on commit e28e6ec

Please sign in to comment.