Skip to content

Commit

Permalink
#993. IntPtr tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Feb 26, 2021
1 parent 975a228 commit 7c1b8d9
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
44 changes: 44 additions & 0 deletions LibTest/ffi/IntPtr/IntPtr_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 Represents a native pointer-sized integer in C.
*
* @description Checks that this represents a native pointer-sized integer in C.
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<IntPtr> p1 = calloc<IntPtr>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expect.equals(-42, p1.value);
p1.value = 256;
Expect.equals(256, p1.value);
p1.value = 32767;
Expect.equals(32767, p1.value);
if (sizeOf<IntPtr>() == 4) {
p1.value = 32768;
Expect.equals(-32768, p1.value);
p1.value = -32768;
Expect.equals(-32768, p1.value);
p1.value = -32769;
Expect.equals(32767, p1.value);
} else {
p1.value = 32768;
Expect.equals(32768, p1.value);
p1.value = -32769;
Expect.equals(-32769, p1.value);
}
} finally {
calloc.free(p1);
}
}
60 changes: 60 additions & 0 deletions LibTest/ffi/IntPtr/IntPtr_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 Represents a native pointer-sized integer in C.
*
* @description Checks that this represents a native pointer-sized integer in C.
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<IntPtr> p1 = calloc<IntPtr>();
try {
if (sizeOf<IntPtr>() == 4) {
Pointer<Int32> p2 = new Pointer<Int32>.fromAddress(p1.address);
p2.value = 42;
Expect.equals(42, p1.value);
p2.value = -42;
Expect.equals(-42, p1.value);
p2.value = 32768;
Expect.equals(32768, p1.value);
p2.value = -32768;
Expect.equals(-32768, p1.value);
p2.value = 2147483647;
Expect.equals(2147483647, p1.value);
p2.value = 2147483648;
Expect.equals(-2147483648, p1.value);
p2.value = -2147483649;
Expect.equals(2147483647, p1.value);
} else {
Pointer<Int64> p2 = new Pointer<Int64>.fromAddress(p1.address);
Expect.equals(0, p1.value);
p2.value = 42;
Expect.equals(42, p1.value);
p2.value = -42;
Expect.equals(-42, p1.value);
p2.value = 32768;
Expect.equals(32768, p1.value);
p2.value = -32768;
Expect.equals(-32768, p1.value);
p2.value = 2147483648;
Expect.equals(2147483648, p1.value);
p2.value = -2147483649;
Expect.equals(-2147483649, p1.value);
p2.value = 0x7FFFFFFFFFFFFFFF;
Expect.equals(9223372036854775807, p1.value);
p2.value = 0xFFFFFFFFFFFFFFFF;
Expect.equals(-1, p1.value);
p2.value = -9223372036854775808;
Expect.equals(-9223372036854775808, p1.value);
}
} finally {
calloc.free(p1);
}
}
29 changes: 29 additions & 0 deletions LibTest/ffi/IntPtr/IntPtr_A01_t03.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 Represents a native pointer-sized integer in C.
*
* @description Checks that this represents a native pointer-sized integer in C.
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import "../ffi_utils.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<IntPtr> p1 = calloc<IntPtr>(3);
try {
if (sizeOf<IntPtr>() == 4) {
Expect.equals(4, p1.elementAt(1).address - p1.address);
Expect.equals(4, p1.elementAt(2).address - p1.elementAt(1).address);
} else {
Expect.equals(8, p1.elementAt(1).address - p1.address);
Expect.equals(8, p1.elementAt(2).address - p1.elementAt(1).address);
}
} finally {
calloc.free(p1);
}
}
10 changes: 10 additions & 0 deletions LibTest/ffi/sizeOf/sizeOf_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ void main() {
sizeOf<NativeFunction>();
//^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

sizeOf<Opaque>();
//^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

sizeOf<Array>();
//^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
21 changes: 21 additions & 0 deletions LibTest/ffi/sizeOf/sizeOf_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 sizeOf <T extends NativeType>()
* Number of bytes used by native type T.
*
* Includes padding and alignment of structs.
*
* @description Checks that this function returns Number of bytes used by native
* type T
* @author sgrekhov@unipro.ru
*/
import "dart:ffi";
import '../../../Utils/expect.dart';

void main() {
Expect.isTrue(sizeOf<IntPtr>() == 4 || sizeOf<IntPtr>() == 8);
}

0 comments on commit 7c1b8d9

Please sign in to comment.