dart-sdk/tests/ffi/inline_array_test.dart
Daco Harkes ffa5d16ae7 [vm/ffi] Support multi-dimensional inline arrays
This CL only changes dart:ffi API, CFE, and analyzer. No VM changes
were needed because the dimensions of inline arrays can be flattened
before passing them to the VM. The multi-dimensionality does not
impact the ABI.

Closes: https://github.com/dart-lang/sdk/issues/45023

TEST=pkg/analyzer/test/src/diagnostics/size_annotation_dimensions_test.dart
TEST=pkg/front_end/testcases/nnbd/ffi_struct_inline_array_multi_dimensional.dart
TEST=tests/ffi/function_structs_by_value_generated_test.dart
TEST=tests/ffi/inline_array_multi_dimensional_test.dart

Change-Id: Ica2c01fccbea7e513879365b34086d8968b54c5b
Cq-Include-Trybots: luci.dart.try:dart-sdk-linux-try,dart-sdk-mac-try,dart-sdk-win-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-kernel-nnbd-mac-release-x64-try,vm-kernel-nnbd-win-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-simarm_x64-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-precomp-android-release-arm_x64-try,analyzer-analysis-server-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/188286
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2021-03-09 10:50:17 +00:00

91 lines
2.4 KiB
Dart

// 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.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import "package:expect/expect.dart";
import 'package:ffi/ffi.dart';
// Reuse struct definitions.
import 'function_structs_by_value_generated_test.dart';
void main() {
testSizeOf();
testLoad();
testStore();
testToString();
testRange();
}
void testSizeOf() {
Expect.equals(8, sizeOf<Struct8BytesInlineArrayInt>());
Expect.equals(10, sizeOf<StructInlineArrayIrregular>());
Expect.equals(4008, sizeOf<StructInlineArrayBig>());
}
// Only tests with Pointer as backing store.
void testLoad() {
final pointer = calloc<Struct8BytesInlineArrayInt>();
final struct = pointer.ref;
final array = struct.a0;
pointer.cast<Uint8>()[0] = 42;
pointer.cast<Uint8>()[7] = 3;
Expect.equals(42, array[0]);
Expect.equals(3, array[7]);
calloc.free(pointer);
}
// Only tests with Pointer as backing store.
void testStore() {
final pointer = calloc<Struct8BytesInlineArrayInt>();
pointer.cast<Uint8>()[0] = 42;
pointer.cast<Uint8>()[7] = 3;
final pointer2 = calloc<Struct8BytesInlineArrayInt>();
pointer2.ref.a0 = pointer.ref.a0;
Expect.equals(42, pointer2.ref.a0[0]);
Expect.equals(3, pointer2.ref.a0[7]);
calloc.free(pointer);
calloc.free(pointer2);
}
// Tests the toString of the test generator.
void testToString() {
final pointer = calloc<Struct8BytesInlineArrayInt>();
final struct = pointer.ref;
final array = struct.a0;
for (var i = 0; i < 8; i++) {
array[i] = i;
}
Expect.equals("([0, 1, 2, 3, 4, 5, 6, 7])", struct.toString());
calloc.free(pointer);
final pointer2 = calloc<StructInlineArrayIrregular>();
final struct2 = pointer2.ref;
struct2.a0[0].a0 = 0;
struct2.a0[0].a1 = 1;
struct2.a0[1].a0 = 2;
struct2.a0[1].a1 = 3;
struct2.a1 = 4;
print(struct2);
Expect.equals("([(0, 1), (2, 3)], 4)", struct2.toString());
calloc.free(pointer2);
}
void testRange() {
final pointer = calloc<Struct8BytesInlineArrayInt>();
final struct = pointer.ref;
final array = struct.a0;
array[0] = 1;
Expect.equals(1, array[0]);
array[7] = 7;
Expect.equals(7, array[7]);
Expect.throws(() => array[-1]);
Expect.throws(() => array[-1] = 0);
Expect.throws(() => array[8]);
Expect.throws(() => array[8] = 0);
calloc.free(pointer);
}