Leaked source code of windows server 2003
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.

722 lines
16 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: genenum.cpp
  7. //
  8. // Contents: implementation of CEnumeratorTest object
  9. // This is the object that does all of the testing.
  10. //
  11. // Classes:
  12. //
  13. // Functions:
  14. //
  15. // History: dd-mmm-yy Author Comment
  16. // 24-May-94 kennethm author
  17. //
  18. //--------------------------------------------------------------------------
  19. #include "headers.hxx"
  20. #pragma hdrstop
  21. //+-------------------------------------------------------------------------
  22. //
  23. // Member: CEnumeratorTest::CEnumeratorTest
  24. //
  25. // Synopsis: constructor
  26. //
  27. // Arguments: none
  28. //
  29. // Returns:
  30. //
  31. // History: dd-mmm-yy Author Comment
  32. // 24-May-94 kennethm author
  33. //
  34. // Notes:
  35. //
  36. //--------------------------------------------------------------------------
  37. CEnumeratorTest::CEnumeratorTest()
  38. {
  39. m_pEnumTest = NULL;
  40. m_ElementSize = 0;
  41. m_ElementCount = -1;
  42. }
  43. //+-------------------------------------------------------------------------
  44. //
  45. // Member: CEnumeratorTest::CEnumeratorTest
  46. //
  47. // Synopsis: constructor
  48. //
  49. // Arguments: [enumtest] -- The enumerator object to be tested
  50. // [elementsize] -- The size of one element from next
  51. // [elementcount] -- The number of elements expected to in
  52. // the enumerator. 0 if unknown.
  53. //
  54. // Returns:
  55. //
  56. // History: dd-mmm-yy Author Comment
  57. // 24-May-94 kennethm author
  58. //
  59. // Notes:
  60. //
  61. //--------------------------------------------------------------------------
  62. CEnumeratorTest::CEnumeratorTest(IGenEnum * enumtest, size_t elementsize, LONG elementcount)
  63. {
  64. m_pEnumTest = enumtest;
  65. m_ElementSize = elementsize;
  66. m_ElementCount = elementcount;
  67. }
  68. //+-------------------------------------------------------------------------
  69. //
  70. // Function: CEnumeratorTest::GetNext
  71. //
  72. // Synopsis: Internal Next Implementation. Does some basic checks on the
  73. // return values.
  74. //
  75. // Effects:
  76. //
  77. // Arguments: [celt] -- the number of items to fetch
  78. // [pceltFetched] -- the number of items fetched
  79. // [phresult] -- the return from next
  80. //
  81. // Requires:
  82. //
  83. // Returns: True if the basic tests passed, false if they didn't
  84. // The result of the next call itself is passed in param 3.
  85. //
  86. // Signals:
  87. //
  88. // Modifies:
  89. //
  90. // Algorithm: Checks:
  91. // That if s_ok is returned celt and pceltFetched are ==
  92. // If a verify is provided it is called
  93. //
  94. // History: dd-mmm-yy Author Comment
  95. // 24-May-94 kennethm author
  96. //
  97. // Notes:
  98. //
  99. //--------------------------------------------------------------------------
  100. BOOL CEnumeratorTest::GetNext( ULONG celt,
  101. ULONG* pceltFetched,
  102. HRESULT* phresult
  103. )
  104. {
  105. void* prgelt;
  106. ULONG ul;
  107. BOOL fRet = TRUE;
  108. //
  109. // Allocate memory for the return elements
  110. //
  111. prgelt = new char[m_ElementSize * celt];
  112. if (prgelt == NULL)
  113. {
  114. printf("IEnumX::GetNext out of memory.\r\n");
  115. return(FALSE);
  116. }
  117. //
  118. // Call next
  119. //
  120. *phresult = m_pEnumTest->Next(celt, prgelt, pceltFetched);
  121. //
  122. // If the return result is S_OK make sure the numbers match
  123. //
  124. if (*phresult == S_OK)
  125. {
  126. if ((pceltFetched) && (celt != *pceltFetched))
  127. {
  128. printf("IEnumX::Next returned S_OK but celt"
  129. " and pceltFetch mismatch.\r\n");
  130. fRet = FALSE;
  131. }
  132. }
  133. //
  134. // If false is returned then make sure celt is less than
  135. // the number actually fetched
  136. //
  137. if (*phresult == S_FALSE)
  138. {
  139. if ((pceltFetched) && (celt < *pceltFetched))
  140. {
  141. printf("IEnumX::Next return S_FALSE but celt is"
  142. " less than pceltFetch.\r\n");
  143. fRet = FALSE;
  144. }
  145. }
  146. //
  147. // Call verify to make sure the elements are ok.
  148. //
  149. if ((*phresult == S_OK) || (*phresult == S_FALSE))
  150. {
  151. //
  152. // If we got S_FALSE back set celt to the number of elements
  153. // returned in pceltFetched. If the user gave NULL for
  154. // pceltFetched and we got S_FALSE back then celt can only be
  155. // zero.
  156. //
  157. if (*phresult == S_FALSE)
  158. {
  159. if (pceltFetched)
  160. {
  161. celt = *pceltFetched;
  162. }
  163. else
  164. {
  165. celt = 0;
  166. }
  167. }
  168. //
  169. // loop through every returned element
  170. //
  171. for (ul=0; ul <= celt ; ul++)
  172. {
  173. if ((fRet == TRUE) &&
  174. (Verify(((char *)prgelt) + (ul * m_ElementSize)) == FALSE))
  175. {
  176. printf("Data element %d returned by IEnumX::Next is bad.\r\n", ul);
  177. fRet = FALSE;
  178. //
  179. // we keep looping anyway just to
  180. // free up resources.
  181. //
  182. }
  183. //
  184. // If the user supplied a cleanup function there is additional
  185. // memory that needs to be freed
  186. //
  187. // Math: cast prgelt to char* to it a one byte size and then scale
  188. // it by the index * the element size
  189. //
  190. Cleanup(((char *)prgelt) + (ul * m_ElementSize));
  191. }
  192. }
  193. delete prgelt;
  194. return fRet;
  195. }
  196. //+-------------------------------------------------------------------------
  197. //
  198. // Method: CEnumeratorTest::TestNext
  199. //
  200. // Synopsis: Test the next enumerator methods
  201. //
  202. // Effects:
  203. //
  204. // Arguments: None.
  205. //
  206. // Requires:
  207. //
  208. // Returns: HRESULT
  209. //
  210. // Signals:
  211. //
  212. // Modifies:
  213. //
  214. // Algorithm:
  215. //
  216. // History: dd-mmm-yy Author Comment
  217. // 24-May-94 kennethm author
  218. //
  219. // Notes: BUGBUG: This function should really be broken down into
  220. // smaller function.
  221. // Also, the return mechanism is unwieldy.
  222. //
  223. //--------------------------------------------------------------------------
  224. HRESULT CEnumeratorTest::TestNext(void)
  225. {
  226. ULONG celtFetched;
  227. LONG lInternalCount = 0;
  228. HRESULT hresult;
  229. ULONG i;
  230. void* prgelt;
  231. //
  232. // First we want to count the element by doing a next on each one.
  233. //
  234. do {
  235. if (!GetNext(1, &celtFetched, &hresult))
  236. {
  237. return(E_FAIL);
  238. }
  239. if (hresult == S_OK)
  240. {
  241. lInternalCount++;
  242. }
  243. } while ( hresult == S_OK );
  244. //
  245. // If the user passed in an amount make sure it matches what we got
  246. //
  247. if ((m_ElementCount != -1) && (lInternalCount != m_ElementCount))
  248. {
  249. printf("IEnumX: enumerated count and passed count do not match!\r\n");
  250. return(E_FAIL);
  251. }
  252. else if (m_ElementCount == -1)
  253. {
  254. //
  255. // If the user didn't pass in the element count let's set it here.
  256. //
  257. m_ElementCount = lInternalCount;
  258. }
  259. hresult = m_pEnumTest->Reset();
  260. if (hresult != S_OK)
  261. {
  262. printf("IEnumnX: Reset failed (%lx)\r\n", hresult );
  263. return(E_FAIL);
  264. }
  265. //
  266. // Make sure we fail on ...Next(celt>1, ...,NULL)
  267. //
  268. if (GetNext(2, NULL, &hresult))
  269. {
  270. if (SUCCEEDED(hresult))
  271. {
  272. printf("IEnumX: celt>1 pceltFetched==NULL returned success\r\n");
  273. return(E_FAIL);
  274. }
  275. }
  276. else
  277. {
  278. return(E_FAIL);
  279. }
  280. //
  281. // This next test will call next getting more each time
  282. //
  283. for (i = 1; i < (ULONG)m_ElementCount; i++)
  284. {
  285. hresult = m_pEnumTest->Reset();
  286. if (hresult != S_OK)
  287. {
  288. printf("IEnumnX: Reset failed (%lx)\r\n", hresult );
  289. return(E_FAIL);
  290. }
  291. if (!GetNext(i, &celtFetched, &hresult))
  292. {
  293. return(E_FAIL);
  294. }
  295. if ((hresult != S_OK) || (celtFetched != i))
  296. {
  297. printf("IEnumX: next/reset test failed!\r\n");
  298. return(E_FAIL);
  299. }
  300. }
  301. //
  302. // Now get more elements than we were supposed to
  303. // This should return S_FALSE with the max number in the number fetched
  304. //
  305. hresult = m_pEnumTest->Reset();
  306. if (hresult != S_OK)
  307. {
  308. printf("IEnumX: Reset failed (%lx)\r\n", hresult );
  309. return(E_FAIL);
  310. }
  311. if (!GetNext(m_ElementCount + 1, &celtFetched, &hresult))
  312. {
  313. return(E_FAIL);
  314. }
  315. if ((hresult != S_FALSE) || (lInternalCount != m_ElementCount))
  316. {
  317. printf("IEnumX: next/reset test failed!\r\n");
  318. return(E_FAIL);
  319. }
  320. //
  321. // Now verifyall. We do it here after the object has been worked on a bit
  322. // since it is more likely to fail at this point
  323. //
  324. hresult = m_pEnumTest->Reset();
  325. if (hresult != S_OK)
  326. {
  327. printf("IEnumX: Reset failed (%lx)\r\n", hresult );
  328. return(E_FAIL);
  329. }
  330. //
  331. // Allocate memory for the return elements
  332. //
  333. prgelt = new char[m_ElementSize * m_ElementCount];
  334. if (prgelt == NULL)
  335. {
  336. printf("IEnumX: verifyall new failed\r\n");
  337. return(E_OUTOFMEMORY);
  338. }
  339. hresult = m_pEnumTest->Next(m_ElementCount, prgelt, &celtFetched);
  340. if ((hresult != S_OK) || (celtFetched != (ULONG)m_ElementCount))
  341. {
  342. printf("IEnumX: verifyall test: next failed (%lx)\r\n", hresult );
  343. delete prgelt;
  344. return(E_FAIL);
  345. }
  346. if (VerifyAll(prgelt, m_ElementCount) == FALSE)
  347. {
  348. printf("IEnumX: verifyall failed (%lx)\r\n", hresult );
  349. delete prgelt;
  350. return(E_FAIL);
  351. }
  352. delete prgelt;
  353. return(S_OK);
  354. }
  355. //+-------------------------------------------------------------------------
  356. //
  357. // Method: CEnumeratorTest::TestSkip
  358. //
  359. // Synopsis: This function calls all the tests
  360. //
  361. // Effects:
  362. //
  363. // Arguments: None
  364. //
  365. // Requires:
  366. //
  367. // Returns: HRESULT
  368. //
  369. // Signals:
  370. //
  371. // Modifies:
  372. //
  373. // Algorithm:
  374. //
  375. // History: dd-mmm-yy Author Comment
  376. // 24-May-94 kennethm author
  377. //
  378. // Notes:
  379. //
  380. //--------------------------------------------------------------------------
  381. HRESULT CEnumeratorTest::TestSkip(void)
  382. {
  383. LONG i;
  384. HRESULT hresult;
  385. ULONG celtFetched;
  386. //
  387. // Make sure we call TestNext to set the element count
  388. //
  389. if (m_ElementCount == -1)
  390. {
  391. TestNext();
  392. }
  393. //
  394. // Call Skip, reset and try to get one element
  395. //
  396. for (i = 0; i < (LONG)m_ElementCount; i++)
  397. {
  398. hresult = m_pEnumTest->Reset();
  399. if (hresult != S_OK)
  400. {
  401. printf("IEnumnX: Reset failed (%lx)\r\n", hresult );
  402. return(E_FAIL);
  403. }
  404. hresult = m_pEnumTest->Skip(i);
  405. if (hresult != S_OK)
  406. {
  407. printf("IEnumnX: Skip failed (%lx)\r\n", hresult );
  408. return(E_FAIL);
  409. }
  410. //
  411. // Now one element to provide some check that the skip worked
  412. //
  413. if (!GetNext(1, &celtFetched, &hresult))
  414. {
  415. return(E_FAIL);
  416. }
  417. if (hresult != S_OK)
  418. {
  419. return(E_FAIL);
  420. }
  421. }
  422. //
  423. // Reset the enumerator before we leave
  424. //
  425. hresult = m_pEnumTest->Reset();
  426. if (hresult != S_OK)
  427. {
  428. printf("IEnumnX: Reset failed (%lx)\r\n", hresult );
  429. return(E_FAIL);
  430. }
  431. return(S_OK);
  432. }
  433. //+-------------------------------------------------------------------------
  434. //
  435. // Method: CEnumeratorTest::TestRelease
  436. //
  437. // Synopsis: This function calls all the tests
  438. //
  439. // Effects:
  440. //
  441. // Arguments: None
  442. //
  443. // Requires:
  444. //
  445. // Returns: HRESULT
  446. //
  447. // Signals:
  448. //
  449. // Modifies:
  450. //
  451. // Algorithm:
  452. //
  453. // History: dd-mmm-yy Author Comment
  454. // 24-May-94 kennethm author
  455. //
  456. // Notes:
  457. //
  458. //--------------------------------------------------------------------------
  459. HRESULT CEnumeratorTest::TestRelease(void)
  460. {
  461. return(S_OK);
  462. }
  463. //+-------------------------------------------------------------------------
  464. //
  465. // Method: CEnumeratorTest::TestClone
  466. //
  467. // Synopsis: This function calls all the tests
  468. //
  469. // Effects:
  470. //
  471. // Arguments: None
  472. //
  473. // Requires:
  474. //
  475. // Returns: HRESULT
  476. //
  477. // Signals:
  478. //
  479. // Modifies:
  480. //
  481. // Algorithm:
  482. //
  483. // History: dd-mmm-yy Author Comment
  484. // 24-May-94 kennethm author
  485. //
  486. // Notes:
  487. //
  488. //--------------------------------------------------------------------------
  489. HRESULT CEnumeratorTest::TestClone(void)
  490. {
  491. return(S_OK);
  492. }
  493. //+-------------------------------------------------------------------------
  494. //
  495. // Method: CEnumeratorTest::TestAll
  496. //
  497. // Synopsis: This function calls all the tests
  498. //
  499. // Effects:
  500. //
  501. // Arguments: None
  502. //
  503. // Requires:
  504. //
  505. // Returns: HRESULT
  506. //
  507. // Signals:
  508. //
  509. // Modifies:
  510. //
  511. // Algorithm:
  512. //
  513. // History: dd-mmm-yy Author Comment
  514. // 24-May-94 kennethm author
  515. //
  516. // Notes:
  517. //
  518. //--------------------------------------------------------------------------
  519. HRESULT CEnumeratorTest::TestAll(void)
  520. {
  521. HRESULT hresult;
  522. hresult = TestNext();
  523. if (hresult == S_OK)
  524. {
  525. hresult = TestSkip();
  526. }
  527. if (hresult == S_OK)
  528. {
  529. hresult = TestClone();
  530. }
  531. if (hresult == S_OK)
  532. {
  533. hresult = TestRelease();
  534. }
  535. return(hresult);
  536. }
  537. //+-------------------------------------------------------------------------
  538. //
  539. // Method: CEnumeratorTest::VerifyAll
  540. //
  541. // Synopsis: Verify entire array of returned results.
  542. //
  543. // Arguments: None
  544. //
  545. // Returns: BOOL
  546. //
  547. // Algorithm: Just default to saying everything is ok
  548. //
  549. // History: dd-mmm-yy Author Comment
  550. // 24-May-94 kennethm author
  551. //
  552. // Notes:
  553. //
  554. //--------------------------------------------------------------------------
  555. BOOL CEnumeratorTest::VerifyAll(void *pv, LONG cl)
  556. {
  557. return TRUE;
  558. }
  559. //+-------------------------------------------------------------------------
  560. //
  561. // Method: CEnumeratorTest::Verify
  562. //
  563. // Synopsis: Verify one element
  564. //
  565. // Arguments: None
  566. //
  567. // Returns: BOOL
  568. //
  569. // Algorithm: Just default to saying everything is ok
  570. //
  571. // History: dd-mmm-yy Author Comment
  572. // 24-May-94 kennethm author
  573. //
  574. // Notes:
  575. //
  576. //--------------------------------------------------------------------------
  577. BOOL CEnumeratorTest::Verify(void *pv)
  578. {
  579. return TRUE;
  580. }
  581. //+-------------------------------------------------------------------------
  582. //
  583. // Method: CEnumeratorTest::Cleanup
  584. //
  585. // Synopsis: Default implementation of cleanup
  586. //
  587. // Arguments: [pv] - pointer to entry enumerated
  588. //
  589. // Algorithm: If there is nothing special to free this implementation
  590. // can be used.
  591. //
  592. // History: dd-mmm-yy Author Comment
  593. // 24-May-94 kennethm author
  594. //
  595. //--------------------------------------------------------------------------
  596. void CEnumeratorTest::Cleanup(void *pv)
  597. {
  598. return;
  599. }