mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
WIP Commits separated as follows: - Update lints in analysis_options files - Run `dart fix --apply` - Clean up leftover analysis issues - Run `dart format .` in the right places. Local analysis and testing passes. Checking CI now. Part of https://github.com/flutter/flutter/issues/178827 - Adoption of flutter_lints in examples/api coming in a separate change (cc @loic-sharma) ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
175 lines
5.9 KiB
Dart
175 lines
5.9 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter/src/foundation/collections.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
test('listEquals', () {
|
|
final listA = <int>[1, 2, 3];
|
|
final listB = <int>[1, 2, 3];
|
|
final listC = <int>[1, 2];
|
|
final listD = <int>[3, 2, 1];
|
|
|
|
expect(listEquals<void>(null, null), isTrue);
|
|
expect(listEquals(listA, null), isFalse);
|
|
expect(listEquals(null, listB), isFalse);
|
|
expect(listEquals(listA, listA), isTrue);
|
|
expect(listEquals(listA, listB), isTrue);
|
|
expect(listEquals(listA, listC), isFalse);
|
|
expect(listEquals(listA, listD), isFalse);
|
|
});
|
|
test('setEquals', () {
|
|
final setA = <int>{1, 2, 3};
|
|
final setB = <int>{1, 2, 3};
|
|
final setC = <int>{1, 2};
|
|
final setD = <int>{3, 2, 1};
|
|
|
|
expect(setEquals<void>(null, null), isTrue);
|
|
expect(setEquals(setA, null), isFalse);
|
|
expect(setEquals(null, setB), isFalse);
|
|
expect(setEquals(setA, setA), isTrue);
|
|
expect(setEquals(setA, setB), isTrue);
|
|
expect(setEquals(setA, setC), isFalse);
|
|
expect(setEquals(setA, setD), isTrue);
|
|
});
|
|
test('mapEquals', () {
|
|
final mapA = <int, int>{1: 1, 2: 2, 3: 3};
|
|
final mapB = <int, int>{1: 1, 2: 2, 3: 3};
|
|
final mapC = <int, int>{1: 1, 2: 2};
|
|
final mapD = <int, int>{3: 3, 2: 2, 1: 1};
|
|
final mapE = <int, int>{3: 1, 2: 2, 1: 3};
|
|
|
|
expect(mapEquals<void, void>(null, null), isTrue);
|
|
expect(mapEquals(mapA, null), isFalse);
|
|
expect(mapEquals(null, mapB), isFalse);
|
|
expect(mapEquals(mapA, mapA), isTrue);
|
|
expect(mapEquals(mapA, mapB), isTrue);
|
|
expect(mapEquals(mapA, mapC), isFalse);
|
|
expect(mapEquals(mapA, mapD), isTrue);
|
|
expect(mapEquals(mapA, mapE), isFalse);
|
|
});
|
|
test('binarySearch', () {
|
|
final items = <int>[1, 2, 3];
|
|
|
|
expect(binarySearch(items, 1), 0);
|
|
expect(binarySearch(items, 2), 1);
|
|
expect(binarySearch(items, 3), 2);
|
|
expect(binarySearch(items, 12), -1);
|
|
});
|
|
test('MergeSortRandom', () {
|
|
final random = Random();
|
|
for (var i = 0; i < 250; i += 1) {
|
|
// Expect some equal elements.
|
|
final list = List<int>.generate(i, (int j) => random.nextInt(i));
|
|
mergeSort(list);
|
|
for (var j = 1; j < i; j++) {
|
|
expect(list[j - 1], lessThanOrEqualTo(list[j]));
|
|
}
|
|
}
|
|
});
|
|
test('MergeSortPreservesOrder', () {
|
|
final random = Random();
|
|
// Small case where only insertion call is called,
|
|
// larger case where the internal moving insertion sort is used
|
|
// larger cases with multiple splittings, numbers just around a power of 2.
|
|
for (final size in <int>[8, 50, 511, 512, 513]) {
|
|
// Class OC compares using id.
|
|
// With size elements with id's in the range 0..size/4, a number of
|
|
// collisions are guaranteed. These should be sorted so that the 'order'
|
|
// part of the objects are still in order.
|
|
final list = List<OrderedComparable>.generate(
|
|
size,
|
|
(int i) => OrderedComparable(random.nextInt(size >> 2), i),
|
|
);
|
|
mergeSort(list);
|
|
OrderedComparable prev = list[0];
|
|
for (var i = 1; i < size; i++) {
|
|
final OrderedComparable next = list[i];
|
|
expect(prev.id, lessThanOrEqualTo(next.id));
|
|
if (next.id == prev.id) {
|
|
expect(prev.order, lessThanOrEqualTo(next.order));
|
|
}
|
|
prev = next;
|
|
}
|
|
// Reverse compare on part of list.
|
|
final List<OrderedComparable> copy = list.toList();
|
|
final int min = size >> 2;
|
|
final int max = size - min;
|
|
mergeSort<OrderedComparable>(
|
|
list,
|
|
start: min,
|
|
end: max,
|
|
compare: (OrderedComparable a, OrderedComparable b) => b.compareTo(a),
|
|
);
|
|
prev = list[min];
|
|
for (int i = min + 1; i < max; i++) {
|
|
final OrderedComparable next = list[i];
|
|
expect(prev.id, greaterThanOrEqualTo(next.id));
|
|
if (next.id == prev.id) {
|
|
expect(prev.order, lessThanOrEqualTo(next.order));
|
|
}
|
|
prev = next;
|
|
}
|
|
// Equals on OC objects is identity, so this means the parts before min,
|
|
// and the parts after max, didn't change at all.
|
|
expect(list.sublist(0, min), equals(copy.sublist(0, min)));
|
|
expect(list.sublist(max), equals(copy.sublist(max)));
|
|
}
|
|
});
|
|
test('MergeSortSpecialCases', () {
|
|
for (final size in <int>[511, 512, 513]) {
|
|
// All equal.
|
|
final list = List<OrderedComparable>.generate(size, (int i) => OrderedComparable(0, i));
|
|
mergeSort(list);
|
|
for (var i = 0; i < size; i++) {
|
|
expect(list[i].order, equals(i));
|
|
}
|
|
// All but one equal, first.
|
|
list[0] = OrderedComparable(1, 0);
|
|
for (var i = 1; i < size; i++) {
|
|
list[i] = OrderedComparable(0, i);
|
|
}
|
|
mergeSort(list);
|
|
for (var i = 0; i < size - 1; i++) {
|
|
expect(list[i].order, equals(i + 1));
|
|
}
|
|
expect(list[size - 1].order, equals(0));
|
|
|
|
// All but one equal, last.
|
|
for (var i = 0; i < size - 1; i++) {
|
|
list[i] = OrderedComparable(0, i);
|
|
}
|
|
list[size - 1] = OrderedComparable(-1, size - 1);
|
|
mergeSort(list);
|
|
expect(list[0].order, equals(size - 1));
|
|
for (var i = 1; i < size; i++) {
|
|
expect(list[i].order, equals(i - 1));
|
|
}
|
|
|
|
// Reversed.
|
|
for (var i = 0; i < size; i++) {
|
|
list[i] = OrderedComparable(size - 1 - i, i);
|
|
}
|
|
mergeSort(list);
|
|
for (var i = 0; i < size; i++) {
|
|
expect(list[i].id, equals(i));
|
|
expect(list[i].order, equals(size - 1 - i));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
class OrderedComparable implements Comparable<OrderedComparable> {
|
|
OrderedComparable(this.id, this.order);
|
|
final int id;
|
|
final int order;
|
|
@override
|
|
int compareTo(OrderedComparable other) => id - other.id;
|
|
@override
|
|
String toString() => 'OverrideComparable[$id,$order]';
|
|
}
|