Skip to content

Commit 4b36bfb

Browse files
committed
refactor: [bits,dsa] Use tests.h
1 parent 63f17fb commit 4b36bfb

File tree

2 files changed

+93
-30
lines changed

2 files changed

+93
-30
lines changed

cpp/bits/shift-gt-type-width.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,54 @@
1010

1111
#include "tests.h"
1212

13+
/* ===========================================================================
14+
* Algorithms implementation
15+
* ===========================================================================
16+
*/
1317
template <class T>
14-
constexpr bitset<sizeof(size_t) * 8> bits(T x)
18+
constexpr bitset<sizeof(T) * 8> bits(T x)
1519
{
16-
return bitset<sizeof(size_t) * 8>(x);
20+
return bitset<sizeof(T) * 8>(x);
1721
}
1822

1923
#define nbits(x) (sizeof(x) * 8)
24+
#define b2s(x) bits(x).to_string()
25+
#define sb(x, i) x << (nbits(x) - i)
2026

2127
template <class T>
22-
inline void check(T x)
28+
T check(T x, string &im, string &om)
2329
{
24-
if (getenv("SHOW_TEST_OUTPUT")) {
25-
cout << "Testing implementation " << 1 << " "
26-
<< "Bit shifting more than type width"
27-
<< "\n"
28-
<< " test-0: "
29-
<< "\n"
30-
<< " input x: " << bits(x)
31-
<< " sizeof(x): " << nbits(x) << "\n"
32-
<< " output: "
33-
<< "\n";
30+
T r;
31+
im = format("x: {}, sizeof(x): {}", b2s(x), nbits(x));
3432

3533
#pragma GCC diagnostic push
3634
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
37-
cout << " x << sizeof(x): " << bits(x << nbits(x))
38-
<< "\n"
39-
<< " x << (sizeof(x) + 1): "
40-
<< bits(x << (nbits(x) + 1)) << "\n";
35+
om = format("x << (sizeof(x) - 1): {}, x << sizeof(x): {}",
36+
b2s(sb(x, 1)), b2s(sb(x, 0)));
37+
r = sb(x, 0);
4138
#pragma GCC diagnostic pop
39+
return r;
40+
}
4241

43-
cout << endl;
42+
/* ===========================================================================
43+
* Test code
44+
* ===========================================================================
45+
*/
46+
#define _bsg_check(T) \
47+
{ \
48+
T i = 1, a = i, e = 0; \
49+
string im, om; \
50+
a = check(a, im, om); \
51+
CHECK_EQ(e, a); \
52+
SHOW_OUTPUT(im, om); \
4453
}
45-
}
4654

47-
int main(int, char **)
55+
TEST(check, "Bit shifting more than type width (undefined behavior)")
4856
{
49-
size_t a = 1;
50-
51-
check(a);
52-
53-
cout << "Executed " << 1 << " implementations"
54-
<< " with " << 1 << " tests." << endl;
55-
return 0;
57+
_bsg_check(char);
58+
_bsg_check(short);
59+
_bsg_check(int);
60+
_bsg_check(size_t);
5661
}
62+
63+
INIT_TEST_MAIN();

cpp/dsa/lru_cache.cpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
/**
2+
* Least Recently Used (LRU) Cache
3+
* ===============================
4+
*
5+
* Refer:
6+
*
7+
* https://leetcode.com/problems/lru-cache
8+
*/
9+
110
#include "tests.h"
211

12+
/* ===========================================================================
13+
* Algorithms implementation
14+
* ===========================================================================
15+
*/
316
struct Node;
417
using NodePtr = Node *;
518

@@ -79,8 +92,51 @@ class LRUCache
7992
NodePtr head, tail; // anchor nodes
8093
};
8194

82-
int main(int, char **)
95+
/* ===========================================================================
96+
* Test code
97+
* ===========================================================================
98+
*/
99+
string to_string(const vector<string> &c, const vi2_t &d)
100+
{
101+
int n = size(c), i = 0;
102+
string im = format("LRUCache({})", d[i++][0]);
103+
for (; i < n; i++) {
104+
im += format(", {}", c[i]);
105+
int k = d[i][0];
106+
if (c[i] == "put") {
107+
int v = d[i][1];
108+
im += format("({}, {})", k, v);
109+
} else if (c[i] == "get")
110+
im += format("({})", k);
111+
}
112+
return im;
113+
}
114+
115+
#define _lru_check(c, d, e) \
116+
{ \
117+
int n = size(c), i = 0; \
118+
LRUCache cch(d[i++][0]); \
119+
vi_t a; \
120+
for (; i < n; i++) { \
121+
int k = d[i][0]; \
122+
if (c[i] == "put") { \
123+
int v = d[i][1]; \
124+
cch.put(k, v); \
125+
} else if (c[i] == "get") \
126+
a.push_back(cch.get(k)); \
127+
} \
128+
CHECK_EQ(e, a); \
129+
SHOW_OUTPUT(to_string(c, d), a); \
130+
}
131+
132+
TEST(LRUCache, "Least Recently Used (LRU) Cache")
83133
{
84-
cout << "Executed 1 implementations with 1 tests." << endl;
85-
return 0;
134+
vector<string> _c = {"LRUCache", "put", "put", "get", "put",
135+
"get", "put", "get", "get", "get"};
136+
vi2_t _d = {{2}, {1, 1}, {2, 2}, {1}, {3, 3},
137+
{2}, {4, 4}, {1}, {3}, {4}};
138+
vi_t _e = {1, -1, -1, 3, 4};
139+
_lru_check(_c, _d, _e);
86140
}
141+
142+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)