Skip to content

Commit

Permalink
push_back and assign function for rvalues passing with their test cas…
Browse files Browse the repository at this point in the history
…es for rvalues
  • Loading branch information
Emmankoko committed Oct 25, 2023
1 parent 23c39b1 commit 9fea96a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 7 additions & 6 deletions source/stdcpp/test/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
module stdcpp.test.vector;
import stdcpp.vector;
import stdcpp.allocator;
int a = 42;
int b = 9;
allocator!int alloc_instance = allocator!(int).init;
unittest
{
auto vec = vector!int(4, alloc_instance);
vec.push_back(a);
vec.push_back(42);
assert(vec.length == 5);
assert(vec[4] == 42);
assert(vec.at(3) == 0);
Expand All @@ -27,16 +25,19 @@ unittest

unittest
{
int b = 9;
auto p = vector!int(4, b, alloc_instance);
assert(p.capacity() == 4);
p.reserve(6);
assert(p.capacity() == 6);
//3 push backs to reallocate memory by 2 after initially capacity fills up
p.push_back(a);
p.push_back(a);//capacity is 6 now
p.push_back(a);
p.push_back(9);
p.push_back(9);//capacity is 6 now
p.push_back(9);
assert(p.capacity() == 12);
p.resize(5);
assert(p.length == 5);
assert(p.sizeof == 24);//verifying three pointers
p.assign(3,8);
assert(p.length == 3);
}
9 changes: 9 additions & 0 deletions source/stdcpp/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,15 @@ extern(D):
return size_type(this._M_finish - this._M_start);
}

extern(D) void push_back(const T item)
{
return this.push_back(item);
}

extern(D) void assign(size_t n, const T x)
{
return this.assign(n,x);
}

size_t capacity() const @safe nothrow pure @nogc
{
Expand Down

0 comments on commit 9fea96a

Please sign in to comment.