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

Unified Diff: tests/language_strong/generic_methods_recursive_bound_test.dart

Issue 2737933002: Add tests for generic methods in strong mode (Closed)
Patch Set: Created 3 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 side-by-side diff with in-line comments
Download patch
Index: tests/language_strong/generic_methods_recursive_bound_test.dart
diff --git a/tests/language_strong/generic_methods_recursive_bound_test.dart b/tests/language_strong/generic_methods_recursive_bound_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2e92c00274a9ecf535589b6ea532e38a763a5040
--- /dev/null
+++ b/tests/language_strong/generic_methods_recursive_bound_test.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library generic_methods_f_bounded_test;
+
+import "package:expect/expect.dart";
+
+abstract class Ordered<T> {
+ bool operator <(T x);
+}
+
+void bubbleSort<T extends Ordered<T>>(List<T> a) {
+ for (int n = a.length; n > 1; n--) {
+ for (int i = 1; i < n; i++) {
+ if (a[i] < a[i - 1]) {
+ T t = a[i];
+ a[i] = a[i - 1];
+ a[i - 1] = t;
+ }
+ }
+ }
+}
+
+class MyNum implements Ordered<MyNum> {
+ final num x;
+ MyNum(this.x);
+
+ @override
+ bool operator <(MyNum other) {
+ return x < other.x;
+ }
+}
+
+main() {
+ List<MyNum> list = <MyNum>[
+ new MyNum(5),
+ new MyNum(4),
+ new MyNum(3),
+ new MyNum(2),
+ new MyNum(1)
+ ];
+ bubbleSort<MyNum>(list);
+
+ Expect.isTrue(list[0].x == 1);
floitsch 2017/03/09 11:27:17 Expect.listEquals
Dmitry Stefantsov 2017/03/10 13:21:55 Yes. My comments from above apply here as well.
+ Expect.isTrue(list[1].x == 2);
+ Expect.isTrue(list[2].x == 3);
+ Expect.isTrue(list[3].x == 4);
+ Expect.isTrue(list[4].x == 5);
+
+ dynamic someSort = bubbleSort;
+ List<int> list2 = <int>[5, 4, 3, 2, 1];
+ // int does not extend Ordered<int>
floitsch 2017/03/09 11:27:17 Try to start with upper case, and finish with ".".
Dmitry Stefantsov 2017/03/10 13:21:55 Done.
+ Expect.throws(() => someSort<int>(list2), (e) => e is TypeError);
+}

Powered by Google App Engine
This is Rietveld 408576698