diff options
author | Thomas Lively <tlively@google.com> | 2024-08-01 15:08:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-01 12:08:25 -0700 |
commit | 2a7c0931edf681076e678a0e8092e170333c6e86 (patch) | |
tree | af60ec405b6d6d2a38b1b05466120cebcde45249 /test/gtest | |
parent | 705c28d3a33d606c8c938d079dbf82de7e7a6b7d (diff) | |
download | binaryen-2a7c0931edf681076e678a0e8092e170333c6e86.tar.gz binaryen-2a7c0931edf681076e678a0e8092e170333c6e86.tar.bz2 binaryen-2a7c0931edf681076e678a0e8092e170333c6e86.zip |
Add a disjoint sets (union-find) utility (#6797)
This will be used in an upcoming type optimization pass and may be
generally useful.
Diffstat (limited to 'test/gtest')
-rw-r--r-- | test/gtest/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/gtest/disjoint_sets.cpp | 106 |
2 files changed, 107 insertions, 0 deletions
diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index 4604eea77..538ec1cdc 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories(../../src/wasm) set(unittest_SOURCES cfg.cpp dfa_minimization.cpp + disjoint_sets.cpp json.cpp lattices.cpp possible-contents.cpp diff --git a/test/gtest/disjoint_sets.cpp b/test/gtest/disjoint_sets.cpp new file mode 100644 index 000000000..f974957a3 --- /dev/null +++ b/test/gtest/disjoint_sets.cpp @@ -0,0 +1,106 @@ +/* + * Copyright 2024 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "support/disjoint_sets.h" +#include "gtest/gtest.h" + +using namespace wasm; + +TEST(DisjointSetsTest, NewSets) { + DisjointSets sets; + auto elem1 = sets.addSet(); + auto elem2 = sets.addSet(); + EXPECT_NE(elem1, elem2); + + auto root1 = sets.getRoot(elem1); + EXPECT_EQ(elem1, root1); + + auto root2 = sets.getRoot(elem2); + EXPECT_EQ(elem2, root2); +} + +TEST(DisjointSetsTest, Union) { + DisjointSets sets; + auto elem1 = sets.addSet(); + auto elem2 = sets.addSet(); + auto root = sets.getUnion(elem1, elem2); + EXPECT_TRUE(root == elem1 || root == elem2); + + auto root1 = sets.getRoot(elem1); + auto root2 = sets.getRoot(elem2); + EXPECT_EQ(root1, root); + EXPECT_EQ(root2, root); +} + +TEST(DisjointSetsTest, TwoUnions) { + DisjointSets sets; + auto elem1 = sets.addSet(); + auto elem2 = sets.addSet(); + auto elem3 = sets.addSet(); + auto elem4 = sets.addSet(); + + auto rootA = sets.getUnion(elem1, elem3); + auto rootB = sets.getUnion(elem2, elem4); + EXPECT_EQ(sets.getRoot(elem1), rootA); + EXPECT_EQ(sets.getRoot(elem2), rootB); + EXPECT_EQ(sets.getRoot(elem3), rootA); + EXPECT_EQ(sets.getRoot(elem4), rootB); + EXPECT_NE(rootA, rootB); +} + +TEST(DisjointSetsTest, UnionList) { + constexpr size_t count = 16; + DisjointSets sets; + size_t elems[count]; + for (size_t i = 0; i < count; ++i) { + elems[i] = sets.addSet(); + } + + for (size_t i = 1; i < count; ++i) { + sets.getUnion(elems[i], elems[i - 1]); + } + + auto root = sets.getRoot(elems[0]); + for (size_t rep = 0; rep < 2; ++rep) { + for (size_t i = 0; i < count; ++i) { + auto currRoot = sets.getRoot(elems[i]); + EXPECT_EQ(currRoot, root); + } + } +} + +TEST(DisjointSetsTest, UnionTree) { + constexpr size_t count = 16; + DisjointSets sets; + size_t elems[count]; + for (size_t i = 0; i < count; ++i) { + elems[i] = sets.addSet(); + } + + for (size_t stride = 2; stride <= count; stride *= 2) { + for (size_t i = 0; i < count; i += stride) { + sets.getUnion(elems[i], elems[i + stride / 2]); + } + } + + auto root = sets.getRoot(elems[0]); + for (size_t rep = 0; rep < 2; ++rep) { + for (size_t i = 0; i < count; ++i) { + auto currRoot = sets.getRoot(elems[i]); + EXPECT_EQ(currRoot, root); + } + } +} |