Skip to content

Commit 5d065bf

Browse files
committed
add testing for push_pack and emplace
1 parent 2452fba commit 5d065bf

2 files changed

Lines changed: 140 additions & 5 deletions

File tree

vector/include/vector.hpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class vector {
1212
// TODO: provide allocator felxibility
1313
vector() {
1414
// allocate memory in cosntructor
15-
reallocate(2); // initial capacity of 2
15+
reallocate(4); // initial capacity of 2
1616
}
1717

1818
~vector() {
@@ -34,6 +34,59 @@ class vector {
3434
m_size++;
3535
}
3636

37+
/**
38+
* @brief Add an element to the end of the vector.
39+
* @param value The (R-value ref) value to add to the end of the vector.
40+
* @return void.
41+
*/
42+
void push_back(T&& value) {
43+
if (m_size >= m_capacity) {
44+
reallocate(m_capacity * 2); // double the capacity - TODO: use an allcoater to be dynamic
45+
}
46+
47+
// add element to the end of the vector
48+
m_data[m_size] = std2::move(value);
49+
m_size++;
50+
}
51+
52+
/**
53+
* @brief Add an element to the end of the vector.
54+
* @param args Arguments to forward to the constructor of T.
55+
* @return T reference.
56+
*/
57+
template <typename... Args> //variadic template
58+
T& emplace_back(Args&&... args) {
59+
if (m_size >= m_capacity) {
60+
reallocate(m_capacity * 2); // double the capacity - TODO: use an allcoater to be dynamic
61+
}
62+
63+
// add element to the end of the vector
64+
m_data[m_size] = T(std2::forward<Args>(args)...);
65+
return m_data[m_size++];
66+
}
67+
68+
/**
69+
* @brief Remove the last element from the vector.
70+
* @return void.
71+
*/
72+
void pop_back() {
73+
if (m_size > 0) {
74+
m_size--;
75+
m_data[m_size].~T(); // call destructor explicitly
76+
}
77+
}
78+
79+
/**
80+
* @brief Clear the vector - remove all elements.
81+
* @return void.
82+
*/
83+
void clear() {
84+
for (size_t i = 0; i < m_size; ++i) {
85+
m_data[i].~T(); // call destructor explicitly
86+
}
87+
m_size = 0;
88+
}
89+
3790
/**
3891
* @brief Get the number of elements in the vector.
3992
* @return the number of elements in the vector.
@@ -54,7 +107,7 @@ class vector {
54107
/**
55108
* @brief Index operator - access element at the given index.
56109
* @param index The index of the element to access.
57-
* @return the value at the index.
110+
* @return the referecne to the value at the index.
58111
*/
59112
T& operator[](std::size_t index) {
60113
return m_data[index];
@@ -77,8 +130,8 @@ class vector {
77130
T* new_block = new T[new_capacity];
78131

79132
for (std::size_t i = 0; i < m_size; ++i) {
80-
// new_block[i] = std2::move(m_data[i]);
81-
new_block[i] = m_data[i];
133+
new_block[i] = std2::move(m_data[i]);
134+
// new_block[i] = m_data[i];
82135
}
83136

84137
delete[] m_data; // free old memory block

vector/tests/vector_test.cpp

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,99 @@ TEST_F(VectorTest, PushBackAndSize) {
114114
points.push_back(Point3D());
115115

116116
print_vector(points);
117+
}
118+
119+
// Test push_back and size
120+
TEST_F(VectorTest, EmplaceBackAndSize) {
117121

122+
std2::vector<int> vals;
123+
vals.emplace_back(10);
124+
vals.emplace_back(20);
125+
vals.emplace_back(30);
126+
print_vector(vals);
127+
EXPECT_EQ(vals.size(), 3);
118128

129+
std2::vector<Point3D> points;
130+
131+
points.emplace_back(1.0f, 2.0f, 3.0f);
132+
points.emplace_back(1.0f);
133+
points.emplace_back();
134+
135+
print_vector(points);
119136
}
120137

121-
// Test push_back and size
138+
// Test [] operator accessing elements
122139
TEST_F(VectorTest, IndexOperator) {
123140

124141
std2::vector<int> vals;
125142
vals.push_back(10);
126143

144+
std::cout << "Replacing element at index 0 from " << 10 << " to " << 20 << std::endl;
145+
print_vector(vals);
146+
127147
EXPECT_EQ(vals[0], 10);
148+
149+
// Update an element
150+
vals[0] = 20;
151+
152+
print_vector(vals);
153+
154+
EXPECT_EQ(vals[0], 20);
155+
156+
std2::vector<Point3D> points;
157+
158+
// Add a point
159+
points.push_back(Point3D(1.0f, 2.0f, 3.0f));
160+
std::cout << "Initial vector: ";
161+
print_vector(points);
162+
163+
// Get reference and modify
164+
Point3D& point_ref = points[0];
165+
point_ref.x = 10.0f;
166+
std::cout << "After modifying through reference: ";
167+
print_vector(points);
168+
EXPECT_EQ(points[0].x, 10.0f);
169+
170+
// Direct assignment through operator[]
171+
points[0] = Point3D(20.0f, 20.0f, 20.0f);
172+
std::cout << "After direct assignment: ";
173+
print_vector(points);
174+
EXPECT_EQ(points[0].x, 20.0f);
175+
176+
// Test const access
177+
const std2::vector<Point3D>& const_points = points;
178+
EXPECT_EQ(const_points[0].x, 20.0f);
179+
// Following line would not compile (good!):
180+
// const_points[0] = Point3D(); // Error: assignment of read-only location
128181
}
129182

183+
// Test clear
184+
TEST_F(VectorTest, Clear) {
185+
186+
std2::vector<int> vals;
187+
vals.push_back(10);
188+
vals.push_back(10);
189+
vals.push_back(10);
190+
191+
EXPECT_EQ(vals.size(), 3);
192+
193+
vals.clear();
194+
195+
EXPECT_EQ(vals.size(), 0);
196+
}
130197

198+
// Test pop_back
199+
TEST_F(VectorTest, PopBack) {
200+
201+
std2::vector<int> vals;
202+
vals.push_back(10);
203+
vals.push_back(10);
204+
vals.push_back(10);
205+
206+
EXPECT_EQ(vals.size(), 3);
207+
208+
vals.pop_back();
209+
vals.pop_back();
210+
211+
EXPECT_EQ(vals.size(), 1);
212+
}

0 commit comments

Comments
 (0)