Skip to content

Commit 917f8fa

Browse files
author
sgrekhov
committed
#993. More tests for Union added
1 parent 1c091fa commit 917f8fa

13 files changed

+532
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 All field declarations in a Struct subclass declaration must
6+
/// either have type int or double and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer or subtype of Struct.
8+
///
9+
/// @description Checks that it is a compile error if any of the field in Struct
10+
/// subclass is not 'int', 'double' or 'Pointer' or subtype of 'Struct'. Test
11+
/// field of 'Union' and 'Struct' subclass type
12+
/// @author [email protected]
13+
/// @issue 46194
14+
15+
import "dart:ffi";
16+
import "package:ffi/ffi.dart";
17+
18+
class U extends Union {
19+
@Int32()
20+
external int x;
21+
}
22+
23+
class S extends Struct {
24+
@Int32()
25+
external int y;
26+
}
27+
28+
class S1 extends Struct {
29+
@Int8()
30+
external int i;
31+
32+
external S s;
33+
34+
external U u;
35+
}
36+
37+
void main() {
38+
Pointer<S1> p = calloc<S1>();
39+
S1 s1 = p.ref;
40+
s1.u.x = 42;
41+
print(s1.u.x);
42+
s1.s.y = -42;
43+
print(s1.s.y);
44+
}

LibTest/ffi/Union/Union_A01_t06.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that it is a compile error if any of the field in Struct
10+
/// subclass is not 'int', 'double' or 'Pointer'. Test
11+
/// field of 'Union' and 'Struct' subclass type
12+
/// @author [email protected]
13+
/// @issue 46194
14+
15+
import "dart:ffi";
16+
import "package:ffi/ffi.dart";
17+
18+
class U extends Union {
19+
@Int32()
20+
external int x;
21+
}
22+
23+
class S extends Struct {
24+
@Int32()
25+
external int y;
26+
}
27+
28+
class U1 extends Union {
29+
@Int8()
30+
external int i;
31+
32+
external S s;
33+
34+
external U u;
35+
}
36+
37+
void main() {
38+
Pointer<U1> p = calloc<U1>();
39+
U1 u1 = p.ref;
40+
u1.u.x = 42;
41+
print(u1.u.x);
42+
u1.s.y = -42;
43+
print(u1.s.y);
44+
}

LibTest/ffi/Union/Union_A05_t01.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Int64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Int32()
23+
external int x;
24+
}
25+
26+
class U3 extends Union {
27+
@Uint32()
28+
external int x;
29+
}
30+
31+
void main() {
32+
Pointer<U1> u1 = calloc<U1>();
33+
try {
34+
u1.ref.x = 0x1111111122222222;
35+
Expect.equals(0x1111111122222222, u1.ref.x);
36+
37+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
38+
Expect.equals(0x22222222, u2.ref.x);
39+
Expect.equals(0x11111111, u2.elementAt(1).ref.x);
40+
41+
Pointer<U3> u3 = new Pointer.fromAddress(u1.address);
42+
Expect.equals(0x22222222, u3.ref.x);
43+
Expect.equals(0x11111111, u3.elementAt(1).ref.x);
44+
45+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
46+
Expect.equals(-1, u2.ref.x);
47+
Expect.equals(4294967295, u3.ref.x);
48+
} finally {
49+
calloc.free(u1);
50+
}
51+
}

LibTest/ffi/Union/Union_A05_t02.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Int64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Int16()
23+
external int x;
24+
}
25+
26+
class U3 extends Union {
27+
@Uint16()
28+
external int x;
29+
}
30+
31+
void main() {
32+
Pointer<U1> u1 = calloc<U1>();
33+
try {
34+
u1.ref.x = 0x1111222233334444;
35+
Expect.equals(0x1111222233334444, u1.ref.x);
36+
37+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
38+
Expect.equals(0x4444, u2.ref.x);
39+
Expect.equals(0x3333, u2.elementAt(1).ref.x);
40+
Expect.equals(0x2222, u2.elementAt(2).ref.x);
41+
Expect.equals(0x1111, u2.elementAt(3).ref.x);
42+
43+
Pointer<U3> u3 = new Pointer.fromAddress(u1.address);
44+
Expect.equals(0x4444, u3.ref.x);
45+
Expect.equals(0x3333, u3.elementAt(1).ref.x);
46+
Expect.equals(0x2222, u3.elementAt(2).ref.x);
47+
Expect.equals(0x1111, u3.elementAt(3).ref.x);
48+
49+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
50+
Expect.equals(-1, u2.ref.x);
51+
Expect.equals(65535, u3.ref.x);
52+
} finally {
53+
calloc.free(u1);
54+
}
55+
}

LibTest/ffi/Union/Union_A05_t03.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Int64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Int8()
23+
external int x;
24+
}
25+
26+
class U3 extends Union {
27+
@Uint8()
28+
external int x;
29+
}
30+
31+
void main() {
32+
Pointer<U1> u1 = calloc<U1>();
33+
try {
34+
u1.ref.x = 0x1122334455667712;
35+
Expect.equals(0x1122334455667712, u1.ref.x);
36+
37+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
38+
Expect.equals(0x12, u2.ref.x);
39+
Expect.equals(0x77, u2.elementAt(1).ref.x);
40+
Expect.equals(0x66, u2.elementAt(2).ref.x);
41+
Expect.equals(0x55, u2.elementAt(3).ref.x);
42+
Expect.equals(0x44, u2.elementAt(4).ref.x);
43+
Expect.equals(0x33, u2.elementAt(5).ref.x);
44+
Expect.equals(0x22, u2.elementAt(6).ref.x);
45+
Expect.equals(0x11, u2.elementAt(7).ref.x);
46+
47+
Pointer<U3> u3 = new Pointer.fromAddress(u1.address);
48+
Expect.equals(0x12, u3.ref.x);
49+
Expect.equals(0x77, u3.elementAt(1).ref.x);
50+
Expect.equals(0x66, u3.elementAt(2).ref.x);
51+
Expect.equals(0x55, u3.elementAt(3).ref.x);
52+
Expect.equals(0x44, u3.elementAt(4).ref.x);
53+
Expect.equals(0x33, u3.elementAt(5).ref.x);
54+
Expect.equals(0x22, u3.elementAt(6).ref.x);
55+
Expect.equals(0x11, u3.elementAt(7).ref.x);
56+
57+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
58+
Expect.equals(-1, u2.ref.x);
59+
Expect.equals(255, u3.ref.x);
60+
} finally {
61+
calloc.free(u1);
62+
}
63+
}

LibTest/ffi/Union/Union_A05_t04.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Int64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Uint64()
23+
external int x;
24+
}
25+
26+
void main() {
27+
Pointer<U1> u1 = calloc<U1>();
28+
try {
29+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
30+
Expect.equals(0xFFFFFFFFFFFFFFFF, u1.ref.x);
31+
32+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
33+
Expect.equals(0xFFFFFFFFFFFFFFFF, u2.ref.x);
34+
35+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
36+
Expect.equals(-1, u1.ref.x);
37+
Expect.equals(-1, u2.ref.x);
38+
} finally {
39+
calloc.free(u1);
40+
}
41+
}

LibTest/ffi/Union/Union_A05_t05.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Uint64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Int32()
23+
external int x;
24+
}
25+
26+
class U3 extends Union {
27+
@Uint32()
28+
external int x;
29+
}
30+
31+
void main() {
32+
Pointer<U1> u1 = calloc<U1>();
33+
try {
34+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
35+
36+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
37+
Expect.equals(-1, u2.ref.x);
38+
39+
Pointer<U3> u3 = new Pointer.fromAddress(u1.address);
40+
Expect.equals(4294967295, u3.ref.x);
41+
} finally {
42+
calloc.free(u1);
43+
}
44+
}

LibTest/ffi/Union/Union_A05_t06.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 All field declarations in a Union subclass declaration must
6+
/// either have type int or float and be annotated with a NativeType
7+
/// representing the native type, or must be of type Pointer.
8+
///
9+
/// @description Checks that 'Union' subtype value depends on annotation
10+
/// @author [email protected]
11+
12+
import "dart:ffi";
13+
import "package:ffi/ffi.dart";
14+
import "../../../Utils/expect.dart";
15+
16+
class U1 extends Union {
17+
@Uint64()
18+
external int x;
19+
}
20+
21+
class U2 extends Union {
22+
@Int16()
23+
external int x;
24+
}
25+
26+
class U3 extends Union {
27+
@Uint16()
28+
external int x;
29+
}
30+
31+
void main() {
32+
Pointer<U1> u1 = calloc<U1>();
33+
try {
34+
u1.ref.x = 0xFFFFFFFFFFFFFFFF;
35+
36+
Pointer<U2> u2 = new Pointer.fromAddress(u1.address);
37+
Expect.equals(-1, u2.ref.x);
38+
39+
Pointer<U3> u3 = new Pointer.fromAddress(u1.address);
40+
Expect.equals(65535, u3.ref.x);
41+
} finally {
42+
calloc.free(u1);
43+
}
44+
}

0 commit comments

Comments
 (0)