Skip to content

Commit

Permalink
dart-lang#993. [FFI] Missed tests added for some new types
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Aug 2, 2022
1 parent 9291df7 commit 2e78f3e
Show file tree
Hide file tree
Showing 16 changed files with 545 additions and 0 deletions.
28 changes: 28 additions & 0 deletions LibTest/ffi/Bool/Bool_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 Represents a native bool in C.
///
/// 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.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Bool> p1 = calloc<Bool>();
try {
Expect.isFalse(p1.value);
p1.value = true;
Expect.isTrue(p1.value);
p1.value = false;
Expect.isFalse(p1.value);
} finally {
calloc.free(p1);
}
}
38 changes: 38 additions & 0 deletions LibTest/ffi/Bool/Bool_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 Represents a native bool in C.
///
/// 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.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Bool> p1 = calloc<Bool>();
Pointer<Int16> p2 = new Pointer<Int16>.fromAddress(p1.address);
try {
p2.value = 0;
Expect.isFalse(p1.value);
p2.value = 1;
Expect.isTrue(p1.value);
p2.value = 2;
Expect.isTrue(p1.value);
p2.value = 127;
Expect.isTrue(p1.value);
p2.value = 128;
Expect.isTrue(p1.value);
p2.value = 255;
Expect.isTrue(p1.value);
p2.value = 256;
Expect.isFalse(p1.value);
} finally {
calloc.free(p1);
}
}
25 changes: 25 additions & 0 deletions LibTest/ffi/Bool/Bool_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 Represents a native bool in C.
///
/// 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.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Bool> p1 = calloc<Bool>(3);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, p1.elementAt(2).address - p1.elementAt(1).address);
} finally {
calloc.free(p1);
}
}
34 changes: 34 additions & 0 deletions LibTest/ffi/Char/Char_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 char type.
///
/// 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
/// [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.
///
/// @description Checks that this type represents a native char in C.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Char> p1 = calloc<Char>();
try {
p1.value = 48;
Expect.equals(48, p1.value);
p1.value = -42;
Expect.equals(-42, p1.value);
} finally {
calloc.free(p1);
}
}
39 changes: 39 additions & 0 deletions LibTest/ffi/Char/Char_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 char type.
///
/// 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
/// [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.
///
/// @description Checks that this type represents a native char in C.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Char> p1 = calloc<Char>();
Pointer<Int16> p2 = new Pointer<Int16>.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 = 256;
Expect.equals(0, p1.value);
} finally {
calloc.free(p1);
}
}
32 changes: 32 additions & 0 deletions LibTest/ffi/Char/Char_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 char type.
///
/// 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
/// [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.
///
/// @description Checks that this type represents a native char in C.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Char> p1 = calloc<Char>(3);
try {
Expect.equals(1, p1.elementAt(1).address - p1.address);
Expect.equals(1, p1.elementAt(2).address - p1.elementAt(1).address);
} finally {
calloc.free(p1);
}
}
35 changes: 35 additions & 0 deletions LibTest/ffi/Long/Long_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 long int, aka. long, type.
///
/// Typically a signed 32- or 64-bit integer. For a guaranteed 32-bit integer,
/// use [Int32] with the C int32_t type. For a guaranteed 64-bit integer, use
/// [Int64] with the C int64_t type. For an unsigned long, use [UnsignedLong].
///
/// The [Long] 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 long int in C.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Long> p1 = calloc<Long>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = 32768;
Expect.equals(32768, p1.value);
p1.value = 2147483647; // 0x7FFFFFFF
Expect.equals(2147483647, p1.value);
} finally {
calloc.free(p1);
}
}
30 changes: 30 additions & 0 deletions LibTest/ffi/Long/Long_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 long int, aka. long, type.
///
/// Typically a signed 32- or 64-bit integer. For a guaranteed 32-bit integer,
/// use [Int32] with the C int32_t type. For a guaranteed 64-bit integer, use
/// [Int64] with the C int64_t type. For an unsigned long, use [UnsignedLong].
///
/// The [Long] 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 32- or 64-bit integer.
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Long> p1 = calloc<Long>(2);
try {
Expect.isTrue(p1.elementAt(1).address - p1.address == 4 ||
p1.elementAt(1).address - p1.address == 8);
} finally {
calloc.free(p1);
}
}
41 changes: 41 additions & 0 deletions LibTest/ffi/LongLong/LongLong_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 long long type.
///
/// Typically a signed 64-bit integer. For a guaranteed 64-bit integer, use
/// [Int64] with the C int64_t type. For an unsigned long long, use
/// [UnsignedLongLong].
///
/// The [LongLong] 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 long long type
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<LongLong> p1 = calloc<LongLong>();
try {
Expect.equals(0, p1.value);
p1.value = 42;
Expect.equals(42, p1.value);
p1.value = 32768;
Expect.equals(32768, p1.value);
p1.value = 2147483648;
Expect.equals(2147483648, p1.value);
p1.value = 0x7FFFFFFFFFFFFFFF;
Expect.equals(0x7FFFFFFFFFFFFFFF, p1.value);
p1.value = 0xFFFFFFFFFFFFFFFF;
Expect.equals(-1, p1.value);
p1.value = -9223372036854775808;
Expect.equals(-9223372036854775808, p1.value);
} finally {
calloc.free(p1);
}
}
30 changes: 30 additions & 0 deletions LibTest/ffi/LongLong/LongLong_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 The C long long type.
///
/// Typically a signed 64-bit integer. For a guaranteed 64-bit integer, use
/// [Int64] with the C int64_t type. For an unsigned long long, use
/// [UnsignedLongLong].
///
/// The [LongLong] 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 64-bit integer
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<LongLong> p1 = calloc<LongLong>(3);
try {
Expect.equals(8, p1.elementAt(1).address - p1.address);
Expect.equals(8, p1.elementAt(2).address - p1.elementAt(1).address);
} finally {
calloc.free(p1);
}
}
42 changes: 42 additions & 0 deletions LibTest/ffi/Short/Short_A01_t01.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 short type.
///
/// 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.
///
/// @description Checks that this type represents C short type
/// @author sgrekhov22@gmail.com
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

void main() {
Pointer<Short> p1 = calloc<Short>();
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);
p1.value = 32768;
Expect.equals(-32768, p1.value);
p1.value = -32768;
Expect.equals(-32768, p1.value);
p1.value = -32769;
Expect.equals(32767, p1.value);
} finally {
calloc.free(p1);
}
}

0 comments on commit 2e78f3e

Please sign in to comment.