1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/utils/queue.h"
16
17 #include <utility>
18 #include <vector>
19
20 #include "gtest/gtest.h"
21
22 namespace libgav1 {
23 namespace {
24
25 struct TestClass {
26 TestClass() = default;
TestClasslibgav1::__anon3748340b0111::TestClass27 explicit TestClass(int i) : i(i) {}
28 int i;
29 // The vector exists simply so that the class is not trivially copyable.
30 std::vector<int> dummy;
31 };
32
TEST(QueueTest,Basic)33 TEST(QueueTest, Basic) {
34 Queue<TestClass> queue;
35 ASSERT_TRUE(queue.Init(8));
36 EXPECT_TRUE(queue.Empty());
37
38 for (int i = 0; i < 8; ++i) {
39 EXPECT_FALSE(queue.Full());
40 TestClass test(i);
41 queue.Push(std::move(test));
42 EXPECT_EQ(queue.Back().i, i);
43 EXPECT_FALSE(queue.Empty());
44 }
45 EXPECT_TRUE(queue.Full());
46
47 for (int i = 0; i < 8; ++i) {
48 EXPECT_FALSE(queue.Empty());
49 EXPECT_EQ(queue.Front().i, i);
50 queue.Pop();
51 EXPECT_FALSE(queue.Full());
52 }
53 EXPECT_TRUE(queue.Empty());
54
55 for (int i = 0; i < 8; ++i) {
56 EXPECT_FALSE(queue.Full());
57 TestClass test(i);
58 queue.Push(std::move(test));
59 EXPECT_EQ(queue.Back().i, i);
60 EXPECT_FALSE(queue.Empty());
61 }
62 EXPECT_TRUE(queue.Full());
63 queue.Clear();
64 EXPECT_TRUE(queue.Empty());
65 EXPECT_FALSE(queue.Full());
66 }
67
TEST(QueueTest,WrapAround)68 TEST(QueueTest, WrapAround) {
69 Queue<TestClass> queue;
70 ASSERT_TRUE(queue.Init(8));
71 EXPECT_TRUE(queue.Empty());
72
73 for (int i = 0; i < 100; ++i) {
74 EXPECT_FALSE(queue.Full());
75 TestClass test(i);
76 queue.Push(std::move(test));
77 EXPECT_EQ(queue.Back().i, i);
78 EXPECT_FALSE(queue.Empty());
79 EXPECT_EQ(queue.Front().i, i);
80 queue.Pop();
81 EXPECT_TRUE(queue.Empty());
82 }
83 }
84
85 } // namespace
86 } // namespace libgav1
87