Skip to content

Commit 6deafb3

Browse files
committed
refactor: [sort] Use tests.h
1 parent 656035e commit 6deafb3

File tree

1 file changed

+76
-123
lines changed

1 file changed

+76
-123
lines changed

cpp/sort/sort-01.cpp

Lines changed: 76 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -10,161 +10,114 @@
1010
* https://www.programiz.com/dsa/bubble-sort
1111
*/
1212

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

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

20+
#define _sort_BubbleSort_desc "Bubble Sort"
21+
3922
/* TC : Best O(n^2), Average O(n^2), Worst O(n^2)
4023
* SC : O(1)
4124
*/
42-
class _01_bubble_sort : public _00_test
25+
namespace BubbleSort
4326
{
44-
public:
45-
_01_bubble_sort() : _00_test("Bubble sort") {}
46-
47-
void sort(vector<int> &nums) override
48-
{
49-
int n = static_cast<int>(nums.size());
50-
for (int i = 0; i < n - 1; i++) {
51-
bool swapped = false;
52-
for (int j = 0; j < n - i - 1; j++) {
53-
if (nums[j] > nums[j + 1]) {
54-
swap(nums[j], nums[j + 1]);
55-
swapped = true;
56-
}
27+
void sort(vi_t &a)
28+
{
29+
int n = size(a);
30+
fii (i, n - 1) {
31+
bool swapped = false;
32+
fii (j, n - i - 1) {
33+
if (a[j] > a[j + 1]) {
34+
swap(a[j], a[j + 1]);
35+
swapped = true;
5736
}
58-
if (!swapped) break;
5937
}
38+
if (!swapped) break;
6039
}
61-
};
40+
}
41+
} // namespace BubbleSort
42+
43+
#define _sort_SelectionSort_desc "Selection Sort"
6244

6345
/* TC : Best O(n^2), Average O(n^2), Worst O(n^2)
6446
* SC : O(1)
6547
*/
66-
class _02_selection_sort : public _00_test
48+
namespace SelectionSort
6749
{
68-
public:
69-
_02_selection_sort() : _00_test("Selection sort") {}
70-
71-
void sort(vector<int> &nums) override
72-
{
73-
int n = static_cast<int>(nums.size());
74-
for (int i = 0; i < n - 1; i++) {
75-
int m = i;
76-
for (int j = i + 1; j < n; j++)
77-
m = nums[j] < nums[m] ? j : m;
78-
swap(nums[i], nums[m]);
79-
}
50+
void sort(vi_t &a)
51+
{
52+
int n = size(a);
53+
fii (i, n - 1) {
54+
int m = i;
55+
for (int j = i + 1; j < n; j++) m = a[j] < a[m] ? j : m;
56+
swap(a[i], a[m]);
8057
}
81-
};
58+
}
59+
} // namespace SelectionSort
60+
61+
#define _sort_InsertionSort_desc "Insertion Sort"
8262

8363
/* TC : Best O(n^2), Average O(n^2), Worst O(n^2)
8464
* SC : O(1)
8565
*/
86-
class _03_insertion_sort : public _00_test
66+
namespace InsertionSort
8767
{
88-
public:
89-
_03_insertion_sort() : _00_test("Insertion sort") {}
90-
91-
void sort(vector<int> &nums) override
92-
{
93-
int n = static_cast<int>(nums.size());
94-
for (int i = 1; i < n; i++) {
95-
int key = nums[i];
96-
int j = i - 1;
97-
98-
while (j >= 0 && nums[j] > key) {
99-
nums[j + 1] = nums[j];
100-
j = j - 1;
101-
}
102-
nums[j + 1] = key;
68+
void sort(vi_t &a)
69+
{
70+
int n = size(a);
71+
for (int i = 1; i < n; i++) {
72+
int key = a[i];
73+
int j = i - 1;
74+
while (j >= 0 && a[j] > key) {
75+
a[j + 1] = a[j];
76+
j = j - 1;
10377
}
78+
a[j + 1] = key;
10479
}
105-
};
80+
}
81+
} // namespace InsertionSort
10682

10783
/* ===========================================================================
10884
* Test code
10985
* ===========================================================================
11086
*/
111-
string _vec2str(vector<int> &vec)
112-
{
113-
ostringstream oss;
114-
oss << "{";
115-
copy(vec.begin(), vec.end() - 1, ostream_iterator<int>(oss, ", "));
116-
oss << vec.back();
117-
oss << "}";
118-
return oss.str();
119-
}
120-
121-
void test_impl(vector<vector<int>> &ip, vector<vector<int>> &op,
122-
shared_ptr<_00_test> f)
123-
{
124-
for (size_t i = 0; i < ip.size(); i++) {
125-
vector<int> t = ip[i];
126-
f->sort(t);
127-
if (t != op[i]) {
128-
cerr << f->getName() << " test failed: "
129-
<< "expected " << _vec2str(op[i]) << ", actual "
130-
<< _vec2str(t) << "." << endl;
131-
exit(1);
132-
}
133-
134-
if (getenv("SHOW_TEST_OUTPUT"))
135-
cout << " test-" << i << ": "
136-
<< "input: nums = " << _vec2str(ip[i])
137-
<< " output: nums = " << _vec2str(t) << "\n";
87+
#define _sort_check(i, e) \
88+
{ \
89+
vi_t a = i; \
90+
sort(a); \
91+
string im = to_string(i); \
92+
string om = to_string(a); \
93+
string em = to_string(e); \
94+
SET_CUSTOM_FAIL_MSG(em, om); \
95+
SET_CUSTOM_SUCCESS_MSG(im, om); \
96+
CHECK_EQ(e, a); \
97+
SHOW_OUTPUT(i, a); \
13898
}
139-
}
140-
141-
int main(int, char **)
142-
{
143-
vector<vector<int>> ip{
144-
{5, 1, 4, 2, 8},
145-
{-2, 45, 0, 11, -9},
146-
};
147-
148-
vector<vector<int>> op{
149-
{1, 2, 4, 5, 8},
150-
{-9, -2, 0, 11, 45},
151-
};
15299

153-
vector<shared_ptr<_00_test>> impls{
154-
make_shared<_01_bubble_sort>(),
155-
make_shared<_02_selection_sort>(),
156-
make_shared<_03_insertion_sort>(),
157-
};
158-
159-
for (size_t i = 0; i < impls.size(); i++) {
160-
if (getenv("SHOW_TEST_OUTPUT"))
161-
cout << "Testing implementation " << i + 1 << " "
162-
<< impls[i]->getName() << "\n";
163-
164-
test_impl(ip, op, impls[i]);
100+
#define _SORT_NAME(var) var
101+
#define _SORT_DESC(var) _sort_##var##_desc
102+
103+
#define _SORT_TEST(var) \
104+
TEST(_SORT_NAME(var), _SORT_DESC(var)) \
105+
{ \
106+
using namespace _SORT_NAME(var); \
107+
vi2_t ip{ \
108+
{5, 1, 4, 2, 8}, \
109+
{-2, 45, 0, 11, -9}, \
110+
}; \
111+
vi2_t op{ \
112+
{1, 2, 4, 5, 8}, \
113+
{-9, -2, 0, 11, 45}, \
114+
}; \
115+
int n = size(ip); \
116+
fii (i, n) _sort_check(ip[i], op[i]); \
165117
}
166118

167-
cout << "Executed " << impls.size() << " implementations"
168-
<< " with " << ip.size() << " tests." << endl;
169-
return 0;
170-
}
119+
_SORT_TEST(BubbleSort);
120+
_SORT_TEST(SelectionSort);
121+
_SORT_TEST(InsertionSort);
122+
123+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)