Skip to content

Commit 656035e

Browse files
committed
refactor: [search] Use tests.h
1 parent bc7ba5f commit 656035e

File tree

3 files changed

+118
-286
lines changed

3 files changed

+118
-286
lines changed

cpp/search/binary-search-01.cpp

Lines changed: 33 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,112 +9,57 @@
99
* https://leetcode.com/problems/binary-search/
1010
*/
1111

12-
#include <bits/stdc++.h>
13-
14-
using namespace std;
15-
16-
/* ===========================================================================
17-
* Test helpers
18-
* ===========================================================================
19-
*/
20-
class _00_test
21-
{
22-
public:
23-
_00_test(const string &name) : name(name) {}
24-
25-
string getName(void) const { return name; }
26-
27-
virtual int search(vector<int> &nums, int target) = 0;
28-
29-
private:
30-
string name;
31-
};
12+
#include "tests.h"
3213

3314
/* ===========================================================================
3415
* Algorithms implementation
3516
* ===========================================================================
3617
*/
3718

19+
#define _bs_desc "Binary search"
20+
3821
/* TC : Best O(1), Average O(log n), Worst O(log n)
3922
* SC : O(1)
4023
*/
41-
class _01_binary_search : public _00_test
24+
int search(const vi_t &a, int t)
4225
{
43-
public:
44-
_01_binary_search() : _00_test("Binary search") {}
45-
46-
int search(vector<int> &a, int t) override
47-
{
48-
int l = 0, r = a.size() - 1;
49-
while (l <= r) {
50-
int m = (l + r) / 2;
51-
if (t == a[m])
52-
return m;
53-
else if (t < a[m])
54-
r = m - 1;
55-
else
56-
l = m + 1;
57-
}
58-
return -1;
26+
int n = size(a), l = 0, r = n - 1;
27+
while (l <= r) {
28+
int m = (l + r) / 2;
29+
if (t == a[m])
30+
return m;
31+
else if (t < a[m])
32+
r = m - 1;
33+
else
34+
l = m + 1;
5935
}
60-
};
36+
return -1;
37+
}
6138

6239
/* ===========================================================================
6340
* Test code
6441
* ===========================================================================
6542
*/
66-
string _vec2str(vector<int> &vec)
67-
{
68-
ostringstream oss;
69-
oss << "{";
70-
copy(vec.begin(), vec.end() - 1, ostream_iterator<int>(oss, ", "));
71-
oss << vec.back();
72-
oss << "}";
73-
return oss.str();
74-
}
75-
76-
void test_impl(vector<pair<vector<int>, int>> &ip, vector<int> &op,
77-
shared_ptr<_00_test> f)
78-
{
79-
for (size_t i = 0; i < ip.size(); i++) {
80-
int t = f->search(ip[i].first, ip[i].second);
81-
if (t != op[i]) {
82-
cerr << f->getName() << " test failed: "
83-
<< "expected " << op[i] << ", actual " << t
84-
<< "." << endl;
85-
exit(1);
86-
}
87-
88-
if (getenv("SHOW_TEST_OUTPUT"))
89-
cout << " test-" << i << ": "
90-
<< "input: nums = " << _vec2str(ip[i].first)
91-
<< ", target = " << ip[i].second
92-
<< " output: index = " << t << "\n";
43+
#define _bs_check(arr, target, e) \
44+
{ \
45+
int a = search(arr, target); \
46+
string im = format("array = {}, target = {}", \
47+
to_string(arr), target); \
48+
SET_CUSTOM_SUCCESS_MSG(im, to_string(a)); \
49+
CHECK_EQ(e, a); \
50+
SHOW_OUTPUT(im, a); \
9351
}
94-
}
9552

96-
int main(int, char **)
53+
TEST(search, _bs_desc)
9754
{
98-
vector<pair<vector<int>, int>> ip{
99-
{{-1, 0, 3, 5, 9, 12}, 9},
100-
{{-1, 0, 3, 5, 9, 12}, 2},
55+
vi2_t arrs{
56+
{-1, 0, 3, 5, 9, 12},
57+
{-1, 0, 3, 5, 9, 12},
10158
};
102-
103-
vector<int> op{4, -1};
104-
105-
vector<shared_ptr<_00_test>> impls{
106-
make_shared<_01_binary_search>(),
107-
};
108-
109-
for (size_t i = 0; i < impls.size(); i++) {
110-
if (getenv("SHOW_TEST_OUTPUT"))
111-
cout << "Testing implementation " << i + 1 << " "
112-
<< impls[i]->getName() << "\n";
113-
114-
test_impl(ip, op, impls[i]);
115-
}
116-
117-
cout << "Executed " << impls.size() << " implementations"
118-
<< " with " << ip.size() << " tests." << endl;
119-
return 0;
59+
vi_t targets{9, 2};
60+
vi_t exps{4, -1};
61+
int n = size(arrs);
62+
fii (i, n) _bs_check(arrs[i], targets[i], exps[i]);
12063
}
64+
65+
INIT_TEST_MAIN();

cpp/search/binary-search-02.cpp

Lines changed: 40 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,121 +9,65 @@
99
* https://leetcode.com/problems/search-in-rotated-sorted-array
1010
*/
1111

12-
#include <bits/stdc++.h>
13-
14-
using namespace std;
15-
16-
/* ===========================================================================
17-
* Test helpers
18-
* ===========================================================================
19-
*/
20-
class _00_test
21-
{
22-
public:
23-
_00_test(const string &name) : name(name) {}
24-
25-
string getName(void) const { return name; }
26-
27-
virtual int search(vector<int> &nums, int target) = 0;
28-
29-
private:
30-
string name;
31-
};
12+
#include "tests.h"
3213

3314
/* ===========================================================================
3415
* Algorithms implementation
3516
* ===========================================================================
3617
*/
3718

19+
#define _bs_desc "Binary search in sorted rotated array"
20+
3821
/* TC : Best O(1), Average O(log n), Worst O(log n)
3922
* SC : O(1)
4023
*/
41-
class _01_binary_search : public _00_test
24+
int search(const vi_t &a, int t)
4225
{
43-
public:
44-
_01_binary_search()
45-
: _00_test("Binary search in sorted rotated array")
46-
{
47-
}
48-
49-
int search(vector<int> &a, int t) override
50-
{
51-
int l = 0, r = a.size() - 1;
52-
while (l <= r) {
53-
int m = (l + r) / 2;
54-
if (t == a[m])
55-
return m;
56-
else if (a[l] <= a[m]) {
57-
if (a[l] <= t && t < a[m])
58-
r = m - 1;
59-
else
60-
l = m + 1;
61-
} else {
62-
if (a[m] < t && t <= a[r])
63-
l = m + 1;
64-
else
65-
r = m - 1;
66-
}
26+
int n = size(a), l = 0, r = n - 1;
27+
while (l <= r) {
28+
int m = (l + r) / 2;
29+
if (t == a[m])
30+
return m;
31+
else if (a[l] <= a[m]) {
32+
if (a[l] <= t && t < a[m])
33+
r = m - 1;
34+
else
35+
l = m + 1;
36+
} else {
37+
if (a[m] < t && t <= a[r])
38+
l = m + 1;
39+
else
40+
r = m - 1;
6741
}
68-
return -1;
6942
}
70-
};
43+
return -1;
44+
}
7145

7246
/* ===========================================================================
7347
* Test code
7448
* ===========================================================================
7549
*/
76-
string _vec2str(vector<int> &vec)
77-
{
78-
ostringstream oss;
79-
copy(vec.begin(), vec.end() - 1, ostream_iterator<int>(oss, ", "));
80-
oss << vec.back();
81-
return oss.str();
82-
}
83-
84-
void test_impl(vector<pair<vector<int>, int>> &ip, vector<int> &op,
85-
shared_ptr<_00_test> f)
86-
{
87-
for (size_t i = 0; i < ip.size(); i++) {
88-
int t = f->search(ip[i].first, ip[i].second);
89-
if (t != op[i]) {
90-
cerr << f->getName() << " test failed: "
91-
<< "expected " << op[i] << ", actual " << t
92-
<< "." << endl;
93-
exit(1);
94-
}
95-
96-
if (getenv("SHOW_TEST_OUTPUT"))
97-
cout << " test-" << i << ": "
98-
<< "input: nums = " << _vec2str(ip[i].first)
99-
<< ", target = " << ip[i].second
100-
<< " output: index = " << t << "\n";
50+
#define _bs_check(arr, target, e) \
51+
{ \
52+
int a = search(arr, target); \
53+
string im = format("array = {}, target = {}", \
54+
to_string(arr), target); \
55+
SET_CUSTOM_SUCCESS_MSG(im, to_string(a)); \
56+
CHECK_EQ(e, a); \
57+
SHOW_OUTPUT(im, a); \
10158
}
102-
}
10359

104-
int main(int, char **)
60+
TEST(search, _bs_desc)
10561
{
106-
vector<pair<vector<int>, int>> ip{
107-
{{4, 5, 6, 7, 0, 1, 2}, 0},
108-
{{4, 5, 6, 7, 0, 1, 2}, 3},
109-
{{1}, 0},
110-
};
111-
112-
vector<int> op{4, -1, -1};
113-
114-
vector<shared_ptr<_00_test>> impls{
115-
make_shared<_01_binary_search>(),
62+
vi2_t arrs{
63+
{4, 5, 6, 7, 0, 1, 2},
64+
{4, 5, 6, 7, 0, 1, 2},
65+
{1},
11666
};
117-
118-
for (size_t i = 0; i < impls.size(); i++) {
119-
if (getenv("SHOW_TEST_OUTPUT"))
120-
cout << "Testing implementation " << i + 1 << " "
121-
<< impls[i]->getName() << "\n";
122-
123-
test_impl(ip, op, impls[i]);
124-
}
125-
126-
cout << "Executed " << impls.size() << " implementations"
127-
<< " with " << ip.size() << " tests." << endl;
128-
return 0;
67+
vi_t targets{0, 3, 0};
68+
vi_t exps{4, -1, -1};
69+
int n = size(arrs);
70+
fii (i, n) _bs_check(arrs[i], targets[i], exps[i]);
12971
}
72+
73+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)