Skip to content

Commit aff3412

Browse files
committed
feat: [tests] Add SUCCESS(), FAIL()
This is done to support creating custom checks when standard CHECK_EQ() is not sufficient. Other changes added as fixes for this: * Support printing std::pair & SinglyLinkedList::to_string(). * Print strings in double quotes.
1 parent 5cf0238 commit aff3412

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

cpp/includes/linked-lists.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ ostream &operator<<(ostream &out, NodePtr node)
6868
out << "}";
6969
return out;
7070
}
71+
72+
string to_string(NodePtr node)
73+
{
74+
ostringstream oss;
75+
oss << node;
76+
return oss.str();
77+
}
7178
} // namespace SinglyLinkedList

cpp/includes/tests.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,9 @@ static vector<_test *> _tests;
7777
this->em.clear(), this->am.clear(); \
7878
this->im.clear(), this->om.clear()
7979

80-
#define _GET_FAIL_MSG(e, a) \
81-
cerr << "expected: "; \
82-
if (this->em.empty()) \
83-
cerr << (e); \
84-
else \
85-
cerr << this->em; \
86-
cerr << " "; \
87-
cerr << "actual: "; \
88-
if (this->am.empty()) \
89-
cerr << (a); \
90-
else \
91-
cerr << this->am; \
92-
cerr << "." << endl
80+
#define _GET_FAIL_MSG() \
81+
cerr << format("expected: {} actual: {}.", this->em, this->am) \
82+
<< endl
9383

9484
#define _GET_SUCCESS_MSG(i, o) \
9585
cout << "input: "; \
@@ -105,14 +95,21 @@ static vector<_test *> _tests;
10595
cout << this->om; \
10696
cout << ".\n"
10797

98+
#define SUCCESS() this->incTc()
99+
100+
#define FAIL() \
101+
cerr << " test-" << (this->getTc() + 1) << " failed! "; \
102+
for (io_t _e : this->getIo()) cerr << _e; \
103+
_GET_FAIL_MSG(); \
104+
exit(1);
105+
108106
#define _CHECK(op, e, a) \
109107
if (e op a) { \
110-
this->incTc(); \
108+
SUCCESS(); \
111109
} else { \
112-
cerr << " test-" << (this->getTc() + 1) << " failed! "; \
113-
for (io_t _e : this->getIo()) cerr << _e; \
114-
_GET_FAIL_MSG(e, a); \
115-
exit(1); \
110+
string em = to_string(e), am = to_string(a); \
111+
SET_CUSTOM_FAIL_MSG(em, am); \
112+
FAIL(); \
116113
}
117114

118115
#define CHECK_EQ(e, a) _CHECK(==, (e), (a))
@@ -155,6 +152,13 @@ static vector<_test *> _tests;
155152
#define vi_v(v, n, i) vi_t v((n), (i))
156153
#define vi2_v(v, n, i) vi2_t v((n), vi_t((n), (i)))
157154

155+
template <class T>
156+
ostream &operator<<(ostream &out, const pair<T, T> &p)
157+
{
158+
out << "{" << format("{}, {}", p.first, p.second) << "}";
159+
return out;
160+
}
161+
158162
template <class T>
159163
ostream &operator<<(ostream &out, const vector<T> &c)
160164
{
@@ -180,3 +184,9 @@ string to_string(const vector<T> &c)
180184
oss << c;
181185
return oss.str();
182186
}
187+
188+
string to_string(const string &s)
189+
{
190+
string q("\"");
191+
return q + s + q;
192+
}

0 commit comments

Comments
 (0)