Files
leetcode/tests/test_1233.cpp
2025-07-21 18:14:17 +08:00

52 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <gtest/gtest.h>
#include <solution/1233.h>
#include <string>
#include <vector>
bool ArrayEquals(char **actual, int actualSize, const std::vector<std::string> &expected)
{
if (actualSize != expected.size())
return false;
std::vector<std::string> actualVec(actual, actual + actualSize);
return std::is_permutation(actualVec.begin(), actualVec.end(), expected.begin());
}
// 测试类
class RemoveSubfoldersTest : public ::testing::Test
{
protected:
void AssertFolders(std::vector<std::string> input, std::vector<std::string> expected)
{
int returnSize = 0;
std::vector<char *> c_input;
for (auto &s : input)
c_input.push_back(const_cast<char *>(s.c_str()));
char **result = removeSubfolders(c_input.data(), (int)c_input.size(), &returnSize);
ASSERT_TRUE(ArrayEquals(result, returnSize, expected));
// 可选释放 result 内存如果实现中是动态分配的free
// 例如for (int i = 0; i < returnSize; ++i) free(result[i]);
// free(result);
}
};
// 示例 1
TEST_F(RemoveSubfoldersTest, Example1)
{
AssertFolders({"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}, {"/a", "/c/d", "/c/f"});
}
// 示例 2
TEST_F(RemoveSubfoldersTest, Example2)
{
AssertFolders({"/a", "/a/b/c", "/a/b/d"}, {"/a"});
}
// 示例 3
TEST_F(RemoveSubfoldersTest, Example3)
{
AssertFolders({"/a/b/c", "/a/b/ca", "/a/b/d"}, {"/a/b/c", "/a/b/ca", "/a/b/d"});
}