|
| 1 | +import 'dart:math'; |
1 | 2 |
|
2 | 3 | import 'package:checks/checks.dart';
|
| 4 | +import 'package:collection/collection.dart'; |
3 | 5 | import 'package:test/scaffolding.dart';
|
4 | 6 | import 'package:zulip/model/algorithms.dart';
|
5 | 7 |
|
@@ -55,4 +57,95 @@ void main() {
|
55 | 57 | });
|
56 | 58 | }
|
57 | 59 | });
|
| 60 | + |
| 61 | + group('bucketSort', () { |
| 62 | + /// Same spec as [bucketSort], except slow: N * B time instead of N + B. |
| 63 | + List<T> simpleBucketSort<T>(Iterable<T> xs, int Function(T) bucketOf, { |
| 64 | + required int numBuckets, |
| 65 | + }) { |
| 66 | + return Iterable.generate(numBuckets, |
| 67 | + (k) => xs.where((s) => bucketOf(s) == k)).flattenedToList; |
| 68 | + } |
| 69 | + |
| 70 | + void checkBucketSort<T>(Iterable<T> xs, { |
| 71 | + required int Function(T) bucketOf, required int numBuckets, |
| 72 | + }) { |
| 73 | + check(bucketSort(xs, bucketOf, numBuckets: numBuckets)).deepEquals( |
| 74 | + simpleBucketSort<T>(xs, bucketOf, numBuckets: numBuckets)); |
| 75 | + } |
| 76 | + |
| 77 | + int stringBucket(String s) => s.codeUnits.last - '0'.codeUnits.single; |
| 78 | + |
| 79 | + test('explicit result, interleaved: 4 elements, 2 buckets', () { |
| 80 | + check(bucketSort(['a1', 'd0', 'c1', 'b0'], stringBucket, numBuckets: 2)) |
| 81 | + .deepEquals(['d0', 'b0', 'a1', 'c1']); |
| 82 | + }); |
| 83 | + |
| 84 | + List<_SortablePair> generatePairs(Iterable<int> keys) { |
| 85 | + var token = 0; |
| 86 | + return keys.map((k) => _SortablePair(k, "${token++}")).toList(); |
| 87 | + } |
| 88 | + |
| 89 | + void checkSortPairs(int numBuckets, Iterable<int> keys) { |
| 90 | + checkBucketSort(numBuckets: numBuckets, bucketOf: (p) => p.key, |
| 91 | + generatePairs(keys)); |
| 92 | + } |
| 93 | + |
| 94 | + test('empty list, zero buckets', () { |
| 95 | + checkSortPairs(0, []); |
| 96 | + }); |
| 97 | + |
| 98 | + test('empty, some buckets', () { |
| 99 | + checkSortPairs(3, []); |
| 100 | + }); |
| 101 | + |
| 102 | + test('interleaved: 4 elements, 2 buckets', () { |
| 103 | + checkSortPairs(2, [1, 0, 1, 0]); |
| 104 | + }); |
| 105 | + |
| 106 | + test('some buckets empty: 10 elements in 3 of 10 buckets', () { |
| 107 | + checkSortPairs(10, [9, 9, 9, 5, 5, 5, 1, 1, 1, 1]); |
| 108 | + }); |
| 109 | + |
| 110 | + test('one big bucket', () { |
| 111 | + checkSortPairs(1, Iterable.generate(100, (_) => 0)); |
| 112 | + }); |
| 113 | + |
| 114 | + const seed = 4321; |
| 115 | + |
| 116 | + Iterable<int> randomKeys({required int numBuckets, required int length}) { |
| 117 | + final rand = Random(seed); |
| 118 | + return Iterable.generate(length, (_) => rand.nextInt(numBuckets)); |
| 119 | + } |
| 120 | + |
| 121 | + test('long random list, 1000 in 2 buckets', () { |
| 122 | + checkSortPairs(2, randomKeys(numBuckets: 2, length: 1000)); |
| 123 | + }); |
| 124 | + |
| 125 | + test('long random list, 1000 in 1000 buckets', () { |
| 126 | + checkSortPairs(1000, randomKeys(numBuckets: 1000, length: 1000)); |
| 127 | + }); |
| 128 | + |
| 129 | + test('sparse random list, 100 in 1000 buckets', () { |
| 130 | + checkSortPairs(1000, randomKeys(numBuckets: 1000, length: 100)); |
| 131 | + }); |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +class _SortablePair { |
| 136 | + _SortablePair(this.key, this.tag); |
| 137 | + |
| 138 | + final int key; |
| 139 | + final String tag; |
| 140 | + |
| 141 | + @override |
| 142 | + bool operator ==(Object other) { |
| 143 | + return other is _SortablePair && key == other.key && tag == other.tag; |
| 144 | + } |
| 145 | + |
| 146 | + @override |
| 147 | + int get hashCode => Object.hash(key, tag); |
| 148 | + |
| 149 | + @override |
| 150 | + String toString() => "$tag:$key"; |
58 | 151 | }
|
0 commit comments