-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (60 loc) · 1.4 KB
/
main.cpp
File metadata and controls
65 lines (60 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "MyVector.h"
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
// Basic functionality test
void test1() {
MyVector v;
assert(v.size() == 0);
v.push_back(10);
v.push_back(7);
assert(v.size() == 2 && v.capacity() == 3);
assert(v[0] == 10 && v[1] == 7);
v.push_back(20);
assert(v.size() == 3 && v.capacity() == 3);
v.push_back(30);
assert(v.size() == 4 && v.capacity() == 6);
assert(v[0] == 10);
assert(v[3] == 30);
v[3] = 50;
assert(v[3] == 50);
}
// Test copy constructor
void test2() {
MyVector v;
for (int i = 0; i < 10; i++) {
v.push_back(i * 5);
}
MyVector v2(v);
v.push_back(60);
assert(v.size() == 11 && v.capacity() == 12);
assert(v2.size() == 10 && v2.capacity() == 12);
}
// Test assignment operator
void test3() {
MyVector v;
for (int i = 0; i < 5; i++) {
v.push_back(i * 10);
}
MyVector v2;
v2.push_back(100);
v2 = v;
assert(v2.size() == 5 && v2.capacity() == 6);
v.push_back(60);
assert(v.size() == 6 && v.capacity() == 6);
assert(v2.size() == 5 && v2.capacity() == 6);
v2 = v2; // Self-assignment
assert(v2.size() == 5 && v2.capacity() == 6);
v2 = v;
assert(v2.size() == 6 && v2.capacity() == 6);
v.push_back(70);
assert(v.size() == 7 && v.capacity() == 12);
assert(v2.size() == 6 && v2.capacity() == 6);
}
int main() {
test1();
test2();
test3();
cout << "All tests passed!" << endl;
}