Update 'samples/sample_extension' to Dart 2

- Ran `dartfmt --fix`
- Fixed Dart 2 type errors
- Other misc cleanup (to conform to Effective Dart)

Contributes to https://github.com/dart-lang/site-www/issues/980

cc @a-siva @kwalrath @kevmoo

Closes #33711
https://github.com/dart-lang/sdk/pull/33711

GitOrigin-RevId: a832220272fe50a8bd96c6bc71692d54dcbf9465
Change-Id: Ia542ce073d2f9a4c78934a74ee17b60156a49d25
Reviewed-on: https://dart-review.googlesource.com/c/63260
Commit-Queue: Kathy Walrath <kathyw@google.com>
Reviewed-by: Kathy Walrath <kathyw@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
This commit is contained in:
Patrice Chalin 2018-12-09 03:34:04 +00:00 committed by commit-bot@chromium.org
parent 3d6d1d1755
commit fba5a58b47
6 changed files with 57 additions and 70 deletions

View file

@ -13,30 +13,25 @@ class RandomArray {
static SendPort _port;
Future<List<int>> randomArray(int seed, int length) {
var completer = new Completer();
var replyPort = new RawReceivePort();
var args = new List(3);
var completer = Completer<List<int>>();
var replyPort = RawReceivePort();
var args = List<Object>(3);
args[0] = seed;
args[1] = length;
args[2] = replyPort.sendPort;
_servicePort.send(args);
replyPort.handler = (result) {
replyPort.handler = (List<int> result) {
replyPort.close();
if (result != null) {
completer.complete(result);
} else {
completer.completeError(new Exception("Random array creation failed"));
completer.completeError(Exception('Random array creation failed'));
}
};
return completer.future;
}
SendPort get _servicePort {
if (_port == null) {
_port = _newServicePort();
}
return _port;
}
SendPort get _servicePort => _port ??= _newServicePort();
SendPort _newServicePort() native "RandomArray_ServicePort";
SendPort _newServicePort() native 'RandomArray_ServicePort';
}

View file

@ -7,6 +7,6 @@ library sample_synchronous_extension;
import 'dart-ext:sample_extension';
// The simplest way to call native code: top-level functions.
int systemRand() native "SystemRand";
int noScopeSystemRand() native "NoScopeSystemRand";
bool systemSrand(int seed) native "SystemSrand";
int systemRand() native 'SystemRand';
int noScopeSystemRand() native 'NoScopeSystemRand';
bool systemSrand(int seed) native 'SystemSrand';

View file

@ -6,7 +6,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import "package:expect/expect.dart";
import "package:path/path.dart";
@ -45,7 +44,6 @@ Future run(String program, List<String> arguments) async {
}
Future testNativeExtensions(String snapshotKind) async {
String buildDirectory = dirname(Platform.executable);
Directory tempDirectory =
Directory.systemTemp.createTempSync('sample_extension_');
try {
@ -73,14 +71,14 @@ Future testNativeExtensions(String snapshotKind) async {
snapshot = script;
} else {
snapshot = join(testDirectory, "$test.snapshot");
List<String> args = new List<String>.from(Platform.executableArguments);
List<String> args = List<String>.from(Platform.executableArguments);
args.add('--snapshot=$snapshot');
args.add('--snapshot-kind=$snapshotKind');
args.add(script);
await run(Platform.executable, args);
}
List<String> args = new List<String>.from(Platform.executableArguments);
List<String> args = List<String>.from(Platform.executableArguments);
args.add(snapshot);
await run(Platform.executable, args);
}

View file

@ -7,17 +7,15 @@ library test_sample_extension;
import 'sample_asynchronous_extension.dart';
void check(bool condition, String message) {
if (!condition) {
throw new StateError(message);
}
if (!condition) throw StateError(message);
}
void main() {
RandomArray r = new RandomArray();
RandomArray r = RandomArray();
r.randomArray(17, 100).then((list_100) {
r.randomArray(17, 200).then((list_200) {
for (var i = 0; i < 100; ++i) {
check(list_100[i] == list_200[i], "list_100[i] == list_200[i]");
check(list_100[i] == list_200[i], 'list_100[i] == list_200[i]');
}
});
});
@ -27,32 +25,26 @@ void main() {
r.randomArray(19, 256000).then(checkNormal);
}
void checkNormal(List l) {
void checkNormal(List<int> l) {
// Count how many times each byte value occurs. Assert that the counts
// are all within a reasonable (six-sigma) range.
List counts = new List<int>.filled(256, 0);
for (var e in l) {
counts[e]++;
}
new RandomArray().randomArray(18, 256000).then(checkCorrelation(counts));
final counts = List<int>.filled(256, 0);
for (var e in l) counts[e]++;
RandomArray().randomArray(18, 256000).then(checkCorrelation(counts));
}
Function checkCorrelation(List counts) {
return (List l) {
List counts_2 = new List<int>.filled(256, 0);
for (var e in l) {
counts_2[e]++;
}
var product = 0;
for (var i = 0; i < 256; ++i) {
check(counts[i] < 1200, "counts[i] < 1200");
check(counts_2[i] < 1200, "counts_2[i] < 1200");
check(counts[i] > 800, "counts[i] > 800");
check(counts[i] > 800, "counts[i] > 800");
void Function(List<int>) checkCorrelation(List<int> counts) => (l) {
final counts_2 = List<int>.filled(256, 0);
for (var e in l) counts_2[e]++;
var product = 0;
for (var i = 0; i < 256; ++i) {
check(counts[i] < 1200, 'counts[i] < 1200');
check(counts_2[i] < 1200, 'counts_2[i] < 1200');
check(counts[i] > 800, 'counts[i] > 800');
check(counts[i] > 800, 'counts[i] > 800');
product += counts[i] * counts_2[i];
}
check(product < 256000000 * 1.001, "product < 256000000 * 1.001");
check(product > 256000000 * 0.999, "product > 256000000 * 0.999");
};
}
product += counts[i] * counts_2[i];
}
check(product < 256000000 * 1.001, 'product < 256000000 * 1.001');
check(product > 256000000 * 0.999, 'product > 256000000 * 0.999');
};

View file

@ -7,9 +7,8 @@ library test_sample_extension;
import 'sample_synchronous_extension.dart';
void check(bool condition, String message) {
if (!condition) {
throw new StateError(message);
}
if (!condition) throw StateError(message);
print('$message - passed');
}
void checkSystemRand() {
@ -17,17 +16,17 @@ void checkSystemRand() {
var x1 = systemRand();
var x2 = systemRand();
var x3 = systemRand();
check(x1 != x2, "x1 != x2");
check(x1 != x3, "x1 != x3");
check(x2 != x3, "x2 != x3");
check(x1 != x2, 'x1 != x2');
check(x1 != x3, 'x1 != x3');
check(x2 != x3, 'x2 != x3');
systemSrand(17);
check(x1 == systemRand(), "x1 == systemRand()");
check(x2 == systemRand(), "x2 == systemRand()");
check(x3 == systemRand(), "x3 == systemRand()");
check(x1 == systemRand(), 'x1 == systemRand()');
check(x2 == systemRand(), 'x2 == systemRand()');
check(x3 == systemRand(), 'x3 == systemRand()');
systemSrand(18);
check(x1 != systemRand(), "x1 != systemRand()");
check(x2 != systemRand(), "x2 != systemRand()");
check(x3 != systemRand(), "x3 != systemRand()");
check(x1 != systemRand(), 'x1 != systemRand()');
check(x2 != systemRand(), 'x2 != systemRand()');
check(x3 != systemRand(), 'x3 != systemRand()');
}
void checkNoScopeSystemRand() {
@ -35,17 +34,17 @@ void checkNoScopeSystemRand() {
var x1 = noScopeSystemRand();
var x2 = noScopeSystemRand();
var x3 = noScopeSystemRand();
check(x1 != x2, "x1 != x2");
check(x1 != x3, "x1 != x3");
check(x2 != x3, "x2 != x3");
check(x1 != x2, 'x1 != x2');
check(x1 != x3, 'x1 != x3');
check(x2 != x3, 'x2 != x3');
systemSrand(17);
check(x1 == noScopeSystemRand(), "x1 == noScopeSystemRand()");
check(x2 == noScopeSystemRand(), "x2 == noScopeSystemRand()");
check(x3 == noScopeSystemRand(), "x3 == noScopeSystemRand()");
check(x1 == noScopeSystemRand(), 'x1 == noScopeSystemRand()');
check(x2 == noScopeSystemRand(), 'x2 == noScopeSystemRand()');
check(x3 == noScopeSystemRand(), 'x3 == noScopeSystemRand()');
systemSrand(18);
check(x1 != noScopeSystemRand(), "x1 != noScopeSystemRand()");
check(x2 != noScopeSystemRand(), "x2 != noScopeSystemRand()");
check(x3 != noScopeSystemRand(), "x3 != noScopeSystemRand()");
check(x1 != noScopeSystemRand(), 'x1 != noScopeSystemRand()');
check(x2 != noScopeSystemRand(), 'x2 != noScopeSystemRand()');
check(x3 != noScopeSystemRand(), 'x3 != noScopeSystemRand()');
}
void main() {

View file

@ -26,10 +26,13 @@ sample_extension/test/sample_extension_app_snapshot_test: Pass, RuntimeError # I
[ !$preview_dart_2 && ($runtime == dart_precompiled || $runtime == vm) ]
*: SkipByDesign # Deprecating all Dart1 modes of execution
[ $compiler == app_jitk || $compiler == dartk || $compiler == dartkb || $compiler == dartkp ]
[ $compiler == app_jitk || $compiler == dartkb || $compiler == dartkp ]
sample_extension/test/sample_extension_app_snapshot_test: RuntimeError
sample_extension/test/sample_extension_test: RuntimeError
[ $compiler == dartk ]
sample_extension/test/sample_extension_app_snapshot_test: RuntimeError
# Skip tests that use dart:io
[ $runtime == d8 || $browser ]
sample_extension/*: Skip