Skip to content

Commit

Permalink
#993. Array class extensions tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Jun 2, 2021
1 parent ac0b73a commit 47bae86
Show file tree
Hide file tree
Showing 22 changed files with 538 additions and 515 deletions.
43 changes: 43 additions & 0 deletions LibTest/ffi/Array/ArrayArray_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion ArrayArray<T extends NativeType> extension Null safety
//. Bounds checking indexing methods on Arrays of Array.
///
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Array
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2, 3)
external Array<Array<Int16>> a0;
@Array.multi([2, 3])
external Array<Array<Int16>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
array[i][j] = 42;
Expect.equals(42, array[i][j]);
}
}
final array2 = pointer.ref.a1;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
array2[i][j] = 42;
Expect.equals(42, array2[i][j]);
}
}
} finally {
calloc.free(pointer);
}
}
40 changes: 40 additions & 0 deletions LibTest/ffi/Array/ArrayArray_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion ArrayArray<T extends NativeType> extension Null safety
//. Bounds checking indexing methods on Arrays of Array.
///
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Array
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2, 3)
external Array<Array<Int16>> a0;
@Array.multi([2, 3])
external Array<Array<Int16>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array1 = pointer.ref.a0;
Expect.throws(() {array1[3][0];});
Expect.throws(() {array1[3][0] = 42;});
Expect.throws(() {array1[0][3];});
Expect.throws(() {array1[0][3] = 42;});

final array2 = pointer.ref.a1;
Expect.throws(() {array2[3][0];});
Expect.throws(() {array2[3][0] = 42;});
Expect.throws(() {array2[0][3];});
Expect.throws(() {array2[0][3] = 42;});
} finally {
calloc.free(pointer);
}
}
41 changes: 41 additions & 0 deletions LibTest/ffi/Array/Array_A02_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that it is a compile error if Array annotation has a
/// wrong dimension
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";

class MyStruct extends Struct {
@Array(2)
//^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
external Array<Array<Int16>> a0;
}

void main() {
MyStruct ms;
}
44 changes: 20 additions & 24 deletions LibTest/ffi/Array/DoubleArray_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
/// @assertion DoubleArray extension Null safety
/// Bounds checking indexing methods on Arrays of Double.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that this class represents a fixed-size array of Double
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Double
/// @author sgrekhov@unipro.ru
import "dart:ffi";
Expand All @@ -31,17 +16,28 @@ import "../../../Utils/expect.dart";
class MyStruct extends Struct {
@Array(2)
external Array<Double> a0;
@Array.multi([2, 3])
external Array<Array<Double>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
final array1 = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
array1[i] = 3.14;
Expect.approxEquals(3.14, array1[i]);
array1[i] = -3.14;
Expect.approxEquals(-3.14, array1[i]);
}
final array2 = pointer.ref.a1;
for (int i = 0; i < 2; i++) {
array[i] = 3.14;
Expect.approxEquals(3.14, array[i]);
array[i] = -3.14;
Expect.approxEquals(-3.14, array[i]);
for (int j = 0; j < 3; j++) {
array2[i][j] = 3.14;
Expect.approxEquals(3.14, array2[i][j]);
array2[i][j] = -3.14;
Expect.approxEquals(-3.14, array2[i][j]);
}
}
} finally {
calloc.free(pointer);
Expand Down
45 changes: 16 additions & 29 deletions LibTest/ffi/Array/DoubleArray_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,36 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
/// @assertion DoubleArray extension Null safety
/// Bounds checking indexing methods on Arrays of Double.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that this class represents a fixed-size array of Double
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Double
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Double> a0;
@Array.multi([2, 3])
external Array<Array<Double>> a0;
external Array<Array<Double>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = 3.14;
Expect.approxEquals(3.14, array[i][j]);
array[i][j] = -3.14;
Expect.approxEquals(-3.14, array[i][j]);
}
}
final array1 = pointer.ref.a0;
Expect.throws(() {array1[3];});
Expect.throws(() {array1[3] = 42;});

final array2 = pointer.ref.a1;
Expect.throws(() {array2[3][0];});
Expect.throws(() {array2[3][0] = 42;});
Expect.throws(() {array2[0][3];});
Expect.throws(() {array2[0][3] = 42;});
} finally {
calloc.free(pointer);
}
Expand Down
44 changes: 20 additions & 24 deletions LibTest/ffi/Array/FloatArray_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
/// @assertion FloatArray extension Null safety
/// Bounds checking indexing methods on Arrays of Float.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that this class represents a fixed-size array of Float
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Float
/// @author sgrekhov@unipro.ru
import "dart:ffi";
Expand All @@ -31,17 +16,28 @@ import "../../../Utils/expect.dart";
class MyStruct extends Struct {
@Array(2)
external Array<Float> a0;
@Array.multi([2, 3])
external Array<Array<Float>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
final array1 = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
array1[i] = 3.14;
Expect.approxEquals(3.14, array1[i]);
array1[i] = -3.14;
Expect.approxEquals(-3.14, array1[i]);
}
final array2 = pointer.ref.a1;
for (int i = 0; i < 2; i++) {
array[i] = 3.14;
Expect.approxEquals(3.14, array[i]);
array[i] = -3.14;
Expect.approxEquals(-3.14, array[i]);
for (int j = 0; j < 3; j++) {
array2[i][j] = 3.14;
Expect.approxEquals(3.14, array2[i][j]);
array2[i][j] = -3.14;
Expect.approxEquals(-3.14, array2[i][j]);
}
}
} finally {
calloc.free(pointer);
Expand Down
45 changes: 16 additions & 29 deletions LibTest/ffi/Array/FloatArray_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,36 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
/// @assertion FloatArray extension Null safety
/// Bounds checking indexing methods on Arrays of Float.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that this class represents a fixed-size array of Float
/// @description Checks that this extension contains bounds checking indexing
/// methods on Arrays of Float
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Float> a0;
@Array.multi([2, 3])
external Array<Array<Float>> a0;
external Array<Array<Float>> a1;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = 3.14;
Expect.approxEquals(3.14, array[i][j]);
array[i][j] = -3.14;
Expect.approxEquals(-3.14, array[i][j]);
}
}
final array1 = pointer.ref.a0;
Expect.throws(() {array1[3];});
Expect.throws(() {array1[3] = 42;});

final array2 = pointer.ref.a1;
Expect.throws(() {array2[3][0];});
Expect.throws(() {array2[3][0] = 42;});
Expect.throws(() {array2[0][3];});
Expect.throws(() {array2[0][3] = 42;});
} finally {
calloc.free(pointer);
}
Expand Down

0 comments on commit 47bae86

Please sign in to comment.