Skip to content

Add more test for array.dart and CvVec #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions lib/src/core/array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:ffi' as ffi;
import 'package:equatable/equatable.dart';
import 'package:ffi/ffi.dart';

import 'base.dart';

// Dart does not support multiple upper bounds for T now, if they implement it, this can be simplified.
// https://github.com/dart-lang/language/issues/2709
abstract class NativeArray<T extends ffi.NativeType, P extends num>
Expand All @@ -25,8 +27,14 @@ abstract class INativeArray<T> {
}

class U8Array extends NativeArray<ffi.Uint8, int> {
U8Array([int length = 0]) : super(length) {
U8Array([int length = 0, int value = 0]) : super(length) {
assert(length >= 0 && value <= CV_U8_MAX && value >= CV_U8_MIN);
ptr = calloc<ffi.Uint8>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand Down Expand Up @@ -63,8 +71,14 @@ class U8Array extends NativeArray<ffi.Uint8, int> {
}

class I8Array extends NativeArray<ffi.Int8, int> {
I8Array([int length = 0]) : super(length) {
I8Array([int length = 0, int value = 0]) : super(length) {
assert(length >= 0 && value <= CV_I8_MAX && value >= CV_I8_MIN);
ptr = calloc<ffi.Int8>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -76,6 +90,11 @@ class I8Array extends NativeArray<ffi.Int8, int> {
return array;
}

I8Array.fromPointer(ffi.Pointer<ffi.Int8> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand All @@ -96,8 +115,14 @@ class I8Array extends NativeArray<ffi.Int8, int> {
}

class U16Array extends NativeArray<ffi.Uint16, int> {
U16Array([int length = 0]) : super(length) {
U16Array([int length = 0, int value = 0]) : super(length) {
assert(length >= 0 && value <= CV_U16_MAX && value >= CV_U16_MIN);
ptr = calloc<ffi.Uint16>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -109,6 +134,11 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
return array;
}

U16Array.fromPointer(ffi.Pointer<ffi.Uint16> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand All @@ -129,8 +159,14 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
}

class I16Array extends NativeArray<ffi.Int16, int> {
I16Array([int length = 0]) : super(length) {
I16Array([int length = 0, int value = 0]) : super(length) {
assert(length >= 0 && value <= CV_I16_MAX && value >= CV_I16_MIN);
ptr = calloc<ffi.Int16>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -142,6 +178,11 @@ class I16Array extends NativeArray<ffi.Int16, int> {
return array;
}

I16Array.fromPointer(ffi.Pointer<ffi.Int16> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand All @@ -162,8 +203,14 @@ class I16Array extends NativeArray<ffi.Int16, int> {
}

class I32Array extends NativeArray<ffi.Int, int> {
I32Array([int length = 0]) : super(length) {
I32Array([int length = 0, int value = 0]) : super(length) {
assert(length >= 0 && value <= CV_I32_MAX && value >= CV_I32_MIN);
ptr = calloc<ffi.Int>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -175,6 +222,11 @@ class I32Array extends NativeArray<ffi.Int, int> {
return array;
}

I32Array.fromPointer(ffi.Pointer<ffi.Int> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand All @@ -195,8 +247,14 @@ class I32Array extends NativeArray<ffi.Int, int> {
}

class F32Array extends NativeArray<ffi.Float, double> {
F32Array([int length = 0]) : super(length) {
F32Array([int length = 0, double value = 0]) : super(length) {
assert(length >= 0 && value <= CV_F32_MAX);
ptr = calloc<ffi.Float>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -208,6 +266,11 @@ class F32Array extends NativeArray<ffi.Float, double> {
return array;
}

F32Array.fromPointer(ffi.Pointer<ffi.Float> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand All @@ -228,8 +291,14 @@ class F32Array extends NativeArray<ffi.Float, double> {
}

class F64Array extends NativeArray<ffi.Double, double> {
F64Array([int length = 0]) : super(length) {
F64Array([int length = 0, double value = 0]) : super(length) {
assert(length >= 0 && value <= CV_F64_MAX);
ptr = calloc<ffi.Double>(length);
if (value != 0) {
for (var idx = 0; idx < length; idx++) {
ptr[idx] = value;
}
}
finalizer.attach(this, ptr.cast());
}

Expand All @@ -241,6 +310,11 @@ class F64Array extends NativeArray<ffi.Double, double> {
return array;
}

F64Array.fromPointer(ffi.Pointer<ffi.Double> ptr, int length) : super(length) {
this.ptr = ptr;
finalizer.attach(this, ptr.cast());
}

static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);

@override
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/error_code.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// coverage:ignore-file
// ignore_for_file: non_constant_identifier_names

/* Error status codes
Original codes were from OpenCVSharp
Original Author: @shimat
LICENSE: Apache-2.0
https://github.com/shimat/opencvsharp/blob/main/src/OpenCvSharp/Modules/core/Enum/ErrorCode.cs

Ported to Dart By: @Rainyl
LICENSE: Apache-2.0
*/
Expand Down
1 change: 1 addition & 0 deletions lib/src/opencv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export 'contrib/aruco.dart';
export 'contrib/aruco_dict.dart';
export 'contrib/img_hash.dart';

export 'core/array.dart';
export 'core/asyncarray.dart';
export 'core/base.dart';
export 'core/core.dart';
Expand Down
95 changes: 95 additions & 0 deletions test/core/array_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'package:test/test.dart';
import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
test('cv.U8Array', () {
final array = cv.U8Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.U8Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.I8Array', () {
final array = cv.I8Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.I8Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.U16Array', () {
final array = cv.U16Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.U16Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.I16Array', () {
final array = cv.I16Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.I16Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.I32Array', () {
final array = cv.I32Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.I32Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.F32Array', () {
final array = cv.F32Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.F32Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});

test('cv.F64Array', () {
final array = cv.F64Array(10, 1);
expect(array[0], 1);
array[1] = 2;
expect(array[1], 2);
array[1] = 1;
expect(array.toList(), List.generate(10, (i) => 1));

final array2 = cv.F64Array.fromList(List.generate(array.length, (i) => 1));
expect(array2.props, isNotEmpty);
expect(array2.toList(), array2.toList());
});
}
32 changes: 32 additions & 0 deletions test/core/others_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,36 @@ void main() {
});
expect(s5, cv.Scalar.blue);
});

test('cv.Vec6i', () {
final vec = cv.Vec6i(1, 2, 3, 4, 5, 6);
expect(vec.val, [1, 2, 3, 4, 5, 6]);
final vec1 = cv.Vec6i.fromNative(vec.ref);
expect(vec1.val, vec.val);
expect(vec.toString(), "Vec6i(1, 2, 3, 4, 5, 6)");
});

test('cv.Vec6f', () {
final vec = cv.Vec6f(1, 2, 3, 4, 5, 6);
expect(vec.val, [1, 2, 3, 4, 5, 6]);
final vec1 = cv.Vec6f.fromNative(vec.ref);
expect(vec1.val, vec.val);
expect(vec.toString(), "Vec6f(1.000, 2.000, 3.000, 4.000, 5.000, 6.000)");
});

test('cv.Vec6d', () {
final vec = cv.Vec6d(1, 2, 3, 4, 5, 6);
expect(vec.val, [1, 2, 3, 4, 5, 6]);
final vec1 = cv.Vec6d.fromNative(vec.ref);
expect(vec1.val, vec.val);
expect(vec.toString(), "Vec6d(1.000, 2.000, 3.000, 4.000, 5.000, 6.000)");
});

test('cv.Vec8i', () {
final vec = cv.Vec8i(1, 2, 3, 4, 5, 6, 7, 8);
expect(vec.val, [1, 2, 3, 4, 5, 6, 7, 8]);
final vec1 = cv.Vec8i.fromNative(vec.ref);
expect(vec1.val, vec.val);
expect(vec.toString(), "Vec8i(1, 2, 3, 4, 5, 6, 7, 8)");
});
}