Skip to content

Commit

Permalink
Improve some missing test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Dec 26, 2023
1 parent 18c98ac commit cc224fb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lib/src/collection/iterable/unique.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import 'dart:collection' show HashSet;
extension UniqueIterableExtension<E> on Iterable<E> {
/// Returns a lazy iterable that filters out duplicates from this [Iterator].
///
/// If [equals] and [hashCode] are omitted, the iterator uses the objects'
/// intrinsic [Object.operator==] and [Object.hashCode] for comparison.
/// Duplicates are filtered out using a [Set] keeping track of the unique
/// objects seen so far. If a [factory] is provided, it is used to create a
/// new set instance. Otherwise a standard [HashSet] is created using
/// [equals] and [hashCode], and if those are missing the iterator uses the
/// objects' intrinsic [Object.operator==] and [Object.hashCode] for
/// comparison.
///
/// The following expression iterates over 1, 2, 3, and 4; skipping the second
/// occurrence of 2:
///
/// [1, 2, 3, 2, 4].unique()
///
Iterable<E> unique(
{Set<E> Function()? factory,
bool Function(E e1, E e2)? equals,
int Function(E e)? hashCode}) sync* {
Iterable<E> unique({
Set<E> Function()? factory,
bool Function(E e1, E e2)? equals,
int Function(E e)? hashCode,
}) sync* {
final uniques = factory == null
? HashSet(equals: equals, hashCode: hashCode)
: factory();
Expand Down
3 changes: 3 additions & 0 deletions test/char_matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ void main() {
'\u{f384a}\u{100e70}\u{1010a4}\u{10c920}\u{1090f4}\u{f280d}\u{fccb9}',
'012abcABC_!@# ');
});
test('otherNotAssigned', () {
verify(UnicodeCharMatcher.otherNotAssigned(), '', '012abcABC_!@# ');
});
test('otherSurrogate', () {
final matcher = UnicodeCharMatcher.otherSurrogate();
for (final code in [0xd851, 0xd97c, 0xdcc4, 0xd96c, 0xded6, 0xd948]) {
Expand Down
42 changes: 40 additions & 2 deletions test/collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:math';

import 'package:more/collection.dart';
import 'package:more/comparator.dart';
import 'package:more/graph.dart';
import 'package:more/math.dart';
import 'package:more/number.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -663,7 +664,7 @@ void main() {
() => allHeapTests(<T>(List<T> list, {Comparator<T>? comparator}) =>
Heap<T>.of(list, comparator: comparator)));
group(
'pushAll',
'addAll',
() => allHeapTests(<T>(List<T> list, {Comparator<T>? comparator}) =>
Heap<T>(comparator: comparator)..addAll(list)));
});
Expand Down Expand Up @@ -1789,6 +1790,20 @@ void main() {
expect(uniques, [1, 2, 3]);
expect(uniques, [1, 2, 3]);
});
test('factory', () {
final uniques = [1, 2, 2, 3, 3, 3]
.unique(factory: StorageStrategy.positiveInteger().createSet);
expect(uniques, [1, 2, 3]);
expect(uniques, [1, 2, 3]);
});
test('equals and hashCode', () {
final a = const Point(1, 2), b = const Point(1, 1) + const Point(0, 1);
final uniques = [a, b, a].unique(
equals: (a, b) => identical(a, b),
hashCode: (a) => identityHashCode(a));
expect(uniques, [a, b]);
expect(uniques, [a, b]);
});
});
group('window', () {
test('error', () {
Expand Down Expand Up @@ -4635,6 +4650,17 @@ void allHeapTests(
expect(heap.addAndRemoveFirst('World'), 'World');
expect(heap.length, 0);
});
test('remove', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
expect(heap.remove('Emma'), isTrue);
expect(heap.remove('Emma'), isFalse);
expect(heap, unorderedEquals(['Olivia', 'Sophia']));
});
test('removeAll', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
expect(heap.removeAll(), unorderedEquals(['Olivia', 'Emma', 'Sophia']));
expect(heap, isEmpty);
});
test('removeFirstAndAdd', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
expect(heap.removeFirstAndAdd('Amelia'), 'Sophia');
Expand All @@ -4647,7 +4673,17 @@ void allHeapTests(
expect(heap.addAndRemoveFirst('Amelia'), 'Sophia');
expect(heap.addAndRemoveFirst('Nora'), 'Olivia');
expect(heap.addAndRemoveFirst('Violet'), 'Violet');
expect(heap.toList()..sort(), ['Amelia', 'Emma', 'Nora']);
expect(heap, unorderedEquals(['Amelia', 'Emma', 'Nora']));
});
test('unorderedElements', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
expect(
heap.unorderedElements, unorderedEquals(['Olivia', 'Emma', 'Sophia']));
});
test('unorderedList', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
expect(
heap.toUnorderedList(), unorderedEquals(['Olivia', 'Emma', 'Sophia']));
});
test('clear', () {
final heap = createHeap<String>(['Olivia', 'Emma', 'Sophia']);
Expand Down Expand Up @@ -4813,6 +4849,8 @@ void allSortedListTests(
final list = createSortedList<int>([5, 1, 3]);
expect(() => list[0] = 2, throwsUnsupportedError);
expect(() => list.length = 2, throwsUnsupportedError);
expect(() => list.insert(1, 4), throwsUnsupportedError);
expect(() => list.insertAll(1, [2, 4]), throwsUnsupportedError);
expect(() => list.sort(), throwsUnsupportedError);
expect(() => list.shuffle(), throwsUnsupportedError);
});
Expand Down
10 changes: 8 additions & 2 deletions test/graph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2571,16 +2571,22 @@ void main() {
]));
expect(minCut.weight, 4);
});
test('empty graph errors', () {
test('empty graph error', () {
final graph = Graph<String, void>.undirected();
expect(graph.minCut, throwsArgumentError);
});
test('directed graph errors', () {
test('directed graph error', () {
final graph = Graph<int, void>.directed()
..addEdge(0, 1)
..addEdge(1, 2);
expect(graph.minCut, throwsArgumentError);
});
test('negative edge error', () {
final graph = Graph<int, int>.undirected()
..addEdge(0, 1, value: 1)
..addEdge(1, 2, value: -1);
expect(graph.minCut, throwsArgumentError);
});
});
});
group('traverse', () {
Expand Down
4 changes: 4 additions & 0 deletions test/interval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ void verify<T>(
test('isSingle', () {
expect(interval.isSingle, isSingle);
});
test('isBounded', () {
expect(interval.isBounded,
interval.lower.isBounded && interval.upper.isBounded);
});
if (intLength != null) {
test('toIntLength', () {
if (intLength.isInfinite) {
Expand Down

0 comments on commit cc224fb

Please sign in to comment.