dart-sdk/tests/ffi/function_structs_test.dart
Martin Kustermann b0155a72a7 [vm/ffi] Add script to extract existing positive ffi tests into bundle to be used for flutter/flutter integration test
The `tests/ffi/prepare_flutter_bundle.dart` script will try to discover
all synchronous, positive test from the "ffi" test suite, rewrite them
slightly and output a directory which can be used in a flutter/flutter
FFI integration test.

We split the ffi test functions into two parts, because a subset of the
C functions will issue calls to the VM via `dart_api.h`, which is not
available for the flutter/flutter integration test.

=> We make `runtime/bin/ffi_test/ffi_test_functions.cc` a pure C
   library, usable without `dart_api.h` and move the remaining VM
   specific code to .../ffi_test_functions_special.cc contains.

All tests from `tests/ffi/*_test.dart` will be included in the generated
bundle, which

   * don't use async/isolates
   * don't rely on VM api
   * don't rely on DynamicLibrary.{process,executable}
   * don't take too long to execute

The script can be used as follows:

   sdk % dart tests/ffi/prepare_flutter_bundle.dart foo
   Using SDK root: .../sdk
   The following tests will be included:
      aliasing_test.dart
      data_not_asan_test.dart
      data_test.dart
      extension_methods_test.dart
      external_typed_data_test.dart
      function_structs_test.dart
      negative_function_test.dart
      regress_37254_test.dart
      regress_39044_test.dart
      regress_39063_test.dart
      regress_39068_test.dart
      stacktrace_regress_37910_test.dart
      structs_test.dart
      variance_function_test.dart
   The following tests were filtered due to using dart_api.h/async/DynamicLibrary.{process,executable}/...
      function_callbacks_test.dart
      function_gc_test.dart
      function_test.dart
      object_gc_test.dart
      regress_37100_test.dart
      regress_37511_callbacks_test.dart
      regress_37511_test.dart
      regress_37780_test.dart

   Please copy generated files into FFI flutter test application
      * foo/lib/src/generated
      * foo/ios/Classes

Change-Id: Ia13f97df3bbc90829bb8fde8265a7e1d2c0f8260
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127006
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2019-12-04 13:02:48 +00:00

119 lines
3.2 KiB
Dart

// Copyright (c) 2019, 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.
//
// Dart test program for testing dart:ffi function pointers with struct
// arguments.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import 'dylib_utils.dart';
import "package:expect/expect.dart";
import "package:ffi/ffi.dart";
import 'coordinate.dart';
import 'very_large_struct.dart';
typedef NativeCoordinateOp = Pointer<Coordinate> Function(Pointer<Coordinate>);
void main() {
testFunctionWithStruct();
// testFunctionWithStructArray();
// testFunctionWithVeryLargeStruct();
}
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
/// pass a struct to a c function and get a struct as return value
void testFunctionWithStruct() {
Pointer<NativeFunction<NativeCoordinateOp>> p1 =
ffiTestFunctions.lookup("TransposeCoordinate");
NativeCoordinateOp f1 = p1.asFunction();
Pointer<Coordinate> c1 = Coordinate.allocate(10.0, 20.0, nullptr).addressOf;
Pointer<Coordinate> c2 = Coordinate.allocate(42.0, 84.0, c1).addressOf;
c1.ref.next = c2;
Coordinate result = f1(c1).ref;
Expect.approxEquals(20.0, c1.ref.x);
Expect.approxEquals(30.0, c1.ref.y);
Expect.approxEquals(42.0, result.x);
Expect.approxEquals(84.0, result.y);
free(c1);
free(c2);
}
/// pass an array of structs to a c funtion
void testFunctionWithStructArray() {
Pointer<NativeFunction<NativeCoordinateOp>> p1 =
ffiTestFunctions.lookup("CoordinateElemAt1");
NativeCoordinateOp f1 = p1.asFunction();
Coordinate c1 = allocate<Coordinate>(count: 3).ref;
Coordinate c2 = c1.addressOf[1];
Coordinate c3 = c1.addressOf[2];
c1.x = 10.0;
c1.y = 10.0;
c1.next = c3.addressOf;
c2.x = 20.0;
c2.y = 20.0;
c2.next = c1.addressOf;
c3.x = 30.0;
c3.y = 30.0;
c3.next = c2.addressOf;
Coordinate result = f1(c1.addressOf).ref;
Expect.approxEquals(20.0, result.x);
Expect.approxEquals(20.0, result.y);
free(c1.addressOf);
}
typedef VeryLargeStructSum = int Function(Pointer<VeryLargeStruct>);
typedef NativeVeryLargeStructSum = Int64 Function(Pointer<VeryLargeStruct>);
void testFunctionWithVeryLargeStruct() {
Pointer<NativeFunction<NativeVeryLargeStructSum>> p1 =
ffiTestFunctions.lookup("SumVeryLargeStruct");
VeryLargeStructSum f = p1.asFunction();
VeryLargeStruct vls1 = allocate<VeryLargeStruct>(count: 2).ref;
VeryLargeStruct vls2 = vls1.addressOf[1];
List<VeryLargeStruct> structs = [vls1, vls2];
for (VeryLargeStruct struct in structs) {
struct.a = 1;
struct.b = 2;
struct.c = 4;
struct.d = 8;
struct.e = 16;
struct.f = 32;
struct.g = 64;
struct.h = 128;
struct.i = 256;
struct.j = 512;
struct.k = 1024;
struct.smallLastField = 1;
}
vls1.parent = vls2.addressOf;
vls1.numChildren = 2;
vls1.children = vls1.addressOf;
vls2.parent = vls2.addressOf;
vls2.parent = nullptr;
vls2.numChildren = 0;
vls2.children = nullptr;
int result = f(vls1.addressOf);
Expect.equals(2051, result);
result = f(vls2.addressOf);
Expect.equals(2048, result);
free(vls1.addressOf);
}