-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_sum_ut.cpp
More file actions
78 lines (61 loc) · 2.02 KB
/
three_sum_ut.cpp
File metadata and controls
78 lines (61 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../src/leetcode/arrays/3sum/Solution.h"
using ::testing::ElementsAre;
using ::testing::UnorderedElementsAre;
/**
* Test case definition
*/
struct ThreeSumCase {
std::vector<int> input;
std::vector<std::vector<int>> expected;
};
/**
* Parameterized test fixture
*/
class ThreeSumTest : public ::testing::TestWithParam<ThreeSumCase> {};
/**
* Main test
*
* Note:
* - Order of triplets does NOT matter
* - Order inside each triplet DOES matter (sorted by implementation)
*/
TEST_P(ThreeSumTest, ReturnsCorrectTriplets) {
const auto& tc = GetParam();
Solution s{};
auto nums = tc.input;
auto result = s.threeSum(nums);
std::vector<::testing::Matcher<std::vector<int>>> matchers;
for (const auto& triplet : tc.expected) {
matchers.push_back(ElementsAre(triplet[0], triplet[1], triplet[2]));
}
EXPECT_THAT(result, UnorderedElementsAreArray(matchers));
}
/**
* Test data
*/
INSTANTIATE_TEST_SUITE_P(
BasicAndEdgeCases, ThreeSumTest,
::testing::Values(ThreeSumCase{{}, {}}, ThreeSumCase{{1}, {}},
ThreeSumCase{{1, 2}, {}}, ThreeSumCase{{1, 2, 3}, {}},
ThreeSumCase{{2, 1, 3}, {}}, ThreeSumCase{{0, 1, 1}, {}},
ThreeSumCase{{-5, -4, -3, -2}, {}},
ThreeSumCase{{1, 2, 3, 4}, {}},
ThreeSumCase{{-1, 0, 1, 2, -1, -4},
{{-1, -1, 2}, {-1, 0, 1}}},
ThreeSumCase{{0, 0, 0}, {{0, 0, 0}}},
ThreeSumCase{{0, 0, 0, 0}, {{0, 0, 0}}},
ThreeSumCase{{1, 2, 0, 1, 0, 0, 0, 0}, {{0, 0, 0}}},
ThreeSumCase{{-2, 0, 0, 2, 2}, {{-2, 0, 2}}}));
/**
* Extra safety test:
* ensure no duplicate triplets are returned
*/
TEST(ThreeSumExtra, NoDuplicateTriplets) {
std::vector<int> nums{-2, 0, 0, 2, 2};
Solution s{};
auto result = s.threeSum(nums);
ASSERT_EQ(result.size(), 1);
EXPECT_EQ(result[0], std::vector<int>({-2, 0, 2}));
}