Skip to content

Commit

Permalink
test: Add more unit tests for comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Dec 5, 2019
1 parent 7d7fd7d commit b140ae3
Showing 1 changed file with 90 additions and 50 deletions.
140 changes: 90 additions & 50 deletions test/unittests/cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,62 @@ TEST(cpp, std_maps)
EXPECT_FALSE(unordered_storage.begin()->first);
}

enum relation : bool
{
eq,
less
};

/// Compares x and y using all comparison operators (also with reversed argument order)
/// and validates results against the expected relation: eq: x == y, less: x < y.
template <typename T>
static void expect_cmp(const T& x, const T& y, relation expected)
{
switch (expected)
{
case eq:
EXPECT_TRUE(x == y);
EXPECT_FALSE(x != y);
EXPECT_FALSE(x < y);
EXPECT_TRUE(x <= y);
EXPECT_FALSE(x > y);
EXPECT_TRUE(x >= y);

EXPECT_TRUE(y == x);
EXPECT_FALSE(y != x);
EXPECT_FALSE(y < x);
EXPECT_TRUE(y <= x);
EXPECT_FALSE(y > x);
EXPECT_TRUE(y >= x);
break;
case less:
EXPECT_FALSE(x == y);
EXPECT_TRUE(x != y);
EXPECT_TRUE(x < y);
EXPECT_TRUE(x <= y);
EXPECT_FALSE(x > y);
EXPECT_FALSE(x >= y);

EXPECT_FALSE(y == x);
EXPECT_TRUE(y != x);
EXPECT_FALSE(y < x);
EXPECT_FALSE(y <= x);
EXPECT_TRUE(y > x);
EXPECT_TRUE(y >= x);
break;
}
}

TEST(cpp, address_comparison)
{
const auto zero = evmc::address{};
auto max = evmc::address{};
std::fill_n(max.bytes, sizeof(max), uint8_t{0xff});

expect_cmp(zero, zero, eq);
expect_cmp(max, max, eq);
expect_cmp(zero, max, less);

for (size_t i = 0; i < sizeof(evmc::address); ++i)
{
auto t = evmc::address{};
Expand All @@ -167,37 +220,34 @@ TEST(cpp, address_comparison)
auto f = evmc::address{};
f.bytes[i] = 0xff;

EXPECT_TRUE(zero < t);
EXPECT_TRUE(zero < u);
EXPECT_TRUE(zero < f);
EXPECT_TRUE(zero != t);
EXPECT_TRUE(zero != u);
EXPECT_TRUE(zero != f);

EXPECT_TRUE(t < u);
EXPECT_TRUE(t < f);
EXPECT_TRUE(u < f);

EXPECT_FALSE(u < t);
EXPECT_FALSE(f < t);
EXPECT_FALSE(f < u);

EXPECT_TRUE(t != u);
EXPECT_TRUE(t != f);
EXPECT_TRUE(u != t);
EXPECT_TRUE(u != f);
EXPECT_TRUE(f != t);
EXPECT_TRUE(f != u);

EXPECT_TRUE(t == t);
EXPECT_TRUE(u == u);
EXPECT_TRUE(f == f);
expect_cmp(zero, t, less);
expect_cmp(zero, u, less);
expect_cmp(zero, f, less);

expect_cmp(t, max, less);
expect_cmp(u, max, less);
expect_cmp(f, max, less);

expect_cmp(t, u, less);
expect_cmp(t, f, less);
expect_cmp(u, f, less);

expect_cmp(t, t, eq);
expect_cmp(u, u, eq);
expect_cmp(f, f, eq);
}
}

TEST(cpp, bytes32_comparison)
{
const auto zero = evmc::bytes32{};
auto max = evmc::bytes32{};
std::fill_n(max.bytes, sizeof(max), uint8_t{0xff});

expect_cmp(zero, zero, eq);
expect_cmp(max, max, eq);
expect_cmp(zero, max, less);

for (size_t i = 0; i < sizeof(evmc::bytes32); ++i)
{
auto t = evmc::bytes32{};
Expand All @@ -207,31 +257,21 @@ TEST(cpp, bytes32_comparison)
auto f = evmc::bytes32{};
f.bytes[i] = 0xff;

EXPECT_TRUE(zero < t);
EXPECT_TRUE(zero < u);
EXPECT_TRUE(zero < f);
EXPECT_TRUE(zero != t);
EXPECT_TRUE(zero != u);
EXPECT_TRUE(zero != f);

EXPECT_TRUE(t < u);
EXPECT_TRUE(t < f);
EXPECT_TRUE(u < f);

EXPECT_FALSE(u < t);
EXPECT_FALSE(f < t);
EXPECT_FALSE(f < u);

EXPECT_TRUE(t != u);
EXPECT_TRUE(t != f);
EXPECT_TRUE(u != t);
EXPECT_TRUE(u != f);
EXPECT_TRUE(f != t);
EXPECT_TRUE(f != u);

EXPECT_TRUE(t == t);
EXPECT_TRUE(u == u);
EXPECT_TRUE(f == f);
expect_cmp(zero, t, less);
expect_cmp(zero, u, less);
expect_cmp(zero, f, less);

expect_cmp(t, max, less);
expect_cmp(u, max, less);
expect_cmp(f, max, less);

expect_cmp(t, u, less);
expect_cmp(t, f, less);
expect_cmp(u, f, less);

expect_cmp(t, t, eq);
expect_cmp(u, u, eq);
expect_cmp(f, f, eq);
}
}

Expand Down

0 comments on commit b140ae3

Please sign in to comment.