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_shadowing; | |
|
eernst
2017/03/09 14:55:03
Not sure what makes this test 'shadowing'. A few w
Dmitry Stefantsov
2017/03/10 13:21:55
Agree, I didn't include the comments the first tim
| |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 class Pair<T extends Comparable<T>, S extends Comparable<S>> | |
| 10 implements Comparable<Pair<T, S>> { | |
| 11 final T first; | |
| 12 final S second; | |
| 13 | |
| 14 Pair(this.first, this.second); | |
| 15 | |
| 16 @override | |
| 17 int compareTo(Pair<T, S> other) { | |
| 18 if (first.compareTo(other.first) == 0) { | |
| 19 return second.compareTo(other.second); | |
| 20 } | |
| 21 | |
| 22 return first.compareTo(other.first); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 void bubbleSort<T extends Comparable<T>, S extends Pair<T, T>>(List<S> list) { | |
| 27 void swap<T>(List<T> list, int i, int j) { | |
| 28 T t = list[i]; | |
| 29 list[i] = list[j]; | |
| 30 list[j] = t; | |
| 31 | |
| 32 Expect.isTrue(t is T); | |
| 33 Expect.isTrue(t is S); // S is passed as T below, so T = S here | |
|
floitsch
2017/03/09 11:27:17
Finish with ".".
Dmitry Stefantsov
2017/03/10 13:21:55
I rewrote the test, so that it's more compact. And
| |
| 34 } | |
| 35 | |
| 36 for (int n = list.length; n > 1; n--) { | |
| 37 for (int i = 1; i < n; i++) { | |
| 38 if (list[i - 1].compareTo(list[i]) > 0) { | |
| 39 swap<S>(list, i - 1, i); | |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 main() { | |
| 46 List<Pair<String, String>> list = <Pair<String, String>>[ | |
| 47 new Pair<String, String>("b", "b"), | |
| 48 new Pair<String, String>("b", "a"), | |
| 49 new Pair<String, String>("a", "b"), | |
| 50 new Pair<String, String>("a", "a"), | |
| 51 ]; | |
| 52 bubbleSort<String, Pair<String, String>>(list); | |
| 53 | |
| 54 Expect.isTrue(list[0].first == "a" && list[0].second == "a"); | |
| 55 Expect.isTrue(list[1].first == "a" && list[1].second == "b"); | |
| 56 Expect.isTrue(list[2].first == "b" && list[2].second == "a"); | |
| 57 Expect.isTrue(list[3].first == "b" && list[3].second == "b"); | |
| 58 } | |
| OLD | NEW |