Add test and fix for non-default constructor
This commit is contained in:
parent
bbb64dd8b7
commit
d1fd4eb017
9
Vector.h
9
Vector.h
@ -10,13 +10,14 @@ class Vector {
|
|||||||
// If the needed capacity is known, it can be passed; other wise, a default
|
// If the needed capacity is known, it can be passed; other wise, a default
|
||||||
// capacity is reserved.
|
// capacity is reserved.
|
||||||
Vector(unsigned int capacity = 10)
|
Vector(unsigned int capacity = 10)
|
||||||
: capacity(capacity), element_count(0), data(new T[capacity]) {
|
: capacity(capacity), element_count(0),
|
||||||
|
data((T *)::operator new(capacity * sizeof(T))) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy Constructor
|
// Copy Constructor
|
||||||
Vector(const Vector &other)
|
Vector(const Vector &other)
|
||||||
: capacity(other.capacity), element_count(other.element_count),
|
: capacity(other.capacity), element_count(other.element_count),
|
||||||
data(new T[capacity]) {
|
data((T *)::operator new(capacity * sizeof(T))) {
|
||||||
// `std::copy` is used because it is more flexible than `std::memcpy`,
|
// `std::copy` is used because it is more flexible than `std::memcpy`,
|
||||||
// and the compiler will replace it with `memcpy` anyway if appropriate,
|
// and the compiler will replace it with `memcpy` anyway if appropriate,
|
||||||
// so there is no performance loss.
|
// so there is no performance loss.
|
||||||
@ -24,7 +25,7 @@ class Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move Constructor using the copy-and-swap-idiom
|
// Move Constructor using the copy-and-swap-idiom
|
||||||
Vector(Vector &&other) : data(new T[capacity]) {
|
Vector(Vector &&other) : data((T *)::operator new(capacity * sizeof(T))) {
|
||||||
swap(*this, other);
|
swap(*this, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +176,7 @@ class Vector {
|
|||||||
// Note that this function doesn't validate its input for optimization
|
// Note that this function doesn't validate its input for optimization
|
||||||
// reasons!
|
// reasons!
|
||||||
void reallocate(unsigned int new_size) {
|
void reallocate(unsigned int new_size) {
|
||||||
T *new_data = new T[new_size];
|
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;
|
delete[] data;
|
||||||
|
|
||||||
|
22
test.cpp
22
test.cpp
@ -236,3 +236,25 @@ SCENARIO("Resizing a vector", "[vector]") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SCENARIO("Non-default constructor", "[vector]") {
|
||||||
|
GIVEN("A class object with a non-default constructor") {
|
||||||
|
class NonDefault {
|
||||||
|
public:
|
||||||
|
NonDefault(int a) : a(a) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
WHEN("Creating a vector and adding an element") {
|
||||||
|
Vector<NonDefault> non_default_vector;
|
||||||
|
|
||||||
|
non_default_vector.push_back(NonDefault(1));
|
||||||
|
|
||||||
|
THEN("The element should be accessible as usual") {
|
||||||
|
REQUIRE(non_default_vector[0].a == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user