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.

436 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: memtest.cxx
  7. //
  8. // Contents: Memory allocation API unit test
  9. //
  10. // Functions:
  11. //
  12. // History: 13-Aug-93 CarlH Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "memtest.hxx"
  16. #pragma hdrstop
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. static DWORD g_grfGlobal = GLOBAL_RUN | GLOBAL_CLEANUP;
  20. static DWORD g_grfPreInit = 0;
  21. static DWORD g_grfMemory = 0;
  22. static DWORD g_grfMIDL = MIDL_AUTOGO | MIDL_AUTOEND;
  23. static DWORD g_grfCompat = 0;
  24. static BOOL g_fServer = FALSE;
  25. static BOOL ParseArguments(char *pszName, int cpsz, char **ppsz);
  26. static BOOL Initialize(void);
  27. static BOOL Uninitialize(void);
  28. static void PrintUsage(char *pszName);
  29. //+-------------------------------------------------------------------------
  30. //
  31. // Function: main, public
  32. //
  33. // Synopsis: Memory allocation test entry point
  34. //
  35. // Arguments: [argc] - count of arguments
  36. // [argv] - list of arguments
  37. //
  38. // Returns: Zero if successful, non-zero otherwise
  39. //
  40. // History: 19-May-93 CarlH Created
  41. //
  42. //--------------------------------------------------------------------------
  43. int _cdecl main(int argc, char *argv[])
  44. {
  45. DWORD grfOptions;
  46. BOOL fPassed;
  47. if (!(fPassed = ParseArguments(argv[0], argc - 1, argv + 1)))
  48. goto done;
  49. if (g_fServer)
  50. {
  51. if (!(fPassed = Initialize()))
  52. goto done;
  53. grfOptions = g_grfGlobal | g_grfMIDL;
  54. if (grfOptions & GLOBAL_RUN)
  55. {
  56. if (!(fPassed = TestMIDLServer(grfOptions)))
  57. goto done;
  58. }
  59. }
  60. else
  61. {
  62. // This test has to be run before initialization since that is
  63. // its point. It tests the memory allocation APIs functionality
  64. // before CoInitialize() is called.
  65. //
  66. grfOptions = g_grfGlobal | g_grfPreInit;
  67. if (grfOptions & GLOBAL_RUN)
  68. {
  69. if (!(fPassed = TestPreInit(grfOptions)))
  70. goto done;
  71. }
  72. if (!(fPassed = Initialize()))
  73. goto done;
  74. grfOptions = g_grfGlobal | g_grfMemory;
  75. if (grfOptions & GLOBAL_RUN)
  76. {
  77. if (!(fPassed = TestMemory(grfOptions)))
  78. goto done;
  79. }
  80. grfOptions = g_grfGlobal | g_grfMIDL;
  81. if (grfOptions & GLOBAL_RUN)
  82. {
  83. WCHAR wszServer[MAX_PATH + 1];
  84. mbstowcs(wszServer, argv[0], MAX_PATH);
  85. if (!(fPassed = TestMIDLClient(wszServer, grfOptions)))
  86. goto done;
  87. }
  88. #ifdef LINKED_COMPATIBLE
  89. grfOptions = g_grfGlobal | g_grfCompat;
  90. if (grfOptions & GLOBAL_RUN)
  91. {
  92. if (!(fPassed = TestCompatibility(grfOptions)))
  93. goto done;
  94. }
  95. #endif // LINKED_COMPATIBLE
  96. }
  97. if (!(fPassed = Uninitialize()))
  98. goto done;
  99. done:
  100. fprintf(stdout, "%s: %s\n", argv[0], fPassed ? "PASSED" : "FAILED");
  101. return (fPassed ? 0 : 1);
  102. }
  103. //+-------------------------------------------------------------------------
  104. //
  105. // Function: Initialize, public
  106. //
  107. // Synopsis: Global initialization routine
  108. //
  109. // Returns: TRUE if successful, FALSE otherwise
  110. //
  111. // History: 07-May-93 CarlH Created
  112. //
  113. //--------------------------------------------------------------------------
  114. BOOL Initialize(void)
  115. {
  116. return (SUCCEEDED(CoInitialize(NULL)));
  117. }
  118. //+-------------------------------------------------------------------------
  119. //
  120. // Function: Uninitialize, public
  121. //
  122. // Synopsis: Global clean-up routine
  123. //
  124. // Returns: TRUE if successful, FALSE otherwise
  125. //
  126. // History: 07-May-93 CarlH Created
  127. //
  128. //--------------------------------------------------------------------------
  129. BOOL Uninitialize(void)
  130. {
  131. CoUninitialize();
  132. return (TRUE);
  133. }
  134. //+-------------------------------------------------------------------------
  135. //
  136. // Function: PrintHeader, public
  137. //
  138. // Synopsis: Prints the header for a component's test
  139. //
  140. // Arguments: [pszComponent] - component test to print header for
  141. //
  142. // History: 28-Feb-93 CarlH Created
  143. //
  144. //--------------------------------------------------------------------------
  145. void PrintHeader(char const *pszComponent)
  146. {
  147. if (g_grfGlobal & GLOBAL_STATUS)
  148. {
  149. fprintf(stdout, "%s - running tests\n", pszComponent);
  150. }
  151. }
  152. //+-------------------------------------------------------------------------
  153. //
  154. // Function: PrintResult, public
  155. //
  156. // Synopsis: Prints the result of a component's test
  157. //
  158. // Arguments: [pszComponent] - component test to print result for
  159. //
  160. // History: 28-Feb-93 CarlH Created
  161. //
  162. //--------------------------------------------------------------------------
  163. void PrintResult(char const *pszComponent, BOOL fPassed)
  164. {
  165. if (g_grfGlobal & GLOBAL_STATUS)
  166. {
  167. fprintf(
  168. stdout,
  169. "%s - tests %s\n",
  170. pszComponent,
  171. fPassed ? "passed" : "failed");
  172. }
  173. }
  174. //+-------------------------------------------------------------------------
  175. //
  176. // Function: PrintTrace, public
  177. //
  178. // Synopsis: Prints a trace message if verbose mode on
  179. //
  180. // Arguments: [pszComponent] - component name issuing trace
  181. // [pszFormat] - format string
  182. // [...] - arguments for format string
  183. //
  184. // History: 24-Feb-93 CarlH Created
  185. //
  186. //--------------------------------------------------------------------------
  187. void PrintTrace(char const *pszComponent, char const *pszFormat, ...)
  188. {
  189. if (g_grfGlobal & GLOBAL_VERBOSE)
  190. {
  191. va_list va;
  192. fprintf(stdout, "trace: %s - ", pszComponent);
  193. va_start(va, pszFormat);
  194. vfprintf(stdout, pszFormat, va);
  195. va_end(va);
  196. }
  197. }
  198. //+-------------------------------------------------------------------------
  199. //
  200. // Function: PrintError, public
  201. //
  202. // Synopsis: Prints an error message
  203. //
  204. // Arguments: [pszComponent] - component name issuing trace
  205. // [pszFormat] - format string
  206. // [...] - arguments for format string
  207. //
  208. // History: 24-Feb-93 CarlH Created
  209. //
  210. //--------------------------------------------------------------------------
  211. void PrintError(char const *pszComponent, char const *pszFormat, ...)
  212. {
  213. va_list va;
  214. fprintf(stderr, "error: %s - ", pszComponent);
  215. va_start(va, pszFormat);
  216. vfprintf(stderr, pszFormat, va);
  217. va_end(va);
  218. }
  219. //+-------------------------------------------------------------------------
  220. //
  221. // Function: PrintUsage, private
  222. //
  223. // Synopsis: Prints a the usage message for this test
  224. //
  225. // Arguments: [pszName] - name of the executable
  226. //
  227. // History: 24-Feb-93 CarlH Created
  228. //
  229. //--------------------------------------------------------------------------
  230. void PrintUsage(char *pszName)
  231. {
  232. fprintf(stdout, "Usage: %s {<flag>|<comp>}*\n", pszName);
  233. fprintf(stdout, "Where: <flag> = {+|-}{?dgecsv}+\n");
  234. fprintf(stdout, " <comp> = {preinit|memory|compat|midl}\n");
  235. fprintf(stdout, " ? - displays this message\n");
  236. fprintf(stdout, " d - turns MIDL debugging on/OFF\n");
  237. fprintf(stdout, " g - turns MIDL debugging auto go ON/off\n");
  238. fprintf(stdout, " e - turns MIDL debugging auto end ON/off\n");
  239. fprintf(stdout, " c - turns cleanup ON/off\n");
  240. fprintf(stdout, " s - turns status messages on/OFF\n");
  241. fprintf(stdout, " v - turns verbosity on/OFF\n");
  242. }
  243. //+-------------------------------------------------------------------------
  244. //
  245. // Function: ParseArguments, private
  246. //
  247. // Synopsis: Parses command line arguments
  248. //
  249. // Arguments: [pszName] - name of executable
  250. // [cpsz] - number of strings in [ppsz]
  251. // [ppsz] - array of command line arguments
  252. //
  253. // Returns: TRUE if successfull, FALSE otherwise
  254. //
  255. // History: 24-Feb-93 CarlH Created
  256. //
  257. //--------------------------------------------------------------------------
  258. BOOL ParseArguments(char *pszName, int cpsz, char **ppsz)
  259. {
  260. BOOL fOK = TRUE;
  261. // As long as we haven't encountered an error, we want to loop
  262. // through all of the arguments, excluding the first, which is
  263. // the name of the program (argv[0]).
  264. //
  265. for (int ipsz = 0; fOK && (ipsz < cpsz); ipsz++)
  266. {
  267. // If the first character of the argument is a plus or minus,
  268. // this argument must be a series of flags.
  269. //
  270. if ((ppsz[ipsz][0] == '+') || (ppsz[ipsz][0] == '-'))
  271. {
  272. BOOL fFlag = (ppsz[ipsz][0] == '+');
  273. // We want to check the rest of the characters in the
  274. // argument.
  275. //
  276. for (int ich = 1; fOK && (ppsz[ipsz][ich] != '\0'); ich++)
  277. {
  278. switch (ppsz[ipsz][ich])
  279. {
  280. case '?':
  281. // User is requesting help, so print the usage
  282. // message and stop parsing.
  283. //
  284. PrintUsage(pszName);
  285. fOK = FALSE;
  286. break;
  287. case 'D':
  288. case 'd':
  289. g_grfMIDL = (fFlag ?
  290. g_grfMIDL | MIDL_DEBUG :
  291. g_grfMIDL & ~MIDL_DEBUG);
  292. break;
  293. case 'G':
  294. case 'g':
  295. g_grfMIDL = (fFlag ?
  296. g_grfMIDL | MIDL_AUTOGO :
  297. g_grfMIDL & ~MIDL_AUTOGO);
  298. break;
  299. case 'E':
  300. case 'e':
  301. g_grfMIDL = (fFlag ?
  302. g_grfMIDL | MIDL_AUTOEND :
  303. g_grfMIDL & ~MIDL_AUTOEND);
  304. break;
  305. case 'C':
  306. case 'c':
  307. // Turn test cleanup on or off depending on the
  308. // first character of this argument.
  309. //
  310. g_grfGlobal = (fFlag ?
  311. g_grfGlobal | GLOBAL_CLEANUP :
  312. g_grfGlobal & ~GLOBAL_CLEANUP);
  313. break;
  314. case 'S':
  315. case 's':
  316. // Turn status messages on or off depending on the
  317. // first character of this argument.
  318. //
  319. g_grfGlobal = (fFlag ?
  320. g_grfGlobal | GLOBAL_STATUS :
  321. g_grfGlobal & ~GLOBAL_STATUS);
  322. break;
  323. case 'V':
  324. case 'v':
  325. // Turn verbose mode on or off depending on the
  326. // first character of this argument.
  327. //
  328. g_grfGlobal = (fFlag ?
  329. g_grfGlobal | GLOBAL_VERBOSE :
  330. g_grfGlobal & ~GLOBAL_VERBOSE);
  331. break;
  332. default:
  333. // We don't know what this is, so tell
  334. // the user and stop parsing.
  335. //
  336. PrintError(
  337. pszName,
  338. "unrecognized switch '%c'\n",
  339. ppsz[ipsz][ich]);
  340. fOK = FALSE;
  341. break;
  342. }
  343. }
  344. }
  345. else
  346. if (stricmp(ppsz[ipsz], "preinit") == 0)
  347. {
  348. g_grfGlobal &= ~GLOBAL_RUN;
  349. g_grfPreInit |= GLOBAL_RUN;
  350. }
  351. else
  352. if (stricmp(ppsz[ipsz], "memory") == 0)
  353. {
  354. g_grfGlobal &= ~GLOBAL_RUN;
  355. g_grfMemory |= GLOBAL_RUN;
  356. }
  357. else
  358. if (stricmp(ppsz[ipsz], "compat") == 0)
  359. {
  360. g_grfGlobal &= ~GLOBAL_RUN;
  361. g_grfCompat |= GLOBAL_RUN;
  362. }
  363. else
  364. if (stricmp(ppsz[ipsz], "midl") == 0)
  365. {
  366. g_grfGlobal &= ~GLOBAL_RUN;
  367. g_grfMIDL |= GLOBAL_RUN;
  368. }
  369. else
  370. if (stricmp(ppsz[ipsz], "midlserver") == 0)
  371. {
  372. g_fServer = TRUE;
  373. }
  374. else
  375. {
  376. PrintError(
  377. pszName,
  378. "unrecognized argument \"%s\"\n",
  379. ppsz[ipsz]);
  380. fOK = FALSE;
  381. }
  382. }
  383. return (fOK);
  384. }