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 "atltest.h" 13 #endif 14 15 #include <atlbase.h> 16 #include <atlcoll.h> 17 #include <atlstr.h> 18 19 static void 20 test_BasicCases() 21 { 22 CAtlList<int> list1; 23 24 ok_size_t(list1.GetCount(), 0); 25 list1.AddTail(56); 26 ok_size_t(list1.GetCount(), 1); 27 POSITION head = list1.AddHead(12); 28 ok_size_t(list1.GetCount(), 2); 29 POSITION tail = list1.AddTail(90); 30 ok_size_t(list1.GetCount(), 3); 31 32 list1.InsertBefore(head, -123); 33 list1.InsertAfter(head, 34); // no longer head, but the POSITION should still be valid.. 34 35 list1.InsertBefore(tail, 78); 36 list1.InsertAfter(tail, -44); 37 38 int expected[] = {-123, 12, 34, 56, 78, 90, -44}; 39 int expected_size = sizeof(expected) / sizeof(expected[0]); 40 int index = 0; 41 POSITION it = list1.GetHeadPosition(); 42 while (it != NULL) 43 { 44 ok(index < expected_size, "Too many items, expected %d, got %d!\n", expected_size, (index + 1)); 45 int value = list1.GetNext(it); 46 if (index < expected_size) 47 { 48 ok(value == expected[index], "Wrong value, got %d, expected %d\n", value, expected[index]); 49 } 50 else 51 { 52 ok(0, "Extra value: %d\n", value); 53 } 54 index++; 55 } 56 ok(it == NULL, "it does still point to something!\n"); 57 } 58 59 static CStringW 60 to_str(const CAtlList<int>& lst) 61 { 62 CStringW tmp; 63 POSITION it = lst.GetHeadPosition(); 64 while (it != NULL) 65 { 66 int value = lst.GetNext(it); 67 tmp.AppendFormat(L"%d,", value); 68 } 69 return tmp; 70 } 71 72 #define ok_list(lst, expected) \ 73 do \ 74 { \ 75 CStringW _value = to_str(lst); \ 76 ok(_value == (expected), "Wrong value for '%s', expected: " #expected " got: \"%S\"\n", #lst, \ 77 _value.GetString()); \ 78 } while (0) 79 80 81 static void 82 test_SwapElements() 83 { 84 CAtlList<int> list; 85 list.AddTail(1); 86 list.AddTail(2); 87 list.AddTail(3); 88 89 ok_list(list, "1,2,3,"); 90 91 POSITION p1 = list.FindIndex(0); 92 POSITION p2 = list.FindIndex(2); 93 94 list.SwapElements(p1, p1); 95 ok_list(list, "1,2,3,"); 96 97 list.SwapElements(p1, p2); 98 ok_list(list, "3,2,1,"); 99 100 p1 = list.FindIndex(0); 101 p2 = list.FindIndex(1); 102 list.SwapElements(p1, p2); 103 ok_list(list, "2,3,1,"); 104 105 p1 = list.FindIndex(1); 106 p2 = list.FindIndex(2); 107 list.SwapElements(p1, p2); 108 ok_list(list, "2,1,3,"); 109 110 p1 = list.FindIndex(0); 111 p2 = list.FindIndex(2); 112 list.SwapElements(p2, p1); 113 ok_list(list, "3,1,2,"); 114 } 115 116 117 START_TEST(CAtlList) 118 { 119 test_BasicCases(); 120 test_SwapElements(); 121 } 122