3
3
* ==========================================
4
4
*/
5
5
6
- #include < bits/stdc++.h>
7
-
8
- using namespace std ;
6
+ #include " tests.h"
9
7
10
8
/* ===========================================================================
11
9
* Algorithms implementation
12
10
* ===========================================================================
13
11
*/
14
12
15
- // Move all 0 to right / end of array in place with same order for other
16
- // elements
13
+ #define _2p_move0toEnd_desc " Move all 0 to end / right in array"
17
14
18
15
/* TC : O(n)
19
16
* SC : O(1)
@@ -22,149 +19,80 @@ void move0toEnd(span<int> a)
22
19
{
23
20
int n = a.size ();
24
21
int j = 0 ;
25
- for (int i = 0 ; i < n; i++) {
26
- if (a[i] != 0 ) {
27
- swap (a[j++], a[i]); // Partitioning the array
28
- }
29
- }
22
+ fii (i, n)
23
+ if (a[i]) swap (a[j++], a[i]);
30
24
}
31
25
32
- // Move all 0 to left / begin of array in place with same order for other
33
- // elements
26
+ #define _2p_move0toBegin_desc " Move all 0 to begin / left in array"
34
27
28
+ /* TC : O(n)
29
+ * SC : O(1)
30
+ */
35
31
void move0toBegin (span<int > a)
36
32
{
37
33
int n = a.size ();
38
34
int j = n - 1 ;
39
- for (int i = n - 1 ; i >= 0 ; i--) {
40
- if (a[i] != 0 ) {
41
- swap (a[j--], a[i]); // Partitioning the array
42
- }
43
- }
35
+ frii (i, j)
36
+ if (a[i]) swap (a[j--], a[i]);
44
37
}
45
38
46
- // Remove repeated / duplicates in sorted array
39
+ # define _2p_rmDupsSorted_desc " Remove duplicates in sorted array"
47
40
48
- void removeDuplicatesSorted (span<int > a)
41
+ /* TC : O(n)
42
+ * SC : O(1)
43
+ */
44
+ void rmDupsSorted (span<int > a)
49
45
{
50
46
int n = a.size ();
51
47
if (n == 0 || n == 1 ) return ;
52
48
53
49
int j = 0 ;
54
- for ( int i = 0 ; i < n - 1 ; i++ )
50
+ fii (i, n - 1 )
55
51
if (a[i] != a[i + 1 ]) a[j++] = a[i];
56
52
a[j++] = a[n - 1 ];
57
53
58
54
// Remove or zero all remaining elements
59
55
for (; j < n; j++) a[j] = 0 ;
60
56
}
61
57
62
- /* ===========================================================================
63
- * Test helpers
64
- * ===========================================================================
65
- */
66
- template <class T >
67
- ostream &operator <<(ostream &out, const vector<T> &c)
68
- {
69
- out << " {" ;
70
- for (int i = 0 ; const auto &e : c) out << (i++ ? " , " : " " ) << e;
71
- out << " }" ;
72
- return out;
73
- }
74
-
75
- struct _00_test {
76
- _00_test (const string &name) : name(name) {}
77
- string getName (void ) const { return name; }
78
- virtual int test_func () = 0;
79
-
80
- private:
81
- string name;
82
- };
83
-
84
- #define CHECK_EQ (l, r ) \
85
- if (l != r) { \
86
- cerr << this ->getName () << " : test-" << tc + 1 << " failed!" \
87
- << " expected " << e << " , actual " << a << " ." \
88
- << endl; \
89
- exit (1 ); \
90
- } else { \
91
- tc++; \
92
- if (getenv (" SHOW_TEST_OUTPUT" )) \
93
- cout << " test-" << tc << " : " \
94
- << " input: " << a << " output: " << e << " \n " ; \
95
- }
96
-
97
- vector<_00_test *> tests;
98
-
99
- #define _TEST_FUNC (func ) test_##func
100
-
101
- #define TEST (func, name ) \
102
- struct _TEST_FUNC (func) : public _00_test { \
103
- int test_func () override ; \
104
- \
105
- _TEST_FUNC (func)() : _00_test (name) \
106
- { \
107
- tests.push_back (this ); \
108
- } \
109
- }; \
110
- _TEST_FUNC (func) _test_instance_##func; \
111
- int _TEST_FUNC (func)::test_func()
112
-
113
- #define RUN_ALL_TESTS () \
114
- size_t tc = 0 , ts = 0 ; \
115
- for (; ts < tests.size(); ts++) { \
116
- if (getenv (" SHOW_TEST_OUTPUT" )) \
117
- cout << " Testing implementation " << ts + 1 << " " \
118
- << tests[ts]->getName () << " \n " ; \
119
- tc += tests[ts]->test_func (); \
120
- } \
121
- \
122
- cout << " Executed " << ts << " implementations" \
123
- << " with " << tc << " tests." << endl;
124
-
125
58
/* ===========================================================================
126
59
* Test code
127
60
* ===========================================================================
128
61
*/
129
- TEST (move0toEnd, " Move all 0 to end / right in array " )
62
+ TEST (move0toEnd, _2p_move0toEnd_desc )
130
63
{
131
- int tc = 0 ;
132
- vector< int > a, e ;
133
- a = { 5 , 6 , 0 , 4 , 6 , 0 , 9 , 0 , 8 } ;
64
+ vi_t i, a, e ;
65
+ i = { 5 , 6 , 0 , 4 , 6 , 0 , 9 , 0 , 8 } ;
66
+ a = i ;
134
67
e = {5 , 6 , 4 , 6 , 9 , 8 , 0 , 0 , 0 };
135
68
move0toEnd (a);
136
69
137
70
CHECK_EQ (e, a);
138
- return tc ;
71
+ SHOW_OUTPUT (i, a) ;
139
72
}
140
73
141
- TEST (move0toBegin, " Move all 0 to begin / left in array " )
74
+ TEST (move0toBegin, _2p_move0toBegin_desc )
142
75
{
143
- int tc = 0 ;
144
- vector< int > a, e ;
145
- a = { 5 , 6 , 0 , 4 , 6 , 0 , 9 , 0 , 8 } ;
76
+ vi_t i, a, e ;
77
+ i = { 5 , 6 , 0 , 4 , 6 , 0 , 9 , 0 , 8 } ;
78
+ a = i ;
146
79
e = {0 , 0 , 0 , 5 , 6 , 4 , 6 , 9 , 8 };
147
80
move0toBegin (a);
148
81
149
82
CHECK_EQ (e, a);
150
- return tc ;
83
+ SHOW_OUTPUT (i, a) ;
151
84
}
152
85
153
- TEST (removeDuplicatesSorted, " Remove duplicates in sorted array " )
86
+ TEST (rmDupsSorted, _2p_rmDupsSorted_desc )
154
87
{
155
-
156
- int tc = 0 ;
157
- vector<int > a, e;
158
- a = {1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 };
88
+ vi_t i, a, e;
89
+ i = {1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 };
90
+ a = i;
159
91
e = {1 , 2 , 3 , 4 , 5 , 0 , 0 , 0 , 0 };
160
- removeDuplicatesSorted (a);
92
+ rmDupsSorted (a);
161
93
162
94
CHECK_EQ (e, a);
163
- return tc ;
95
+ SHOW_OUTPUT (i, a) ;
164
96
}
165
97
166
- int main (int , char **)
167
- {
168
- RUN_ALL_TESTS ();
169
- return 0 ;
170
- }
98
+ INIT_TEST_MAIN ();
0 commit comments