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