bounded_queue_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date: Sun Nov 3 19:16:50 PST 2019
  18. #include <iostream>
  19. #include "butil/containers/bounded_queue.h"
  20. #include <gtest/gtest.h>
  21. namespace {
  22. TEST(BoundedQueueTest, sanity) {
  23. const int N = 36;
  24. char storage[N * sizeof(int)];
  25. butil::BoundedQueue<int> q(storage, sizeof(storage), butil::NOT_OWN_STORAGE);
  26. ASSERT_EQ(0ul, q.size());
  27. ASSERT_TRUE(q.empty());
  28. ASSERT_TRUE(NULL == q.top());
  29. ASSERT_TRUE(NULL == q.bottom());
  30. for (int i = 1; i <= N; ++i) {
  31. if (i % 2 == 0) {
  32. ASSERT_TRUE(q.push(i));
  33. } else {
  34. int* p = q.push();
  35. ASSERT_TRUE(p);
  36. *p = i;
  37. }
  38. ASSERT_EQ((size_t)i, q.size());
  39. ASSERT_EQ(1, *q.top());
  40. ASSERT_EQ(i, *q.bottom());
  41. }
  42. ASSERT_FALSE(q.push(N+1));
  43. ASSERT_FALSE(q.push_top(N+1));
  44. ASSERT_EQ((size_t)N, q.size());
  45. ASSERT_FALSE(q.empty());
  46. ASSERT_TRUE(q.full());
  47. for (int i = 1; i <= N; ++i) {
  48. ASSERT_EQ(i, *q.top());
  49. ASSERT_EQ(N, *q.bottom());
  50. if (i % 2 == 0) {
  51. int tmp = 0;
  52. ASSERT_TRUE(q.pop(&tmp));
  53. ASSERT_EQ(tmp, i);
  54. } else {
  55. ASSERT_TRUE(q.pop());
  56. }
  57. ASSERT_EQ((size_t)(N-i), q.size());
  58. }
  59. ASSERT_EQ(0ul, q.size());
  60. ASSERT_TRUE(q.empty());
  61. ASSERT_FALSE(q.full());
  62. ASSERT_FALSE(q.pop());
  63. for (int i = 1; i <= N; ++i) {
  64. if (i % 2 == 0) {
  65. ASSERT_TRUE(q.push_top(i));
  66. } else {
  67. int* p = q.push_top();
  68. ASSERT_TRUE(p);
  69. *p = i;
  70. }
  71. ASSERT_EQ((size_t)i, q.size());
  72. ASSERT_EQ(i, *q.top());
  73. ASSERT_EQ(1, *q.bottom());
  74. }
  75. ASSERT_FALSE(q.push(N+1));
  76. ASSERT_FALSE(q.push_top(N+1));
  77. ASSERT_EQ((size_t)N, q.size());
  78. ASSERT_FALSE(q.empty());
  79. ASSERT_TRUE(q.full());
  80. for (int i = 1; i <= N; ++i) {
  81. ASSERT_EQ(N, *q.top());
  82. ASSERT_EQ(i, *q.bottom());
  83. if (i % 2 == 0) {
  84. int tmp = 0;
  85. ASSERT_TRUE(q.pop_bottom(&tmp));
  86. ASSERT_EQ(tmp, i);
  87. } else {
  88. ASSERT_TRUE(q.pop_bottom());
  89. }
  90. ASSERT_EQ((size_t)(N-i), q.size());
  91. }
  92. ASSERT_EQ(0ul, q.size());
  93. ASSERT_TRUE(q.empty());
  94. ASSERT_FALSE(q.full());
  95. ASSERT_FALSE(q.pop());
  96. }
  97. } // anonymous namespace