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.

82 lines
2.0 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <ole2.h>
  4. #include <olebind.hxx>
  5. BOOL TestStdMalloc(void)
  6. {
  7. // Get the allocator
  8. IMalloc *pIMalloc;
  9. HRESULT hr = CoGetMalloc(MEMCTX_TASK, &pIMalloc);
  10. TEST_FAILED_HR(FAILED(hr), "CoGetMalloc failed");
  11. // Test AddRef/Release
  12. ULONG cOrigRefs = pIMalloc->AddRef();
  13. #ifdef WIN32
  14. // On Win32, we only guarantee that the reference count will be either
  15. // zero (if we released the object) or nonzero (if we didn't)
  16. TEST_FAILED((pIMalloc->Release() == 0),
  17. "IMalloc: Wrong refcount");
  18. #else
  19. TEST_FAILED((pIMalloc->Release() != cOrigRefs - 1),
  20. "IMalloc: Wrong refcount");
  21. #endif
  22. // Test query interface
  23. IUnknown *punk;
  24. hr = pIMalloc->QueryInterface(IID_IMalloc, (void **) &punk);
  25. TEST_FAILED_HR(FAILED(hr), "IMalloc QueryInterface failed");
  26. punk->Release();
  27. // Test allocation
  28. BYTE *pb = (BYTE *) pIMalloc->Alloc(2048);
  29. // Test get size
  30. TEST_FAILED((pIMalloc->GetSize(pb) < 2048), "GetSize failed");
  31. TEST_FAILED((pb == NULL), "Alloc returned NULL");
  32. for (int i = 0; i < 2048; i++)
  33. {
  34. pb[i] = 'A';
  35. }
  36. // Test reallocation to larger buffer
  37. pb = (BYTE *) pIMalloc->Realloc(pb, 4096);
  38. TEST_FAILED((pb == NULL), "Realloc larger returned NULL");
  39. for (i = 0; i < 2048; i++)
  40. {
  41. TEST_FAILED((pb[i] != 'A'), "Buffer corrupt on larger realloc");
  42. }
  43. // Test reallocation to smaller buffer
  44. pb = (BYTE *) pIMalloc->Realloc(pb, 990);
  45. TEST_FAILED((pb == NULL), "Realloc smaller returned NULL");
  46. for (i = 0; i < 990; i++)
  47. {
  48. TEST_FAILED((pb[i] != 'A'), "Buffer corrupt on smaller realloc");
  49. }
  50. // Test get size (size is allowed to be larger, but not smaller)
  51. TEST_FAILED((pIMalloc->GetSize(pb) < 990), "GetSize failed");
  52. // Test DidAllocate
  53. TEST_FAILED((pIMalloc->DidAlloc(pb) == 0), "Didalloc failed");
  54. // Test freeing the buffer
  55. pIMalloc->Free(pb);
  56. // Do last release
  57. pIMalloc->Release();
  58. return FALSE;
  59. }