Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. strtabs.c
  5. Abstract:
  6. Tests string table routines.
  7. Author:
  8. <full name> (<alias>) <date>
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. #ifdef UNICODE
  14. #pragma message ("You must use UNICODE version of setupapi.dll")
  15. #else
  16. #pragma message ("You must use ANSI version of setupapi.dll")
  17. #endif
  18. BOOL
  19. WINAPI
  20. MigUtil_Entry (
  21. HINSTANCE hInstance,
  22. DWORD dwReason,
  23. LPVOID lpReserved
  24. );
  25. HANDLE g_hHeap;
  26. HINSTANCE g_hInst;
  27. VOID
  28. pTest1 (
  29. VOID
  30. )
  31. {
  32. HASHTABLE Table;
  33. TREE_ENUM e;
  34. GROWBUFFER Buf = GROWBUF_INIT;
  35. LONG rc;
  36. PDWORD dptr;
  37. DWORD Data;
  38. HASHTABLE DupTable;
  39. Table = HtAlloc();
  40. _tprintf (TEXT("Testing full paths... "));
  41. if (EnumFirstFileInTree (&e, TEXT("C:\\"), NULL, FALSE)) {
  42. do {
  43. rc = HtAddString (Table, e.FullPath);
  44. GrowBufAppendDword (&Buf, (DWORD) rc);
  45. MYASSERT (rc);
  46. } while (EnumNextFileInTree (&e));
  47. }
  48. dptr = (PDWORD) Buf.Buf;
  49. if (EnumFirstFileInTree (&e, TEXT("C:\\"), NULL, FALSE)) {
  50. do {
  51. rc = HtFindString (Table, e.FullPath);
  52. MYASSERT (rc);
  53. MYASSERT (*dptr == (DWORD) rc);
  54. dptr++;
  55. } while (EnumNextFileInTree (&e));
  56. }
  57. _tprintf (TEXT("Done\n"));
  58. HtFree (Table);
  59. _tprintf (TEXT("Testing extra data and collisions... "));
  60. Table = HtAllocWithData (sizeof (DWORD));
  61. DupTable = HtAlloc();
  62. Buf.End = 0;
  63. if (EnumFirstFileInTree (&e, TEXT("C:\\"), NULL, FALSE)) {
  64. do {
  65. if (HtFindString (Table, e.Name)) {
  66. HtAddString (DupTable, e.Name);
  67. }
  68. rc = HtAddStringAndData (Table, e.Name, &e.FindData->nFileSizeLow);
  69. GrowBufAppendDword (&Buf, (DWORD) rc);
  70. MYASSERT (rc);
  71. } while (EnumNextFileInTree (&e));
  72. }
  73. dptr = (PDWORD) Buf.Buf;
  74. if (EnumFirstFileInTree (&e, TEXT("C:\\"), NULL, FALSE)) {
  75. do {
  76. rc = HtFindStringAndData (Table, e.Name, &Data);
  77. MYASSERT (rc);
  78. if (!HtFindString (DupTable, e.Name)) {
  79. MYASSERT (*dptr == (DWORD) rc);
  80. MYASSERT (Data == e.FindData->nFileSizeLow);
  81. }
  82. dptr++;
  83. } while (EnumNextFileInTree (&e));
  84. }
  85. HtFree (DupTable);
  86. HtFree (Table);
  87. _tprintf (TEXT("Done\n"));
  88. FreeGrowBuffer (&Buf);
  89. }
  90. VOID
  91. pTest2 (
  92. VOID
  93. )
  94. {
  95. HASHTABLE Table;
  96. TREE_ENUM e;
  97. INT Count;
  98. LONG rc;
  99. HASHTABLE_ENUM e2;
  100. WIN32_FIND_DATA fd;
  101. _tprintf (TEXT("Testing enumeration... "));
  102. Count = 0;
  103. Table = HtAlloc();
  104. if (EnumFirstFileInTree (&e, TEXT("C:\\"), NULL, FALSE)) {
  105. do {
  106. rc = HtAddString (Table, e.FullPath);
  107. Count++;
  108. MYASSERT (rc);
  109. } while (EnumNextFileInTree (&e));
  110. }
  111. if (EnumFirstHashTableString (&e2, Table)) {
  112. do {
  113. MYASSERT (DoesFileExistEx (e2.String, &fd));
  114. Count--;
  115. } while (EnumNextHashTableString (&e2));
  116. }
  117. MYASSERT (Count == 0);
  118. _tprintf (TEXT("Done\n"));
  119. HtFree (Table);
  120. }
  121. VOID
  122. pTest3 (
  123. VOID
  124. )
  125. {
  126. HASHTABLE Table;
  127. LONG rc;
  128. LONG rc2;
  129. BOOL Pass = TRUE;
  130. TCHAR String[6];
  131. INT i;
  132. UINT u = 0;
  133. ZeroMemory (String, sizeof (String));
  134. String[0] = 1;
  135. _tprintf (TEXT("Testing every character combination..."));
  136. while (String[4] == 0) {
  137. if (Pass) {
  138. u++;
  139. if ((u % 10000) == 0) {
  140. _tprintf (TEXT("."));
  141. }
  142. if (!u) {
  143. break;
  144. }
  145. }
  146. #if 0
  147. Table = StringTableInitialize();
  148. rc = StringTableAddString(
  149. Table,
  150. String,
  151. STRTAB_CASE_INSENSITIVE
  152. );
  153. rc2 = StringTableLookUpString (Table, String, STRTAB_CASE_INSENSITIVE);
  154. StringTableDestroy (Table);
  155. #endif
  156. Table = HtAlloc();
  157. rc = HtAddString (Table, String);
  158. rc2 = HtFindString (Table, String);
  159. HtFree (Table);
  160. if (!Pass) {
  161. break;
  162. }
  163. if (rc != rc2) {
  164. Pass = FALSE;
  165. //
  166. // We go through this loop once more against the same string,
  167. // to make debugging easy.
  168. //
  169. } else {
  170. for (i = 0 ; i < 5 ; i++) {
  171. String[i]++;
  172. if (String[i] != 0) {
  173. break;
  174. }
  175. String[i]++;
  176. }
  177. }
  178. }
  179. _tprintf (TEXT("\n"));
  180. if (!Pass) {
  181. _tprintf (TEXT("Test Failed on test %u!\nString: ["), u, String);
  182. for (i = 0 ; i < 5 ; i++) {
  183. _tprintf (TEXT("%c"), String[i]);
  184. }
  185. _tprintf (TEXT("] "));
  186. for (i = 0 ; i < 5 ; i++) {
  187. #ifdef UNICODE
  188. _tprintf (TEXT(" %04X"), (WORD) String[i]);
  189. #else
  190. _tprintf (TEXT(" %02X"), (BYTE) String[i]);
  191. #endif
  192. }
  193. _tprintf (TEXT("\n"));
  194. } else {
  195. _tprintf (TEXT("Test Passed!\n"));
  196. }
  197. }
  198. PCTSTR
  199. pStrToHex (
  200. PTSTR Hex,
  201. PCTSTR Str
  202. )
  203. {
  204. PTSTR p;
  205. PCTSTR q;
  206. p = Hex;
  207. for (q = Str ; *q ; q++) {
  208. #ifdef UNICODE
  209. p += wsprintf (p, TEXT("%04X "), (WORD) *q);
  210. #else
  211. p += wsprintf (p, TEXT("%02X "), (BYTE) *q);
  212. #endif
  213. }
  214. *p++ = TEXT('\"');
  215. for (q = Str ; *q ; q++) {
  216. if (*q < 32 || *q > 255 || *q >= 127) {
  217. *p++ = TEXT('.');
  218. } else {
  219. *p++ = *q;
  220. }
  221. }
  222. *p++ = TEXT('\"');
  223. *p = 0;
  224. return Hex;
  225. }
  226. VOID
  227. pTest4 (
  228. VOID
  229. )
  230. {
  231. TCHAR Str[2];
  232. TCHAR Lower[5];
  233. INT Result;
  234. TCHAR Hex1[80];
  235. TCHAR Hex2[80];
  236. INT i;
  237. INT j;
  238. #ifdef UNICODE
  239. printf ("Testing UNICODE\n\n");
  240. #else
  241. printf ("Testing DBCS\n\n");
  242. #endif
  243. ZeroMemory (Str, sizeof (Str));
  244. Str[0] = 1;
  245. j = (sizeof (Str) / sizeof (Str[0])) - 1;
  246. for (;;) {
  247. lstrcpy (Lower, Str);
  248. CharLower (Lower);
  249. Result = CompareString (
  250. LOCALE_SYSTEM_DEFAULT,
  251. NORM_IGNORECASE,
  252. Str,
  253. -1,
  254. Lower,
  255. -1
  256. );
  257. if (Result != CSTR_EQUAL) {
  258. _tprintf (
  259. TEXT("ERROR: %s does not match %s, Result=%i\n"),
  260. pStrToHex (Hex1, Str),
  261. pStrToHex (Hex2, Lower),
  262. Result
  263. );
  264. } else {
  265. Result = CompareString (
  266. LOCALE_SYSTEM_DEFAULT,
  267. NORM_IGNORECASE,
  268. Lower,
  269. -1,
  270. Str,
  271. -1
  272. );
  273. if (Result != CSTR_EQUAL) {
  274. _tprintf (
  275. TEXT("ERROR: %s does not match %s, Result=%i\n"),
  276. pStrToHex (Hex1, Str),
  277. pStrToHex (Hex2, Lower),
  278. Result
  279. );
  280. }
  281. }
  282. i = 0;
  283. do {
  284. Str[i]++;
  285. if (Str[i] == 0) {
  286. Str[i]++;
  287. } else {
  288. break;
  289. }
  290. i++;
  291. } while (i < j);
  292. if (i == j) {
  293. break;
  294. }
  295. }
  296. }
  297. VOID
  298. pSimplePrimeOutput (
  299. VOID
  300. )
  301. {
  302. double dbl;
  303. int i;
  304. int j;
  305. for (i = 1 ; i < 1200 ; i++) {
  306. for (j = 2 ; j < i ; j++) {
  307. dbl = (DOUBLE) i / (DOUBLE) j;
  308. if ((DOUBLE) ((INT) dbl) == dbl) {
  309. break; // not prime
  310. }
  311. }
  312. if (j >= i) {
  313. _tprintf (TEXT("Likely prime: %i\n"), i);
  314. }
  315. }
  316. }
  317. INT
  318. __cdecl
  319. _tmain (
  320. INT argc,
  321. TCHAR *argv[]
  322. )
  323. {
  324. g_hHeap = GetProcessHeap();
  325. g_hInst = GetModuleHandle (NULL);
  326. MigUtil_Entry (g_hInst, DLL_PROCESS_ATTACH, NULL);
  327. //pSimplePrimeOutput();
  328. //pTest1();
  329. //pTest2();
  330. //pTest3();
  331. pTest4();
  332. return 0;
  333. }