Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test no memory leaks due to exceptions in copy constructor & copy assignment operator. #10

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 20)

find_package(GTest REQUIRED)

add_executable(tests tests.cpp)
add_executable(tests tests.cpp test-classes.cpp)

if (NOT MSVC)
target_compile_options(tests PRIVATE -Wall -Wextra -Wshadow=compatible-local -Wno-sign-compare -pedantic)
Expand Down
69 changes: 69 additions & 0 deletions test-classes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "test-classes.h"

std::unordered_set<address_checking_object const*> address_checking_object::addresses;
void address_checking_object::add_instance() const {
auto [it, was_inserted] = addresses.insert(this);
if (!was_inserted) {
FAIL() << "New object is created at the address "
<< static_cast<void const*>(this)
<< " while the previous object at this address was not destroyed";
}
}
void address_checking_object::remove_instance() const {
size_t erased_count = addresses.erase(this);
if (erased_count != 1) {
FAIL() <<"Destroying non-existing object at the address "
<< static_cast<void const*>(this);
}
}
void address_checking_object::assert_exists() const {
if (!addresses.contains(this)) {
FAIL() << "Accessing an non-existing object at address "
<< static_cast<void const*>(this);
}
}
void address_checking_object::expect_no_instances() {
if (!addresses.empty()) {
addresses.clear();
FAIL() << "Not all instances are destroyed";
}
}

size_t address_checking_object::copy_throw_countdown = 0;
void address_checking_object::process_copying() {
if (copy_throw_countdown != 0)
if (--copy_throw_countdown == 0)
throw std::runtime_error("address_checking_object copying failed");
}
void address_checking_object::set_copy_throw_countdown(size_t new_countdown) {
copy_throw_countdown = new_countdown;
}

address_checking_object::operator int() const {
assert_exists();
return value;
}

address_checking_object::address_checking_object() {
add_instance();
}
address_checking_object::address_checking_object(int value) : value(value) {
add_instance();
}
address_checking_object::address_checking_object(
const address_checking_object& other)
: value(other.value) {
process_copying();
add_instance();
}
address_checking_object&
address_checking_object::operator=(const address_checking_object& other) {
assert_exists();
other.assert_exists();
process_copying();
value = other.value;
return *this;
}
address_checking_object::~address_checking_object() {
remove_instance();
}
33 changes: 33 additions & 0 deletions test-classes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

#include <cmath>
#include <unordered_set>
#include <utility>

#include "gtest/gtest.h"

struct test_object {
int a = 0;
test_object() = default;
Expand Down Expand Up @@ -57,3 +63,30 @@ struct non_default_constructible {
private:
int a;
};

class address_checking_object {
private:
static std::unordered_set<address_checking_object const*> addresses;

void add_instance() const;
void remove_instance() const;
void assert_exists() const;

int value;

static size_t copy_throw_countdown;
static void process_copying();

public:
static void expect_no_instances();

static void set_copy_throw_countdown(size_t new_countdown);

/* implicit */ operator int() const;

address_checking_object();
/* implicit */ address_checking_object(int value);
address_checking_object(address_checking_object const& other);
address_checking_object& operator=(address_checking_object const& other);
~address_checking_object();
};
35 changes: 34 additions & 1 deletion tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "bimap.h"
#include "test-classes.h"
#include "gtest/gtest.h"

TEST(bimap, leak_check) {
bimap<unsigned long, unsigned long> b;
Expand Down Expand Up @@ -67,6 +66,40 @@ TEST(bimap, copies) {
EXPECT_NE(b.find_right(-10), b.end_right());
}

TEST(bimap, throwing_in_copy_assignment) {
{
bimap<address_checking_object, int> a;
a.insert(1, 2);
a.insert(3, 4);
a.insert(5, 6);
a.insert(7, 8);
a.insert(9, 10);
address_checking_object::set_copy_throw_countdown(3);
bimap<address_checking_object, int> b;
try {
b = a;
} catch (std::runtime_error const& error) {}
EXPECT_EQ(b.size(), 0); // Checking strong guarantee.
}
address_checking_object::expect_no_instances();
}

TEST(bimap, throwing_in_copy_constructor) {
{
bimap<address_checking_object, int> a;
a.insert(1, 2);
a.insert(3, 4);
a.insert(5, 6);
a.insert(7, 8);
a.insert(9, 10);
address_checking_object::set_copy_throw_countdown(3);
try {
bimap<address_checking_object, int> b = a;
} catch (std::runtime_error const& error) {}
}
address_checking_object::expect_no_instances();
}

TEST(bimap, insert) {
bimap<int, int> b;
b.insert(4, 10);
Expand Down