1 /* 2 * PROJECT: ReactOS api tests 3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory 4 * PURPOSE: Test for CSimpleMap 5 * PROGRAMMER: Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com) 6 */ 7 8 #ifdef HAVE_APITEST 9 #include <apitest.h> 10 #else 11 #include <stdlib.h> 12 #include <stdio.h> 13 #include <stdarg.h> 14 int g_tests_executed = 0; 15 int g_tests_failed = 0; 16 void ok_func(const char *file, int line, bool value, const char *fmt, ...) 17 { 18 va_list va; 19 va_start(va, fmt); 20 if (!value) 21 { 22 printf("%s (%d): ", file, line); 23 vprintf(fmt, va); 24 g_tests_failed++; 25 } 26 g_tests_executed++; 27 va_end(va); 28 } 29 #undef ok 30 #define ok(value, ...) ok_func(__FILE__, __LINE__, value, __VA_ARGS__) 31 #define START_TEST(x) int main(void) 32 #endif 33 34 #include <atlbase.h> 35 #include <atlsimpcoll.h> 36 37 struct CMonster 38 { 39 static int s_nCount; 40 static int s_nCopyCount; 41 42 CMonster() 43 { 44 CMonster::s_nCount++; 45 } 46 CMonster(const CMonster& c) 47 { 48 CMonster::s_nCount++; 49 } 50 ~CMonster() 51 { 52 CMonster::s_nCount--; 53 } 54 CMonster& operator=(const CMonster& other) 55 { 56 CMonster::s_nCopyCount++; 57 return *this; 58 } 59 }; 60 61 int CMonster::s_nCount = 0; 62 int CMonster::s_nCopyCount = 0; 63 64 START_TEST(CSimpleMap) 65 { 66 CSimpleMap<int, int> map1; 67 68 ok(map1.GetSize() == 0, "Expected map1's size is zero, was %d\n", map1.GetSize()); 69 70 map1.Add(1, 2); 71 ok(map1.GetSize() == 1, "Expected map1's size is 1, was %d\n", map1.GetSize()); 72 map1.Add(2, 3); 73 ok(map1.GetSize() == 2, "Expected map1's size is 2, was %d\n", map1.GetSize()); 74 75 ok(map1.Lookup(1) == 2, "Expected map1.Lookup(1) is 2, was %d\n", map1.Lookup(1)); 76 ok(map1.Lookup(2) == 3, "Expected map1.Lookup(2) is 3, was %d\n", map1.Lookup(2)); 77 ok(map1.Lookup(-1) == 0, "Expected map1.Lookup(-1) is 0, was %d\n", map1.Lookup(-1)); 78 79 ok(map1.ReverseLookup(2) == 1, "Expected map1.ReverseLookup(2) is 1, was %d\n", map1.ReverseLookup(2)); 80 ok(map1.ReverseLookup(3) == 2, "Expected map1.ReverseLookup(3) is 2, was %d\n", map1.ReverseLookup(3)); 81 82 ok(map1.GetKeyAt(0) == 1, "Expected map1.GetKeyAt(0) is 1, was %d\n", map1.GetKeyAt(0)); 83 ok(map1.GetKeyAt(1) == 2, "Expected map1.GetKeyAt(1) is 2, was %d\n", map1.GetKeyAt(1)); 84 85 ok(map1.GetValueAt(0) == 2, "Expected map1.GetValueAt(0) is 2, was %d\n", map1.GetValueAt(0)); 86 ok(map1.GetValueAt(1) == 3, "Expected map1.GetValueAt(1) is 3, was %d\n", map1.GetValueAt(1)); 87 88 map1.SetAt(2, 4); 89 90 ok(map1.Lookup(1) == 2, "Expected map1.Lookup(1) is 2, was %d\n", map1.Lookup(1)); 91 ok(map1.Lookup(2) == 4, "Expected map1.Lookup(2) is 4, was %d\n", map1.Lookup(2)); 92 93 ok(map1.ReverseLookup(2) == 1, "Expected map1.ReverseLookup(2) is 1, was %d\n", map1.ReverseLookup(2)); 94 ok(map1.ReverseLookup(4) == 2, "Expected map1.ReverseLookup(4) is 2, was %d\n", map1.ReverseLookup(4)); 95 96 map1.Remove(1); 97 ok(map1.GetSize() == 1, "Expected map1's size is 1, was %d\n", map1.GetSize()); 98 map1.Remove(2); 99 ok(map1.GetSize() == 0, "Expected map1's size is 0, was %d\n", map1.GetSize()); 100 101 map1.Add(1, 4); 102 ok(map1.GetSize() == 1, "Expected map1's size is 1, was %d\n", map1.GetSize()); 103 map1.Add(2, 8); 104 ok(map1.GetSize() == 2, "Expected map1's size is 2, was %d\n", map1.GetSize()); 105 map1.Add(3, 12); 106 ok(map1.GetSize() == 3, "Expected map1's size is 3, was %d\n", map1.GetSize()); 107 108 map1.RemoveAll(); 109 ok(map1.GetSize() == 0, "Expected map1's size is 0, was %d\n", map1.GetSize()); 110 111 ok(CMonster::s_nCount == 0, "Expected CMonster::s_nCount is 0, was %d\n", CMonster::s_nCount); 112 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 113 114 CSimpleMap<CMonster, CMonster> map2; 115 ok(map2.GetSize() == 0, "Expected map2's size is zero, was %d\n", map2.GetSize()); 116 117 ok(CMonster::s_nCount == 0, "Expected CMonster::s_nCount is 0, was %d\n", CMonster::s_nCount); 118 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 119 120 { 121 CMonster m1; 122 ok(CMonster::s_nCount == 1, "Expected CMonster::s_nCount is 1, was %d\n", CMonster::s_nCount); 123 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 124 125 CMonster m2; 126 ok(CMonster::s_nCount == 2, "Expected CMonster::s_nCount is 2, was %d\n", CMonster::s_nCount); 127 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 128 129 map2.Add(m1, m2); 130 ok(CMonster::s_nCount == 4, "Expected CMonster::s_nCount is 4, was %d\n", CMonster::s_nCount); 131 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 132 } 133 134 ok(map2.GetSize() == 1, "Expected map2's size is 1, was %d\n", map2.GetSize()); 135 ok(CMonster::s_nCount == 2, "Expected CMonster::s_nCount is 2, was %d\n", CMonster::s_nCount); 136 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 137 138 { 139 CMonster m1; 140 ok(CMonster::s_nCount == 3, "Expected CMonster::s_nCount is 3, was %d\n", CMonster::s_nCount); 141 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 142 143 CMonster m2; 144 ok(CMonster::s_nCount == 4, "Expected CMonster::s_nCount is 4, was %d\n", CMonster::s_nCount); 145 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 146 147 map2.Add(m1, m2); 148 ok(CMonster::s_nCount == 6, "Expected CMonster::s_nCount is 6, was %d\n", CMonster::s_nCount); 149 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 150 } 151 152 ok(map2.GetSize() == 2, "Expected map2's size is 2, was %d\n", map2.GetSize()); 153 ok(CMonster::s_nCount == 4, "Expected CMonster::s_nCount is 4, was %d\n", CMonster::s_nCount); 154 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 155 156 map2.RemoveAt(0); 157 ok(CMonster::s_nCount == 2, "Expected CMonster::s_nCount is 2, was %d\n", CMonster::s_nCount); 158 ok(map2.GetSize() == 1, "Expected map2's size is 1, was %d\n", map2.GetSize()); 159 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 160 161 map2.RemoveAt(0); 162 ok(CMonster::s_nCount == 0, "Expected CMonster::s_nCount is 0, was %d\n", CMonster::s_nCount); 163 ok(map2.GetSize() == 0, "Expected map2's size is 0, was %d\n", map2.GetSize()); 164 ok(CMonster::s_nCopyCount == 0, "Expected CMonster::s_nCopyCount is 0, was %d\n", CMonster::s_nCopyCount); 165 166 CSimpleMap<int, CMonster> map3; 167 ok(map3.GetSize() == 0, "Expected map3's size is 0, was %d\n", map3.GetSize()); 168 169 CMonster m3; 170 ok(CMonster::s_nCount == 1, "Expected CMonster::s_nCount is 1, was %d\n", CMonster::s_nCount); 171 172 map3.Add(1, m3); 173 ok(map3.GetSize() == 1, "Expected map3's size is 1, was %d\n", map3.GetSize()); 174 ok(CMonster::s_nCount == 2, "Expected CMonster::s_nCount is 2, was %d\n", CMonster::s_nCount); 175 176 map3.Add(2, m3); 177 ok(map3.GetSize() == 2, "Expected map3's size is 2, was %d\n", map3.GetSize()); 178 ok(CMonster::s_nCount == 3, "Expected CMonster::s_nCount is 3, was %d\n", CMonster::s_nCount); 179 180 map3.Add(3, m3); 181 ok(map3.GetSize() == 3, "Expected map3's size is 3, was %d\n", map3.GetSize()); 182 ok(CMonster::s_nCount == 4, "Expected CMonster::s_nCount is 4, was %d\n", CMonster::s_nCount); 183 184 map3.Remove(2); 185 ok(map3.GetSize() == 2, "Expected map3's size is 2, was %d\n", map3.GetSize()); 186 ok(CMonster::s_nCount == 3, "Expected CMonster::s_nCount is 3, was %d\n", CMonster::s_nCount); 187 188 map3.RemoveAll(); 189 ok(map3.GetSize() == 0, "Expected map3's size is 0, was %d\n", map3.GetSize()); 190 ok(CMonster::s_nCount == 1, "Expected CMonster::s_nCount is 1, was %d\n", CMonster::s_nCount); 191 192 map1.Add(1, 2); 193 ok(map1.GetSize() == 1, "Expected map1's size is 1, was %d\n", map1.GetSize()); 194 map1.Add(2, 3); 195 ok(map1.GetSize() == 2, "Expected map1's size is 2, was %d\n", map1.GetSize()); 196 197 ok(!!map1.RemoveAt(0), "Expected RemoveAt(0) to succeed\n"); 198 ok(map1.GetSize() == 1, "Expected map1's size is 1, was %d\n", map1.GetSize()); 199 ok(!!map1.RemoveAt(0), "Expected RemoveAt(0) to succeed\n"); 200 ok(map1.GetSize() == 0, "Expected map1's size is 0, was %d\n", map1.GetSize()); 201 202 #ifndef HAVE_APITEST 203 printf("CSimpleMap: %i tests executed (0 marked as todo, %i failures), 0 skipped.\n", g_tests_executed, g_tests_failed); 204 return g_tests_failed; 205 #endif 206 } 207