1+
2+ #include " leetcode/problems/find-the-largest-area-of-square-inside-two-rectangles.h"
3+
4+ #include " gtest/gtest.h"
5+
6+ namespace leetcode {
7+ namespace problem_3047 {
8+
9+ class FindTheLargestAreaOfSquareInsideTwoRectanglesTest : public ::testing::TestWithParam<string> {
10+ protected:
11+ void SetUp () override { solution.setStrategy (GetParam ()); }
12+
13+ FindTheLargestAreaOfSquareInsideTwoRectanglesSolution solution;
14+ };
15+
16+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example1) {
17+ vector<vector<int >> bottomLeft = {{1 , 1 }, {2 , 2 }, {3 , 1 }};
18+ vector<vector<int >> topRight = {{3 , 3 }, {4 , 4 }, {6 , 6 }};
19+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 1 );
20+ }
21+
22+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example2) {
23+ vector<vector<int >> bottomLeft = {{1 , 1 }, {1 , 3 }, {1 , 5 }};
24+ vector<vector<int >> topRight = {{5 , 5 }, {5 , 7 }, {5 , 9 }};
25+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 4 );
26+ }
27+
28+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example3) {
29+ vector<vector<int >> bottomLeft = {{1 , 1 }, {2 , 2 }, {1 , 2 }};
30+ vector<vector<int >> topRight = {{3 , 3 }, {4 , 4 }, {3 , 4 }};
31+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 1 );
32+ }
33+
34+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example4) {
35+ vector<vector<int >> bottomLeft = {{1 , 1 }, {3 , 3 }, {3 , 1 }};
36+ vector<vector<int >> topRight = {{2 , 2 }, {4 , 4 }, {4 , 2 }};
37+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 0 );
38+ }
39+
40+ // 添加一些额外的测试用例
41+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, NoIntersection) {
42+ vector<vector<int >> bottomLeft = {{1 , 1 }, {5 , 5 }};
43+ vector<vector<int >> topRight = {{2 , 2 }, {6 , 6 }};
44+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 0 );
45+ }
46+
47+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, PerfectOverlap) {
48+ vector<vector<int >> bottomLeft = {{1 , 1 }, {1 , 1 }};
49+ vector<vector<int >> topRight = {{5 , 5 }, {5 , 5 }};
50+ // 宽度和高度都是4,最大正方形边长4,面积16
51+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 16 );
52+ }
53+
54+ TEST_P (FindTheLargestAreaOfSquareInsideTwoRectanglesTest, LargeCoordinates) {
55+ vector<vector<int >> bottomLeft = {{1 , 1 }, {1000000 , 1000000 }};
56+ vector<vector<int >> topRight = {{10000000 , 10000000 }, {2000000 , 2000000 }};
57+ // 相交区域: x1 = max(1,1000000)=1000000, y1=1000000, x2=min(10000000,2000000)=2000000, y2=min(10000000,2000000)=2000000
58+ // 宽度=1000000, 高度=1000000, 边长=1000000, 面积=10^12
59+ EXPECT_EQ (solution.largestSquareArea (bottomLeft, topRight), 1000000000000LL );
60+ }
61+
62+ INSTANTIATE_TEST_SUITE_P (
63+ LeetCode, FindTheLargestAreaOfSquareInsideTwoRectanglesTest,
64+ ::testing::ValuesIn (FindTheLargestAreaOfSquareInsideTwoRectanglesSolution().getStrategyNames()));
65+
66+ } // namespace problem_3047
67+ } // namespace leetcode
0 commit comments