StressTest.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #if DBG
  6. // Recycler requires 16/32 byte alignment
  7. const int OBJALIGN = sizeof(void*) * 4;
  8. enum CreateOptions
  9. {
  10. NormalObj,
  11. LeafObj
  12. };
  13. template<class T> T AlignPtr(T ptr, size_t align)
  14. {
  15. return reinterpret_cast<T>(::Math::Align(reinterpret_cast<size_t>(ptr), align));
  16. }
  17. struct TestObject
  18. {
  19. // Full object size
  20. Field(size_t) size;
  21. // Number of pointers to other objects this object potentially has
  22. Field(int) pointerCount;
  23. // Hash of part of the object's contents, used for corruption detection
  24. Field(size_t) cookie;
  25. TestObject(size_t _size, int _pointerCount);
  26. size_t CalculateCookie();
  27. void CheckCookie();
  28. // Sets an object pointer at index indicated
  29. void Set(int index, TestObject *val);
  30. void SetRandom(TestObject *val);
  31. void CreateFalseReferenceRandom(TestObject *val);
  32. // Does a best-effort attempt to add an object pointer to an unused slot
  33. void Add(TestObject *val);
  34. // Clears the first non-null pointer in the list
  35. void ClearOne();
  36. // Retrieves a pointer
  37. TestObject* Get(int index);
  38. TestObject** GetDataPointer() { return reinterpret_cast<TestObject**>(AlignPtr((char*)this + sizeof(TestObject), OBJALIGN)); }
  39. static void Visit(Recycler *recycler, TestObject *root);
  40. template<class Fn> static void Visit(Recycler *recycler, TestObject *root, Fn fn);
  41. static TestObject* Create(Recycler *recycler, int pointerCount, size_t extraBytes, CreateOptions options = NormalObj);
  42. };
  43. class StressTester
  44. {
  45. Recycler *recycler;
  46. static const int MaxLinkedListLength = 100;
  47. static const int MaxTreeDepth = 8;
  48. static const int MaxNodesInTree = 1000;
  49. size_t GetRandomSize();
  50. TestObject *CreateRandom();
  51. TestObject* CreateLinkedList();
  52. TestObject* CreateTree();
  53. int treeTotal;
  54. void CreateTreeHelper(TestObject *root, int depth);
  55. public:
  56. StressTester(Recycler *_recycler);
  57. void Run();
  58. };
  59. #endif