Add UnionFind tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import Testing
|
||||
|
||||
@testable import Photochk
|
||||
|
||||
@Suite("UnionFind")
|
||||
struct UnionFindTests {
|
||||
@Test("結合していない要素はそれぞれ単独の成分になる")
|
||||
func isolatedElements() {
|
||||
var uf = UnionFind(count: 4)
|
||||
let components = uf.components().map { $0.sorted() }.sorted { $0[0] < $1[0] }
|
||||
#expect(components == [[0], [1], [2], [3]])
|
||||
}
|
||||
|
||||
@Test("推移的に結合した要素は1つの成分にまとまる")
|
||||
func transitiveMerge() {
|
||||
var uf = UnionFind(count: 5)
|
||||
uf.union(0, 1)
|
||||
uf.union(1, 2)
|
||||
uf.union(3, 4)
|
||||
|
||||
let components = uf.components().map { $0.sorted() }.sorted { $0[0] < $1[0] }
|
||||
#expect(components == [[0, 1, 2], [3, 4]])
|
||||
}
|
||||
|
||||
@Test("同じ成分をもう一度 union しても何も変わらない")
|
||||
func repeatedUnion() {
|
||||
var uf = UnionFind(count: 3)
|
||||
#expect(uf.union(0, 1) == true)
|
||||
#expect(uf.union(1, 0) == false)
|
||||
#expect(uf.find(0) == uf.find(1))
|
||||
#expect(uf.find(0) != uf.find(2))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user