blob: 2e5249d6eff6880be7b6b1755aaab64f292455c3 [file] [log] [blame]
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:kernel/src/union_find.dart';
testSame<T>(UnionFind<T> unionFind, T a, T b, bool expected) {
expect(expected, unionFind.nodesInSameSet(unionFind[a], unionFind[b]));
}
testSets<T>(UnionFind<T> unionFind, Set<Set<T>> sets) {
for (Set<T> set in sets) {
UnionFindNode<T> root = unionFind.findNode(unionFind[set.first]);
for (T value in unionFind.values) {
testSame(unionFind, value, root.value, set.contains(value));
}
}
}
testFind<T>(UnionFind<T> unionFind, T value, T expected) {
expect(expected, unionFind.findNode(unionFind[value]).value);
}
testUnion<T>(UnionFind<T> unionFind, T a, T b, T expected) {
expect(expected, unionFind.unionOfNodes(unionFind[a], unionFind[b]).value);
}
main() {
UnionFind<int> unionFind = new UnionFind();
// {0}
testFind(unionFind, 0, 0);
testSame(unionFind, 0, 0, true);
testSets(unionFind, {
{0}
});
// {0}, {1}
testFind(unionFind, 1, 1);
testSame(unionFind, 0, 1, false);
testSame(unionFind, 1, 0, false);
testSame(unionFind, 1, 1, true);
testSets(unionFind, {
{0},
{1}
});
// {0}, {1}, {2}
testFind(unionFind, 2, 2);
testSame(unionFind, 0, 2, false);
testSame(unionFind, 1, 2, false);
testSame(unionFind, 2, 2, true);
testSets(unionFind, {
{0},
{1},
{2}
});
// {0}, {1}, {2}
testUnion(unionFind, 0, 0, 0);
testSame(unionFind, 0, 0, true);
testSame(unionFind, 0, 1, false);
testSame(unionFind, 0, 2, false);
testSets(unionFind, {
{0},
{1},
{2}
});
// {0, 1}, {2}
testUnion(unionFind, 0, 1, 0);
testSame(unionFind, 0, 0, true);
testSame(unionFind, 0, 1, true);
testSame(unionFind, 1, 0, true);
testSame(unionFind, 0, 2, false);
testFind(unionFind, 0, 0);
testFind(unionFind, 1, 0);
testSets(unionFind, {
{0, 1},
{2}
});
// {0, 1}, {2, 3}
testUnion(unionFind, 2, 3, 2);
testSame(unionFind, 0, 0, true);
testSame(unionFind, 0, 1, true);
testSame(unionFind, 0, 2, false);
testSame(unionFind, 0, 3, false);
testSame(unionFind, 0, 0, true);
testSame(unionFind, 0, 1, true);
testSame(unionFind, 0, 2, false);
testSame(unionFind, 0, 3, false);
testFind(unionFind, 2, 2);
testFind(unionFind, 3, 2);
testSets(unionFind, {
{0, 1},
{2, 3}
});
// {0, 1, 2, 3}
testUnion(unionFind, 1, 2, 0);
testSets(unionFind, {
{0, 1, 2, 3}
});
}
expect(expected, actual) {
if (expected != actual) throw 'Expected $expected, actual $actual';
}