test_threadsafe_queue.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <algorithm>
  22. #include <chrono>
  23. #include <functional>
  24. #include <thread>
  25. #include <vector>
  26. #include "util/threadsafe_queue.h"
  27. TEST(InferServerUtil, ThreadSafeQueue) {
  28. infer_server::TSPriorityQueue<int> ts_q;
  29. std::vector<int> vec = {3, 1, 5, 2, 4, 8, 6, 9};
  30. std::vector<int> res;
  31. int tmp;
  32. for (unsigned int i = 0; i < vec.size(); i++) {
  33. ts_q.Push(vec[i]);
  34. }
  35. EXPECT_FALSE(ts_q.Empty());
  36. while (!ts_q.Empty()) {
  37. auto b = ts_q.TryPop(tmp);
  38. EXPECT_TRUE(b);
  39. res.push_back(tmp);
  40. }
  41. EXPECT_TRUE(ts_q.Empty());
  42. auto b = ts_q.TryPop(tmp);
  43. EXPECT_FALSE(b);
  44. std::sort(vec.begin(), vec.end(), std::greater<int>());
  45. EXPECT_EQ(vec.size(), res.size());
  46. for (unsigned int i = 0; i < vec.size(); i++) {
  47. EXPECT_EQ(vec[i], res[i]);
  48. }
  49. }