dart-sdk/tests/ffi/external_typed_data_test.dart
Daco Harkes 9920801424 [vm/ffi] Split FFI tests in NNBD and legacy
Closes: https://github.com/dart-lang/sdk/issues/40233

This CL creates nnbd versions of the tests and runs them on the nnbd sdk.

This CL does not (1) migrate sdk_nnbd/lib/ffi fully yet, and does not (2) fix all the tests/ffi (which is NNBD tests) yet.

Uncovered new issues:
Issue: https://github.com/dart-lang/sdk/issues/40234 nullptr should have type Pointer<Never>.
Issue: https://github.com/dart-lang/sdk/issues/40247 Structs need external fields.
Issue: https://github.com/dart-lang/sdk/issues/40271 Callbacks hit assert in debug.

Change-Id: Icb1b83577e03ed283165eb17703fc8dfc7fa5960
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,analyzer-nnbd-linux-release-try,dart2js-nnbd-linux-x64-chrome-try,ddc-nnbd-linux-release-chrome-try,front-end-nnbd-linux-release-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132604
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2020-01-29 14:04:41 +00:00

296 lines
6.9 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.
import 'dart:ffi';
import 'dart:math';
import 'dart:typed_data';
import 'package:expect/expect.dart';
import "package:ffi/ffi.dart";
main() {
testInt8Load();
testInt8Store();
testUint8Load();
testUint8Store();
testInt16Load();
testInt16Store();
testUint16Load();
testUint16Store();
testInt32Load();
testInt32Store();
testUint32Load();
testUint32Store();
testInt64Load();
testInt64Store();
testUint64Load();
testUint64Store();
testFloatLoad();
testFloatStore();
testDoubleLoad();
testDoubleStore();
testArrayLoad();
testArrayStore();
testNegativeArray();
testAlignment();
}
// For signed int tests, we store 0xf* and load -1 to check sign-extension.
// For unsigned int tests, we store 0xf* and load the same to check truncation.
void testInt8Load() {
// Load
Pointer<Int8> ptr = allocate();
ptr.value = 0xff;
Int8List list = ptr.asTypedList(1);
Expect.equals(list[0], -1);
Expect.equals(list.length, 1);
free(ptr);
}
void testInt8Store() {
// Store
Pointer<Int8> ptr = allocate();
Int8List list = ptr.asTypedList(1);
list[0] = 0xff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, -1);
free(ptr);
}
void testUint8Load() {
// Load
Pointer<Uint8> ptr = allocate();
ptr.value = 0xff;
Uint8List list = ptr.asTypedList(1);
Expect.equals(list[0], 0xff);
Expect.equals(list.length, 1);
free(ptr);
}
void testUint8Store() {
// Store
Pointer<Uint8> ptr = allocate();
Uint8List list = ptr.asTypedList(1);
list[0] = 0xff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, 0xff);
free(ptr);
}
void testInt16Load() {
// Load
Pointer<Int16> ptr = allocate();
ptr.value = 0xffff;
Int16List list = ptr.asTypedList(1);
Expect.equals(list[0], -1);
Expect.equals(list.length, 1);
free(ptr);
}
void testInt16Store() {
// Store
Pointer<Int16> ptr = allocate();
Int16List list = ptr.asTypedList(1);
list[0] = 0xffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, -1);
free(ptr);
}
void testUint16Load() {
// Load
Pointer<Uint16> ptr = allocate();
ptr.value = 0xffff;
Uint16List list = ptr.asTypedList(1);
Expect.equals(list[0], 0xffff);
Expect.equals(list.length, 1);
free(ptr);
}
void testUint16Store() {
// Store
Pointer<Uint16> ptr = allocate();
Uint16List list = ptr.asTypedList(1);
list[0] = 0xffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, 0xffff);
free(ptr);
}
void testInt32Load() {
// Load
Pointer<Int32> ptr = allocate();
ptr.value = 0xffffffff;
Int32List list = ptr.asTypedList(1);
Expect.equals(list[0], -1);
Expect.equals(list.length, 1);
free(ptr);
}
void testInt32Store() {
// Store
Pointer<Int32> ptr = allocate();
Int32List list = ptr.asTypedList(1);
list[0] = 0xffffffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, -1);
free(ptr);
}
void testUint32Load() {
// Load
Pointer<Uint32> ptr = allocate();
ptr.value = 0xffffffff;
Uint32List list = ptr.asTypedList(1);
Expect.equals(list[0], 0xffffffff);
Expect.equals(list.length, 1);
free(ptr);
}
void testUint32Store() {
// Store
Pointer<Uint32> ptr = allocate();
Uint32List list = ptr.asTypedList(1);
list[0] = 0xffffffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, 0xffffffff);
free(ptr);
}
void testInt64Load() {
// Load
Pointer<Int64> ptr = allocate();
ptr.value = 0xffffffffffffffff;
Int64List list = ptr.asTypedList(1);
Expect.equals(list[0], -1);
Expect.equals(list.length, 1);
free(ptr);
}
void testInt64Store() {
// Store
Pointer<Int64> ptr = allocate();
Int64List list = ptr.asTypedList(1);
list[0] = 0xffffffffffffffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, -1);
free(ptr);
}
void testUint64Load() {
// Load
Pointer<Uint64> ptr = allocate();
ptr.value = 0xffffffffffffffff;
Uint64List list = ptr.asTypedList(1);
Expect.equals(list[0], 0xffffffffffffffff);
Expect.equals(list.length, 1);
free(ptr);
}
void testUint64Store() {
// Store
Pointer<Uint64> ptr = allocate();
Uint64List list = ptr.asTypedList(1);
list[0] = 0xffffffffffffffff;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, 0xffffffffffffffff);
free(ptr);
}
double maxFloat = (2 - pow(2, -23)) * pow(2, 127) as double;
double maxDouble = (2 - pow(2, -52)) * pow(2, pow(2, 10) - 1) as double;
void testFloatLoad() {
// Load
Pointer<Float> ptr = allocate();
ptr.value = maxFloat;
Float32List list = ptr.asTypedList(1);
Expect.equals(list[0], maxFloat);
Expect.equals(list.length, 1);
free(ptr);
}
void testFloatStore() {
// Store
Pointer<Float> ptr = allocate();
Float32List list = ptr.asTypedList(1);
list[0] = maxFloat;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, maxFloat);
free(ptr);
}
void testDoubleLoad() {
// Load
Pointer<Double> ptr = allocate();
ptr.value = maxDouble;
Float64List list = ptr.asTypedList(1);
Expect.equals(list[0], maxDouble);
Expect.equals(list.length, 1);
free(ptr);
}
void testDoubleStore() {
// Store
Pointer<Double> ptr = allocate();
Float64List list = ptr.asTypedList(1);
list[0] = maxDouble;
Expect.equals(list.length, 1);
Expect.equals(ptr.value, maxDouble);
free(ptr);
}
void testArrayLoad() {
const int count = 0x100;
Pointer<Int32> ptr = allocate(count: count);
for (int i = 0; i < count; ++i) {
ptr[i] = i;
}
Int32List array = ptr.asTypedList(count);
for (int i = 0; i < count; ++i) {
Expect.equals(array[i], i);
}
free(ptr);
}
void testArrayStore() {
const int count = 0x100;
Pointer<Int32> ptr = allocate(count: count);
Int32List array = ptr.asTypedList(count);
for (int i = 0; i < count; ++i) {
array[i] = i;
}
for (int i = 0; i < count; ++i) {
Expect.equals(ptr[i], i);
}
free(ptr);
}
void testNegativeArray() {
Pointer<Int32> ptr = nullptr;
Expect.throws<ArgumentError>(() => ptr.asTypedList(-1));
}
// Tests that the address we're creating an ExternalTypedData from is aligned to
// the element size.
void testAlignment() {
Expect.throws<ArgumentError>(
() => Pointer<Int16>.fromAddress(1).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Int32>.fromAddress(2).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Int64>.fromAddress(4).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Uint16>.fromAddress(1).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Uint32>.fromAddress(2).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Uint64>.fromAddress(4).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Float>.fromAddress(2).asTypedList(1));
Expect.throws<ArgumentError>(
() => Pointer<Double>.fromAddress(4).asTypedList(1));
}