Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, 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 library generic_methods_f_bounded_test; | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 abstract class Ordered<T> { | |
| 10 bool operator <(T x); | |
| 11 } | |
| 12 | |
| 13 void bubbleSort<T extends Ordered<T>>(List<T> a) { | |
| 14 for (int n = a.length; n > 1; n--) { | |
| 15 for (int i = 1; i < n; i++) { | |
| 16 if (a[i] < a[i - 1]) { | |
| 17 T t = a[i]; | |
| 18 a[i] = a[i - 1]; | |
| 19 a[i - 1] = t; | |
| 20 } | |
| 21 } | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 class MyNum implements Ordered<MyNum> { | |
| 26 final num x; | |
| 27 MyNum(this.x); | |
| 28 | |
| 29 @override | |
| 30 bool operator <(MyNum other) { | |
| 31 return x < other.x; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 main() { | |
| 36 List<MyNum> list = <MyNum>[ | |
| 37 new MyNum(5), | |
| 38 new MyNum(4), | |
| 39 new MyNum(3), | |
| 40 new MyNum(2), | |
| 41 new MyNum(1) | |
| 42 ]; | |
| 43 bubbleSort<MyNum>(list); | |
| 44 | |
| 45 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.
| |
| 46 Expect.isTrue(list[1].x == 2); | |
| 47 Expect.isTrue(list[2].x == 3); | |
| 48 Expect.isTrue(list[3].x == 4); | |
| 49 Expect.isTrue(list[4].x == 5); | |
| 50 | |
| 51 dynamic someSort = bubbleSort; | |
| 52 List<int> list2 = <int>[5, 4, 3, 2, 1]; | |
| 53 // 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.
| |
| 54 Expect.throws(() => someSort<int>(list2), (e) => e is TypeError); | |
| 55 } | |
| OLD | NEW |