Skip to content

Commit

Permalink
#993. More Struct tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey G. Grekhov committed Feb 12, 2021
1 parent 8920358 commit d45cb88
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LibTest/ffi/Struct/Struct_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @description Checks that it is a compile error if any of the field in Struct
* subclass is not 'int', 'double' or 'Pointer' or subtype of 'Struct'
* @author sgrekhov@unipro.ru
* @issue 44986
*/
import "dart:ffi";

Expand Down Expand Up @@ -51,5 +52,4 @@ void main() {
S2? s2;
S3? s3;
S4? s4;
S5? s5;
}
1 change: 1 addition & 0 deletions LibTest/ffi/Struct/Struct_A01_t05.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @description Checks that it is a compile error if any of the field in Struct
* subclass is not 'int', 'double' or 'Pointer' or subtype of 'Struct'
* @author sgrekhov@unipro.ru
* @issue 44985
*/
import "dart:ffi";

Expand Down
25 changes: 25 additions & 0 deletions LibTest/ffi/Struct/Struct_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 Struct()
* Construct a reference to the nullptr.
*
* @description Checks that this constructor constructs a reference to the
* nullptr.
* @author sgrekhov@unipro.ru
* @issue 44987
*/
import "dart:ffi";
import "../../../Utils/expect.dart";

class S1 extends Struct {
S1() : super();
}

void main() {
S1 s1 = new S1();
Expect.equals(nullptr.address, s1.addressOf.address);
}
50 changes: 50 additions & 0 deletions LibTest/ffi/Struct/Struct_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 Struct()
* The supertype of all FFI struct types.
*
* FFI struct types should extend this class and declare fields corresponding
* to the underlying native structure.
*
* @description Checks that FFI struct types should extend this class and
* declare fields corresponding to the underlying native structure
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class Coord extends Struct {
@Double()
external double x;
@Double()
external double y;

external Pointer<Coord> next;
}

// Native C function
typedef TransposeCoordinateCFunc = Pointer<Coord> Function(Pointer<Coord>);
// Dart function
typedef TransposeCoordinateDartFunc = Pointer<Coord> Function(Pointer<Coord>);

void main() {
final dl = new DynamicLibrary.open(libPath(TEST_FUNCTIONS_LIB));
final TransposeCoordinateDartFunc f = dl
.lookup<NativeFunction<TransposeCoordinateCFunc>>('TransposeCoordinate')
.asFunction();
Pointer<Coord> c1 = calloc<Coord>();
try {
c1.ref.x = 1;
c1.ref.y = 2;
f(c1);
Expect.equals(11, c1.ref.x);
Expect.equals(12, c1.ref.y);
} finally {
calloc.free(c1);
}
}
54 changes: 54 additions & 0 deletions LibTest/ffi/Struct/Struct_A03_t02.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 Struct()
* The supertype of all FFI struct types.
*
* FFI struct types should extend this class and declare fields corresponding
* to the underlying native structure.
*
* @description Checks that FFI struct types should extend this class and
* declare fields corresponding to the underlying native structure. Test
* nullable Struct subtype members
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

class Coord extends Struct {
Coord() : super();

@Double()
external double? x;
@Double()
external double? y;

external Pointer<Coord>? next;
}

// Native C function
typedef TransposeCoordinateCFunc = Pointer<Coord> Function(Pointer<Coord>);
// Dart function
typedef TransposeCoordinateDartFunc = Pointer<Coord> Function(Pointer<Coord>);

void main() {
final dl = new DynamicLibrary.open(libPath(TEST_FUNCTIONS_LIB));
final TransposeCoordinateDartFunc f = dl
.lookup<NativeFunction<TransposeCoordinateCFunc>>('TransposeCoordinate')
.asFunction();
Coord c = new Coord();
Pointer<Coord> c1 = calloc<Coord>();
try {
c1.ref.x = 1;
c1.ref.y = 2;
f(c1);
Expect.equals(11, c1.ref.x);
Expect.equals(12, c1.ref.y);
} finally {
calloc.free(c1);
}
}

0 comments on commit d45cb88

Please sign in to comment.