queue.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef __XRCU_TESTS_QUEUE__
  2. #define __XRCU_TESTS_QUEUE__ 1
  3. #include "../queue.hpp"
  4. #include "utils.hpp"
  5. #include <thread>
  6. namespace q_test
  7. {
  8. typedef xrcu::queue<std::string> queue_t;
  9. void test_single_threaded ()
  10. {
  11. {
  12. queue_t q;
  13. ASSERT (q.empty ());
  14. ASSERT (!q.back().has_value ());
  15. }
  16. {
  17. queue_t q { 3, std::string ("???") };
  18. ASSERT (q.size () == 3);
  19. for (const auto& s : q)
  20. ASSERT (s == "???");
  21. }
  22. {
  23. queue_t q { std::string ("abc"), std::string ("def"),
  24. std::string ("ghi"), std::string ("jkl") };
  25. ASSERT (q.size () == 4);
  26. queue_t q2 { q };
  27. ASSERT (q == q2);
  28. queue_t q3 { std::move (q2) };
  29. ASSERT (q3 == q);
  30. }
  31. queue_t q;
  32. const int NELEM = 100;
  33. for (int i = 0; i < NELEM; ++i)
  34. q.push (mkstr (i));
  35. ASSERT (*q.pop () == mkstr (0));
  36. queue_t q2;
  37. q.swap (q2);
  38. ASSERT (q.empty ());
  39. q = q2;
  40. ASSERT (!q.empty ());
  41. ASSERT (q == q2);
  42. q.clear ();
  43. ASSERT (q.empty ());
  44. q.assign (q2.begin (), q2.end ());
  45. ASSERT (q == q2);
  46. q.clear ();
  47. q2.clear ();
  48. q.push (mkstr (10));
  49. q2.push (mkstr (20));
  50. ASSERT (q < q2);
  51. q.pop ();
  52. q.push (mkstr (30));
  53. ASSERT (q > q2);
  54. q2.pop ();
  55. q2.push (mkstr (30));
  56. q2.push (mkstr (40));
  57. ASSERT (q <= q2);
  58. q.push (mkstr (50));
  59. ASSERT (q >= q2);
  60. }
  61. static void
  62. mt_inserter (queue_t *qp, int index)
  63. {
  64. for (int i = 0; i < INSERTER_LOOPS; ++i)
  65. qp->push (mkstr (index * INSERTER_LOOPS + i));
  66. }
  67. void test_push_mt ()
  68. {
  69. queue_t q;
  70. std::vector<std::thread> thrs;
  71. for (int i = 0; i < INSERTER_THREADS; ++i)
  72. thrs.push_back (std::thread (mt_inserter, &q, i));
  73. for (auto& thr : thrs)
  74. thr.join ();
  75. ASSERT (q.size () == INSERTER_THREADS * INSERTER_LOOPS);
  76. }
  77. test_module queue_tests
  78. {
  79. "queue",
  80. {
  81. { "API in a single thread", test_single_threaded },
  82. { "multi threaded pushes", test_push_mt }
  83. }
  84. };
  85. }
  86. #endif