From 9fea96ac3a2bb3729ab2f22f67a9779f7eabc497 Mon Sep 17 00:00:00 2001 From: Emmankoko Date: Wed, 25 Oct 2023 09:41:02 +0000 Subject: [PATCH] push_back and assign function for rvalues passing with their test cases for rvalues --- source/stdcpp/test/vector.d | 13 +++++++------ source/stdcpp/vector.d | 9 +++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/source/stdcpp/test/vector.d b/source/stdcpp/test/vector.d index f37395d..77ae47a 100644 --- a/source/stdcpp/test/vector.d +++ b/source/stdcpp/test/vector.d @@ -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); @@ -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); } diff --git a/source/stdcpp/vector.d b/source/stdcpp/vector.d index 799edcc..c09d9ca 100644 --- a/source/stdcpp/vector.d +++ b/source/stdcpp/vector.d @@ -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 {