Skip to content

Commit

Permalink
#993. Array tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey G. Grekhov committed Mar 4, 2021
1 parent 7978676 commit 07d91ec
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 0 deletions.
33 changes: 33 additions & 0 deletions LibTest/ffi/Array/Array_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of T
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(8)
external Array<Uint8> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 8; i++) {
Expect.equals(0, array[i]);
}
} finally {
calloc.free(pointer);
}
}
41 changes: 41 additions & 0 deletions LibTest/ffi/Array/Array_A02_t01.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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class controls array boundaries
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(8)
external Array<Uint8> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;

for (int i = 0; i < 8; i++) {
array[i] = i + 1;
Expect.equals(i + 1, array[i]);
}
Expect.throws(() {
array[-1];
});
Expect.throws(() {
array[8];
});
} finally {
calloc.free(pointer);
}
}
36 changes: 36 additions & 0 deletions LibTest/ffi/Array/DoubleArray_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Double
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Double> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
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]);
}
} finally {
calloc.free(pointer);
}
}
36 changes: 36 additions & 0 deletions LibTest/ffi/Array/FloatArray_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Float
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Float> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
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]);
}
} finally {
calloc.free(pointer);
}
}
42 changes: 42 additions & 0 deletions LibTest/ffi/Array/Int16Array_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Int16
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Int16> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
array[i] = i + 1;
Expect.equals(i + 1, array[i]);
array[i] = 32767;
Expect.equals(32767, array[i]);
array[i] = 32768;
Expect.equals(-32768, array[i]);
array[i] = -32768;
Expect.equals(-32768, array[i]);
array[i] = -32769;
Expect.equals(32767, array[i]);
}
} finally {
calloc.free(pointer);
}
}
46 changes: 46 additions & 0 deletions LibTest/ffi/Array/Int32Array_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Int32
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Int32> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
array[i] = 42;
Expect.equals(42, array[i]);
array[i] = -42;
Expect.equals(-42, array[i]);
array[i] = 32768;
Expect.equals(32768, array[i]);
array[i] = -32768;
Expect.equals(-32768, array[i]);
array[i] = 2147483647;
Expect.equals(2147483647, array[i]);
array[i] = 2147483648;
Expect.equals(-2147483648, array[i]);
array[i] = -2147483649;
Expect.equals(2147483647, array[i]);
}
} finally {
calloc.free(pointer);
}
}
51 changes: 51 additions & 0 deletions LibTest/ffi/Array/Int64Array_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Int64
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Int64> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
Expect.equals(0, array[i]);
array[i] = 42;
Expect.equals(42, array[i]);
array[i] = -42;
Expect.equals(-42, array[i]);
array[i] = 32768;
Expect.equals(32768, array[i]);
array[i] = -32768;
Expect.equals(-32768, array[i]);
array[i] = 2147483648;
Expect.equals(2147483648, array[i]);
array[i] = -2147483649;
Expect.equals(-2147483649, array[i]);
array[i] = 0x7FFFFFFFFFFFFFFF;
Expect.equals(9223372036854775807, array[i]);
array[i] = 0xFFFFFFFFFFFFFFFF;
Expect.equals(-1, array[i]);
array[i] = -9223372036854775808;
Expect.equals(-9223372036854775808, array[i]);
}
} finally {
calloc.free(pointer);
}
}
40 changes: 40 additions & 0 deletions LibTest/ffi/Array/Int8Array_A01_t01.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
* const Array<T extends NativeType>(int dimension1)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of Int8
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<Int8> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
array[i] = i + 1;
Expect.equals(i + 1, array[i]);
array[i] = 127;
Expect.equals(127, array[i]);
array[i] = 128;
Expect.equals(-128, array[i]);
array[i] = -129;
Expect.equals(127, array[i]);
}
} finally {
calloc.free(pointer);
}
}
54 changes: 54 additions & 0 deletions LibTest/ffi/Array/IntPtrArray_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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)
* Const constructor to specify Array dimensions in Structs.
*
* @description Checks that this class represents a fixed-size array of IntPtr
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class MyStruct extends Struct {
@Array(2)
external Array<IntPtr> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
Expect.equals(0, array[i]);
array[i] = 42;
Expect.equals(42, array[i]);
array[i] = -42;
Expect.equals(-42, array[i]);
array[i] = 256;
Expect.equals(256, array[i]);
array[i] = 32767;
Expect.equals(32767, array[i]);
if (sizeOf<IntPtr>() == 4) {
array[i] = 32768;
Expect.equals(-32768, array[i]);
array[i] = -32768;
Expect.equals(-32768, array[i]);
array[i] = -32769;
Expect.equals(32767, array[i]);
} else {
array[i] = 32768;
Expect.equals(32768, array[i]);
array[i] = -32769;
Expect.equals(-32769, array[i]);
}
}
} finally {
calloc.free(pointer);
}
}

0 comments on commit 07d91ec

Please sign in to comment.