Skip to content

Commit 99bb9f1

Browse files
author
sgrekhov
committed
Fixes #993. More Union tests for FFI library added
1 parent a5bbcad commit 99bb9f1

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion
6+
/// const Array<T extends NativeType>(
7+
/// int dimension1,
8+
/// [int dimension2,
9+
/// int dimension3,
10+
/// int dimension4,
11+
/// int dimension5]
12+
/// )
13+
/// Const constructor to specify Array dimensions in Structs.
14+
///
15+
/// class MyStruct extends Struct {
16+
/// @Array(8)
17+
/// external Array<Uint8> inlineArray;
18+
///
19+
/// @Array(2, 2, 2)
20+
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
21+
/// }
22+
/// Do not invoke in normal code.
23+
///
24+
/// @description Checks that this class represents a fixed-size array of Pointer
25+
/// @author [email protected]
26+
27+
import "dart:ffi";
28+
import "package:ffi/ffi.dart";
29+
import "../../../Utils/expect.dart";
30+
31+
class Coord extends Union {
32+
@Double()
33+
external double x;
34+
@Double()
35+
external double y;
36+
}
37+
38+
class MyStruct extends Struct {
39+
@Array(2)
40+
external Array<Coord> a0;
41+
}
42+
43+
void main() {
44+
final pointer = calloc<MyStruct>();
45+
try {
46+
final array = pointer.ref.a0;
47+
for (int i = 0; i < 2; i++) {
48+
Expect.equals(0, array[i].x);
49+
Expect.equals(0, array[i].y);
50+
array[i].x = 42;
51+
array[i].y = 3.14;
52+
Expect.equals(3.14, array[i].x);
53+
Expect.equals(3.14, array[i].y);
54+
array[i].x = 42;
55+
Expect.equals(42, array[i].x);
56+
Expect.equals(42, array[i].y);
57+
}
58+
} finally {
59+
calloc.free(pointer);
60+
}
61+
}

LibTest/ffi/Union/Union_A08_t01.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Field declarations in a Union subclass declaration are
6+
/// automatically given a setter and getter implementation which accesses the
7+
/// native union's field in memory.
8+
///
9+
/// @description Checks that fields in a Union subclass shares the same memory
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U extends Union {
17+
@Int32()
18+
external int x;
19+
20+
@Int16()
21+
external int y;
22+
}
23+
24+
void main() {
25+
Pointer<U> p = calloc<U>();
26+
U u = p.ref;
27+
u.y = -42;
28+
u.x = 42;
29+
Expect.equals(42, u.x);
30+
Expect.equals(42, u.y);
31+
32+
u.x = 1;
33+
u.y = 2;
34+
Expect.equals(2, u.x);
35+
Expect.equals(2, u.y);
36+
37+
u.x = 65537;
38+
Expect.equals(65537, u.x);
39+
Expect.equals(1, u.y);
40+
41+
u.x = 65538;
42+
Expect.equals(65538, u.x);
43+
Expect.equals(2, u.y);
44+
45+
u.y = 2;
46+
Expect.equals(65538, u.x);
47+
Expect.equals(2, u.y);
48+
}

0 commit comments

Comments
 (0)