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

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

Issue 1723443003: First step of support for parsing and ignoring generic methods. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fixes bug with nested type arguments Created 4 years, 9 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 // DartOptions=--generic-method-syntax
6 // AnalyzerOptions=--options=./generic_methods_test.options
7
8 import "package:expect/expect.dart";
9
10 // Dart test verifying that the parser can handle type parameterization of
11 // method declarations and method invocations. Slightly adjusted version of
12 // code from DEP #22.
13
14 class BinaryTreeNode<K extends Comparable<K>, V> {
15 final K _key;
16 final V _value;
17 final BinaryTreeNode<K, V> _left;
18 final BinaryTreeNode<K, V> _right;
19
20 BinaryTreeNode(this._key, this._value,
21 {BinaryTreeNode<K, V> left: null, BinaryTreeNode<K, V> right: null}) :
22 _left = left, _right = right;
23
24 // Use fresh type variables.
25 static BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>(
26 BinaryTreeNode<K2, V2> t, K2 key, V2 value) {
27 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value);
28 }
29
30 BinaryTreeNode<K, V> insert(K key, V value) {
31 int c = key.compareTo(_key);
32 if (c == 0) return this;
33 var _insert = (BinaryTreeNode<K, V> node, K key, V value) =>
34 insertOpt<K, V>(node, key, value);
35 BinaryTreeNode<K, V> left = _left;
36 BinaryTreeNode<K, V> right = _right;
37 if (c < 0) {
38 left = _insert(_left, key, value);
39 } else {
40 right = _insert(_right, key, value);
41 }
42 return new BinaryTreeNode<K, V>(_key, _value, left: left, right: right);
43 }
44
45 // Reuse type variables [K], [V] to test shadowing.
46 static BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>
47 (BinaryTreeNode<K, V> t, U f(V x)) {
48 return (t == null) ? null : t.map<U>(f);
49 }
50
51 BinaryTreeNode<K, U> map<U>(U f(V x)){
52 var _map = (BinaryTreeNode<K, V> t, U f(V x)) => mapOpt<K, V, U>(t, f);
53 return new BinaryTreeNode<K, U>(
54 _key,
55 f(_value),
56 left: _map(_left, f),
57 right: _map(_right, f));
58 }
59
60 // Use fresh [K2], shadowing [V].
61 static S foldPreOpt<K2 extends Comparable<K2>, V, S>(
62 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) {
63 return (t == null) ? init : t.foldPre<S>(init, f);
64 }
65
66 S foldPre<S>(S init, S f(V t, S s)) {
67 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) =>
68 foldPreOpt<K, V, S>(t, s, f);
69 S s = init;
70 s = f(_value, s);
71 s = _fold(_left, s, f);
72 s = _fold(_right, s, f);
73 return s;
74 }
75 }
76
77 class BinaryTree<K extends Comparable<K>, V> {
78 final BinaryTreeNode<K, V> _root;
79
80 BinaryTree._internal(this._root);
81 BinaryTree.empty() : this._internal(null);
82
83 BinaryTree<K, V> insert(K key, V value) {
84 BinaryTreeNode<K, V> root =
85 BinaryTreeNode.insertOpt<K, V>(_root, key, value);
86 return new BinaryTree<K, V>._internal(root);
87 }
88
89 BinaryTree<K, U> map<U>(U f(V x)) {
90 BinaryTreeNode<K, U> root = BinaryTreeNode.mapOpt<K, V, U>(_root, f);
91 return new BinaryTree<K, U>._internal(root);
92 }
93
94 S foldPre<S>(S init, S f(V t, S s)) {
95 return BinaryTreeNode.foldPreOpt<K, V, S>(_root, init, f);
96 }
97 }
98
99 main() {
100 BinaryTree<num, String> sT = new BinaryTree<num, String>.empty();
101
102 sT = sT.insert(0, "");
103 sT = sT.insert(1, " ");
104 sT = sT.insert(2, " ");
105 sT = sT.insert(3, " ");
106
107 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length);
108
109 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6);
110 }
OLDNEW
« no previous file with comments | « tests/language/generic_functions_test.options ('k') | tests/language/generic_methods_test.options » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698