1 /* 2 * PROJECT: ReactOS api tests 3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory 4 * PURPOSE: Test for CSimpleArray 5 * PROGRAMMER: Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com) 6 */ 7 8 #ifdef HAVE_APITEST 9 #include <apitest.h> 10 #else 11 #include "atltest.h" 12 #endif 13 14 #include <atlbase.h> 15 #include <atlsimpcoll.h> 16 17 struct CSimpleCreature 18 { 19 static int s_nCount; 20 static int s_nCopyCount; 21 CSimpleCreature() 22 { 23 CSimpleCreature::s_nCount++; 24 } 25 CSimpleCreature(const CSimpleCreature& c) 26 { 27 CSimpleCreature::s_nCount++; 28 } 29 ~CSimpleCreature() 30 { 31 CSimpleCreature::s_nCount--; 32 } 33 CSimpleCreature& operator=(const CSimpleCreature& other) 34 { 35 CSimpleCreature::s_nCopyCount++; 36 return *this; 37 } 38 }; 39 40 int CSimpleCreature::s_nCount = 0; 41 int CSimpleCreature::s_nCopyCount = 0; 42 43 44 START_TEST(CSimpleArray) 45 { 46 CSimpleArray<int> array1; 47 48 ok_int(array1.GetSize(), 0); 49 50 array1.Add(123); 51 52 ok_int(array1.GetSize(), 1); 53 ok_int(array1.GetData()[0], 123); 54 ok_int(array1[0], 123); 55 56 array1.Add(456); 57 58 ok_int(array1.GetSize(), 2); 59 ok_int(array1.GetData()[0], 123); 60 ok_int(array1[0], 123); 61 ok_int(array1.GetData()[1], 456); 62 ok_int(array1[1], 456); 63 64 array1.RemoveAll(); 65 ok_int(array1.GetSize(), 0); 66 67 array1.Add(1); 68 array1.Add(1); 69 array1.Add(1); 70 array1.Add(2); 71 array1.Add(2); 72 array1.Add(3); 73 ok_int(array1.GetSize(), 6); 74 75 array1.Remove(2); 76 ok_int(array1.GetSize(), 5); 77 78 array1.Remove(1); 79 ok_int(array1.GetSize(), 4); 80 81 ok_int(array1[0], 1); 82 ok_int(array1[1], 1); 83 ok_int(array1[2], 2); 84 ok_int(array1[3], 3); 85 86 ok_int(CSimpleCreature::s_nCount, 0); 87 ok_int(CSimpleCreature::s_nCopyCount, 0); 88 89 CSimpleArray<CSimpleCreature> array2; 90 { 91 CSimpleCreature creature1, creature2; 92 93 ok_int(CSimpleCreature::s_nCount, 2); 94 ok_int(CSimpleCreature::s_nCopyCount, 0); 95 array2.Add(creature1); 96 ok_int(CSimpleCreature::s_nCount, 3); 97 ok_int(CSimpleCreature::s_nCopyCount, 0); 98 array2.Add(creature2); 99 ok_int(CSimpleCreature::s_nCount, 4); 100 ok_int(CSimpleCreature::s_nCopyCount, 0); 101 } 102 ok_int(CSimpleCreature::s_nCount, 2); 103 ok_int(CSimpleCreature::s_nCopyCount, 0); 104 105 { 106 CSimpleArray<CSimpleCreature> array3(array2), array4, array5; 107 ok_int(CSimpleCreature::s_nCount, 4); 108 ok_int(CSimpleCreature::s_nCopyCount, 0); 109 110 array4 = array2; 111 ok_int(CSimpleCreature::s_nCount, 6); 112 ok_int(CSimpleCreature::s_nCopyCount, 0); 113 114 CSimpleCreature creature1; 115 ok_int(CSimpleCreature::s_nCount, 7); 116 ok_int(CSimpleCreature::s_nCopyCount, 0); 117 118 array4.Add(creature1); 119 ok_int(CSimpleCreature::s_nCount, 8); 120 ok_int(CSimpleCreature::s_nCopyCount, 0); 121 122 array3 = array4; 123 ok_int(CSimpleCreature::s_nCount, 9); 124 ok_int(CSimpleCreature::s_nCopyCount, 0); 125 126 array5 = array2; 127 ok_int(CSimpleCreature::s_nCount, 11); 128 ok_int(CSimpleCreature::s_nCopyCount, 0); 129 130 array5 = array2; 131 ok_int(CSimpleCreature::s_nCount, 11); 132 ok_int(CSimpleCreature::s_nCopyCount, 0); 133 } 134 ok_int(CSimpleCreature::s_nCount, 2); 135 ok_int(CSimpleCreature::s_nCopyCount, 0); 136 137 array2.RemoveAll(); 138 ok_int(CSimpleCreature::s_nCount, 0); 139 ok_int(CSimpleCreature::s_nCopyCount, 0); 140 141 array1.RemoveAll(); 142 ok_int(array1.GetSize(), 0); 143 for (int i = 0; i < 100; ++i) 144 { 145 array1.Add(i); 146 } 147 ok_int(array1.GetSize(), 100); 148 149 array1.RemoveAll(); 150 ok_int(array1.GetSize(), 0); 151 array1.Add(123); 152 array1.Add(321); 153 ok(!!array1.RemoveAt(0), "Expected RemoveAt(0) to succeed\n"); 154 ok_int(array1.GetSize(), 1); 155 if (array1.GetSize() == 1) 156 { 157 ok_int(array1[0], 321); 158 } 159 ok(!!array1.RemoveAt(0), "Expected RemoveAt(0) to succeed\n"); 160 ok_int(array1.GetSize(), 0); 161 } 162