/
bstr_test.dart
86 lines (65 loc) · 2.59 KB
/
bstr_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@TestOn('windows')
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:test/test.dart';
import 'package:win32/win32.dart';
const TEST_RUNS = 500;
void main() {
test('BSTR allocation', () {
const testString = 'Hello world';
final bstr = BSTR.fromString(testString);
// A BSTR should have a DWORD-length prefix containing its length.
final pIndex =
Pointer<DWORD>.fromAddress(bstr.ptr.address - sizeOf<DWORD>());
expect(pIndex.value, equals(testString.length * 2));
expect(bstr.ptr.toDartString(), equals(testString));
// A BSTR should end with a word-length null terminator.
final pNull =
Pointer<DWORD>.fromAddress(bstr.ptr.address + testString.length * 2);
expect(pNull.value, isZero);
bstr.free();
});
test('Long BSTRs', () {
// This string is 4MB (32 chars * 2 bytes * 65536)
final longString = 'A very long string with padding.' * 65536;
final bstr = BSTR.fromString(longString);
// A BSTR should have a DWORD-length prefix containing its length.
final pIndex =
Pointer<DWORD>.fromAddress(bstr.ptr.address - sizeOf<DWORD>());
expect(pIndex.value, equals(longString.length * 2));
expect(bstr.ptr.toDartString(), equals(longString));
// A BSTR should end with a word-length null terminator.
final pNull =
Pointer<DWORD>.fromAddress(bstr.ptr.address + longString.length * 2);
expect(pNull.value, isZero);
bstr.free();
});
test('BSTR lengths', () {
const testString = 'Longhorn is a bar in the village resort between the '
'Whistler and Blackcomb mountains';
final bstr = BSTR.fromString(testString);
expect(testString.length, equals(84));
expect(bstr.byteLength, equals(84 * 2));
expect(bstr.length, equals(84));
expect(bstr.toString(), equals(testString));
bstr.free();
});
test('BSTR clone', () {
const testString = 'This message is not unique.';
final original = BSTR.fromString(testString);
final clone = original.clone();
// Text should be equal, but pointer address should not be equal
expect(original.ptr.toDartString(), equals(clone.ptr.toDartString()));
expect(original.toString(), equals(clone.toString()));
expect(original.ptr, isNot(equals(clone.ptr)));
clone.free();
original.free();
});
test('BSTR concatenation', () {
final first = BSTR.fromString('Windows');
final second = BSTR.fromString(' and Dart');
final matchInHeaven = first + second;
expect(matchInHeaven.toString(), equals('Windows and Dart'));
[first, second, matchInHeaven].map((object) => object.free());
});
}