Skip to content

Commit

Permalink
#993. FFI tests refactored and improved (#1387)
Browse files Browse the repository at this point in the history
Authored by @sgrekhov.
  • Loading branch information
sgrekhov committed Aug 4, 2022
1 parent 81a96e7 commit b572e42
Show file tree
Hide file tree
Showing 37 changed files with 113 additions and 113 deletions.
2 changes: 1 addition & 1 deletion LibTest/ffi/Bool/Bool_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/// @assertion Represents a native bool in C.
///
/// Bool is not constructible in the Dart code and serves purely as marker in
/// [Bool] is not constructible in the Dart code and serves purely as marker in
/// type signatures.
///
/// @description Checks that this type represents a native bool in C.
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/Bool/Bool_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/// @assertion Represents a native bool in C.
///
/// Bool is not constructible in the Dart code and serves purely as marker in
/// [Bool] is not constructible in the Dart code and serves purely as marker in
/// type signatures.
///
/// @description Checks that this type represents a native bool in C.
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Bool/Bool_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/// @assertion Represents a native bool in C.
///
/// Bool is not constructible in the Dart code and serves purely as marker in
/// [Bool] is not constructible in the Dart code and serves purely as marker in
/// type signatures.
///
/// @description Checks that this type represents a native bool in C.
Expand All @@ -15,7 +15,7 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Bool> p1 = calloc<Bool>(3);
Pointer<Bool> p1 = calloc<Bool>(2);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, sizeOf<Bool>());
Expand Down
9 changes: 4 additions & 5 deletions LibTest/ffi/Char/Char_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
///
/// Typically a signed or unsigned 8-bit integer. For a guaranteed 8-bit
/// integer, use [Int8] with the C int8_t type or [Uint8] with the C uint8_t
/// type.
/// For a specifically signed or unsigned char, use [SignedChar] or
/// type. For a specifically signed or unsigned char, use [SignedChar] or
/// [UnsignedChar].
///
/// The Char 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.
/// The [Char] 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 native char in C.
/// @author sgrekhov22@gmail.com
Expand Down
9 changes: 4 additions & 5 deletions LibTest/ffi/Char/Char_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
///
/// Typically a signed or unsigned 8-bit integer. For a guaranteed 8-bit
/// integer, use [Int8] with the C int8_t type or [Uint8] with the C uint8_t
/// type.
/// For a specifically signed or unsigned char, use [SignedChar] or
/// type. For a specifically signed or unsigned char, use [SignedChar] or
/// [UnsignedChar].
///
/// The Char 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.
/// The [Char] 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 native char in C.
/// @author sgrekhov22@gmail.com
Expand Down
11 changes: 5 additions & 6 deletions LibTest/ffi/Char/Char_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
///
/// Typically a signed or unsigned 8-bit integer. For a guaranteed 8-bit
/// integer, use [Int8] with the C int8_t type or [Uint8] with the C uint8_t
/// type.
/// For a specifically signed or unsigned char, use [SignedChar] or
/// type. For a specifically signed or unsigned char, use [SignedChar] or
/// [UnsignedChar].
///
/// The Char 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.
/// The [Char] 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 native char in C.
/// @author sgrekhov22@gmail.com
Expand All @@ -22,7 +21,7 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Char> p1 = calloc<Char>(3);
Pointer<Char> p1 = calloc<Char>(2);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, sizeOf<Char>());
Expand Down
1 change: 0 additions & 1 deletion LibTest/ffi/Int16/Int16_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../../../Utils/expect.dart";
void main() {
Pointer<Int16> p1 = calloc<Int16>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/Int16/Int16_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../../../Utils/expect.dart";

void main() {
Pointer<Int16> p1 = calloc<Int16>();
Pointer<Int32> p2 = new Pointer<Int32>.fromAddress(p1.address);
Pointer<Uint16> p2 = new Pointer<Uint16>.fromAddress(p1.address);
try {
p2.value = 42;
Expect.equals(42, p1.value);
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Int16/Int16_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int16> p1 = calloc<Int16>(3);
Pointer<Int16> p1 = calloc<Int16>(2);
try {
Expect.equals(2, p1.elementAt(1).address - p1.address);
Expect.equals(2, p1.elementAt(2).address - p1.elementAt(1).address);
Expect.equals(2, sizeOf<Int16>());
} finally {
calloc.free(p1);
}
Expand Down
1 change: 0 additions & 1 deletion LibTest/ffi/Int32/Int32_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../../../Utils/expect.dart";
void main() {
Pointer<Int32> p1 = calloc<Int32>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/Int32/Int32_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../../../Utils/expect.dart";

void main() {
Pointer<Int32> p1 = calloc<Int32>();
Pointer<Int64> p2 = new Pointer<Int64>.fromAddress(p1.address);
Pointer<Uint32> p2 = new Pointer<Uint32>.fromAddress(p1.address);
try {
p2.value = 42;
Expect.equals(42, p1.value);
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Int32/Int32_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int32> p1 = calloc<Int32>(3);
Pointer<Int32> p1 = calloc<Int32>(2);
try {
Expect.equals(4, p1.elementAt(1).address - p1.address);
Expect.equals(4, p1.elementAt(2).address - p1.elementAt(1).address);
Expect.equals(4, sizeOf<Int32>());
} finally {
calloc.free(p1);
}
Expand Down
1 change: 0 additions & 1 deletion LibTest/ffi/Int64/Int64_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../../../Utils/expect.dart";
void main() {
Pointer<Int64> p1 = calloc<Int64>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Int64/Int64_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int64> p1 = calloc<Int64>(3);
Pointer<Int64> p1 = calloc<Int64>(2);
try {
Expect.equals(8, p1.elementAt(1).address - p1.address);
Expect.equals(8, p1.elementAt(2).address - p1.elementAt(1).address);
Expect.equals(8, sizeOf<Int64>());
} finally {
calloc.free(p1);
}
Expand Down
1 change: 0 additions & 1 deletion LibTest/ffi/Int8/Int8_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../../../Utils/expect.dart";
void main() {
Pointer<Int8> p1 = calloc<Int8>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = -42;
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/Int8/Int8_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../../../Utils/expect.dart";

void main() {
Pointer<Int8> p1 = calloc<Int8>();
Pointer<Int16> p2 = new Pointer<Int16>.fromAddress(p1.address);
Pointer<Uint8> p2 = new Pointer<Uint8>.fromAddress(p1.address);
try {
p2.value = 42;
Expect.equals(42, p1.value);
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Int8/Int8_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Int8> p1 = calloc<Int8>(3);
Pointer<Int8> p1 = calloc<Int8>(2);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, p1.elementAt(2).address - p1.elementAt(1).address);
Expect.equals(1, sizeOf<Int8>());
} finally {
calloc.free(p1);
}
Expand Down
7 changes: 3 additions & 4 deletions LibTest/ffi/IntPtr/IntPtr_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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;
Expand All @@ -27,10 +26,10 @@ void main() {
Expect.equals(32768, p1.value);
p1.value = -32769;
Expect.equals(-32769, p1.value);
p1.value = 0x7FFFFFFF;
Expect.equals(2147483647, p1.value);
if (sizeOf<IntPtr>() == 4) {
p1.value = 2147483647;
Expect.equals(2147483647, p1.value);
p1.value = 2147483648;
p1.value = 0x80000000;
Expect.equals(-2147483648, p1.value);
p1.value = -2147483649;
Expect.equals(2147483647, p1.value);
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/IntPtr/IntPtr_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
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);
Expect.equals(8, sizeOf<IntPtr>());
}
} finally {
calloc.free(p1);
Expand Down
2 changes: 2 additions & 0 deletions LibTest/ffi/Long/Long_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void main() {
Expect.equals(32768, p1.value);
p1.value = 0x7FFFFFFF;
Expect.equals(0x7FFFFFFF, p1.value);
p1.value = 0x80000000;
Expect.equals(-2147483648, p1.value);
} finally {
calloc.free(p1);
}
Expand Down
8 changes: 4 additions & 4 deletions LibTest/ffi/LongLong/LongLong_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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.

Expand All @@ -13,17 +13,17 @@
/// and [Union] fields.
///
/// @description Checks that this type is a signed 64-bit integer
/// @author sgrekhov@unipro.ru
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<LongLong> p1 = calloc<LongLong>(3);
Pointer<LongLong> p1 = calloc<LongLong>(2);
try {
Expect.equals(8, p1.elementAt(1).address - p1.address);
Expect.equals(8, sizeOf<LongLong>);
Expect.equals(8, sizeOf<LongLong>());
} finally {
calloc.free(p1);
}
Expand Down
6 changes: 3 additions & 3 deletions LibTest/ffi/Short/Short_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
/// Typically a signed 16-bit integer. For a guaranteed 16-bit integer, use
/// [Int16] with the C int16_t type. For an unsigned short, use [UnsignedShort].
///
/// The Short 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.
/// The [Short] 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 short type
/// @author sgrekhov22@gmail.com
Expand Down
6 changes: 3 additions & 3 deletions LibTest/ffi/Short/Short_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
/// Typically a signed 16-bit integer. For a guaranteed 16-bit integer, use
/// [Int16] with the C int16_t type. For an unsigned short, use [UnsignedShort].
///
/// The Short 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.
/// The [Short] 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 short type
/// @author sgrekhov22@gmail.com
Expand Down
8 changes: 4 additions & 4 deletions LibTest/ffi/Short/Short_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
/// Typically a signed 16-bit integer. For a guaranteed 16-bit integer, use
/// [Int16] with the C int16_t type. For an unsigned short, use [UnsignedShort].
///
/// The Short 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.
/// The [Short] 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 is a signed 16-bit integer
/// @author sgrekhov22@gmail.com
Expand All @@ -19,7 +19,7 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Short> p1 = calloc<Short>(3);
Pointer<Short> p1 = calloc<Short>(2);
try {
Expect.equals(2, p1.elementAt(1).address - p1.address);
Expect.equals(2, sizeOf<Short>());
Expand Down
2 changes: 1 addition & 1 deletion LibTest/ffi/SignedChar/SignedChar_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<SignedChar> p1 = calloc<SignedChar>(3);
Pointer<SignedChar> p1 = calloc<SignedChar>(2);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, sizeOf<SignedChar>());
Expand Down
3 changes: 2 additions & 1 deletion LibTest/ffi/Uint16/Uint16_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../../../Utils/expect.dart";
void main() {
Pointer<Uint16> p1 = calloc<Uint16>();
try {
Expect.equals(0, p1.value);
p1.value = 256;
Expect.equals(256, p1.value);
p1.value = 32768;
Expand All @@ -26,6 +25,8 @@ void main() {
Expect.equals(0, p1.value);
p1.value = -1;
Expect.equals(65535, p1.value);
p1.value = -32767; //0x8000
Expect.equals(32769, p1.value);
} finally {
calloc.free(p1);
}
Expand Down
24 changes: 14 additions & 10 deletions LibTest/ffi/Uint16/Uint16_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ void main() {
Pointer<Uint16> p1 = calloc<Uint16>();
Pointer<Uint32> p2 = new Pointer<Uint32>.fromAddress(p1.address);
try {
p2.value = 256;
Expect.equals(256, p1.value);
p2.value = 32768;
Expect.equals(32768, p1.value);
p2.value = 65535;
Expect.equals(65535, p1.value);
p2.value = 65536;
Expect.equals(0, p1.value);
p2.value = -1;
Expect.equals(65535, p1.value);
p2.value = 1;
// Test works for little endian encoding of the number only
if (p1.value != 0) {
p2.value = 256;
Expect.equals(256, p1.value);
p2.value = 32768;
Expect.equals(32768, p1.value);
p2.value = 65535;
Expect.equals(65535, p1.value);
p2.value = 65536;
Expect.equals(0, p1.value);
p2.value = -1;
Expect.equals(65535, p1.value);
}
} finally {
calloc.free(p1);
}
Expand Down
4 changes: 2 additions & 2 deletions LibTest/ffi/Uint16/Uint16_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Uint16> p1 = calloc<Uint16>(3);
Pointer<Uint16> p1 = calloc<Uint16>(2);
try {
Expect.equals(2, p1.elementAt(1).address - p1.address);
Expect.equals(2, p1.elementAt(2).address - p1.elementAt(1).address);
Expect.equals(2, sizeOf<Uint16>());
} finally {
calloc.free(p1);
}
Expand Down

0 comments on commit b572e42

Please sign in to comment.