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

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

Issue 3008573002: Migrate test block 113 to Dart 2.0. (Closed)
Patch Set: Merge fixups 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 /// function declarations and function invocations. Variant of code from
9 /// DEP #22, adjusted to use generic top level functions.
10
11 library generic_functions_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 BinaryTreeNode<K, V> insert(K key, V value) {
27 int c = key.compareTo(_key);
28 if (c == 0) return this;
29 var _insert = (BinaryTreeNode<K, V> t, K key, V value) =>
30 insertOpt<K, V>(t, key, value);
31 BinaryTreeNode<K, V> left = _left;
32 BinaryTreeNode<K, V> right = _right;
33 if (c < 0) {
34 left = _insert(_left, key, value);
35 } else {
36 right = _insert(_right, key, value);
37 }
38 return new BinaryTreeNode<K, V>(_key, _value, left: left, right: right);
39 }
40
41 BinaryTreeNode<K, U> map<U>(U f(V x)) {
42 var _map = (BinaryTreeNode<K, V> t, U f(V x)) => mapOpt<K, V, U>(t, f);
43 return new BinaryTreeNode<K, U>(_key, f(_value),
44 left: _map(_left, f), right: _map(_right, f));
45 }
46
47 S foldPre<S>(S init, S f(V t, S s)) {
48 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) =>
49 foldPreOpt<K, V, S>(t, s, f);
50 S s = init;
51 s = f(_value, s);
52 s = _fold(_left, s, f);
53 s = _fold(_right, s, f);
54 return s;
55 }
56 }
57
58 BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>(
59 BinaryTreeNode<K2, V2> t, K2 key, V2 value) {
60 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value);
61 }
62
63 BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>(
64 BinaryTreeNode<K, V> t, U f(V x)) {
65 return (t == null) ? null : t.map<U>(f);
66 }
67
68 S foldPreOpt<K2 extends Comparable<K2>, V, S>(
69 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) {
70 return (t == null) ? init : t.foldPre<S>(init, f);
71 }
72
73 class BinaryTree<K extends Comparable<K>, V> {
74 final BinaryTreeNode<K, V> _root;
75
76 BinaryTree._internal(this._root);
77 BinaryTree.empty() : this._internal(null);
78
79 BinaryTree<K, V> insert(K key, V value) {
80 BinaryTreeNode<K, V> root = insertOpt<K, V>(_root, key, value);
81 return new BinaryTree<K, V>._internal(root);
82 }
83
84 BinaryTree<K, U> map<U>(U f(V x)) {
85 BinaryTreeNode<K, U> root = mapOpt<K, V, U>(_root, f);
86 return new BinaryTree<K, U>._internal(root);
87 }
88
89 S foldPre<S>(S init, S f(V t, S s)) {
90 return foldPreOpt<K, V, S>(_root, init, f);
91 }
92 }
93
94 main() {
95 BinaryTree<num, String> sT = new BinaryTree<num, String>.empty();
96
97 sT = sT.insert(0, "");
98 sT = sT.insert(1, " ");
99 sT = sT.insert(2, " ");
100 sT = sT.insert(3, " ");
101
102 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length);
103
104 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6);
105 }
OLDNEW
« no previous file with comments | « tests/language/generic_function_typedef_test.dart ('k') | tests/language/generic_inheritance_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698