threadSafeDequeTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/threadSafeDeque.h"
  25. #include "platform/threads/thread.h"
  26. #include "core/util/tVector.h"
  27. #include "console/console.h"
  28. FIXTURE(ThreadSafeDeque)
  29. {
  30. public:
  31. // Used by the concurrent test.
  32. struct Value : public ThreadSafeRefCount<Value>
  33. {
  34. U32 mIndex;
  35. U32 mTick;
  36. Value() {}
  37. Value(U32 index, U32 tick)
  38. : mIndex(index), mTick(tick) {}
  39. };
  40. typedef ThreadSafeRef<Value> ValueRef;
  41. struct Deque : public ThreadSafeDeque<ValueRef>
  42. {
  43. typedef ThreadSafeDeque<ValueRef> Parent;
  44. U32 mPushIndex;
  45. U32 mPopIndex;
  46. Deque()
  47. : mPushIndex(0), mPopIndex(0) {}
  48. void pushBack(const ValueRef& value)
  49. {
  50. EXPECT_EQ(value->mIndex, mPushIndex) << "index out of line";
  51. mPushIndex++;
  52. Parent::pushBack(value);
  53. }
  54. bool tryPopFront(ValueRef& outValue)
  55. {
  56. if(Parent::tryPopFront(outValue))
  57. {
  58. EXPECT_EQ(outValue->mIndex, mPopIndex) << "index out of line";
  59. mPopIndex++;
  60. return true;
  61. }
  62. else
  63. return false;
  64. }
  65. };
  66. struct ProducerThread : public Thread
  67. {
  68. Vector<U32>& mValues;
  69. Deque& mDeque;
  70. ProducerThread(Vector<U32>& values, Deque& deque)
  71. : mValues(values), mDeque(deque) {}
  72. virtual void run(void*)
  73. {
  74. for(U32 i = 0; i < mValues.size(); i++)
  75. {
  76. U32 tick = Platform::getRealMilliseconds();
  77. mValues[i] = tick;
  78. ValueRef val = new Value(i, tick);
  79. mDeque.pushBack(val);
  80. }
  81. }
  82. };
  83. struct ConsumerThread : public Thread
  84. {
  85. Vector<U32>& mValues;
  86. Deque& mDeque;
  87. ConsumerThread(Vector<U32>& values, Deque& deque)
  88. : mValues(values), mDeque(deque) {}
  89. virtual void run(void*)
  90. {
  91. for(U32 i = 0; i < mValues.size(); i++)
  92. {
  93. ValueRef value;
  94. while(!mDeque.tryPopFront(value));
  95. EXPECT_EQ(i, value->mIndex);
  96. EXPECT_EQ(value->mTick, mValues[i]);
  97. }
  98. }
  99. };
  100. };
  101. // Test deque without concurrency.
  102. TEST_FIX(ThreadSafeDeque, PopFront)
  103. {
  104. ThreadSafeDeque<char> deque;
  105. String str = "teststring";
  106. for(U32 i = 0; i < str.length(); i++)
  107. deque.pushBack(str[i]);
  108. EXPECT_FALSE(deque.isEmpty());
  109. char ch;
  110. for(U32 i = 0; i < str.length(); i++)
  111. {
  112. EXPECT_TRUE(deque.tryPopFront(ch));
  113. EXPECT_EQ(str[i], ch);
  114. }
  115. ASSERT_TRUE(deque.isEmpty());
  116. }
  117. TEST_FIX(ThreadSafeDeque, PopBack)
  118. {
  119. ThreadSafeDeque<char> deque;
  120. String str = "teststring";
  121. const char* p1 = str.c_str() + 4;
  122. const char* p2 = p1 + 1;
  123. while(*p2)
  124. {
  125. deque.pushFront(*p1);
  126. deque.pushBack(*p2);
  127. --p1;
  128. ++p2;
  129. }
  130. char ch;
  131. for(S32 i = str.length()-1; i >= 0; i--)
  132. {
  133. EXPECT_TRUE(deque.tryPopBack(ch));
  134. EXPECT_EQ(str[i], ch);
  135. }
  136. ASSERT_TRUE(deque.isEmpty());
  137. }
  138. // Test deque in a concurrent setting.
  139. TEST_FIX(ThreadSafeDeque, Concurrent1)
  140. {
  141. const U32 NumValues = 100;
  142. Deque mDeque;
  143. Vector<U32> mValues;
  144. mValues.setSize(NumValues);
  145. ProducerThread pThread(mValues, mDeque);
  146. ConsumerThread cThread(mValues, mDeque);
  147. pThread.start();
  148. cThread.start();
  149. pThread.join();
  150. cThread.join();
  151. mValues.clear();
  152. };
  153. #endif