-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35.cpp
66 lines (52 loc) · 1.45 KB
/
35.cpp
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
#include <gtest/gtest.h>
#include <vector>
using namespace std;
class Solution {
public:
inline int solver(vector<int>& nums, int target, int left, int right) {
int mid = left + (right - left) / 2;
if (left > right) {
return left;
}
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < target) {
return solver(nums, target, mid + 1, right);
}
else {
return solver(nums, target, left, mid - 1);
}
}
int searchInsert(vector<int>& nums, int target) {
return solver(nums, target, 0, nums.size() - 1);
}
};
class Testing : public testing::Test {
public:
Solution sol;
};
TEST_F(Testing, tc1) {
vector<int> nums = {1, 3, 5, 6};
EXPECT_EQ(sol.searchInsert(nums, 5), 2);
}
TEST_F(Testing, tc2) {
vector<int> nums = {1, 3, 5, 6};
EXPECT_EQ(sol.searchInsert(nums, 2), 1);
}
TEST_F(Testing, tc3) {
vector<int> nums = {1, 3, 5, 6};
EXPECT_EQ(sol.searchInsert(nums, 7), 4);
}
TEST_F(Testing, tc4) {
vector<int> nums = {1, 3, 5, 6};
EXPECT_EQ(sol.searchInsert(nums, 0), 0);
}
TEST_F(Testing, tc5) {
vector<int> nums = {1};
EXPECT_EQ(sol.searchInsert(nums, 0), 0);
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}