2
0

threadSafePriorityQueueTest.cpp 4.4 KB

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