|
| 1 | +// Copyright (c) 2018, 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 | +import 'dart:collection'; |
| 6 | + |
| 7 | +import 'package:graphs/graphs.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import 'utils/utils.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + group('sorts a graph', () { |
| 14 | + test('with no nodes', () { |
| 15 | + expect(_topologicalSort({}), isEmpty); |
| 16 | + }); |
| 17 | + |
| 18 | + test('with only one node', () { |
| 19 | + expect(_topologicalSort({1: []}), equals([1])); |
| 20 | + }); |
| 21 | + |
| 22 | + test('with no edges', () { |
| 23 | + expect(_topologicalSort({1: [], 2: [], 3: [], 4: []}), |
| 24 | + unorderedEquals([1, 2, 3, 4])); |
| 25 | + }); |
| 26 | + |
| 27 | + test('with single edges', () { |
| 28 | + expect( |
| 29 | + _topologicalSort({ |
| 30 | + 1: [2], |
| 31 | + 2: [3], |
| 32 | + 3: [4], |
| 33 | + 4: [] |
| 34 | + }), |
| 35 | + equals([1, 2, 3, 4])); |
| 36 | + }); |
| 37 | + |
| 38 | + test('with many edges from one node', () { |
| 39 | + var result = _topologicalSort({ |
| 40 | + 1: [2, 3, 4], |
| 41 | + 2: [], |
| 42 | + 3: [], |
| 43 | + 4: [] |
| 44 | + }); |
| 45 | + expect(result.indexOf(1), lessThan(result.indexOf(2))); |
| 46 | + expect(result.indexOf(1), lessThan(result.indexOf(3))); |
| 47 | + expect(result.indexOf(1), lessThan(result.indexOf(4))); |
| 48 | + }); |
| 49 | + |
| 50 | + test('with transitive edges', () { |
| 51 | + var result = _topologicalSort({ |
| 52 | + 1: [2, 4], |
| 53 | + 2: [], |
| 54 | + 3: [], |
| 55 | + 4: [3] |
| 56 | + }); |
| 57 | + expect(result.indexOf(1), lessThan(result.indexOf(2))); |
| 58 | + expect(result.indexOf(1), lessThan(result.indexOf(3))); |
| 59 | + expect(result.indexOf(1), lessThan(result.indexOf(4))); |
| 60 | + expect(result.indexOf(4), lessThan(result.indexOf(3))); |
| 61 | + }); |
| 62 | + |
| 63 | + test('with diamond edges', () { |
| 64 | + var result = _topologicalSort({ |
| 65 | + 1: [2, 3], |
| 66 | + 2: [4], |
| 67 | + 3: [4], |
| 68 | + 4: [] |
| 69 | + }); |
| 70 | + expect(result.indexOf(1), lessThan(result.indexOf(2))); |
| 71 | + expect(result.indexOf(1), lessThan(result.indexOf(3))); |
| 72 | + expect(result.indexOf(1), lessThan(result.indexOf(4))); |
| 73 | + expect(result.indexOf(2), lessThan(result.indexOf(4))); |
| 74 | + expect(result.indexOf(3), lessThan(result.indexOf(4))); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('respects custom equality and hash functions', () { |
| 79 | + expect( |
| 80 | + _topologicalSort<int>({ |
| 81 | + 0: [2], |
| 82 | + 3: [4], |
| 83 | + 5: [6], |
| 84 | + 7: [] |
| 85 | + }, |
| 86 | + equals: (i, j) => (i ~/ 2) == (j ~/ 2), |
| 87 | + hashCode: (i) => (i ~/ 2).hashCode), |
| 88 | + equals([ |
| 89 | + 0, |
| 90 | + anyOf([2, 3]), |
| 91 | + anyOf([4, 5]), |
| 92 | + anyOf([6, 7]) |
| 93 | + ])); |
| 94 | + }); |
| 95 | + |
| 96 | + group('throws a CycleException for a graph with', () { |
| 97 | + test('a one-node cycle', () { |
| 98 | + expect( |
| 99 | + () => _topologicalSort({ |
| 100 | + 1: [1] |
| 101 | + }), |
| 102 | + throwsCycleException([1])); |
| 103 | + }); |
| 104 | + |
| 105 | + test('a multi-node cycle', () { |
| 106 | + expect( |
| 107 | + () => _topologicalSort({ |
| 108 | + 1: [2], |
| 109 | + 2: [3], |
| 110 | + 3: [4], |
| 111 | + 4: [1] |
| 112 | + }), |
| 113 | + throwsCycleException([1, 2, 3, 4])); |
| 114 | + }); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +/// Runs a topological sort on a graph represented a map from keys to edges. |
| 119 | +List<T> _topologicalSort<T>(Map<T, List<T>> graph, |
| 120 | + {bool Function(T, T)? equals, int Function(T)? hashCode}) { |
| 121 | + if (equals != null) { |
| 122 | + graph = LinkedHashMap(equals: equals, hashCode: hashCode)..addAll(graph); |
| 123 | + } |
| 124 | + return topologicalSort(graph.keys, (node) { |
| 125 | + expect(graph, contains(node)); |
| 126 | + return graph[node]!; |
| 127 | + }, equals: equals, hashCode: hashCode); |
| 128 | +} |
0 commit comments