1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // FixedVector_unittest:
7 // Tests of the FixedVector class
8 //
9
10 #include <gtest/gtest.h>
11
12 #include "common/FixedVector.h"
13
14 namespace angle
15 {
16 // Make sure the various constructors compile and do basic checks
TEST(FixedVector,Constructors)17 TEST(FixedVector, Constructors)
18 {
19 FixedVector<int, 5> defaultContructor;
20 EXPECT_EQ(0u, defaultContructor.size());
21
22 FixedVector<int, 5> count(3);
23 EXPECT_EQ(3u, count.size());
24
25 FixedVector<int, 5> countAndValue(3, 2);
26 EXPECT_EQ(3u, countAndValue.size());
27 EXPECT_EQ(2, countAndValue[1]);
28
29 FixedVector<int, 5> initializerList{1, 2, 3, 4, 5};
30 EXPECT_EQ(5u, initializerList.size());
31 EXPECT_EQ(3, initializerList[2]);
32
33 FixedVector<int, 5> copy(initializerList);
34 EXPECT_EQ(copy, initializerList);
35
36 FixedVector<int, 5> copyRValue(std::move(copy));
37 EXPECT_EQ(copyRValue, initializerList);
38 EXPECT_EQ(0u, copy.size());
39
40 FixedVector<int, 5> assignCopy;
41 assignCopy = copyRValue;
42 EXPECT_EQ(assignCopy, initializerList);
43
44 FixedVector<int, 5> assignRValue;
45 assignRValue = std::move(assignCopy);
46 EXPECT_EQ(assignRValue, initializerList);
47 EXPECT_EQ(0u, assignCopy.size());
48
49 FixedVector<int, 5> assignmentInitializerList;
50 assignmentInitializerList = {1, 2, 3, 4, 5};
51 EXPECT_EQ(5u, assignmentInitializerList.size());
52 EXPECT_EQ(3, assignmentInitializerList[2]);
53 }
54
55 // Test indexing operations (at, operator[])
TEST(FixedVector,Indexing)56 TEST(FixedVector, Indexing)
57 {
58 FixedVector<int, 5> vec = {0, 1, 2, 3, 4};
59 EXPECT_EQ(0, vec.at(0));
60 EXPECT_EQ(vec[0], vec.at(0));
61 }
62
63 // Test the push_back functions
TEST(FixedVector,PushBack)64 TEST(FixedVector, PushBack)
65 {
66 FixedVector<int, 5> vec;
67 vec.push_back(1);
68 EXPECT_EQ(1, vec[0]);
69 vec.push_back(1);
70 vec.push_back(1);
71 vec.push_back(1);
72 vec.push_back(1);
73 EXPECT_EQ(vec.size(), vec.max_size());
74 }
75
76 // Test the pop_back function
TEST(FixedVector,PopBack)77 TEST(FixedVector, PopBack)
78 {
79 FixedVector<int, 5> vec;
80 vec.push_back(1);
81 EXPECT_EQ(1, (int)vec.size());
82 vec.pop_back();
83 EXPECT_EQ(0, (int)vec.size());
84 }
85
86 // Test the back function
TEST(FixedVector,Back)87 TEST(FixedVector, Back)
88 {
89 FixedVector<int, 5> vec;
90 vec.push_back(1);
91 vec.push_back(2);
92 EXPECT_EQ(2, vec.back());
93 }
94
95 // Test the sizing operations
TEST(FixedVector,Size)96 TEST(FixedVector, Size)
97 {
98 FixedVector<int, 5> vec;
99 EXPECT_TRUE(vec.empty());
100 EXPECT_EQ(0u, vec.size());
101 EXPECT_EQ(5u, vec.max_size());
102
103 vec.push_back(1);
104 EXPECT_FALSE(vec.empty());
105 EXPECT_EQ(1u, vec.size());
106 }
107
108 // Test clearing the vector
TEST(FixedVector,Clear)109 TEST(FixedVector, Clear)
110 {
111 FixedVector<int, 5> vec = {0, 1, 2, 3, 4};
112 vec.clear();
113 EXPECT_TRUE(vec.empty());
114 }
115
116 // Test resizing the vector
TEST(FixedVector,Resize)117 TEST(FixedVector, Resize)
118 {
119 FixedVector<int, 5> vec;
120 vec.resize(5u, 1);
121 EXPECT_EQ(5u, vec.size());
122 EXPECT_EQ(1, vec[4]);
123
124 vec.resize(2u);
125 EXPECT_EQ(2u, vec.size());
126 }
127
128 // Test iterating over the vector
TEST(FixedVector,Iteration)129 TEST(FixedVector, Iteration)
130 {
131 FixedVector<int, 5> vec = {0, 1, 2, 3};
132
133 int vistedCount = 0;
134 for (int value : vec)
135 {
136 EXPECT_EQ(vistedCount, value);
137 vistedCount++;
138 }
139 EXPECT_EQ(4, vistedCount);
140 }
141
142 // Test the "full" method.
TEST(FixedVector,Full)143 TEST(FixedVector, Full)
144 {
145 FixedVector<int, 2> vec;
146
147 EXPECT_FALSE(vec.full());
148 vec.push_back(0);
149 EXPECT_FALSE(vec.full());
150 vec.push_back(1);
151 EXPECT_TRUE(vec.full());
152 }
153 } // namespace angle
154