Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: tests/language/generic_methods_test.dart

Issue 3001803002: Migrate block 114 (and some of 113). (Closed)
Patch Set: Tweak app_jit status. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // VMOptions=--generic-method-syntax
6
7 /// Dart test verifying that the parser can handle type parameterization of
8 /// method declarations and method invocations. Slightly adjusted version of
9 /// code from DEP #22.
10
11 library generic_methods_test;
12
13 import "package:expect/expect.dart";
14
15 class BinaryTreeNode<K extends Comparable<K>, V> {
16 final K _key;
17 final V _value;
18 final BinaryTreeNode<K, V> _left;
19 final BinaryTreeNode<K, V> _right;
20
21 BinaryTreeNode(this._key, this._value,
22 {BinaryTreeNode<K, V> left: null, BinaryTreeNode<K, V> right: null})
23 : _left = left,
24 _right = right;
25
26 // Use fresh type variables.
27 static BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>(
28 BinaryTreeNode<K2, V2> t, K2 key, V2 value) {
29 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value);
30 }
31
32 BinaryTreeNode<K, V> insert(K key, V value) {
33 int c = key.compareTo(_key);
34 if (c == 0) return this;
35 var _insert = (BinaryTreeNode<K, V> node, K key, V value) =>
36 insertOpt<K, V>(node, key, value);
37 BinaryTreeNode<K, V> left = _left;
38 BinaryTreeNode<K, V> right = _right;
39 if (c < 0) {
40 left = _insert(_left, key, value);
41 } else {
42 right = _insert(_right, key, value);
43 }
44 return new BinaryTreeNode<K, V>(_key, _value, left: left, right: right);
45 }
46
47 // Reuse type variables [K], [V] to test shadowing.
48 static BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>(
49 BinaryTreeNode<K, V> t, U f(V x)) {
50 return (t == null) ? null : t.map<U>(f);
51 }
52
53 BinaryTreeNode<K, U> map<U>(U f(V x)) {
54 var _map = (BinaryTreeNode<K, V> t, U f(V x)) => mapOpt<K, V, U>(t, f);
55 return new BinaryTreeNode<K, U>(_key, f(_value),
56 left: _map(_left, f), right: _map(_right, f));
57 }
58
59 // Use fresh [K2], shadowing [V].
60 static S foldPreOpt<K2 extends Comparable<K2>, V, S>(
61 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) {
62 return (t == null) ? init : t.foldPre<S>(init, f);
63 }
64
65 S foldPre<S>(S init, S f(V t, S s)) {
66 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) =>
67 foldPreOpt<K, V, S>(t, s, f);
68 S s = init;
69 s = f(_value, s);
70 s = _fold(_left, s, f);
71 s = _fold(_right, s, f);
72 return s;
73 }
74 }
75
76 class BinaryTree<K extends Comparable<K>, V> {
77 final BinaryTreeNode<K, V> _root;
78
79 BinaryTree._internal(this._root);
80 BinaryTree.empty() : this._internal(null);
81
82 BinaryTree<K, V> insert(K key, V value) {
83 BinaryTreeNode<K, V> root =
84 BinaryTreeNode.insertOpt<K, V>(_root, key, value);
85 return new BinaryTree<K, V>._internal(root);
86 }
87
88 BinaryTree<K, U> map<U>(U f(V x)) {
89 BinaryTreeNode<K, U> root = BinaryTreeNode.mapOpt<K, V, U>(_root, f);
90 return new BinaryTree<K, U>._internal(root);
91 }
92
93 S foldPre<S>(S init, S f(V t, S s)) {
94 return BinaryTreeNode.foldPreOpt<K, V, S>(_root, init, f);
95 }
96 }
97
98 main() {
99 BinaryTree<num, String> sT = new BinaryTree<num, String>.empty();
100
101 sT = sT.insert(0, "");
102 sT = sT.insert(1, " ");
103 sT = sT.insert(2, " ");
104 sT = sT.insert(3, " ");
105
106 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length);
107
108 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6);
109 }
OLDNEW
« no previous file with comments | « tests/language/generic_methods_new_test.dart ('k') | tests/language/generic_methods_type_expression_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698