Skip to content

Commit

Permalink
Reduce test outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Dec 31, 2023
1 parent 8a3863c commit 5f2a607
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 73 deletions.
12 changes: 8 additions & 4 deletions src/Algos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ findSimilarStr(StringRef lhs, std::span<const StringRef> candidates) {

# include <limits>

void test_levDistance() {
DEFINE_TEST(test_levDistance) {
// Test bytelength agnosticity
for (char c = 0; c < std::numeric_limits<char>::max(); ++c) {
String str = String(1, c);
ASSERT_EQ(levDistance(str, str), 0);
}
}
END_TEST

void test_levDistance2() {
DEFINE_TEST(test_levDistance2) {
constexpr StringRef A = "\nMäry häd ä little lämb\n\nLittle lämb\n";
constexpr StringRef B = "\nMary häd ä little lämb\n\nLittle lämb\n";
constexpr StringRef C = "Mary häd ä little lämb\n\nLittle lämb\n";
Expand All @@ -164,11 +165,12 @@ void test_levDistance2() {
ASSERT_EQ(levDistance("aab", "aac"), 1);
ASSERT_EQ(levDistance("aaab", "aaac"), 1);
}
END_TEST

// ref:
// https://github.com/llvm/llvm-project/commit/a247ba9d15635d96225ef39c8c150c08f492e70a#diff-fd993637669817b267190e7de029b75af5a0328d43d9b70c2e8dd512512091a2

void test_findSimilarStr() {
DEFINE_TEST(test_findSimilarStr) {
constexpr Arr<StringRef, 8> CANDIDATES{"if", "ifdef", "ifndef",
"elif", "else", "endif",
"elifdef", "elifndef"};
Expand All @@ -188,15 +190,17 @@ void test_findSimilarStr() {
ASSERT_EQ(findSimilarStr("i", CANDIDATES), None);
ASSERT_EQ(findSimilarStr("special_compiler_directive", CANDIDATES), None);
}
END_TEST

void test_findSimilarStr2() {
DEFINE_TEST(test_findSimilarStr2) {
constexpr Arr<StringRef, 2> CANDIDATES{"aaab", "aaabc"};
ASSERT_EQ(findSimilarStr("aaaa", CANDIDATES), "aaab"sv);
ASSERT_EQ(findSimilarStr("1111111111", CANDIDATES), None);

constexpr Arr<StringRef, 1> CANDIDATES2{"AAAA"};
ASSERT_EQ(findSimilarStr("aaaa", CANDIDATES2), "AAAA"sv);
}
END_TEST

int main() {
test_levDistance();
Expand Down
18 changes: 12 additions & 6 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ String getMakeCommand() {

# include "TestUtils.hpp"

void test_cycle_vars() {
DEFINE_TEST(test_cycle_vars) {
BuildConfig config;
config.defineSimpleVariable("a", "b", {"b"});
config.defineSimpleVariable("b", "c", {"c"});
Expand All @@ -670,8 +670,9 @@ void test_cycle_vars() {
std::runtime_error,
"too complex build graph");
}
END_TEST

void test_simple_vars() {
DEFINE_TEST(test_simple_vars) {
BuildConfig config;
config.defineSimpleVariable("c", "3", {"b"});
config.defineSimpleVariable("b", "2", {"a"});
Expand All @@ -687,8 +688,9 @@ void test_simple_vars() {
"c := 3\n"
);
}
END_TEST

void test_depend_on_unregistered_var() {
DEFINE_TEST(test_depend_on_unregistered_var) {
BuildConfig config;
config.defineSimpleVariable("a", "1", {"b"});

Expand All @@ -697,8 +699,9 @@ void test_depend_on_unregistered_var() {

ASSERT_EQ(ss.str(), "a := 1\n");
}
END_TEST

void test_cycle_targets() {
DEFINE_TEST(test_cycle_targets) {
BuildConfig config;
config.defineTarget("a", {"echo a"}, {"b"});
config.defineTarget("b", {"echo b"}, {"c"});
Expand All @@ -708,8 +711,9 @@ void test_cycle_targets() {
std::runtime_error,
"too complex build graph");
}
END_TEST

void test_simple_targets() {
DEFINE_TEST(test_simple_targets) {
BuildConfig config;
config.defineTarget("a", {"echo a"});
config.defineTarget("b", {"echo b"}, {"a"});
Expand All @@ -731,8 +735,9 @@ void test_simple_targets() {
"\n"
);
}
END_TEST

void test_depend_on_unregistered_target() {
DEFINE_TEST(test_depend_on_unregistered_target) {
BuildConfig config;
config.defineTarget("a", {"echo a"}, {"b"});

Expand All @@ -746,6 +751,7 @@ void test_depend_on_unregistered_target() {
"\n"
);
}
END_TEST

int main() {
test_cycle_vars();
Expand Down
27 changes: 18 additions & 9 deletions src/Semver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ Version parseSemver(StringRef s) {
// Thanks to:
// https://github.com/dtolnay/semver/blob/55fa2cadd6ec95be02e5a2a87b24355304e44d40/tests/test_version.rs#L13

void test_parse() {
DEFINE_TEST(test_parse) {
ASSERT_EXCEPTION(
parseSemver(""), SemverException,
"invalid semver:\n"
Expand Down Expand Up @@ -586,23 +586,26 @@ void test_parse() {
(Version{1, 1, 0, Prerelease::parse("beta-10"), BuildMetadata()})
);
}
END_TEST

void test_eq() {
DEFINE_TEST(test_eq) {
ASSERT_EQ(parseSemver("1.2.3"), parseSemver("1.2.3"));
ASSERT_EQ(parseSemver("1.2.3-alpha1"), parseSemver("1.2.3-alpha1"));
ASSERT_EQ(parseSemver("1.2.3+build.42"), parseSemver("1.2.3+build.42"));
ASSERT_EQ(parseSemver("1.2.3-alpha1+42"), parseSemver("1.2.3-alpha1+42"));
}
END_TEST

void test_ne() {
DEFINE_TEST(test_ne) {
ASSERT_NE(parseSemver("0.0.0"), parseSemver("0.0.1"));
ASSERT_NE(parseSemver("0.0.0"), parseSemver("0.1.0"));
ASSERT_NE(parseSemver("0.0.0"), parseSemver("1.0.0"));
ASSERT_NE(parseSemver("1.2.3-alpha"), parseSemver("1.2.3-beta"));
ASSERT_NE(parseSemver("1.2.3+23"), parseSemver("1.2.3+42"));
}
END_TEST

void test_display() {
DEFINE_TEST(test_display) {
{
std::ostringstream oss;
oss << parseSemver("1.2.3");
Expand All @@ -624,8 +627,9 @@ void test_display() {
ASSERT_EQ(oss.str(), "1.2.3-alpha1+42");
}
}
END_TEST

void test_lt() {
DEFINE_TEST(test_lt) {
ASSERT_LT(parseSemver("0.0.0"), parseSemver("1.2.3-alpha2"));
ASSERT_LT(parseSemver("1.0.0"), parseSemver("1.2.3-alpha2"));
ASSERT_LT(parseSemver("1.2.0"), parseSemver("1.2.3-alpha2"));
Expand All @@ -634,17 +638,19 @@ void test_lt() {
ASSERT_FALSE(parseSemver("1.2.3-alpha2") < parseSemver("1.2.3-alpha2"));
ASSERT_LT(parseSemver("1.2.3+23"), parseSemver("1.2.3+42"));
}
END_TEST

void test_le() {
DEFINE_TEST(test_le) {
ASSERT_TRUE(parseSemver("0.0.0") <= parseSemver("1.2.3-alpha2"));
ASSERT_TRUE(parseSemver("1.0.0") <= parseSemver("1.2.3-alpha2"));
ASSERT_TRUE(parseSemver("1.2.0") <= parseSemver("1.2.3-alpha2"));
ASSERT_TRUE(parseSemver("1.2.3-alpha1") <= parseSemver("1.2.3-alpha2"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") <= parseSemver("1.2.3-alpha2"));
ASSERT_TRUE(parseSemver("1.2.3+23") <= parseSemver("1.2.3+42"));
}
END_TEST

void test_gt() {
DEFINE_TEST(test_gt) {
ASSERT_TRUE(parseSemver("1.2.3-alpha2") > parseSemver("0.0.0"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") > parseSemver("1.0.0"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") > parseSemver("1.2.0"));
Expand All @@ -653,17 +659,19 @@ void test_gt() {
ASSERT_FALSE(parseSemver("1.2.3-alpha2") > parseSemver("1.2.3-alpha2"));
ASSERT_FALSE(parseSemver("1.2.3+23") > parseSemver("1.2.3+42"));
}
END_TEST

void test_ge() {
DEFINE_TEST(test_ge) {
ASSERT_TRUE(parseSemver("1.2.3-alpha2") >= parseSemver("0.0.0"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") >= parseSemver("1.0.0"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") >= parseSemver("1.2.0"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") >= parseSemver("1.2.3-alpha1"));
ASSERT_TRUE(parseSemver("1.2.3-alpha2") >= parseSemver("1.2.3-alpha2"));
ASSERT_FALSE(parseSemver("1.2.3+23") >= parseSemver("1.2.3+42"));
}
END_TEST

void test_spec_order() {
DEFINE_TEST(test_spec_order) {
const Vec<String> vs = {
"1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta",
"1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0",
Expand All @@ -672,6 +680,7 @@ void test_spec_order() {
ASSERT_LT(parseSemver(vs[i - 1]), parseSemver(vs[i]));
}
}
END_TEST

int main() {
test_parse();
Expand Down
100 changes: 46 additions & 54 deletions src/TestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,62 @@
#include <ostream>
#include <string>

#define ASSERT_TRUE(cond) \
if (!(cond)) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< #cond << std::endl; \
std::exit(EXIT_FAILURE); \
} else { \
std::cout << bold(green("PASS: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< #cond << std::endl; \
#define DEFINE_TEST(name) \
void name() { \
std::cout << "test " << __FILE__ << "::" << __func__ << " ... ";

#define END_TEST \
std::cout << green("ok") << std::endl; \
}

#define TEST_ERROR_HEADER \
std::cerr << red("FAILED") << "\n\n" \
<< __FILE__ << "::" << __func__ << ':' << __LINE__ << ": "

#define ASSERT_TRUE(cond) \
if (!(cond)) { \
TEST_ERROR_HEADER << #cond << std::endl; \
std::exit(EXIT_FAILURE); \
}

#define ASSERT_FALSE(cond) ASSERT_TRUE(!(cond))

#define ASSERT_EQ(lhs, rhs) \
if ((lhs) != (rhs)) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< (lhs) << " == " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
} else { \
std::cout << bold(green("PASS: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< #lhs << " == " << #rhs << std::endl; \
#define ASSERT_EQ(lhs, rhs) \
if ((lhs) != (rhs)) { \
TEST_ERROR_HEADER << (lhs) << " == " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
}

#define ASSERT_NE(lhs, rhs) \
if ((lhs) == (rhs)) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< (lhs) << " != " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
} else { \
std::cout << bold(green("PASS: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< #lhs << " != " << #rhs << std::endl; \
#define ASSERT_NE(lhs, rhs) \
if ((lhs) == (rhs)) { \
TEST_ERROR_HEADER << (lhs) << " != " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
}

#define ASSERT_LT(lhs, rhs) \
if ((lhs) >= (rhs)) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< (lhs) << " < " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
} else { \
std::cout << bold(green("PASS: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< #lhs << " < " << #rhs << std::endl; \
#define ASSERT_LT(lhs, rhs) \
if ((lhs) >= (rhs)) { \
TEST_ERROR_HEADER << (lhs) << " < " << (rhs) << std::endl; \
std::exit(EXIT_FAILURE); \
}

#define ASSERT_EXCEPTION(statements, exception, msg) \
try { \
statements; \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< "expected exception `" << #exception << "` not thrown" \
<< std::endl; \
std::exit(EXIT_FAILURE); \
} catch (const exception& e) { \
if (e.what() != std::string(msg)) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< "expected exception message `" << msg << "` but got `" \
<< e.what() << "`" << std::endl; \
std::exit(EXIT_FAILURE); \
} else { \
std::cout << bold(green("PASS: ")) << __FILE__ << ":" << __LINE__ \
<< ": " << #statements << std::endl; \
} \
} catch (...) { \
std::cerr << bold(red("FAIL: ")) << __FILE__ << ":" << __LINE__ << ": " \
<< "expected exception `" << #exception << "` thrown" \
<< std::endl; \
std::exit(EXIT_FAILURE); \
#define ASSERT_EXCEPTION(statements, exception, msg) \
try { \
statements; \
TEST_ERROR_HEADER << "expected exception `" << #exception \
<< "` not " \
"thrown" \
<< std::endl; \
std::exit(EXIT_FAILURE); \
} catch (const exception& e) { \
if (e.what() != std::string(msg)) { \
TEST_ERROR_HEADER << "expected exception message `" << msg \
<< "` but got `" << e.what() << "`" << std::endl; \
std::exit(EXIT_FAILURE); \
} \
} catch (...) { \
TEST_ERROR_HEADER << "expected exception `" << #exception \
<< "` but got another exception" << std::endl; \
std::exit(EXIT_FAILURE); \
}

template <typename T>
Expand Down

0 comments on commit 5f2a607

Please sign in to comment.