More tests and corresponding fixes
especially related to destructor calls
This commit is contained in:
parent
84846a5619
commit
b778751254
9
Vector.h
9
Vector.h
@ -43,7 +43,10 @@ class Vector {
|
|||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Vector() {
|
~Vector() {
|
||||||
delete[] data;
|
for (unsigned int i = 0; i < element_count; i++) {
|
||||||
|
data[i].~T();
|
||||||
|
}
|
||||||
|
::operator delete(data, capacity * sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bracket Operator for accessing elements
|
// Bracket Operator for accessing elements
|
||||||
@ -164,7 +167,7 @@ class Vector {
|
|||||||
}
|
}
|
||||||
} else if (difference < 0) {
|
} else if (difference < 0) {
|
||||||
// Call the destructor on all excess items
|
// Call the destructor on all excess items
|
||||||
for (int i = -1; i > difference; i--) {
|
for (int i = -1; i >= difference; i--) {
|
||||||
data[element_count + i].~T();
|
data[element_count + i].~T();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +209,7 @@ class Vector {
|
|||||||
void reallocate(unsigned int new_size) {
|
void reallocate(unsigned int new_size) {
|
||||||
T *new_data = (T *)::operator new(new_size * sizeof(T));
|
T *new_data = (T *)::operator new(new_size * sizeof(T));
|
||||||
std::copy(data, data + element_count, new_data);
|
std::copy(data, data + element_count, new_data);
|
||||||
delete[] data;
|
::operator delete(data, capacity * sizeof(T));
|
||||||
|
|
||||||
data = new_data;
|
data = new_data;
|
||||||
}
|
}
|
||||||
|
81
test.cpp
81
test.cpp
@ -297,3 +297,84 @@ SCENARIO("Erase by swap", "[vector]") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
class ReferenceCounter {
|
||||||
|
public:
|
||||||
|
ReferenceCounter() {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReferenceCounter(const ReferenceCounter &) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReferenceCounter(ReferenceCounter &&) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReferenceCounter &operator=(const ReferenceCounter &) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReferenceCounter &operator=(ReferenceCounter &&) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~ReferenceCounter() {
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SCENARIO("Constructors and Destructors are called as expected", "[vector]") {
|
||||||
|
GIVEN("A vector with a custom reference-counting class") {
|
||||||
|
|
||||||
|
Vector<ReferenceCounter> v(3);
|
||||||
|
v.push_back(ReferenceCounter());
|
||||||
|
v.push_back(ReferenceCounter());
|
||||||
|
v.push_back(ReferenceCounter());
|
||||||
|
|
||||||
|
REQUIRE(count == 3);
|
||||||
|
|
||||||
|
WHEN("Erasing the first element") {
|
||||||
|
v.erase(0);
|
||||||
|
|
||||||
|
THEN("The reference count should have decreased") {
|
||||||
|
REQUIRE(count == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Erasing by swap") {
|
||||||
|
v.erase_by_swap(0);
|
||||||
|
|
||||||
|
THEN("The reference count should have decreased") {
|
||||||
|
REQUIRE(count == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Resizing to 1") {
|
||||||
|
v.resize(1);
|
||||||
|
|
||||||
|
THEN("The reference count should be 1") {
|
||||||
|
REQUIRE(count == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Resizing to 10") {
|
||||||
|
v.resize(10);
|
||||||
|
|
||||||
|
THEN("The reference count should be 10") {
|
||||||
|
REQUIRE(count == 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Move-constructing another vector") {
|
||||||
|
Vector<ReferenceCounter> v2 = std::move(v);
|
||||||
|
|
||||||
|
THEN("The reference count should have remained the same") {
|
||||||
|
REQUIRE(count == 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user