Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar04 / src / tests.cpp
blob: dc638d292ed9eaa1c43cd45081dff68eebdf31ff (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cassert>
#include <iostream>
#include "doubly-linked-list.hpp"

void insert_basic_tests()
{
	doubly_linked_list<int> ll;
	ll.push_back(1);
	assert(ll.front() == ll.back() && ll.front() == 1);

	ll.pop_back();
	assert(ll.empty());

	ll.push_front(2);
	assert(ll.front() == ll.back() && ll.front() == 2);

	ll.pop_back();
	assert(ll.empty());

	ll.push_back(1);
	ll.push_front(2);

	assert(ll.front() == 2 && ll.back() == 1 && ll.size() == 2);
}

void insert_erase_tests()
{
	// push front and back
	doubly_linked_list<int> ll;
	for (size_t i = 10; i < 20; i++)
	{
		ll.push_back(i);
	}

	for (size_t i = 0; i < 10; i++)
	{
		ll.push_front(9 - i);
	}

	int start = 0;
	for (auto it = ll.begin(); it != ll.end(); ++it)
	{
		assert(*it == start++);
	}

	// remove from the middle
	auto it = ll.begin();
	for (size_t i = 0; i < 10; i++)
		++it;

	while (it != ll.end())
	{
		auto prev = it++;
		if (*prev == 19)
			int x = 10;
		ll.remove(prev);
	}

	start = 0;
	for (const auto& x : ll)
	{
		assert(x == start++);
	}

	// insert into the middle
	{
		auto it = ll.begin();
		for (size_t i = 0; i < 5; i++)
			++it;

		for (size_t i = 0; i < 10; i++)
		{
			ll.insert(it, i + 10);
		}
	}

	{
		auto it = ll.begin();
		for (size_t i = 0; i < 5; i++)
		{
			assert(*it == i);
			++it;
		}

		for (size_t i = 0; i < 10; i++)
		{
			assert(*it == i + 10);
			++it;
		}

		for (size_t i = 0; i < 5; i++)
		{
			assert(*it == i + 5);
			++it;
		}

		assert(it == ll.end());
	}
}

void constructor_tests()
{
	doubly_linked_list<int> ll;

	for (size_t i = 0; i < 100; i++)
	{
		ll.push_back(i);
	}

	doubly_linked_list<int> copy(ll);
	assert(copy.size() == ll.size());

	auto it = ll.begin();
	for (const auto& x : copy)
	{
		assert(x == *it);
		++it;
	}

	doubly_linked_list<int> move(std::move(copy));
	assert(copy.empty());

	assert(move.size() == ll.size());

	auto it1 = ll.begin();
	for (const auto& x : move)
	{
		assert(x == *it1);
		++it1;
	}
}

int main()
{
	std::cout << "Starting tests..." << std::endl;
	insert_basic_tests();
	insert_erase_tests();
	constructor_tests();
	std::cout << "No assertions failed" << std::endl;
}