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/stack.h"
16
17 #include <cstdint>
18 #include <utility>
19
20 #include "gtest/gtest.h"
21
22 namespace libgav1 {
23 namespace {
24
25 constexpr int kStackSize = 8;
26
TEST(StackTest,SimpleType)27 TEST(StackTest, SimpleType) {
28 Stack<int, kStackSize> stack;
29 EXPECT_TRUE(stack.Empty());
30
31 for (int i = 0; i < kStackSize; ++i) {
32 stack.Push(i);
33 EXPECT_FALSE(stack.Empty());
34 }
35
36 for (int i = kStackSize - 1; i >= 0; --i) {
37 EXPECT_EQ(stack.Pop(), i);
38 }
39 EXPECT_TRUE(stack.Empty());
40 }
41
TEST(StackTest,LargeStruct)42 TEST(StackTest, LargeStruct) {
43 struct LargeMoveOnlyStruct {
44 LargeMoveOnlyStruct() = default;
45 // Move only.
46 LargeMoveOnlyStruct(LargeMoveOnlyStruct&& other) = default;
47 LargeMoveOnlyStruct& operator=(LargeMoveOnlyStruct&& other) = default;
48
49 int32_t array1[1000];
50 uint64_t array2[2000];
51 };
52
53 Stack<LargeMoveOnlyStruct, kStackSize> stack;
54 EXPECT_TRUE(stack.Empty());
55
56 LargeMoveOnlyStruct large_move_only_struct[kStackSize];
57 for (int i = 0; i < kStackSize; ++i) {
58 LargeMoveOnlyStruct& l = large_move_only_struct[i];
59 l.array1[0] = i;
60 l.array2[0] = i;
61 stack.Push(std::move(l));
62 EXPECT_FALSE(stack.Empty());
63 }
64
65 for (int i = kStackSize - 1; i >= 0; --i) {
66 LargeMoveOnlyStruct l = stack.Pop();
67 EXPECT_EQ(l.array1[0], i);
68 EXPECT_EQ(l.array2[0], i);
69 }
70 EXPECT_TRUE(stack.Empty());
71 }
72
73 } // namespace
74 } // namespace libgav1
75