dart-sdk/tests/ffi/abi_specific_int_test.dart
Oleh Prypin 5a1ef6089c Revert "[ffi]: Remove pointer elementAt method."
This reverts commit f706ff4ee2.

Reason for revert: b/321667799 - package:win32 uses this method - a78ff108fb/lib/src/com/iapplicationactivationmanager.dart (L46)

Original change's description:
> [ffi]: Remove pointer elementAt method.
>
> Closes #54250
>
> TEST=test/ffi
>
> R=dacoharkes@google.com
> Change-Id: I0e88adfcfe3caef0ad3bb6814ad8f27dce5dc7f4
> CoreLibraryReviewExempt: FFI only
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346380
> Reviewed-by: Daco Harkes <dacoharkes@google.com>
> Commit-Queue: Martin Kustermann <kustermann@google.com>
> Reviewed-by: Martin Kustermann <kustermann@google.com>
> Auto-Submit: Shikhar <shikharish05@gmail.com>

Change-Id: I1b7a48d14e9b85676a27f76a926e21cac9c76c85
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347600
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2024-01-22 12:20:48 +00:00

136 lines
3 KiB
Dart

// 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.
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';
void main() {
testSizeOf();
testStoreLoad();
testStoreLoadIndexed();
testStruct();
testInlineArray();
testInlineArray2();
testInlineArray2WithOperators();
}
void testSizeOf() {
final size = sizeOf<WChar>();
if (Platform.isWindows) {
Expect.equals(2, size);
} else {
Expect.equals(4, size);
}
}
void testStoreLoad() {
final p = calloc<WChar>();
p.value = 10;
Expect.equals(10, p.value);
calloc.free(p);
}
void testStoreLoadIndexed() {
final p = calloc<WChar>(2);
p[0] = 10;
p[1] = 3;
Expect.equals(10, p[0]);
Expect.equals(3, p[1]);
calloc.free(p);
}
final class WCharStruct extends Struct {
@WChar()
external int a0;
@WChar()
external int a1;
}
void testStruct() {
final p = calloc<WCharStruct>();
p.ref.a0 = 1;
Expect.equals(1, p.ref.a0);
p.ref.a0 = 2;
Expect.equals(2, p.ref.a0);
calloc.free(p);
}
final class WCharArrayStruct extends Struct {
@Array(100)
external Array<WChar> a0;
}
void testInlineArray() {
final p = calloc<WCharArrayStruct>();
final array = p.ref.a0;
for (int i = 0; i < 100; i++) {
array[i] = i;
}
for (int i = 0; i < 100; i++) {
Expect.equals(i, array[i]);
}
calloc.free(p);
}
const _dim0 = 3;
const _dim1 = 8;
const _dim2 = 4;
final class WCharArrayArrayStruct extends Struct {
@Array(_dim1, _dim2)
external Array<Array<WChar>> a0;
}
void testInlineArray2() {
int someValue(int a, int b, int c) => a * 1337 + b * 42 + c;
final p = calloc<WCharArrayArrayStruct>(_dim0);
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
array2[i2] = someValue(i0, i1, i2);
}
}
}
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
Expect.equals(someValue(i0, i1, i2), array2[i2]);
}
}
}
calloc.free(p);
}
void testInlineArray2WithOperators() {
int someValue(int a, int b, int c) => a * 1337 + b * 42 + c;
final p = calloc<WCharArrayArrayStruct>(_dim0);
for (int i0 = 0; i0 < _dim0; i0++) {
final array = (p + i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
array2[i2] = someValue(i0, i1, i2);
}
}
}
for (int i0 = 0; i0 < _dim0; i0++) {
final array = (p + i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
Expect.equals(someValue(i0, i1, i2), array2[i2]);
}
}
}
calloc.free(p);
}