Files
sdk/tests/ffi/structs_nested_test.dart
T
Daco Harkes 7fbbe4fca3 [vm/ffi] Migrate off .addressOf in tests
Removes invocations of `.addressOf` in tests.

Removes the constructor from the Coordinate testing class which
has become useless with no access to the underlying pointer.

Removes the bare variant of Coordinate because it is identical to the
non-bare variant.

Issue: https://github.com/dart-lang/sdk/issues/40667

Change-Id: Ie26fa3a9cac38e794e93f5fadfec2562a814c1ae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181403
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2021-01-28 21:40:15 +00:00

105 lines
2.3 KiB
Dart

// Copyright (c) 2020, 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.
//
// This tests the non trampoline aspects of nested structs.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import "package:expect/expect.dart";
import "package:ffi/ffi.dart";
import 'calloc.dart';
import 'dylib_utils.dart';
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
void main() {
for (int i = 0; i < 10; ++i) {
testSizeOf();
testAllocate();
testRead();
testWrite();
testCopy();
}
}
class Struct4BytesHomogeneousInt16 extends Struct {
@Int16()
external int a0;
@Int16()
external int a1;
String toString() => "(${a0}, ${a1})";
}
class Struct8BytesNestedInt extends Struct {
external Struct4BytesHomogeneousInt16 a0;
external Struct4BytesHomogeneousInt16 a1;
String toString() => "(${a0}, ${a1})";
}
void testSizeOf() {
print(sizeOf<Struct8BytesNestedInt>());
Expect.equals(8, sizeOf<Struct8BytesNestedInt>());
}
void testAllocate() {
final p = calloc<Struct8BytesNestedInt>();
Expect.type<Pointer<Struct8BytesNestedInt>>(p);
print(p);
calloc.free(p);
}
/// Test that reading does not segfault, even uninitialized.
void testRead() {
print("read");
final p = calloc<Struct8BytesNestedInt>();
print(p);
print(p.ref.runtimeType);
print(p.address);
print(p.ref.a0.runtimeType);
print(p.ref.a0.a0);
calloc.free(p);
print("read");
}
void testWrite() {
print("write");
final p = calloc<Struct8BytesNestedInt>(2);
p[0].a0.a0 = 12;
p[0].a0.a1 = 13;
p[0].a1.a0 = 14;
p[0].a1.a1 = 15;
p[1].a0.a0 = 16;
p[1].a0.a1 = 17;
p[1].a1.a0 = 18;
p[1].a1.a1 = 19;
Expect.equals(12, p[0].a0.a0);
Expect.equals(13, p[0].a0.a1);
Expect.equals(14, p[0].a1.a0);
Expect.equals(15, p[0].a1.a1);
Expect.equals(16, p[1].a0.a0);
Expect.equals(17, p[1].a0.a1);
Expect.equals(18, p[1].a1.a0);
Expect.equals(19, p[1].a1.a1);
calloc.free(p);
print("written");
}
void testCopy() {
print("copy");
final p = calloc<Struct8BytesNestedInt>();
p.ref.a0.a0 = 12;
p.ref.a0.a1 = 13;
p.ref.a1 = p.ref.a0;
Expect.equals(12, p.ref.a1.a0);
Expect.equals(13, p.ref.a1.a1);
calloc.free(p);
print("copied");
}