Add as_array
This commit is contained in:
parent
2b62e37ccd
commit
560abcb201
9
Vector.h
9
Vector.h
@ -105,6 +105,10 @@ class Vector {
|
||||
}
|
||||
}
|
||||
|
||||
T *as_array() {
|
||||
return data;
|
||||
}
|
||||
|
||||
// Returns the number of elements in the vector, regardless of the actually
|
||||
// reserved memory.
|
||||
unsigned int size() const {
|
||||
@ -132,7 +136,8 @@ class Vector {
|
||||
|
||||
// Resize the size of the vector to the given new_size
|
||||
// If this decreases the size, some elements are deleted
|
||||
// If this increases the size, some default-constructed elements are added
|
||||
// If this increases the size, some default-constructed elements are added.
|
||||
// If no default constructor is available, a padder must be passed.
|
||||
void resize(unsigned int new_size, const T &padder = T()) {
|
||||
int difference = new_size - size();
|
||||
|
||||
@ -141,7 +146,7 @@ class Vector {
|
||||
reallocate(new_size);
|
||||
}
|
||||
|
||||
// Add additional default-constructed items
|
||||
// Add additional items
|
||||
for (int i = 0; i < difference; i++) {
|
||||
new (data + element_count + i) T(padder);
|
||||
}
|
||||
|
8
test.cpp
8
test.cpp
@ -14,12 +14,20 @@ SCENARIO("Vector size and length are correct", "[vector]") {
|
||||
}
|
||||
|
||||
SCENARIO("The bracket operator returns the correct element", "[vector]") {
|
||||
GIVEN("A vector with some elements") {
|
||||
Vector<int> v(20);
|
||||
v.push_back(1);
|
||||
v.push_back(2);
|
||||
v.push_back(3);
|
||||
|
||||
THEN("The element in the middle must have the expected value") {
|
||||
REQUIRE(v[1] == 2);
|
||||
}
|
||||
|
||||
THEN("The same value must be accessible using as_array") {
|
||||
REQUIRE(v.as_array()[1] == 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO("Equality is returned correctly", "[vector]") {
|
||||
|
Loading…
x
Reference in New Issue
Block a user