This commit is contained in:
2025-09-29 10:56:06 +08:00
parent f6e998beff
commit af0fe8a168
3 changed files with 103 additions and 0 deletions

65
tests/test_976.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <gtest/gtest.h>
#include <solution/976.h>
// 测试类定义
class LargestPerimeterTest : public ::testing::Test
{
protected:
void AssertLargestPerimeter(std::vector<int> input, int expected)
{
int *nums = new int[input.size()];
std::copy(input.begin(), input.end(), nums);
int result = largestPerimeter(nums, input.size());
ASSERT_EQ(result, expected);
delete[] nums;
}
};
// 示例 1
TEST_F(LargestPerimeterTest, Example1)
{
// 输入: nums = [2,1,2]
// 输出: 5
AssertLargestPerimeter({2, 1, 2}, 5);
}
// 示例 2
TEST_F(LargestPerimeterTest, Example2)
{
// 输入: nums = [1,2,1,10]
// 输出: 0
AssertLargestPerimeter({1, 2, 1, 10}, 0);
}
// 边界测试:最小输入
TEST_F(LargestPerimeterTest, SmallestInput)
{
// 输入: nums = [3,4,5]
// 输出: 12
AssertLargestPerimeter({3, 4, 5}, 12);
}
// 边界测试:无法组成三角形的情况
TEST_F(LargestPerimeterTest, CannotFormTriangle)
{
// 输入: nums = [1,1,2]
// 输出: 0
AssertLargestPerimeter({1, 1, 2}, 0);
}
// 边界测试:重复元素
TEST_F(LargestPerimeterTest, RepeatedElements)
{
// 输入: nums = [3,3,3,3,3]
// 输出: 9
AssertLargestPerimeter({3, 3, 3, 3, 3}, 9);
}
// 边界测试:无有效三角形
TEST_F(LargestPerimeterTest, NoValidTriangle)
{
// 输入: nums = [1,1,1,10]
// 输出: 3
AssertLargestPerimeter({1, 1, 1, 10}, 3);
}