-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Authored by @sgrekhov. Missing tests added for some new types. Use unsigned types to avoid little/big endian problems.
- Loading branch information
Showing
16 changed files
with
542 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 { | ||
p1.value = true; | ||
Expect.isTrue(p1.value); | ||
p1.value = false; | ||
Expect.isFalse(p1.value); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Uint8> p2 = new Pointer<Uint8>.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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, sizeOf<Bool>()); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Uint8> p2 = new Pointer<Uint8>.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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, sizeOf<Char>()); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 { | ||
p1.value = 42; | ||
Expect.equals(42, p1.value); | ||
p1.value = 32768; | ||
Expect.equals(32768, p1.value); | ||
p1.value = 0x7FFFFFFF; | ||
Expect.equals(0x7FFFFFFF, p1.value); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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); | ||
Expect.isTrue(sizeOf<Long>() == 4 || sizeOf<Long>() == 8); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 { | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, sizeOf<LongLong>); | ||
} finally { | ||
calloc.free(p1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 { | ||
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); | ||
} | ||
} |
Oops, something went wrong.