FreeRDP
TestLinkedList.c
1 
2 #include <winpr/crt.h>
3 #include <winpr/tchar.h>
4 #include <winpr/collections.h>
5 
6 int TestLinkedList(int argc, char* argv[])
7 {
8  WINPR_UNUSED(argc);
9  WINPR_UNUSED(argv);
10 
11  wLinkedList* list = LinkedList_New();
12  if (!list)
13  return -1;
14 
15  if (!LinkedList_AddFirst(list, (void*)(size_t)1))
16  return -1;
17  if (!LinkedList_AddLast(list, (void*)(size_t)2))
18  return -1;
19  if (!LinkedList_AddLast(list, (void*)(size_t)3))
20  return -1;
21  size_t count = LinkedList_Count(list);
22 
23  if (count != 3)
24  {
25  printf("LinkedList_Count: expected 3, actual: %" PRIuz "\n", count);
26  return -1;
27  }
28 
29  LinkedList_Enumerator_Reset(list);
30 
31  while (LinkedList_Enumerator_MoveNext(list))
32  {
33  printf("\t%p\n", LinkedList_Enumerator_Current(list));
34  }
35 
36  printf("\n");
37  printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
38  LinkedList_RemoveFirst(list);
39  LinkedList_RemoveLast(list);
40  count = LinkedList_Count(list);
41 
42  if (count != 1)
43  {
44  printf("LinkedList_Count: expected 1, actual: %" PRIuz "\n", count);
45  return -1;
46  }
47 
48  LinkedList_Enumerator_Reset(list);
49 
50  while (LinkedList_Enumerator_MoveNext(list))
51  {
52  printf("\t%p\n", LinkedList_Enumerator_Current(list));
53  }
54 
55  printf("\n");
56  printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
57  LinkedList_RemoveFirst(list);
58  LinkedList_RemoveLast(list);
59  count = LinkedList_Count(list);
60 
61  if (count != 0)
62  {
63  printf("LinkedList_Count: expected 0, actual: %" PRIuz "\n", count);
64  return -1;
65  }
66 
67  if (!LinkedList_AddFirst(list, (void*)(size_t)4))
68  return -1;
69  if (!LinkedList_AddLast(list, (void*)(size_t)5))
70  return -1;
71  if (!LinkedList_AddLast(list, (void*)(size_t)6))
72  return -1;
73  count = LinkedList_Count(list);
74 
75  if (count != 3)
76  {
77  printf("LinkedList_Count: expected 3, actual: %" PRIuz "\n", count);
78  return -1;
79  }
80 
81  LinkedList_Enumerator_Reset(list);
82 
83  while (LinkedList_Enumerator_MoveNext(list))
84  {
85  printf("\t%p\n", LinkedList_Enumerator_Current(list));
86  }
87 
88  printf("\n");
89  printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
90  if (!LinkedList_Remove(list, (void*)(size_t)5))
91  return -1;
92  LinkedList_Enumerator_Reset(list);
93 
94  while (LinkedList_Enumerator_MoveNext(list))
95  {
96  printf("\t%p\n", LinkedList_Enumerator_Current(list));
97  }
98 
99  printf("\n");
100  printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
101  LinkedList_Free(list);
102  /* Test enumerator robustness */
103  /* enumerator on an empty list */
104  list = LinkedList_New();
105  if (!list)
106  return -1;
107  LinkedList_Enumerator_Reset(list);
108 
109  while (LinkedList_Enumerator_MoveNext(list))
110  {
111  printf("\terror: %p\n", LinkedList_Enumerator_Current(list));
112  }
113 
114  printf("\n");
115  LinkedList_Free(list);
116  /* Use an enumerator without reset */
117  list = LinkedList_New();
118  if (!list)
119  return -1;
120  if (!LinkedList_AddFirst(list, (void*)(size_t)4))
121  return -1;
122  if (!LinkedList_AddLast(list, (void*)(size_t)5))
123  return -1;
124  if (!LinkedList_AddLast(list, (void*)(size_t)6))
125  return -1;
126 
127  while (LinkedList_Enumerator_MoveNext(list))
128  {
129  printf("\t%p\n", LinkedList_Enumerator_Current(list));
130  }
131 
132  printf("\n");
133  LinkedList_Free(list);
134  return 0;
135 }