Hash :
2231b4e0
Author :
Date :
2018-03-26T16:44:59
Add a FixedVector class to have "variable" size vectors on the stack. Wraps a std::array and a size parameter to give the std::vector interface without making allocations. BUG=angleproject:2435 Change-Id: I7df0be1310446a2f163766149bf631a8692be9ad Reviewed-on: https://chromium-review.googlesource.com/981267 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Corentin Wallez <cwallez@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
//
// 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> copy(countAndValue);
EXPECT_EQ(copy, countAndValue);
FixedVector<int, 5> copyRValue(std::move(count));
EXPECT_EQ(3u, copyRValue.size());
FixedVector<int, 5> initializerList{1, 2, 3, 4, 5};
EXPECT_EQ(5u, initializerList.size());
EXPECT_EQ(3, initializerList[2]);
FixedVector<int, 5> assignCopy(copyRValue);
EXPECT_EQ(3u, assignCopy.size());
FixedVector<int, 5> assignRValue(std::move(assignCopy));
EXPECT_EQ(3u, assignRValue.size());
FixedVector<int, 5> 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]);
}
// 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);
}
} // namespace angle