[ATL][ATL_APITEST] Add CAtlList::InsertBefore/After + test
[reactos.git] / modules / rostests / apitests / atl / CAtlList.cpp
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Test for CAtlList
5 * COPYRIGHT: Copyright 2016-2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 * Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
7 */
8
9 #ifdef HAVE_APITEST
10 #include <apitest.h>
11 #else
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15 int g_tests_executed = 0;
16 int g_tests_failed = 0;
17 void ok_func(const char *file, int line, bool value, const char *fmt, ...)
18 {
19 va_list va;
20 va_start(va, fmt);
21 if (!value)
22 {
23 printf("%s (%d): ", file, line);
24 vprintf(fmt, va);
25 g_tests_failed++;
26 }
27 g_tests_executed++;
28 va_end(va);
29 }
30 #undef ok
31 #define ok(value, ...) ok_func(__FILE__, __LINE__, value, __VA_ARGS__)
32 #define START_TEST(x) int main(void)
33 #endif
34
35 #include <atlbase.h>
36 #include <atlcoll.h>
37
38
39 START_TEST(CAtlList)
40 {
41 CAtlList<int> list1;
42
43 ok(list1.GetCount() == 0, "Expected list1's size is zero, was %d\n", list1.GetCount());
44 list1.AddTail(56);
45 ok(list1.GetCount() == 1, "Expected list1's size is 1, was %d\n", list1.GetCount());
46 POSITION head = list1.AddHead(12);
47 ok(list1.GetCount() == 2, "Expected list1's size is 2, was %d\n", list1.GetCount());
48 POSITION tail = list1.AddTail(90);
49 ok(list1.GetCount() == 3, "Expected list1's size is 3, was %d\n", list1.GetCount());
50
51 list1.InsertBefore(head, -123);
52 list1.InsertAfter(head, 34); // no longer head, but the POSITION should still be valid..
53
54 list1.InsertBefore(tail, 78);
55 list1.InsertAfter(tail, -44);
56
57 int expected[] = {-123, 12, 34, 56, 78, 90, -44 };
58 int expected_size = sizeof(expected) / sizeof(expected[0]);
59 int index = 0;
60 POSITION it = list1.GetHeadPosition();
61 while (it != NULL)
62 {
63 ok(index < expected_size, "Too many items, expected %d, got %d!\n", expected_size, (index+1));
64 int value = list1.GetNext(it);
65 if (index < expected_size)
66 {
67 ok(value == expected[index], "Wrong value, got %d, expected %d\n", value, expected[index]);
68 }
69 else
70 {
71 ok(0, "Extra value: %d\n", value);
72 }
73 index++;
74 }
75 ok(it == NULL, "it does still point to something!\n");
76
77 #ifndef HAVE_APITEST
78 printf("CAtlList: %i tests executed (0 marked as todo, %i failures), 0 skipped.\n", g_tests_executed, g_tests_failed);
79 return g_tests_failed;
80 #endif
81 }