Skip to content

Commit

Permalink
dart-lang#993. [FFI] Missed tests added for some new types (dart-lang…
Browse files Browse the repository at this point in the history
…#1386)

Authored by @sgrekhov.

Missing tests added for some new types. Excessive test deleted, UintPtr fixed for 32-bit platforms.
  • Loading branch information
sgrekhov committed Aug 4, 2022
1 parent 707824e commit d973447
Show file tree
Hide file tree
Showing 23 changed files with 856 additions and 0 deletions.
41 changes: 41 additions & 0 deletions LibTest/ffi/Int/Int_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022, 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 The C int type.
///
/// Typically a signed 32-bit integer. For a guaranteed 32-bit integer, use
/// [Int32] with the C int32_t type. For an unsigned int, use [UnsignedInt].
///
/// The [Int] type is a native type, and should not be constructed in Dart code.
/// It occurs only in native type signatures and as annotation on [Struct] and
/// [Union] fields.
///
/// @description Checks that this type represents C int type.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int> p1 = calloc<Int>();
try {
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expect.equals(-42, p1.value);
p1.value = 32768;
Expect.equals(32768, p1.value);
p1.value = -32768;
Expect.equals(-32768, p1.value);
p1.value = 2147483647;
Expect.equals(2147483647, p1.value);
p1.value = 2147483648;
Expect.equals(-2147483648, p1.value);
p1.value = -2147483649;
Expect.equals(2147483647, p1.value);
} finally {
calloc.free(p1);
}
}
42 changes: 42 additions & 0 deletions LibTest/ffi/Int/Int_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2022, 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 The C int type.
///
/// Typically a signed 32-bit integer. For a guaranteed 32-bit integer, use
/// [Int32] with the C int32_t type. For an unsigned int, use [UnsignedInt].
///
/// The [Int] type is a native type, and should not be constructed in Dart code.
/// It occurs only in native type signatures and as annotation on [Struct] and
/// [Union] fields.
///
/// @description Checks that this type represents C int type.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int> p1 = calloc<Int>();
Pointer<Uint32> p2 = new Pointer<Uint32>.fromAddress(p1.address);
try {
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 = 0x7FFFFFFF;
Expect.equals(2147483647, p1.value);
p2.value = 2147483648;
Expect.equals(-2147483648, p1.value);
p2.value = -2147483649;
Expect.equals(2147483647, p1.value);
} finally {
calloc.free(p1);
}
}
29 changes: 29 additions & 0 deletions LibTest/ffi/Int/Int_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022, 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 The C int type.
///
/// Typically a signed 32-bit integer. For a guaranteed 32-bit integer, use
/// [Int32] with the C int32_t type. For an unsigned int, use [UnsignedInt].
///
/// The [Int] type is a native type, and should not be constructed in Dart code.
/// It occurs only in native type signatures and as annotation on [Struct] and
/// [Union] fields.
///
/// @description Checks that this type represents a signed 32-bit integer
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int> p1 = calloc<Int>(2);
try {
Expect.equals(4, p1.elementAt(1).address - p1.address);
Expect.equals(4, sizeOf<Int>());
} finally {
calloc.free(p1);
}
}
35 changes: 35 additions & 0 deletions LibTest/ffi/Size/Size_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2022, 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 The C size_t type.
///
/// The [Size] type is a native type, and should not be constructed in Dart
/// code. It occurs only in native type signatures and as annotation on [Struct]
/// and [Union] fields.
///
/// @description Checks that this type represents C size_t type and is big
/// enough to contain the size of the biggest object the host system can handle
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Size> p1 = calloc<Size>();
try {
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expect.equals(-42, p1.value);
p1.value = 0xFFFFFFFF; // max 32-bit
Expect.equals(0xFFFFFFFF, p1.value);
if (sizeOf<IntPtr>() == 8) { // 64-bit system
p1.value = 0xFFFFFFFFFFFFFFFF;
Expect.equals(0xFFFFFFFFFFFFFFFF, p1.value);
}
} finally {
calloc.free(p1);
}
}
21 changes: 21 additions & 0 deletions LibTest/ffi/Size/Size_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2022, 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 The C size_t type.
///
/// The [Size] type is a native type, and should not be constructed in Dart
/// code. It occurs only in native type signatures and as annotation on [Struct]
/// and [Union] fields.
///
/// @description Checks that this type represents C size_t type and is big
/// enough to contain the size of the biggest object the host system can handle
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Expect.equals(sizeOf<IntPtr>(), sizeOf<Size>());
}
51 changes: 51 additions & 0 deletions LibTest/ffi/UintPtr/UintPtr_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2022, 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 The C uintptr_t type.
///
/// The [UintPtr] type is a native type, and should not be constructed in Dart
/// code. It occurs only in native type signatures and as annotation on [Struct]
/// and [Union] fields.
///
/// @description Checks that this represents C uintptr_t type
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<UintPtr> p1 = calloc<UintPtr>();
try {
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = 256;
Expect.equals(256, p1.value);
p1.value = 32767;
Expect.equals(32767, p1.value);
p1.value = 32768;
Expect.equals(32768, p1.value);
p1.value = 0x7FFFFFFF;
Expect.equals(2147483647, p1.value);
p1.value = 0x80000000; // 2147483648
Expect.equals(2147483648, p1.value);
if (sizeOf<UintPtr>() == 4) {
p1.value = -42;
Expect.equals(4294967254, p1.value);
p1.value = -32769;
Expect.equals(4294934527, p1.value);
p1.value = -2147483649;
Expect.equals(2147483647, p1.value);
} else {
p1.value = -42;
Expect.equals(-42, p1.value);
p1.value = -32769;
Expect.equals(-32769, p1.value);
p1.value = -2147483649; // 0xFFFFFFFF7FFFFFFF
Expect.equals(-2147483649, p1.value);
}
} finally {
calloc.free(p1);
}
}
62 changes: 62 additions & 0 deletions LibTest/ffi/UintPtr/UintPtr_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2022, 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 The C uintptr_t type.
///
/// The [UintPtr] type is a native type, and should not be constructed in Dart
/// code. It occurs only in native type signatures and as annotation on [Struct]
/// and [Union] fields.
///
/// @description Checks that this represents C uintptr_t type
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<UintPtr> p1 = calloc<UintPtr>();
try {
if (sizeOf<UintPtr>() == 4) {
Pointer<IntPtr> p2 = new Pointer<IntPtr>.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 = 0x7FFFFFFF;
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);
}
}
30 changes: 30 additions & 0 deletions LibTest/ffi/UintPtr/UintPtr_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2022, 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 The C uintptr_t type.
///
/// The [UintPtr] type is a native type, and should not be constructed in Dart
/// code. It occurs only in native type signatures and as annotation on [Struct]
/// and [Union] fields.
///
/// @description Checks that this represents C uintptr_t type
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import 'package:ffi/ffi.dart';
import "../../../Utils/expect.dart";

void main() {
Pointer<UintPtr> p1 = calloc<UintPtr>(2);
try {
if (sizeOf<UintPtr>() == 4) {
Expect.equals(4, p1.elementAt(1).address - p1.address);
} else {
Expect.equals(8, p1.elementAt(1).address - p1.address);
Expect.equals(8, sizeOf<UintPtr>());
}
} finally {
calloc.free(p1);
}
}
31 changes: 31 additions & 0 deletions LibTest/ffi/UnsignedChar/UnsignedChar_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022, 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 The C unsigned char type.
///
/// Typically an unsigned 8-bit integer. For a guaranteed 8-bit integer, use
/// [Uint8] with the C uint8_t type. For a signed char, use [SignedChar].
///
/// The [UnsignedChar] type is a native type, and should not be constructed in
/// Dart code. It occurs only in native type signatures and as annotation on
/// [Struct] and [Union] fields.
///
/// @description Checks that this type represents C unsigned char type.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<UnsignedChar> p1 = calloc<UnsignedChar>();
try {
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expect.equals(214, p1.value);
} finally {
calloc.free(p1);
}
}
40 changes: 40 additions & 0 deletions LibTest/ffi/UnsignedChar/UnsignedChar_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022, 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 The C unsigned char type.
///
/// Typically an unsigned 8-bit integer. For a guaranteed 8-bit integer, use
/// [Uint8] with the C uint8_t type. For a signed char, use [SignedChar].
///
/// The [UnsignedChar] type is a native type, and should not be constructed in
/// Dart code. It occurs only in native type signatures and as annotation on
/// [Struct] and [Union] fields.
///
/// @description Checks that this type represents C unsigned char type.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<UnsignedChar> p1 = calloc<UnsignedChar>();
Pointer<Int8> p2 = new Pointer<Int8>.fromAddress(p1.address);
try {
p2.value = 0;
Expect.equals(0, p1.value);
p2.value = 1;
Expect.equals(1, p1.value);
p2.value = 127;
Expect.equals(127, p1.value);
p2.value = 128;
Expect.equals(128, p1.value);
p2.value = 255;
Expect.equals(255, p1.value);
p2.value = 256;
Expect.equals(0, p1.value);
} finally {
calloc.free(p1);
}
}

0 comments on commit d973447

Please sign in to comment.