Skip to content

Commit

Permalink
#993. Pointer tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Mar 1, 2021
1 parent ba0d762 commit c3ffd4d
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LibTest/ffi/Pointer/Pointer.fromAddress_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 Pointer<T extends NativeType>.fromAddress(int ptr)
* Construction from raw integer.
*
* @description Checks that this constructor constructs pointer from a raw
* integer
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';
import '../../../Utils/expect.dart';

void main() {
Pointer<Int8> p1 = calloc<Int8>();
p1.value = 42;
Pointer<Int8> p2 = new Pointer.fromAddress(p1.address);
Expect.equals(42, p2.value);
Expect.equals(p1.address, p2.address);
}
37 changes: 37 additions & 0 deletions LibTest/ffi/Pointer/Pointer_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 Pointer<T extends NativeType>
* Represents a pointer into the native C memory. Cannot be extended.
*
* @description Checks that this class cannot be extended
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";

abstract class C1<T extends NativeType> extends Pointer<T> {
// ^^^^^^^^^^
// [cfe] unspecified
// [analyzer] unspecified
}

abstract class C2 extends Pointer<Int8> {
// ^^^^^^^^^^^^^
// [cfe] unspecified
// [analyzer] unspecified
}

abstract class C3 implements Pointer<Int8> {
// ^^^^^^^^^^^^^
// [cfe] unspecified
// [analyzer] unspecified
}

void main() {
C1? c1;
C2? c2;
C3? c3;
}
27 changes: 27 additions & 0 deletions LibTest/ffi/Pointer/address_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 int address
* Access to the raw pointer value. On 32-bit systems, the upper 32-bits of the
* result are 0.
*
* @description Checks that this getter returns the raw pointer value
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';
import '../../../Utils/expect.dart';

void main() {
Pointer<Int8> p1 = calloc<Int8>();
try {
Expect.isTrue(p1.address != 0);
Pointer<Int8> p2 = calloc<Int8>();
Expect.notEquals(p1.address, p2.address);
} finally {
calloc.free(p1);
}
}
28 changes: 28 additions & 0 deletions LibTest/ffi/Pointer/elementAt_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 Pointer<T> elementAt(int index)
* Pointer arithmetic (takes element size into account).
* This method must be invoked with a compile-time constant T.
* Does not accept dynamic invocations -- where the type of the receiver is
* dynamic.
*
* @description Checks that this method implements pointer arithmetic
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';
import '../../../Utils/expect.dart';

void main() {
Pointer<Int16> p1 = calloc<Int16>(3);
try {
Expect.equals(2, p1.elementAt(1).address - p1.address);
Expect.equals(2, p1.elementAt(2).address - p1.elementAt(1).address);
} finally {
calloc.free(p1);
}
}
31 changes: 31 additions & 0 deletions LibTest/ffi/Pointer/elementAt_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 Pointer<T> elementAt(int index)
* Pointer arithmetic (takes element size into account).
* This method must be invoked with a compile-time constant T.
* Does not accept dynamic invocations -- where the type of the receiver is
* dynamic.
*
* @description Checks that this method must be invoked with a compile-time
* constant T
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';
import '../../../Utils/expect.dart';

void main() {
Pointer<Int64> p1 = calloc<Int64>(3);
try {
Pointer<Void> p2 = new Pointer.fromAddress(p1.address);
Expect.throws(() {
p2.elementAt(1);
});
} finally {
calloc.free(p1);
}
}
25 changes: 25 additions & 0 deletions LibTest/ffi/Pointer/elementAt_A03_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 Pointer<T> elementAt(int index)
* Pointer arithmetic (takes element size into account).
* This method must be invoked with a compile-time constant T.
* Does not accept dynamic invocations -- where the type of the receiver is
* dynamic.
*
* @description Checks that this method does not accept dynamic invocations
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';

void main() {
Pointer p1 = calloc<Int64>(3);
p1.elementAt(1);
//^^^^^^^^^^^^^^^
// [cfe] unspecified
// [analyzer] unspecified
}
29 changes: 29 additions & 0 deletions LibTest/ffi/Pointer/elementAt_A04_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 Pointer<T> elementAt(int index)
* Pointer arithmetic (takes element size into account).
* This method must be invoked with a compile-time constant T.
* Does not accept dynamic invocations -- where the type of the receiver is
* dynamic.
*
* @description Checks negative argument value
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';
import '../../../Utils/expect.dart';

void main() {
Pointer<Int8> p1 = calloc<Int8>(3);
try {
p1.elementAt(0).value = 42;
Pointer<Int8> p2 = new Pointer.fromAddress(p1.elementAt(1).address);
Expect.equals(42, p2.elementAt(-1).value);
} finally {
calloc.free(p1);
}
}
26 changes: 26 additions & 0 deletions LibTest/ffi/Pointer/elementAt_A04_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 Pointer<T> elementAt(int index)
* Pointer arithmetic (takes element size into account).
* This method must be invoked with a compile-time constant T.
* Does not accept dynamic invocations -- where the type of the receiver is
* dynamic.
*
* @description Checks that it is allowed to read memory outside the border
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../ffi_utils.dart';

void main() {
Pointer<Int8> p1 = calloc<Int8>(3);
try {
p1.elementAt(42);
} finally {
calloc.free(p1);
}
}

0 comments on commit c3ffd4d

Please sign in to comment.