threadSafePriorityQueueTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/threadSafePriorityQueue.h"
  25. #include "platform/threads/thread.h"
  26. #include "core/util/tVector.h"
  27. #include "console/console.h"
  28. // Test queue without concurrency.
  29. TEST(ThreadSafePriorityQueue, Serial)
  30. {
  31. const U32 min = 0;
  32. const U32 max = 9;
  33. const U32 len = 11;
  34. U32 indices[len] = { 2, 7, 4, 6, 1, 5, 3, 8, 6, 9, 0};
  35. F32 priorities[len] = {0.2, 0.7, 0.4, 0.6, 0.1, 0.5, 0.3, 0.8, 0.6, 0.9, 0};
  36. ThreadSafePriorityQueue<U32, F32, true> minQueue;
  37. ThreadSafePriorityQueue<U32, F32, false> maxQueue;
  38. for(U32 i = 0; i < len; i++)
  39. {
  40. minQueue.insert(priorities[i], indices[i]);
  41. maxQueue.insert(priorities[i], indices[i]);
  42. }
  43. EXPECT_FALSE(minQueue.isEmpty());
  44. EXPECT_FALSE(maxQueue.isEmpty());
  45. U32 index = min;
  46. for(U32 i = 0; i < len; i++)
  47. {
  48. U32 popped;
  49. EXPECT_TRUE(minQueue.takeNext(popped))
  50. << "Failed to pop element from minQueue";
  51. EXPECT_LE(index, popped)
  52. << "Element from minQueue was not in sort order";
  53. index = popped;
  54. }
  55. index = max;
  56. for(U32 i = 0; i < len; i++)
  57. {
  58. U32 popped;
  59. EXPECT_TRUE(maxQueue.takeNext(popped))
  60. << "Failed to pop element from maxQueue";
  61. EXPECT_GE(index, popped)
  62. << "Element from maxQueue was not in sort order";
  63. index = popped;
  64. }
  65. }
  66. // Test queue with concurrency.
  67. TEST(ThreadSafePriorityQueue, Concurrent)
  68. {
  69. #define MIN 0
  70. #define MAX 9
  71. #define LEN 11
  72. typedef ThreadSafePriorityQueue<U32, F32, true> MinQueue;
  73. typedef ThreadSafePriorityQueue<U32, F32, false> MaxQueue;
  74. struct ProducerThread : public Thread
  75. {
  76. MinQueue& minQueue;
  77. MaxQueue& maxQueue;
  78. ProducerThread(MinQueue& min, MaxQueue& max)
  79. : minQueue(min), maxQueue(max) {}
  80. virtual void run(void*)
  81. {
  82. U32 indices[LEN] = { 2, 7, 4, 6, 1, 5, 3, 8, 6, 9, 0};
  83. F32 priorities[LEN] = {0.2, 0.7, 0.4, 0.6, 0.1, 0.5, 0.3, 0.8, 0.6, 0.9, 0};
  84. for(U32 i = 0; i < LEN; i++)
  85. {
  86. minQueue.insert(priorities[i], indices[i]);
  87. maxQueue.insert(priorities[i], indices[i]);
  88. }
  89. }
  90. };
  91. MinQueue minQueue;
  92. MaxQueue maxQueue;
  93. ProducerThread producers[] = {
  94. ProducerThread(minQueue, maxQueue),
  95. ProducerThread(minQueue, maxQueue),
  96. ProducerThread(minQueue, maxQueue)
  97. };
  98. const U32 len = sizeof(producers) / sizeof(ProducerThread);
  99. for(U32 i = 0; i < len; i++)
  100. producers[i].start();
  101. for(U32 i = 0; i < len; i++)
  102. producers[i].join();
  103. U32 index = MIN;
  104. for(U32 i = 0; i < LEN * len; i++)
  105. {
  106. U32 popped;
  107. EXPECT_TRUE(minQueue.takeNext(popped))
  108. << "Failed to pop element from minQueue";
  109. EXPECT_LE(index, popped)
  110. << "Element from minQueue was not in sort order";
  111. index = popped;
  112. }
  113. index = MAX;
  114. for(U32 i = 0; i < LEN * len; i++)
  115. {
  116. U32 popped;
  117. EXPECT_TRUE(maxQueue.takeNext(popped))
  118. << "Failed to pop element from maxQueue";
  119. EXPECT_GE(index, popped)
  120. << "Element from maxQueue was not in sort order";
  121. index = popped;
  122. }
  123. #undef MIN
  124. #undef MAX
  125. #undef LEN
  126. }
  127. #endif