aboutsummaryrefslogtreecommitdiff
path: root/test/list.cpp
blob: cdb0bc1e600bb0d6661e8713f2c1d25e1dcbfa8e (plain)
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
#include <gtest/gtest.h>

#include "backend/ListIterator.h"
#include "backend/List.h"

static void add_test_items(List<int> & foo) {
	foo.push_back(1);
	foo.push_back(2);
	foo.push_back(3);
}

TEST(ListTest, PushBack) {
	List<int> foo;
	ASSERT_EQ(foo.size(), 0);

	add_test_items(foo);

	ASSERT_EQ(foo.size(), 3);
}

TEST(ListTest, Clear) {
	List<int> foo;
	foo.clear();

	add_test_items(foo);

	foo.clear();
	ASSERT_EQ(foo.size(), 0);
}

TEST(ListTest, Remove) {
	List<int> foo;
	add_test_items(foo);

	ASSERT_EQ(foo.size(), 3);
	foo.remove(2);
	ASSERT_EQ(foo.size(), 2);
	foo.remove(2);
	ASSERT_EQ(foo.size(), 2);
}

TEST(ListTest, RemoveDuplicates) {
	List<int> foo;

	add_test_items(foo);
	foo.push_back(2);

	ASSERT_EQ(foo.size(), 4);
	foo.remove(2);
	ASSERT_EQ(foo.size(), 3);
	foo.remove(2);
	ASSERT_EQ(foo.size(), 2);
}

TEST(ListTest, RemoveDuringIterator) {
	List<int> foo;
	add_test_items(foo);

	for (int & value : foo) {
		if (value == 2)
			foo.remove(value);
	}

	ASSERT_EQ(foo.size(), 2);
}

TEST(ListTest, UnmanagedPointers) {
	char * ptr1 = static_cast<char *>(malloc(3));
	char * ptr2 = static_cast<char *>(malloc(3));

	{
		List<char *> foo;

		foo.push_back(ptr1);
		foo.push_back(ptr2);

		// this destructor shouldn't free ptr1 and ptr2
	}

	ASSERT_NE(ptr1, nullptr);
	ASSERT_NE(ptr2, nullptr);

	// thus, these shouldn't cause double free() errors:
	free(ptr1);
	free(ptr2);
}