1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "test/pc/e2e/analyzer/video/multi_head_queue.h"
12 #include "absl/types/optional.h"
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 namespace webrtc_pc_e2e {
17 namespace {
18 
TEST(MultiHeadQueueTest,GetOnEmpty)19 TEST(MultiHeadQueueTest, GetOnEmpty) {
20   MultiHeadQueue<int> queue = MultiHeadQueue<int>(10);
21   EXPECT_TRUE(queue.IsEmpty(0));
22   for (int i = 0; i < 10; ++i) {
23     EXPECT_FALSE(queue.PopFront(i).has_value());
24     EXPECT_FALSE(queue.Front(i).has_value());
25   }
26 }
27 
TEST(MultiHeadQueueTest,SingleHeadOneAddOneRemove)28 TEST(MultiHeadQueueTest, SingleHeadOneAddOneRemove) {
29   MultiHeadQueue<int> queue = MultiHeadQueue<int>(1);
30   queue.PushBack(1);
31   EXPECT_EQ(queue.size(), 1lu);
32   EXPECT_TRUE(queue.Front(0).has_value());
33   EXPECT_EQ(queue.Front(0).value(), 1);
34   absl::optional<int> value = queue.PopFront(0);
35   EXPECT_TRUE(value.has_value());
36   EXPECT_EQ(value.value(), 1);
37   EXPECT_EQ(queue.size(), 0lu);
38   EXPECT_TRUE(queue.IsEmpty(0));
39 }
40 
TEST(MultiHeadQueueTest,SingleHead)41 TEST(MultiHeadQueueTest, SingleHead) {
42   MultiHeadQueue<size_t> queue = MultiHeadQueue<size_t>(1);
43   for (size_t i = 0; i < 10; ++i) {
44     queue.PushBack(i);
45     EXPECT_EQ(queue.size(), i + 1);
46   }
47   for (size_t i = 0; i < 10; ++i) {
48     absl::optional<size_t> value = queue.PopFront(0);
49     EXPECT_EQ(queue.size(), 10 - i - 1);
50     ASSERT_TRUE(value.has_value());
51     EXPECT_EQ(value.value(), i);
52   }
53 }
54 
TEST(MultiHeadQueueTest,ThreeHeadsAddAllRemoveAllPerHead)55 TEST(MultiHeadQueueTest, ThreeHeadsAddAllRemoveAllPerHead) {
56   MultiHeadQueue<size_t> queue = MultiHeadQueue<size_t>(3);
57   for (size_t i = 0; i < 10; ++i) {
58     queue.PushBack(i);
59     EXPECT_EQ(queue.size(), i + 1);
60   }
61   for (size_t i = 0; i < 10; ++i) {
62     absl::optional<size_t> value = queue.PopFront(0);
63     EXPECT_EQ(queue.size(), 10lu);
64     ASSERT_TRUE(value.has_value());
65     EXPECT_EQ(value.value(), i);
66   }
67   for (size_t i = 0; i < 10; ++i) {
68     absl::optional<size_t> value = queue.PopFront(1);
69     EXPECT_EQ(queue.size(), 10lu);
70     ASSERT_TRUE(value.has_value());
71     EXPECT_EQ(value.value(), i);
72   }
73   for (size_t i = 0; i < 10; ++i) {
74     absl::optional<size_t> value = queue.PopFront(2);
75     EXPECT_EQ(queue.size(), 10 - i - 1);
76     ASSERT_TRUE(value.has_value());
77     EXPECT_EQ(value.value(), i);
78   }
79 }
80 
TEST(MultiHeadQueueTest,ThreeHeadsAddAllRemoveAll)81 TEST(MultiHeadQueueTest, ThreeHeadsAddAllRemoveAll) {
82   MultiHeadQueue<size_t> queue = MultiHeadQueue<size_t>(3);
83   for (size_t i = 0; i < 10; ++i) {
84     queue.PushBack(i);
85     EXPECT_EQ(queue.size(), i + 1);
86   }
87   for (size_t i = 0; i < 10; ++i) {
88     absl::optional<size_t> value1 = queue.PopFront(0);
89     absl::optional<size_t> value2 = queue.PopFront(1);
90     absl::optional<size_t> value3 = queue.PopFront(2);
91     EXPECT_EQ(queue.size(), 10 - i - 1);
92     ASSERT_TRUE(value1.has_value());
93     ASSERT_TRUE(value2.has_value());
94     ASSERT_TRUE(value3.has_value());
95     EXPECT_EQ(value1.value(), i);
96     EXPECT_EQ(value2.value(), i);
97     EXPECT_EQ(value3.value(), i);
98   }
99 }
100 
101 }  // namespace
102 }  // namespace webrtc_pc_e2e
103 }  // namespace webrtc
104