FreeRDP/winpr/libwinpr/utils/test/TestLinkedList.c

138 lines
3.1 KiB
C
Raw Normal View History

#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/collections.h>
int TestLinkedList(int argc, char* argv[])
{
int count;
wLinkedList* list;
2021-07-28 17:20:32 +03:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
list = LinkedList_New();
2021-07-28 17:20:32 +03:00
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;
count = LinkedList_Count(list);
if (count != 3)
{
printf("LinkedList_Count: expected 3, actual: %d\n", count);
return -1;
}
LinkedList_Enumerator_Reset(list);
while (LinkedList_Enumerator_MoveNext(list))
{
2021-07-28 17:20:32 +03:00
printf("\t%p\n", LinkedList_Enumerator_Current(list));
}
2017-02-16 15:17:49 +03:00
printf("\n");
2021-07-28 17:20:32 +03:00
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: %d\n", count);
return -1;
}
LinkedList_Enumerator_Reset(list);
while (LinkedList_Enumerator_MoveNext(list))
{
2021-07-28 17:20:32 +03:00
printf("\t%p\n", LinkedList_Enumerator_Current(list));
}
2017-02-16 15:17:49 +03:00
printf("\n");
2021-07-28 17:20:32 +03:00
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: %d\n", count);
return -1;
}
2021-07-28 17:20:32 +03:00
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: %d\n", count);
return -1;
}
LinkedList_Enumerator_Reset(list);
while (LinkedList_Enumerator_MoveNext(list))
{
2021-07-28 17:20:32 +03:00
printf("\t%p\n", LinkedList_Enumerator_Current(list));
}
2017-02-16 15:17:49 +03:00
printf("\n");
2021-07-28 17:20:32 +03:00
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))
{
2021-07-28 17:20:32 +03:00
printf("\t%p\n", LinkedList_Enumerator_Current(list));
}
2017-02-16 15:17:49 +03:00
printf("\n");
2021-07-28 17:20:32 +03:00
printf("LinkedList First: %p Last: %p\n", LinkedList_First(list), LinkedList_Last(list));
LinkedList_Free(list);
2014-09-24 15:03:46 +04:00
/* Test enumerator robustness */
/* enumerator on an empty list */
list = LinkedList_New();
2021-07-28 17:20:32 +03:00
if (!list)
return -1;
2014-09-24 15:03:46 +04:00
LinkedList_Enumerator_Reset(list);
2017-02-16 15:17:49 +03:00
2014-09-24 15:03:46 +04:00
while (LinkedList_Enumerator_MoveNext(list))
{
2021-07-28 17:20:32 +03:00
printf("\terror: %p\n", LinkedList_Enumerator_Current(list));
2014-09-24 15:03:46 +04:00
}
2017-02-16 15:17:49 +03:00
2014-09-24 15:03:46 +04:00
printf("\n");
LinkedList_Free(list);
/* Use an enumerator without reset */
list = LinkedList_New();
2021-07-28 17:20:32 +03:00
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;
2017-02-16 15:17:49 +03:00
2014-09-24 15:03:46 +04:00
while (LinkedList_Enumerator_MoveNext(list))
{
2021-07-28 17:20:32 +03:00
printf("\t%p\n", LinkedList_Enumerator_Current(list));
2014-09-24 15:03:46 +04:00
}
2017-02-16 15:17:49 +03:00
2014-09-24 15:03:46 +04:00
printf("\n");
LinkedList_Free(list);
return 0;
}