Skip to content

Commit

Permalink
Fixes #993. More Union tests for FFI library added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Jul 29, 2021
1 parent a5bbcad commit 99bb9f1
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
61 changes: 61 additions & 0 deletions LibTest/ffi/Array/UnionArray_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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
/// const Array<T extends NativeType>(
/// int dimension1,
/// [int dimension2,
/// int dimension3,
/// int dimension4,
/// int dimension5]
/// )
/// Const constructor to specify Array dimensions in Structs.
///
/// class MyStruct extends Struct {
/// @Array(8)
/// external Array<Uint8> inlineArray;
///
/// @Array(2, 2, 2)
/// external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
/// }
/// Do not invoke in normal code.
///
/// @description Checks that this class represents a fixed-size array of Pointer
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class Coord extends Union {
@Double()
external double x;
@Double()
external double y;
}

class MyStruct extends Struct {
@Array(2)
external Array<Coord> a0;
}

void main() {
final pointer = calloc<MyStruct>();
try {
final array = pointer.ref.a0;
for (int i = 0; i < 2; i++) {
Expect.equals(0, array[i].x);
Expect.equals(0, array[i].y);
array[i].x = 42;
array[i].y = 3.14;
Expect.equals(3.14, array[i].x);
Expect.equals(3.14, array[i].y);
array[i].x = 42;
Expect.equals(42, array[i].x);
Expect.equals(42, array[i].y);
}
} finally {
calloc.free(pointer);
}
}
48 changes: 48 additions & 0 deletions LibTest/ffi/Union/Union_A08_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 Field declarations in a Union subclass declaration are
/// automatically given a setter and getter implementation which accesses the
/// native union's field in memory.
///
/// @description Checks that fields in a Union subclass shares the same memory
/// @author sgrekhov@unipro.ru
import "dart:ffi";
import "package:ffi/ffi.dart";
import "../../../Utils/expect.dart";

class U extends Union {
@Int32()
external int x;

@Int16()
external int y;
}

void main() {
Pointer<U> p = calloc<U>();
U u = p.ref;
u.y = -42;
u.x = 42;
Expect.equals(42, u.x);
Expect.equals(42, u.y);

u.x = 1;
u.y = 2;
Expect.equals(2, u.x);
Expect.equals(2, u.y);

u.x = 65537;
Expect.equals(65537, u.x);
Expect.equals(1, u.y);

u.x = 65538;
Expect.equals(65538, u.x);
Expect.equals(2, u.y);

u.y = 2;
Expect.equals(65538, u.x);
Expect.equals(2, u.y);
}

0 comments on commit 99bb9f1

Please sign in to comment.