Adding rostests as part of the tree restructure
[reactos.git] / rostests / tests / global_mem / global_mem.c
1 /* File: global_mem.c
2 **
3 ** This program is a test application used for testing correctness of the
4 ** GlobalXXX memory API implementation.
5 **
6 ** Programmer: Mark Tempel
7 */
8 #include <windows.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 /*
13 ** All output is line wrapped to fit a 80 column screen.
14 ** these defines control that formatting. To shrink the
15 ** screen columns, just change the DISPLAY_COMUMNS macro.
16 */
17 #define DISPLAY_COLUMNS 78
18 #define LINE_BUFFER_SIZE DISPLAY_COLUMNS + 2
19
20 /*
21 ** This define controls the size of a memory allocation request.
22 ** For this test suite to really be comprehensive, we should
23 ** probably be testing many different block sizes.
24 */
25 #define MEM_BLOCK_SIZE 0x80000
26
27 /*
28 ** This enumeration is really the return value for the program.
29 ** All test return a TestStatus and test statuses can be combined
30 ** with the relation TEST_CombineStatus.
31 */
32 typedef enum TestStatus
33 {
34 FAILED = 0,
35 PASSED = 1,
36 SKIPPED = -1
37 } TEST_STATUS;
38
39 /*---------------------------------------------------------------------------
40 ** This is a relation used to combine two test statuses.
41 ** The combine rules are as follows:
42 ** FAIL & Anything == FAIL
43 ** SKIPPED & Anything == Anything
44 **
45 */
46 TEST_STATUS TEST_CombineStatus(TEST_STATUS a, TEST_STATUS b)
47 {
48 TEST_STATUS result = a;
49
50 switch (a)
51 {
52 case PASSED: result = (PASSED == b || SKIPPED == b) ? (PASSED) : (FAILED); break;
53 case FAILED: result = FAILED; break;
54 case SKIPPED: result = b; break;
55 }
56
57 return result;
58 }
59
60 /*---------------------------------------------------------------------------
61 ** This outputs the banner border lines.
62 */
63 void OUTPUT_BannerLine()
64 {
65 int c = 0;
66 printf("+");
67 for (c = 1; c < DISPLAY_COLUMNS; c++)
68 {
69 printf("-");
70 }
71 printf("\n");
72 }
73
74
75 /*---------------------------------------------------------------------------
76 ** This method prints a line that has a | on the left, and is line wrapped
77 ** to be no more that DISPLAY_COLUMNS +2 wide.
78 */
79 void OUTPUT_Line(const char *szLine)
80 {
81 int spaceIndex = 0;
82 char output[LINE_BUFFER_SIZE];
83
84 memset(output, 0, DISPLAY_COLUMNS + 2);
85
86 /*If this line is longer than DISPLAY_COLUMNS,
87 * break it at the first space.
88 */
89 if (DISPLAY_COLUMNS - 2 < strlen(szLine))
90 {
91 for (spaceIndex = DISPLAY_COLUMNS / 2; spaceIndex < DISPLAY_COLUMNS - 2; spaceIndex++)
92 {
93 if (' ' == szLine[spaceIndex])
94 {
95 break;
96 }
97 }
98
99 memcpy(output + 2, szLine, spaceIndex + 1);
100 output[0] = '|';
101 output[1] = ' ';
102 output[strlen(output)] = '\n';
103 printf(output);
104
105 OUTPUT_Line(szLine + spaceIndex + 1);
106 }
107 else
108 {
109 sprintf(output,"| %s\n", szLine);
110 printf(output);
111 }
112
113 }
114
115 /*---------------------------------------------------------------------------
116 **
117 */
118 void OUTPUT_Banner(const char *szBanner)
119 {
120 OUTPUT_BannerLine();
121 OUTPUT_Line(szBanner);
122 OUTPUT_BannerLine();
123 }
124
125 /*---------------------------------------------------------------------------
126 **
127 */
128 void OUTPUT_Result(TEST_STATUS status)
129 {
130 switch (status)
131 {
132 case PASSED: OUTPUT_Line("==> PASSED"); break;
133 case FAILED: OUTPUT_Line("*** FAILED"); break;
134 case SKIPPED: OUTPUT_Line("==> SKIPPED"); break;
135 }
136 OUTPUT_Line("");
137 }
138
139 /*---------------------------------------------------------------------------
140 **
141 */
142 void OUTPUT_HexDword(DWORD dw)
143 {
144 char buffer[32];
145 sprintf(buffer, "0x%lX",dw);
146 OUTPUT_Line(buffer);
147 }
148
149 /*---------------------------------------------------------------------------
150 **
151 */
152 void OutputAllocFlags(UINT pFlags)
153 {
154 if (pFlags & GMEM_MOVEABLE)
155 {
156 OUTPUT_Line("Movable Memory");
157 }
158 else
159 {
160 OUTPUT_Line("Fixed Memory");
161 }
162
163 if (pFlags & GMEM_ZEROINIT)
164 {
165 OUTPUT_Line("Zero Initialized Memory");
166 }
167 }
168
169 /*---------------------------------------------------------------------------
170 **
171 */
172 void OutputErrorCode()
173 {
174 char buffer[256];
175
176 sprintf(buffer,"GetLastError() returned %lu", GetLastError());
177
178 OUTPUT_Line(buffer);
179 }
180 /*---------------------------------------------------------------------------
181 **
182 */
183 TEST_STATUS TEST_MemoryWrite(LPVOID mem, DWORD cbSize)
184 {
185 TEST_STATUS result = FAILED;
186
187 if (0 == IsBadWritePtr(mem, cbSize))
188 {
189 result = PASSED;
190 }
191 return result;
192 }
193
194 /*---------------------------------------------------------------------------
195 **
196 */
197 TEST_STATUS TEST_MemoryRead(LPVOID mem, DWORD cbSize)
198 {
199 TEST_STATUS result = FAILED;
200
201 if (0 == IsBadReadPtr(mem, cbSize))
202 {
203 result = PASSED;
204 }
205 return result;
206 }
207
208 /*---------------------------------------------------------------------------
209 ** This method tests to see if a block of global memory is movable
210 ** by seeing if the value returned from GlobalLock is different from
211 ** the passed in value.
212 */
213 int IsMovable(HGLOBAL hMem)
214 {
215 LPVOID pMem = 0;
216 int rc = 0;
217
218 pMem = GlobalLock(hMem);
219 if (pMem != hMem)
220 {
221 rc = 1;
222 }
223 GlobalUnlock(hMem);
224
225 return rc;
226 }
227
228 /*---------------------------------------------------------------------------
229 **
230 */
231 TEST_STATUS TestGlobalAllocNFree(UINT allocFlags)
232 {
233 TEST_STATUS status = SKIPPED;
234 HGLOBAL hTest = 0;
235 OUTPUT_Banner("Testing the GlobalAlloc and GlobalFree calls");
236 OUTPUT_Line("Allocate a buffer");
237
238 OutputAllocFlags(allocFlags);
239
240 status = FAILED;
241 hTest = GlobalAlloc(allocFlags, MEM_BLOCK_SIZE);
242 if (0 != hTest)
243 {
244 if (0 == GlobalFree(hTest));
245 {
246 status = PASSED;
247 }
248 }
249
250 OUTPUT_Line("Result for this run:");
251 OUTPUT_Result(status);
252 OUTPUT_Line("");
253
254 return status;
255 }
256
257 /*---------------------------------------------------------------------------
258 **
259 */
260 TEST_STATUS TestGlobalLockNUnlock(UINT allocFlags)
261 {
262 HGLOBAL hMem = 0;
263 LPVOID pMem = 0;
264 TEST_STATUS subtest = SKIPPED;
265 TEST_STATUS result = FAILED;
266
267 OUTPUT_Banner("Testing the GlobalLock/Unlock functions.");
268 OutputAllocFlags(allocFlags);
269 OUTPUT_Line("");
270
271 hMem = GlobalAlloc(allocFlags, MEM_BLOCK_SIZE);
272 if (0 != hMem)
273 {
274 OUTPUT_Line("Allocated a memory block");
275
276 OUTPUT_Line("Testing Lock");
277 pMem = GlobalLock(hMem);
278 if (0 != pMem)
279 {
280 OUTPUT_Result(PASSED);
281
282 OUTPUT_Line("Testing memory for read.");
283 subtest = TEST_MemoryRead(pMem, MEM_BLOCK_SIZE);
284 OUTPUT_Result(subtest);
285 result = TEST_CombineStatus(PASSED, subtest);
286
287
288 OUTPUT_Line("Testing memory for write.");
289 subtest = TEST_MemoryRead(pMem, MEM_BLOCK_SIZE);
290 OUTPUT_Result(subtest);
291 result = TEST_CombineStatus(result, subtest);
292
293
294 OUTPUT_Line("Unlocking memory");
295 if (GlobalUnlock(hMem))
296 {
297 OUTPUT_Result(PASSED);
298 result = TEST_CombineStatus(result, PASSED);
299 }
300 else
301 {
302 if (NO_ERROR == GetLastError())
303 {
304 OUTPUT_Result(PASSED);
305 result = TEST_CombineStatus(result, PASSED);
306 }
307 else
308 {
309 OutputErrorCode();
310 OUTPUT_Result(FAILED);
311 result = TEST_CombineStatus(result, FAILED);
312 }
313 }
314 }
315
316 OUTPUT_Line("Freeing memory");
317 if (0 == GlobalFree(hMem))
318 {
319 OUTPUT_Result(PASSED);
320 result = TEST_CombineStatus(result, PASSED);
321 }
322 else
323 {
324 OutputErrorCode();
325 OUTPUT_Result(FAILED);
326 result = TEST_CombineStatus(result, FAILED);
327 }
328 }
329 return result;
330 }
331
332 /*---------------------------------------------------------------------------
333 **
334 */
335 TEST_STATUS TestGlobalReAllocFixed()
336 {
337 HGLOBAL hMem = 0;
338 HGLOBAL hReAlloced = 0;
339 LPVOID pMem = 0;
340 TEST_STATUS subtest = SKIPPED;
341 TEST_STATUS result = SKIPPED;
342
343 OUTPUT_Line("Testing GlobalReAlloc() on memory allocated as GMEM_FIXED");
344
345 /* Case 1: convert a fixed block to a movable block. */
346 OUTPUT_Line("Allocating buffer");
347 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
348 if (0 != hMem)
349 {
350 OUTPUT_Line("Testing to see if this is converted into a movable block");
351 hReAlloced = GlobalReAlloc(hMem, MEM_BLOCK_SIZE + 100, GMEM_MOVEABLE | GMEM_MODIFY);
352 if (0 == hReAlloced)
353 {
354 OUTPUT_Line("GlobalReAlloc failed-- returned NULL");
355 subtest = TEST_CombineStatus(subtest, FAILED);
356 OUTPUT_Result(subtest);
357 }
358 else
359 {
360 hMem = hReAlloced;
361 if (0 == IsMovable(hMem))
362 {
363 OUTPUT_Line("GlobalReAlloc returned a fixed pointer.");
364 subtest = TEST_CombineStatus(subtest, FAILED);
365 OUTPUT_Result(subtest);
366 }
367 else
368 {
369 pMem = GlobalLock(hMem);
370 subtest = TEST_CombineStatus(subtest, PASSED);
371 subtest = TEST_CombineStatus(subtest, TEST_MemoryRead(pMem, MEM_BLOCK_SIZE + 100));
372 if (FAILED == subtest)
373 {
374 OUTPUT_Line("Memory Read failed.");
375 }
376 subtest = TEST_CombineStatus(subtest, TEST_MemoryWrite(pMem, MEM_BLOCK_SIZE + 100));
377 if (FAILED == subtest)
378 {
379 OUTPUT_Line("Memory Write failed.");
380 }
381 GlobalUnlock(hMem);
382 }
383 }
384
385 GlobalFree(hMem);
386 }
387 else
388 {
389 OUTPUT_Line("Global Alloc Failed.");
390 subtest = TEST_CombineStatus(subtest, FAILED);
391 }
392 OUTPUT_Line("Subtest result:");
393 OUTPUT_Result(subtest);
394 OUTPUT_Line("");
395
396 result = TEST_CombineStatus(result, subtest);
397 subtest = SKIPPED;
398
399 /* case 2 only move a fixed block */
400 OUTPUT_Line("Allocating buffer");
401 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
402 if (0 != hMem)
403 {
404 OUTPUT_Line("Testing to see if a new fixed block is returned.");
405 hReAlloced = GlobalReAlloc(hMem, MEM_BLOCK_SIZE - 100, GMEM_MOVEABLE);
406 if (0 == hReAlloced)
407 {
408 OUTPUT_Line("GlobalReAlloc failed-- returned NULL");
409 subtest = TEST_CombineStatus(subtest, FAILED);
410 OUTPUT_Result(subtest);
411 }
412 else
413 {
414 OUTPUT_Line("Alloced Handle: ");
415 OUTPUT_HexDword((DWORD)hMem);
416 OUTPUT_Line("ReAlloced Handle: ");
417 OUTPUT_HexDword((DWORD)hReAlloced);
418 if (hMem == hReAlloced)
419 {
420 OUTPUT_Line("GlobalReAlloc returned the same pointer. The documentation states that this is wrong, but Windows NT works this way.");
421 }
422
423 hMem = hReAlloced;
424 subtest = TEST_CombineStatus(subtest, PASSED);
425 subtest = TEST_CombineStatus(subtest, TEST_MemoryRead((LPVOID)hMem, MEM_BLOCK_SIZE - 100));
426 subtest = TEST_CombineStatus(subtest, TEST_MemoryWrite((LPVOID)hMem, MEM_BLOCK_SIZE - 100));
427 }
428
429 GlobalFree(hMem);
430 }
431 else
432 {
433 subtest = TEST_CombineStatus(subtest, FAILED);
434 }
435 OUTPUT_Line("Subtest result:");
436 OUTPUT_Result(subtest);
437 OUTPUT_Line("");
438
439 result = TEST_CombineStatus(result, subtest);
440 subtest = SKIPPED;
441
442 /* Case 3: re-allocate a fixed block in place */
443 OUTPUT_Line("Allocating buffer");
444 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
445 if (0 != hMem)
446 {
447 OUTPUT_Line("Testing to see if a fixed pointer is reallocated in place.");
448 hReAlloced = GlobalReAlloc(hMem, MEM_BLOCK_SIZE - 100, 0);
449 if (0 == hReAlloced)
450 {
451 OUTPUT_Line("GlobalReAlloc failed-- returned NULL");
452 subtest = TEST_CombineStatus(subtest, FAILED);
453 OUTPUT_Result(subtest);
454 }
455 else
456 {
457 OUTPUT_Line("Alloced Handle: ");
458 OUTPUT_HexDword((DWORD)hMem);
459 OUTPUT_Line("ReAlloced Handle: ");
460 OUTPUT_HexDword((DWORD)hReAlloced);
461 if (hMem != hReAlloced)
462 {
463 OUTPUT_Line("GlobalReAlloc returned a different.");
464 subtest = TEST_CombineStatus(subtest, FAILED);
465 OUTPUT_Result(subtest);
466 }
467 else
468 {
469 subtest = TEST_CombineStatus(subtest, PASSED);
470 subtest = TEST_CombineStatus(subtest, TEST_MemoryRead((LPVOID)hMem, MEM_BLOCK_SIZE - 100));
471 subtest = TEST_CombineStatus(subtest, TEST_MemoryWrite((LPVOID)hMem, MEM_BLOCK_SIZE - 100));
472 }
473 }
474
475 GlobalFree(hMem);
476 }
477 else
478 {
479 subtest = TEST_CombineStatus(subtest, FAILED);
480 }
481 OUTPUT_Line("Subtest result:");
482 OUTPUT_Result(subtest);
483 OUTPUT_Line("");
484
485 result = TEST_CombineStatus(result, subtest);
486
487 OUTPUT_Line("");
488 return result;
489 }
490 /*---------------------------------------------------------------------------
491 **
492 */
493 TEST_STATUS TestGlobalReAllocMovable()
494 {
495 HGLOBAL hMem = 0;
496 HGLOBAL hReAlloced = 0;
497 LPVOID pMem = 0;
498 TEST_STATUS subtest = SKIPPED;
499 TEST_STATUS result = SKIPPED;
500
501 OUTPUT_Line("Testing GlobalReAlloc() on memory allocated as GMGM_MOVEABLE");
502
503 /* case 1 test reallocing a movable block that is unlocked. */
504 OUTPUT_Line("Allocating buffer");
505 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
506 if (0 != hMem)
507 {
508 OUTPUT_Line("Testing GlobalReAlloc on a unlocked block.");
509 hReAlloced = GlobalReAlloc(hMem, MEM_BLOCK_SIZE - 100, 0);
510 if (0 == hReAlloced)
511 {
512 OUTPUT_Line("GlobalReAlloc failed-- returned NULL");
513 subtest = TEST_CombineStatus(subtest, FAILED);
514 OUTPUT_Result(subtest);
515 }
516 else
517 {
518 OUTPUT_Line("Alloced Handle: ");
519 OUTPUT_HexDword((DWORD)hMem);
520 OUTPUT_Line("ReAlloced Handle: ");
521 OUTPUT_HexDword((DWORD)hReAlloced);
522
523 pMem = GlobalLock(hReAlloced);
524 hMem = hReAlloced;
525 subtest = TEST_CombineStatus(subtest, PASSED);
526 subtest = TEST_CombineStatus(subtest, TEST_MemoryRead(pMem, MEM_BLOCK_SIZE - 100));
527 subtest = TEST_CombineStatus(subtest, TEST_MemoryWrite(pMem, MEM_BLOCK_SIZE - 100));
528 GlobalUnlock(hReAlloced);
529 }
530
531 GlobalFree(hMem);
532 }
533 else
534 {
535 subtest = TEST_CombineStatus(subtest, FAILED);
536 }
537 OUTPUT_Line("Subtest result:");
538 OUTPUT_Result(subtest);
539 OUTPUT_Line("");
540
541 result = TEST_CombineStatus(result, subtest);
542 subtest = SKIPPED;
543
544 /* Case 2: re-allocate a moveable block that is locked */
545 OUTPUT_Line("Allocating buffer");
546 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
547 if (0 != hMem)
548 {
549
550 OUTPUT_Line("Testing GlobalReAlloc on a locked block.");
551 pMem = GlobalLock(hMem);
552 hReAlloced = GlobalReAlloc(hMem, MEM_BLOCK_SIZE - 100, 0);
553 if (0 == hReAlloced)
554 {
555 OUTPUT_Line("GlobalReAlloc failed-- returned NULL");
556 subtest = TEST_CombineStatus(subtest, FAILED);
557 OUTPUT_Result(subtest);
558 }
559 else
560 {
561 OUTPUT_Line("Alloced Handle: ");
562 OUTPUT_HexDword((DWORD)hMem);
563 OUTPUT_Line("ReAlloced Handle: ");
564 OUTPUT_HexDword((DWORD)hReAlloced);
565 if (hMem != hReAlloced)
566 {
567 OUTPUT_Line("GlobalReAlloc returned a different block.");
568 }
569 pMem = GlobalLock(hReAlloced);
570 subtest = TEST_CombineStatus(subtest, PASSED);
571 subtest = TEST_CombineStatus(subtest, TEST_MemoryRead(pMem, MEM_BLOCK_SIZE - 100));
572 subtest = TEST_CombineStatus(subtest, TEST_MemoryWrite(pMem , MEM_BLOCK_SIZE - 100));
573 GlobalUnlock(hReAlloced);
574
575 }
576
577 GlobalUnlock(hMem);
578
579 GlobalFree(hMem);
580 }
581 else
582 {
583 subtest = TEST_CombineStatus(subtest, FAILED);
584 }
585 OUTPUT_Line("Subtest result:");
586 OUTPUT_Result(subtest);
587 OUTPUT_Line("");
588
589 result = TEST_CombineStatus(result, subtest);
590
591 OUTPUT_Line("");
592 return result;
593 }
594
595 /*---------------------------------------------------------------------------
596 **
597 */
598 TEST_STATUS TestGlobalReAlloc()
599 {
600 OUTPUT_Banner("Testing GlobalReAlloc()");
601 TEST_STATUS result = SKIPPED;
602
603 result = TEST_CombineStatus(result, TestGlobalReAllocFixed());
604 result = TEST_CombineStatus(result, TestGlobalReAllocMovable());
605
606 OUTPUT_Line("GlobalReAlloc test result:");
607 OUTPUT_Result(result);
608 return result;
609 }
610
611 /*---------------------------------------------------------------------------
612 **
613 */
614 TEST_STATUS TestGlobalFlagsMoveable()
615 {
616 HGLOBAL hMem = 0;
617 UINT uFlags = 0;
618 TEST_STATUS result = SKIPPED;
619
620 OUTPUT_Line("Test for the proper lock count");
621
622 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
623 if (0 != hMem)
624 {
625
626 OUTPUT_Line("Testing initial allocation");
627
628 OUTPUT_Line("Testing for a lock of 0");
629 uFlags = GlobalFlags(hMem);
630 if (((GMEM_LOCKCOUNT & uFlags) == 0)) /*no locks*/
631 {
632 result = TEST_CombineStatus(result, PASSED);
633 }
634 else
635 {
636 result = TEST_CombineStatus(result, FAILED);
637 }
638 OUTPUT_Result(result);
639
640 OUTPUT_Line("Pointer from handle: ");
641 OUTPUT_HexDword((DWORD)GlobalLock(hMem));
642
643 OUTPUT_Line("Testing after a lock");
644 OUTPUT_Line("Testing for a lock of 1");
645 uFlags = GlobalFlags(hMem);
646 if (((GMEM_LOCKCOUNT & uFlags) == 1)) /*no locks*/
647 {
648 result = TEST_CombineStatus(result, PASSED);
649 }
650 else
651 {
652 result = TEST_CombineStatus(result, FAILED);
653 }
654 OUTPUT_Result(result);
655
656 GlobalUnlock(hMem);
657 OUTPUT_Line("Testing after an unlock");
658 OUTPUT_Line("Testing for a lock of 0");
659 uFlags = GlobalFlags(hMem);
660 if (((GMEM_LOCKCOUNT & uFlags) == 0)) /*no locks*/
661 {
662 result = TEST_CombineStatus(result, PASSED);
663 }
664 else
665 {
666 result = TEST_CombineStatus(result, FAILED);
667 }
668 OUTPUT_Result(result);
669 }
670 else
671 {
672 OUTPUT_Line("GlobalAlloc failed!");
673 result = TEST_CombineStatus(result, FAILED);
674 }
675
676 OUTPUT_Line("Test for discarded memory");
677 OUTPUT_Line("Allocating an empty moveable block---automatically marked as discarded");
678 hMem = GlobalAlloc(GMEM_MOVEABLE, 0); /*allocate a discarded block*/
679 if (0 != hMem)
680 {
681 OUTPUT_Line("Allocation handle: ");
682 OUTPUT_HexDword((DWORD)hMem);
683 OUTPUT_Line("Testing for a discarded flag");
684 uFlags = GlobalFlags(hMem);
685 if (0 != (uFlags & GMEM_DISCARDED)) /*discarded*/
686 {
687 result = TEST_CombineStatus(result, PASSED);
688 }
689 else
690 {
691 result = TEST_CombineStatus(result, FAILED);
692 }
693 OUTPUT_Line("Flags:");
694 OUTPUT_HexDword(uFlags);
695 OUTPUT_Result(result);
696
697 GlobalFree(hMem);
698 }
699 else
700 {
701 OUTPUT_Line("GlobalAlloc failed!");
702 result = TEST_CombineStatus(result, FAILED);
703 }
704 return result;
705 }
706
707
708 /*---------------------------------------------------------------------------
709 **
710 */
711 TEST_STATUS TestGlobalFlagsFixed()
712 {
713 HGLOBAL hMem = 0;
714 UINT uFlags = 0;
715 TEST_STATUS result = SKIPPED;
716
717 OUTPUT_Line("Testing for correct handling of GMEM_FIXED memory");
718 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
719 if (0 != hMem)
720 {
721
722 OUTPUT_Line("Allocation handle: ");
723 OUTPUT_HexDword((DWORD)hMem);
724
725 OUTPUT_Line("Testing initial allocation");
726 OUTPUT_Line("Testing for non-discarded and lock of 0");
727 uFlags = GlobalFlags(hMem);
728 if (((GMEM_LOCKCOUNT & uFlags) == 0) && /*no locks*/
729 (((uFlags >> 8) & 0xff) == 0 )) /*not discarded*/
730 {
731 result = TEST_CombineStatus(result, PASSED);
732 }
733 else
734 {
735 result = TEST_CombineStatus(result, FAILED);
736 }
737 OUTPUT_Result(result);
738
739 OUTPUT_Line("Pointer from handle: ");
740 OUTPUT_HexDword((DWORD)GlobalLock(hMem));
741 OUTPUT_Line("Testing after a lock");
742 OUTPUT_Line("Testing for non-discarded and lock of 0");
743 uFlags = GlobalFlags(hMem);
744 if (((GMEM_LOCKCOUNT & uFlags) == 0) && /*no locks*/
745 (((uFlags >> 8) & 0xff) == 0 )) /*not discarded*/
746 {
747 result = TEST_CombineStatus(result, PASSED);
748 }
749 else
750 {
751 result = TEST_CombineStatus(result, FAILED);
752 }
753 OUTPUT_Result(result);
754
755 GlobalFree(hMem);
756 }
757 else
758 {
759 OUTPUT_Line("GlobalAlloc failed!");
760 result = TEST_CombineStatus(result, FAILED);
761 }
762
763 return result;
764 }
765 /*---------------------------------------------------------------------------
766 **
767 */
768 TEST_STATUS TestGlobalFlags()
769 {
770 OUTPUT_Banner("Testing GlobalFlags()");
771 TEST_STATUS result = SKIPPED;
772
773 result = TEST_CombineStatus(result, TestGlobalFlagsFixed());
774 result = TEST_CombineStatus(result, TestGlobalFlagsMoveable());
775
776 OUTPUT_Line("GlobalFlags result:");
777 OUTPUT_Result(result);
778 return result;
779 }
780 /*---------------------------------------------------------------------------
781 **
782 */
783 TEST_STATUS TestGlobalHandle()
784 {
785 HGLOBAL hMem = 0;
786 HGLOBAL hTest = 0;
787 PVOID pMem = 0;
788 TEST_STATUS subtest = SKIPPED;
789 TEST_STATUS result = SKIPPED;
790
791 OUTPUT_Banner("Testing GlobalHandle()");
792
793 OUTPUT_Line("Testing GlobalHandle with a block of GMEM_FIXED memory");
794 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
795 if (0 != hMem)
796 {
797
798 OUTPUT_Line("Allocation handle: ");
799 OUTPUT_HexDword((DWORD)hMem);
800
801 hTest = GlobalHandle(hMem);
802 if (hMem == hTest)
803 {
804 subtest = TEST_CombineStatus(subtest, PASSED);
805 }
806 else
807 {
808 OUTPUT_Line("GlobalHandle returned:");
809 OUTPUT_HexDword((DWORD)hTest);
810 subtest = TEST_CombineStatus(subtest, FAILED);
811 }
812
813 GlobalFree(hMem);
814 }
815 else
816 {
817 OUTPUT_Line("GlobalAlloc failed!");
818 subtest = TEST_CombineStatus(subtest, FAILED);
819 }
820
821 OUTPUT_Line("Result from subtest:");
822 OUTPUT_Result(subtest);
823 result = TEST_CombineStatus(result, subtest);
824
825
826 subtest = SKIPPED;
827 OUTPUT_Line("Testing GlobalHandle with a block of GMEM_MOVEABLE memory");
828 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
829 if (0 != hMem)
830 {
831
832 OUTPUT_Line("Allocation handle: ");
833 OUTPUT_HexDword((DWORD)hMem);
834 pMem = GlobalLock(hMem);
835 hTest = GlobalHandle(pMem);
836 if (hMem == hTest)
837 {
838 subtest = TEST_CombineStatus(subtest, PASSED);
839 }
840 else
841 {
842 OUTPUT_Line("GlobalHandle returned:");
843 OUTPUT_HexDword((DWORD)hTest);
844 subtest = TEST_CombineStatus(subtest, FAILED);
845 }
846
847 GlobalUnlock(hMem);
848 GlobalFree(hMem);
849 }
850 else
851 {
852 OUTPUT_Line("GlobalAlloc failed!");
853 subtest = TEST_CombineStatus(subtest, FAILED);
854 }
855
856 OUTPUT_Line("Result from subtest:");
857 OUTPUT_Result(subtest);
858 result = TEST_CombineStatus(result, subtest);
859
860
861 OUTPUT_Line("Global Handle test results:");
862 OUTPUT_Result(result);
863 return result;
864 }
865
866 /*---------------------------------------------------------------------------
867 **
868 */
869 TEST_STATUS TestGlobalSize()
870 {
871 HGLOBAL hMem = 0;
872 SIZE_T size = 0;
873 TEST_STATUS subtest = SKIPPED;
874 TEST_STATUS result = SKIPPED;
875 OUTPUT_Banner("Testing GlobalSize()");
876
877 OUTPUT_Line("Testing GlobalSize with a block of GMEM_FIXED memory");
878 hMem = GlobalAlloc(GMEM_FIXED, MEM_BLOCK_SIZE);
879 if (0 != hMem)
880 {
881 size = GlobalSize(hMem);
882 if (MEM_BLOCK_SIZE <= size)
883 {
884 subtest = TEST_CombineStatus(subtest, PASSED);
885 }
886 else
887 {
888 OUTPUT_Line("GlobalSize returned:");
889 OUTPUT_HexDword(size);
890 subtest = TEST_CombineStatus(subtest, FAILED);
891 }
892
893 GlobalFree(hMem);
894 }
895 else
896 {
897 OUTPUT_Line("GlobalAlloc failed!");
898 subtest = TEST_CombineStatus(subtest, FAILED);
899 }
900
901 OUTPUT_Line("Result from subtest:");
902 OUTPUT_Result(subtest);
903 result = TEST_CombineStatus(result, subtest);
904
905 OUTPUT_Line("Testing GlobalSize with a block of GMEM_MOVEABLE memory");
906 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
907 if (0 != hMem)
908 {
909 size = GlobalSize(hMem);
910 if (MEM_BLOCK_SIZE <= size)
911 {
912 subtest = TEST_CombineStatus(subtest, PASSED);
913 }
914 else
915 {
916 OUTPUT_Line("GlobalSize returned:");
917 OUTPUT_HexDword(size);
918 subtest = TEST_CombineStatus(subtest, FAILED);
919 }
920
921 GlobalFree(hMem);
922 }
923 else
924 {
925 OUTPUT_Line("GlobalAlloc failed!");
926 subtest = TEST_CombineStatus(subtest, FAILED);
927 }
928
929 OUTPUT_Line("Result from subtest:");
930 OUTPUT_Result(subtest);
931 result = TEST_CombineStatus(result, subtest);
932
933 OUTPUT_Line("Testing GlobalSize with discarded memory");
934 hMem = GlobalAlloc(GMEM_MOVEABLE, 0);
935 if (0 != hMem)
936 {
937 size = GlobalSize(hMem);
938 if (0 == size)
939 {
940 subtest = TEST_CombineStatus(subtest, PASSED);
941 }
942 else
943 {
944 OUTPUT_Line("GlobalSize returned:");
945 OUTPUT_HexDword(size);
946 subtest = TEST_CombineStatus(subtest, FAILED);
947 }
948
949 GlobalFree(hMem);
950 }
951 else
952 {
953 OUTPUT_Line("GlobalAlloc failed!");
954 subtest = TEST_CombineStatus(subtest, FAILED);
955 }
956
957 OUTPUT_Line("Result from subtest:");
958 OUTPUT_Result(subtest);
959 result = TEST_CombineStatus(result, subtest);
960
961 OUTPUT_Line("Test result:");
962 OUTPUT_Result(result);
963 return result;
964 }
965
966 /*---------------------------------------------------------------------------
967 **
968 */
969 TEST_STATUS TestGlobalDiscard()
970 {
971 HGLOBAL hMem = 0;
972 HGLOBAL hTest = 0;
973 DWORD uFlags = 0;
974 TEST_STATUS subtest = SKIPPED;
975 TEST_STATUS result = SKIPPED;
976
977 OUTPUT_Banner("Testing GlobalDiscard()");
978 hMem = GlobalAlloc(GMEM_MOVEABLE, MEM_BLOCK_SIZE);
979 if (0 != hMem)
980 {
981 OUTPUT_Line("Allocation handle: ");
982 OUTPUT_HexDword((DWORD)hMem);
983
984 hTest = GlobalDiscard(hMem);
985 if (0 == hTest)
986 {
987 OUTPUT_Line("GlobalDiscard returned NULL");
988 subtest = TEST_CombineStatus(subtest, FAILED);
989 }
990 else
991 {
992 uFlags = GlobalFlags(hTest);
993 OUTPUT_Line("Flags from the new memory block.");
994 OUTPUT_HexDword(uFlags);
995 if (0 != (uFlags & GMEM_DISCARDED))
996 {
997 subtest = TEST_CombineStatus(subtest, PASSED);
998 }
999 else
1000 {
1001 OUTPUT_Line("Block is not marked as discardable.");
1002 subtest = TEST_CombineStatus(subtest, FAILED);
1003 }
1004 }
1005
1006 GlobalFree(hTest);
1007 }
1008 else
1009 {
1010 OUTPUT_Line("GlobalAlloc failed!");
1011 subtest = TEST_CombineStatus(subtest, FAILED);
1012 }
1013
1014 OUTPUT_Line("Result from subtest:");
1015 OUTPUT_Result(subtest);
1016
1017 result = TEST_CombineStatus(result, subtest);
1018
1019 OUTPUT_Result(result);
1020 return result;
1021 }
1022
1023 /*---------------------------------------------------------------------------
1024 **
1025 */
1026 int main(int argc, char ** argv)
1027 {
1028 TEST_STATUS test_set = SKIPPED;
1029 OUTPUT_Banner("Testing GlobalXXX memory management functions.");
1030
1031 test_set = TEST_CombineStatus(test_set, TestGlobalAllocNFree(GPTR));
1032 test_set = TEST_CombineStatus(test_set, TestGlobalAllocNFree(GHND));
1033 test_set = TEST_CombineStatus(test_set, TestGlobalAllocNFree(GMEM_FIXED));
1034 test_set = TEST_CombineStatus(test_set, TestGlobalAllocNFree(GMEM_MOVEABLE));
1035
1036 if (FAILED == test_set)
1037 {
1038 OUTPUT_Line("Skipping any further tests. GlobalAlloc/Free fails.");
1039 OUTPUT_Result(test_set);
1040 return test_set;
1041 }
1042
1043 test_set = TEST_CombineStatus(test_set, TestGlobalLockNUnlock(GPTR));
1044 test_set = TEST_CombineStatus(test_set, TestGlobalLockNUnlock(GHND));
1045 test_set = TEST_CombineStatus(test_set, TestGlobalLockNUnlock(GMEM_FIXED));
1046 test_set = TEST_CombineStatus(test_set, TestGlobalLockNUnlock(GMEM_MOVEABLE));
1047
1048 test_set = TEST_CombineStatus(test_set, TestGlobalReAlloc());
1049
1050 test_set = TEST_CombineStatus(test_set, TestGlobalFlags());
1051
1052 test_set = TEST_CombineStatus(test_set, TestGlobalHandle());
1053
1054 test_set = TEST_CombineStatus(test_set, TestGlobalSize());
1055
1056 test_set = TEST_CombineStatus(test_set, TestGlobalDiscard());
1057
1058 /* output the result for the entire set of tests*/
1059 OUTPUT_Banner("Test suite result");
1060 OUTPUT_Result(test_set);
1061 return test_set;
1062 }