Solved: 27 - Unordered Containers
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#include "unordered_containers.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(UnorderedContainers, FirstUnique) {
|
||||
@@ -9,3 +12,42 @@ TEST(UnorderedContainers, HasDuplicate) {
|
||||
EXPECT_TRUE(stl_containers::has_duplicate({1, 2, 2}));
|
||||
EXPECT_FALSE(stl_containers::has_duplicate({1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, GroupAnagramsBasic) {
|
||||
auto result = stl_containers::group_anagrams({"eat", "tea", "tan", "ate", "nat", "bat"});
|
||||
// Sort each group for deterministic comparison
|
||||
for (auto &group: result | std::views::values) {
|
||||
std::ranges::sort(group);
|
||||
}
|
||||
ASSERT_EQ(result.size(), 3);
|
||||
EXPECT_EQ(result["aet"], (std::vector<std::string>{"ate", "eat", "tea"}));
|
||||
EXPECT_EQ(result["ant"], (std::vector<std::string>{"nat", "tan"}));
|
||||
EXPECT_EQ(result["abt"], (std::vector<std::string>{"bat"}));
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, GroupAnagramsEmpty) {
|
||||
auto result = stl_containers::group_anagrams({});
|
||||
EXPECT_TRUE(result.empty());
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, GroupAnagramsSingleWord) {
|
||||
auto result = stl_containers::group_anagrams({"hello"});
|
||||
ASSERT_EQ(result.size(), 1);
|
||||
EXPECT_EQ(result["ehllo"], (std::vector<std::string>{"hello"}));
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, GroupAnagramsNoAnagrams) {
|
||||
auto result = stl_containers::group_anagrams({"abc", "def", "ghi"});
|
||||
ASSERT_EQ(result.size(), 3);
|
||||
EXPECT_EQ(result["abc"].size(), 1);
|
||||
EXPECT_EQ(result["def"].size(), 1);
|
||||
EXPECT_EQ(result["ghi"].size(), 1);
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, GroupAnagramsAllSame) {
|
||||
auto result = stl_containers::group_anagrams({"abc", "bca", "cab"});
|
||||
ASSERT_EQ(result.size(), 1);
|
||||
auto& group = result["abc"];
|
||||
std::ranges::sort(group);
|
||||
EXPECT_EQ(group, (std::vector<std::string>{"abc", "bca", "cab"}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user