Skip to content

Commit

Permalink
[test] Further GTest-ize the whole test suite.
Browse files Browse the repository at this point in the history
Use `EXPECT_EQ` in place of `assert`, so that the test suite
can be built (and actually test anything) even in release mode.
No change in test coverage intended.
  • Loading branch information
Quuxplusone committed Oct 18, 2023
1 parent 979baec commit 3b9a4b7
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 329 deletions.
300 changes: 150 additions & 150 deletions test/flat_map_test.cpp

Large diffs are not rendered by default.

92 changes: 46 additions & 46 deletions test/flat_set_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ TEST(flat_set, AmbiguousErase)
fs.emplace("a");
fs.emplace("b");
fs.emplace("c");
assert(fs.size() == 3);
EXPECT_TRUE(fs.size() == 3);
fs.erase(AmbiguousEraseWidget("a")); // calls erase(const Key&)
assert(fs.size() == 2);
EXPECT_TRUE(fs.size() == 2);
fs.erase(fs.cbegin()); // calls erase(const_iterator)
assert(fs.size() == 1);
EXPECT_TRUE(fs.size() == 1);
#if __cplusplus >= 201703L
fs.erase(fs.begin()); // calls erase(iterator)
assert(fs.size() == 0);
EXPECT_TRUE(fs.size() == 0);
#endif
}

Expand All @@ -78,8 +78,8 @@ TEST(flat_set, ExtractDoesntSwap)
std::pmr::polymorphic_allocator<int> a(&mr);
sg14::flat_set<int, std::less<int>, std::pmr::vector<int>> fs({1, 2}, a);
std::pmr::vector<int> v = std::move(fs).extract();
assert(v.get_allocator() == a);
assert(fs.empty());
EXPECT_TRUE(v.get_allocator() == a);
EXPECT_TRUE(fs.empty());
}
#endif

Expand All @@ -88,8 +88,8 @@ TEST(flat_set, ExtractDoesntSwap)
std::allocator<int> a;
sg14::flat_set<int, std::less<int>, std::vector<int>> fs({1, 2}, a);
std::vector<int> v = std::move(fs).extract();
assert(v.get_allocator() == a);
assert(fs.empty());
EXPECT_TRUE(v.get_allocator() == a);
EXPECT_TRUE(fs.empty());
}
}

Expand Down Expand Up @@ -127,10 +127,10 @@ TEST(flat_set, ThrowingSwapDoesntBreakInvariants)
fsy.insert(8);
std::vector<int> expected_fsx = {7, 6, 5, 4};
std::vector<int> expected_fsy = {1, 2, 3, 8};
assert(expected_fsx.size() == fsx.size());
assert(std::equal(expected_fsx.begin(), expected_fsx.end(), fsx.begin()));
assert(expected_fsy.size() == fsy.size());
assert(std::equal(expected_fsy.begin(), expected_fsy.end(), fsy.begin()));
EXPECT_TRUE(expected_fsx.size() == fsx.size());
EXPECT_TRUE(std::equal(expected_fsx.begin(), expected_fsx.end(), fsx.begin()));
EXPECT_TRUE(expected_fsy.size() == fsy.size());
EXPECT_TRUE(std::equal(expected_fsy.begin(), expected_fsy.end(), fsy.begin()));
}

// However, if ComparatorWithThrowingSwap::please_throw were
Expand All @@ -143,20 +143,20 @@ TEST(flat_set, VectorBool)
using FS = sg14::flat_set<bool>;
FS fs;
auto it_inserted = fs.emplace(true);
assert(it_inserted.second);
EXPECT_TRUE(it_inserted.second);
auto it = it_inserted.first;
assert(it == fs.begin());
assert(fs.size() == 1);
EXPECT_TRUE(it == fs.begin());
EXPECT_TRUE(fs.size() == 1);
it = fs.emplace_hint(it, false);
assert(it == fs.begin());
assert(fs.size() == 2);
EXPECT_TRUE(it == fs.begin());
EXPECT_TRUE(fs.size() == 2);
auto count = fs.erase(false);
assert(count == 1);
assert(fs.size() == 1);
EXPECT_TRUE(count == 1);
EXPECT_TRUE(fs.size() == 1);
it = fs.erase(fs.begin());
assert(fs.empty());
assert(it == fs.begin());
assert(it == fs.end());
EXPECT_TRUE(fs.empty());
EXPECT_TRUE(it == fs.begin());
EXPECT_TRUE(it == fs.end());
#endif
}

Expand Down Expand Up @@ -217,34 +217,34 @@ TEST(flat_set, MoveOperationsPilferOwnership)
InstrumentedWidget::copy_ctors = 0;
FS fs;
fs.insert(InstrumentedWidget("abc"));
assert(InstrumentedWidget::move_ctors == 1);
assert(InstrumentedWidget::copy_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 1);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 0);

fs.emplace(InstrumentedWidget("def")); fs.erase("def"); // poor man's reserve()
InstrumentedWidget::copy_ctors = 0;
InstrumentedWidget::move_ctors = 0;

fs.emplace("def"); // is still not directly emplaced; a temporary is created to find()
assert(InstrumentedWidget::move_ctors == 1);
assert(InstrumentedWidget::copy_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 1);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 0);
InstrumentedWidget::move_ctors = 0;

FS fs2 = std::move(fs); // should just transfer buffer ownership
assert(InstrumentedWidget::move_ctors == 0);
assert(InstrumentedWidget::copy_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 0);

fs = std::move(fs2); // should just transfer buffer ownership
assert(InstrumentedWidget::move_ctors == 0);
assert(InstrumentedWidget::copy_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 0);

FS fs3(fs, std::allocator<InstrumentedWidget>());
assert(InstrumentedWidget::move_ctors == 0);
assert(InstrumentedWidget::copy_ctors == 2);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 2);
InstrumentedWidget::copy_ctors = 0;

FS fs4(std::move(fs), std::allocator<InstrumentedWidget>()); // should just transfer buffer ownership
assert(InstrumentedWidget::move_ctors == 0);
assert(InstrumentedWidget::copy_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::move_ctors == 0);
EXPECT_TRUE(InstrumentedWidget::copy_ctors == 0);
}

TYPED_TEST(flat_sett, Construction)
Expand All @@ -258,14 +258,14 @@ TYPED_TEST(flat_sett, Construction)
if (true) {
FS fs; // default constructor
fs = {1, 3, 5}; // assignment operator
assert(std::is_sorted(fs.begin(), fs.end(), fs.key_comp()));
EXPECT_TRUE(std::is_sorted(fs.begin(), fs.end(), fs.key_comp()));
}
if (true) {
FS fs {1, 3, 1, 5, 3};
assert(fs.size() == 3); // assert that uniqueing takes place
EXPECT_TRUE(fs.size() == 3); // assert that uniqueing takes place
std::vector<int> vec2 = {1, 3, 1, 5, 3};
FS fs2(vec2);
assert(fs2.size() == 3); // assert that uniqueing takes place
EXPECT_TRUE(fs2.size() == 3); // assert that uniqueing takes place
}
for (auto&& fs : {
FS(vec),
Expand All @@ -278,14 +278,14 @@ TYPED_TEST(flat_sett, Construction)
FS(vec.rbegin(), vec.rend(), Compare()),
}) {
auto cmp = fs.key_comp();
assert(std::is_sorted(fs.begin(), fs.end(), cmp));
assert(fs.find(0) == fs.end());
assert(fs.find(1) != fs.end());
assert(fs.find(2) == fs.end());
assert(fs.find(3) != fs.end());
assert(fs.find(4) == fs.end());
assert(fs.find(5) != fs.end());
assert(fs.find(6) == fs.end());
EXPECT_TRUE(std::is_sorted(fs.begin(), fs.end(), cmp));
EXPECT_TRUE(fs.find(0) == fs.end());
EXPECT_TRUE(fs.find(1) != fs.end());
EXPECT_TRUE(fs.find(2) == fs.end());
EXPECT_TRUE(fs.find(3) != fs.end());
EXPECT_TRUE(fs.find(4) == fs.end());
EXPECT_TRUE(fs.find(5) != fs.end());
EXPECT_TRUE(fs.find(6) == fs.end());
}
if (std::is_sorted(vec.begin(), vec.end(), Compare())) {
for (auto&& fs : {
Expand All @@ -297,7 +297,7 @@ TYPED_TEST(flat_sett, Construction)
FS(sg14::sorted_unique, {1, 3, 5}, Compare()),
}) {
auto cmp = fs.key_comp();
assert(std::is_sorted(fs.begin(), fs.end(), cmp));
EXPECT_TRUE(std::is_sorted(fs.begin(), fs.end(), cmp));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/hive_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ TEST(hive, RegressionTestIssue8)
h.erase(h.begin());
h.erase(h.begin());
h.insert(6);
assert(h.size() == 4); // {6,3,4,5}
EXPECT_EQ(h.size(), 4u); // {6,3,4,5}
EXPECT_INVARIANTS(h);

auto it = h.begin();
Expand Down
68 changes: 34 additions & 34 deletions test/inplace_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ struct Functor {
Functor() {}
Functor(const Functor&) { copied += 1; }
Functor(Functor&&) noexcept { moved += 1; }
void operator()(int i) { assert(i == expected); called_with = i; }
void operator()(int i) { EXPECT_TRUE(i == expected); called_with = i; }
};

struct ConstFunctor {
ConstFunctor() {}
ConstFunctor(const ConstFunctor&) { copied += 1; }
ConstFunctor(ConstFunctor&&) noexcept { moved += 1; }
void operator()(int i) const { assert(i == expected); called_with = i; }
void operator()(int i) const { EXPECT_TRUE(i == expected); called_with = i; }
};

void Foo(int i)
{
assert(i == expected);
EXPECT_TRUE(i == expected);
called_with = i;
}

Expand Down Expand Up @@ -380,23 +380,23 @@ TEST(inplace_function, ConvertibleFromNullptr)
auto nil = nullptr;
const auto cnil = nullptr;

IPF f; assert(! bool(f));
f = nullptr; assert(! bool(f));
f = IPF(nullptr); assert(! bool(f));
f = IPF(); assert(! bool(f));
f = IPF{}; assert(! bool(f));
f = {}; assert(! bool(f));
f = nil; assert(! bool(f));
f = IPF(nil); assert(! bool(f));
f = IPF(std::move(nil)); assert(! bool(f));
f = cnil; assert(! bool(f));
f = IPF(cnil); assert(! bool(f));
f = IPF(std::move(cnil)); assert(! bool(f));

assert(!f);
assert(f == nullptr);
assert(!(f != nullptr));
expected = 0; try { f(42); } catch (const std::bad_function_call&) { expected = 1; } assert(expected == 1);
IPF f; EXPECT_TRUE(! bool(f));
f = nullptr; EXPECT_TRUE(! bool(f));
f = IPF(nullptr); EXPECT_TRUE(! bool(f));
f = IPF(); EXPECT_TRUE(! bool(f));
f = IPF{}; EXPECT_TRUE(! bool(f));
f = {}; EXPECT_TRUE(! bool(f));
f = nil; EXPECT_TRUE(! bool(f));
f = IPF(nil); EXPECT_TRUE(! bool(f));
f = IPF(std::move(nil)); EXPECT_TRUE(! bool(f));
f = cnil; EXPECT_TRUE(! bool(f));
f = IPF(cnil); EXPECT_TRUE(! bool(f));
f = IPF(std::move(cnil)); EXPECT_TRUE(! bool(f));

EXPECT_TRUE(!f);
EXPECT_TRUE(f == nullptr);
EXPECT_TRUE(!(f != nullptr));
expected = 0; try { f(42); } catch (const std::bad_function_call&) { expected = 1; } EXPECT_TRUE(expected == 1);
}

namespace {
Expand Down Expand Up @@ -570,15 +570,15 @@ TEST(inplace_function, ReturnByMove)
InstrumentedCopyConstructor::copies = 0;
InstrumentedCopyConstructor::moves = 0;
IPF20 f = [cc]() { };
assert(InstrumentedCopyConstructor::copies == 1);
assert(InstrumentedCopyConstructor::moves == 1);
EXPECT_TRUE(InstrumentedCopyConstructor::copies == 1);
EXPECT_TRUE(InstrumentedCopyConstructor::moves == 1);
InstrumentedCopyConstructor::copies = 0;
InstrumentedCopyConstructor::moves = 0;
return f;
};
IPF40 f = foo();
assert(InstrumentedCopyConstructor::copies == 0);
assert(InstrumentedCopyConstructor::moves == 1);
EXPECT_TRUE(InstrumentedCopyConstructor::copies == 0);
EXPECT_TRUE(InstrumentedCopyConstructor::moves == 1);
}

TEST(inplace_function, IsInvocableTrait)
Expand Down Expand Up @@ -679,11 +679,11 @@ TEST(inplace_function, DefaultConstructor)
using IPF = sg14::inplace_function<void(int)>;

IPF func;
assert(!func);
assert(!bool(func));
assert(func == nullptr);
assert(!(func != nullptr));
expected = 0; try { func(42); } catch (std::bad_function_call&) { expected = 1; } assert(expected == 1);
EXPECT_TRUE(!func);
EXPECT_TRUE(!bool(func));
EXPECT_TRUE(func == nullptr);
EXPECT_TRUE(!(func != nullptr));
expected = 0; try { func(42); } catch (std::bad_function_call&) { expected = 1; } EXPECT_TRUE(expected == 1);
}

TEST(inplace_function, Assignment)
Expand All @@ -693,9 +693,9 @@ TEST(inplace_function, Assignment)
IPF func;

func = Foo;
assert(!!func);
assert(func);
assert(!(func == nullptr));
assert(func != nullptr);
called_with = 0; expected = 42; func(42); assert(called_with == 42);
EXPECT_TRUE(!!func);
EXPECT_TRUE(func);
EXPECT_TRUE(!(func == nullptr));
EXPECT_TRUE(func != nullptr);
called_with = 0; expected = 42; func(42); EXPECT_TRUE(called_with == 42);
}
38 changes: 19 additions & 19 deletions test/ring_span_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ TEST(ring_span, IteratorRegression)
r.push_back(1.0);
decltype(r)::iterator it = r.end();
decltype(r)::const_iterator cit = r.end(); // test conversion from non-const to const
assert(it == cit); // test comparison of const and non-const
assert(it + 0 == it);
assert(it - 1 == r.begin());
assert(cit + 0 == cit);
assert(cit - 1 == r.cbegin());
assert(it - cit == 0);
assert(cit - r.begin() == 1);
EXPECT_TRUE(it == cit); // test comparison of const and non-const
EXPECT_TRUE(it + 0 == it);
EXPECT_TRUE(it - 1 == r.begin());
EXPECT_TRUE(cit + 0 == cit);
EXPECT_TRUE(cit - 1 == r.cbegin());
EXPECT_TRUE(it - cit == 0);
EXPECT_TRUE(cit - r.begin() == 1);

std::array<double, 4> B;
sg14::ring_span<double> r2(B.begin(), B.end());
swap(r, r2); // test existence of ADL swap()

// Set up the ring for the TEST_OP tests below.
r = sg14::ring_span<double>(A.begin(), A.end(), A.begin(), 2);
assert(r.size() == 2);
EXPECT_TRUE(r.size() == 2);

#define TEST_OP(op, a, b, c) \
assert(a(r.begin() op r.end())); \
assert(b(r.end() op r.begin())); \
assert(c(r.begin() op r.begin()))
EXPECT_TRUE(a(r.begin() op r.end())); \
EXPECT_TRUE(b(r.end() op r.begin())); \
EXPECT_TRUE(c(r.begin() op r.begin()))
#define _
TEST_OP(==, !, !, _);
TEST_OP(!=, _, _, !);
Expand All @@ -112,12 +112,12 @@ TEST(ring_span, CopyPopper)
std::vector<std::string> v { "quick", "brown", "fox" };
sg14::ring_span<std::string, sg14::copy_popper<std::string>> r(v.begin(), v.end(), {"popped"});
r.emplace_back("slow");
assert((v == std::vector<std::string>{"slow", "brown", "fox"}));
EXPECT_TRUE((v == std::vector<std::string>{"slow", "brown", "fox"}));
r.emplace_back("red");
assert((v == std::vector<std::string>{"slow", "red", "fox"}));
EXPECT_TRUE((v == std::vector<std::string>{"slow", "red", "fox"}));
std::string result = r.pop_front();
assert((v == std::vector<std::string>{"popped", "red", "fox"}));
assert(result == "slow");
EXPECT_TRUE((v == std::vector<std::string>{"popped", "red", "fox"}));
EXPECT_TRUE(result == "slow");
}

TEST(ring_span, ReverseIterator)
Expand All @@ -133,16 +133,16 @@ TEST(ring_span, ReverseIterator)
std::vector<double> v(3);

std::copy(r.begin(), r.end(), v.begin());
assert((v == std::vector<double>{2,3,4}));
EXPECT_TRUE((v == std::vector<double>{2,3,4}));

std::copy(r.cbegin(), r.cend(), v.begin());
assert((v == std::vector<double>{2,3,4}));
EXPECT_TRUE((v == std::vector<double>{2,3,4}));

std::copy(r.rbegin(), r.rend(), v.begin());
assert((v == std::vector<double>{4,3,2}));
EXPECT_TRUE((v == std::vector<double>{4,3,2}));

std::copy(r.crbegin(), r.crend(), v.begin());
assert((v == std::vector<double>{4,3,2}));
EXPECT_TRUE((v == std::vector<double>{4,3,2}));

static_assert(std::is_same<decltype(r.begin()), decltype(r)::iterator>::value, "");
static_assert(std::is_same<decltype(c.begin()), decltype(r)::const_iterator>::value, "");
Expand Down
Loading

0 comments on commit 3b9a4b7

Please sign in to comment.