From b7d5484cb7d503fa5a781e422220ac08fa5fec32 Mon Sep 17 00:00:00 2001 From: yoshikazu Date: Mon, 27 Jul 2026 15:44:15 +0000 Subject: [PATCH] Add UnionFind tests --- Tests/PhotochkTests/UnionFindTests.swift | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Tests/PhotochkTests/UnionFindTests.swift diff --git a/Tests/PhotochkTests/UnionFindTests.swift b/Tests/PhotochkTests/UnionFindTests.swift new file mode 100644 index 0000000..9cb96be --- /dev/null +++ b/Tests/PhotochkTests/UnionFindTests.swift @@ -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)) + } +}