Skip to content

Commit

Permalink
Add testdata for dart
Browse files Browse the repository at this point in the history
  • Loading branch information
steambap authored and alecthomas committed Apr 2, 2018
1 parent 85c342e commit 51d250f
Show file tree
Hide file tree
Showing 2 changed files with 395 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lexers/testdata/dart.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:async';
import 'dart:html';
import 'dart:math' show Random;

// We changed 5 lines of code to make this sample nicer on
// the web (so that the execution waits for animation frame,
// the number gets updated in the DOM, and the program ends
// after 500 iterations).

main() async {
print('Compute π using the Monte Carlo method.');
var output = querySelector("#output");
await for (var estimate in computePi().take(500)) {
print('π ≅ $estimate');
output.text = estimate.toStringAsFixed(5);
await window.animationFrame;
}
}

/// Generates a stream of increasingly accurate estimates of π.
Stream<double> computePi({int batch: 100000}) async* {
var total = 0;
var count = 0;
while (true) {
var points = generateRandom().take(batch);
var inside = points.where((p) => p.isInsideUnitCircle);
total += batch;
count += inside.length;
var ratio = count / total;
// Area of a circle is A = π⋅r², therefore π = A/r².
// So, when given random points with x ∈ <0,1>,
// y ∈ <0,1>, the ratio of those inside a unit circle
// should approach π / 4. Therefore, the value of π
// should be:
yield ratio * 4;
}
}

Iterable<Point> generateRandom([int seed]) sync* {
final random = new Random(seed);
while (true) {
yield new Point(random.nextDouble(), random.nextDouble());
}
}

class Point {
final double x, y;
const Point(this.x, this.y);
bool get isInsideUnitCircle => x * x + y * y <= 1;
}
345 changes: 345 additions & 0 deletions lexers/testdata/dart.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
[
{"type":"Keyword","value":"import"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'dart:async'"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"import"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'dart:html'"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"import"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'dart:math'"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"show"},
{"type":"Text","value":" "},
{"type":"Name","value":"Random"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"CommentSingle","value":"// We changed 5 lines of code to make this sample nicer on\n// the web (so that the execution waits for animation frame, \n// the number gets updated in the DOM, and the program ends \n// after 500 iterations).\n"},
{"type":"Text","value":"\n"},
{"type":"Name","value":"main"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"async"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringSingle","value":"'Compute π using the Monte Carlo method.'"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"output"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"querySelector"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"#output\""},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"await"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"for"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"estimate"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"in"},
{"type":"Text","value":" "},
{"type":"Name","value":"computePi"},
{"type":"Punctuation","value":"()."},
{"type":"Name","value":"take"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumber","value":"500"},
{"type":"Punctuation","value":"))"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringSingle","value":"'π ≅ "},
{"type":"LiteralStringInterpol","value":"$"},
{"type":"Name","value":"estimate"},
{"type":"LiteralStringSingle","value":"'"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"output"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"text"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"estimate"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"toStringAsFixed"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumber","value":"5"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"await"},
{"type":"Text","value":" "},
{"type":"Name","value":"window"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"animationFrame"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"CommentSingle","value":"/// Generates a stream of increasingly accurate estimates of π.\n"},
{"type":"Name","value":"Stream"},
{"type":"Operator","value":"\u003c"},
{"type":"KeywordType","value":"double"},
{"type":"Operator","value":"\u003e"},
{"type":"Text","value":" "},
{"type":"Name","value":"computePi"},
{"type":"Punctuation","value":"({"},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"NameLabel","value":"batch:"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"100000"},
{"type":"Punctuation","value":"})"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"async"},
{"type":"Operator","value":"*"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"total"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"0"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"count"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"0"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"while"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordConstant","value":"true"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"points"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"generateRandom"},
{"type":"Punctuation","value":"()."},
{"type":"Name","value":"take"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"batch"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"inside"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"points"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"where"},
{"type":"Punctuation","value":"(("},
{"type":"Name","value":"p"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Operator","value":"=\u003e"},
{"type":"Text","value":" "},
{"type":"Name","value":"p"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"isInsideUnitCircle"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"total"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+="},
{"type":"Text","value":" "},
{"type":"Name","value":"batch"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"count"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+="},
{"type":"Text","value":" "},
{"type":"Name","value":"inside"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"length"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"ratio"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"count"},
{"type":"Text","value":" "},
{"type":"Operator","value":"/"},
{"type":"Text","value":" "},
{"type":"Name","value":"total"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"CommentSingle","value":"// Area of a circle is A = π⋅r², therefore π = A/r².\n"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// So, when given random points with x ∈ \u003c0,1\u003e,\n"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// y ∈ \u003c0,1\u003e, the ratio of those inside a unit circle\n"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// should approach π / 4. Therefore, the value of π\n"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// should be:\n"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"yield"},
{"type":"Text","value":" "},
{"type":"Name","value":"ratio"},
{"type":"Text","value":" "},
{"type":"Operator","value":"*"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"4"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"Iterable"},
{"type":"Operator","value":"\u003c"},
{"type":"Name","value":"Point"},
{"type":"Operator","value":"\u003e"},
{"type":"Text","value":" "},
{"type":"Name","value":"generateRandom"},
{"type":"Punctuation","value":"(["},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"Name","value":"seed"},
{"type":"Punctuation","value":"])"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"sync"},
{"type":"Operator","value":"*"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"final"},
{"type":"Text","value":" "},
{"type":"Name","value":"random"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Keyword","value":"new"},
{"type":"Text","value":" "},
{"type":"Name","value":"Random"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"seed"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"while"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordConstant","value":"true"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"yield"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"new"},
{"type":"Text","value":" "},
{"type":"Name","value":"Point"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"random"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"nextDouble"},
{"type":"Punctuation","value":"(),"},
{"type":"Text","value":" "},
{"type":"Name","value":"random"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"nextDouble"},
{"type":"Punctuation","value":"());"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Point"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"final"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"double"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"Name","value":"y"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"const"},
{"type":"Text","value":" "},
{"type":"Name","value":"Point"},
{"type":"Punctuation","value":"("},
{"type":"Keyword","value":"this"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"Keyword","value":"this"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"y"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordType","value":"bool"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"get"},
{"type":"Text","value":" "},
{"type":"Name","value":"isInsideUnitCircle"},
{"type":"Text","value":" "},
{"type":"Operator","value":"=\u003e"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Text","value":" "},
{"type":"Operator","value":"*"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+"},
{"type":"Text","value":" "},
{"type":"Name","value":"y"},
{"type":"Text","value":" "},
{"type":"Operator","value":"*"},
{"type":"Text","value":" "},
{"type":"Name","value":"y"},
{"type":"Text","value":" "},
{"type":"Operator","value":"\u003c="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"1"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]

0 comments on commit 51d250f

Please sign in to comment.