summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/utils/test/TestLinkedList.c
blob: 6c6a2c3c36d4917e194d9a8d8088c55a64288c04 (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
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
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/collections.h>

int TestLinkedList(int argc, char* argv[])
{
	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	wLinkedList* list = LinkedList_New();
	if (!list)
		return -1;

	if (!LinkedList_AddFirst(list, (void*)(size_t)1))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)2))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)3))
		return -1;
	size_t count = LinkedList_Count(list);

	if (count != 3)
	{
		printf("LinkedList_Count: expected 3, actual: %" PRIuz "\n", count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\t%p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
	LinkedList_RemoveFirst(list);
	LinkedList_RemoveLast(list);
	count = LinkedList_Count(list);

	if (count != 1)
	{
		printf("LinkedList_Count: expected 1, actual: %" PRIuz "\n", count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\t%p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
	LinkedList_RemoveFirst(list);
	LinkedList_RemoveLast(list);
	count = LinkedList_Count(list);

	if (count != 0)
	{
		printf("LinkedList_Count: expected 0, actual: %" PRIuz "\n", count);
		return -1;
	}

	if (!LinkedList_AddFirst(list, (void*)(size_t)4))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)5))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)6))
		return -1;
	count = LinkedList_Count(list);

	if (count != 3)
	{
		printf("LinkedList_Count: expected 3, actual: %" PRIuz "\n", count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\t%p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
	if (!LinkedList_Remove(list, (void*)(size_t)5))
		return -1;
	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\t%p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
	LinkedList_Free(list);
	/* Test enumerator robustness */
	/* enumerator on an empty list */
	list = LinkedList_New();
	if (!list)
		return -1;
	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\terror: %p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	LinkedList_Free(list);
	/* Use an enumerator without reset */
	list = LinkedList_New();
	if (!list)
		return -1;
	if (!LinkedList_AddFirst(list, (void*)(size_t)4))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)5))
		return -1;
	if (!LinkedList_AddLast(list, (void*)(size_t)6))
		return -1;

	while (LinkedList_Enumerator_MoveNext(list))
	{
		printf("\t%p\n", LinkedList_Enumerator_Current(list));
	}

	printf("\n");
	LinkedList_Free(list);
	return 0;
}