66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
#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);
|
|
}
|