TestGlobal.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EAStdC/EAGlobal.h>
  5. #include <EAStdCTest/EAStdCTest.h>
  6. #include <EATest/EATest.h>
  7. #ifdef _MSC_VER
  8. #pragma warning(push, 0)
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifdef _MSC_VER
  14. #pragma warning(pop)
  15. #endif
  16. struct GlobalTestObject
  17. {
  18. int sequence;
  19. static int sSeqCounter;
  20. GlobalTestObject()
  21. : sequence(++sSeqCounter) {}
  22. };
  23. int GlobalTestObject::sSeqCounter;
  24. // This structure will require 8 byte alignment on most platforms
  25. struct AlignedTestObject
  26. {
  27. uint64_t data;
  28. };
  29. int TestGlobal()
  30. {
  31. using namespace EA::StdC;
  32. int nErrorCount(0);
  33. EA::UnitTest::Report("TestGlobal\n");
  34. const uint32_t id = 0x8e9c5fd7;
  35. GlobalTestObject::sSeqCounter = 0;
  36. // create an OS global object and verify that the same one is returned on a second query
  37. {
  38. AutoOSGlobalPtr<GlobalTestObject, id> pObj;
  39. EATEST_VERIFY(pObj->sequence == 1);
  40. {
  41. AutoOSGlobalPtr<GlobalTestObject, id> pObj2;
  42. EATEST_VERIFY(pObj.get() == pObj2.get());
  43. EATEST_VERIFY(pObj2->sequence == 1);
  44. }
  45. }
  46. // create another OS global object; the last one should have been destroyed so this
  47. // one should be a different count
  48. {
  49. AutoStaticOSGlobalPtr<GlobalTestObject, id> pObj;
  50. EATEST_VERIFY(pObj->sequence == 2);
  51. {
  52. AutoStaticOSGlobalPtr<GlobalTestObject, id> pObj2;
  53. EATEST_VERIFY(pObj.get() == pObj2.get());
  54. EATEST_VERIFY(pObj2->sequence == 2);
  55. }
  56. }
  57. {
  58. AutoStaticOSGlobalPtr<AlignedTestObject, 0x52534562> pObj1;
  59. AutoStaticOSGlobalPtr<AlignedTestObject, 0x93715355> pObj2;
  60. size_t requiredAlignment = EA_ALIGN_OF(AlignedTestObject);
  61. // Ensure the two objects are unique.
  62. EATEST_VERIFY(pObj1.get() != pObj2.get() );
  63. EATEST_VERIFY( (reinterpret_cast<uintptr_t>(pObj1.get()) % requiredAlignment) == 0 );
  64. EATEST_VERIFY( (reinterpret_cast<uintptr_t>(pObj2.get()) % requiredAlignment) == 0 );
  65. pObj1->data = 1;
  66. pObj2->data = 2;
  67. // Verify the data is correct in the objects. This is done to catch errors where the two global objects could potentially overlap.
  68. EATEST_VERIFY( pObj1->data == 1 );
  69. EATEST_VERIFY( pObj2->data == 2 );
  70. }
  71. // create a global pointer object
  72. {
  73. const uint32_t kGUID = 0xef020e29;
  74. GlobalPtr<int, kGUID> pMemory;
  75. EATEST_VERIFY(!pMemory); // Global pointer should be nulled on creation.
  76. if(pMemory) // Global pointer should be nulled on creation.
  77. EATEST_VERIFY(pMemory != NULL);
  78. int* const p = new int[2];
  79. pMemory = p;
  80. EATEST_VERIFY((int*)pMemory == p);
  81. pMemory[0] = 10;
  82. pMemory[1] = 20;
  83. {
  84. GlobalPtr<int, kGUID> pMemory2A;
  85. GlobalPtr<int, kGUID> pMemory2B(pMemory2A);
  86. EATEST_VERIFY(pMemory2A == pMemory);
  87. EATEST_VERIFY(pMemory2B == pMemory);
  88. EATEST_VERIFY(*pMemory2A == 10);
  89. EATEST_VERIFY(pMemory2A[0] == 10);
  90. EATEST_VERIFY(pMemory2A[1] == 20);
  91. }
  92. pMemory = NULL;
  93. EATEST_VERIFY(!pMemory);
  94. delete[] p;
  95. }
  96. return nErrorCount;
  97. }