Path: blob/main_old/src/common/FastVector_unittest.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// FixedVector_unittest:6// Tests of the FastVector class7//89#include <gtest/gtest.h>1011#include "common/FastVector.h"1213namespace angle14{15// Make sure the various constructors compile and do basic checks16TEST(FastVector, Constructors)17{18FastVector<int, 5> defaultContructor;19EXPECT_EQ(0u, defaultContructor.size());2021FastVector<int, 5> count(3);22EXPECT_EQ(3u, count.size());2324FastVector<int, 5> countAndValue(3, 2);25EXPECT_EQ(3u, countAndValue.size());26EXPECT_EQ(2, countAndValue[1]);2728FastVector<int, 5> copy(countAndValue);29EXPECT_EQ(copy, countAndValue);3031FastVector<int, 5> copyRValue(std::move(count));32EXPECT_EQ(3u, copyRValue.size());3334FastVector<int, 5> copyIter(countAndValue.begin(), countAndValue.end());35EXPECT_EQ(copyIter, countAndValue);3637FastVector<int, 5> copyIterEmpty(countAndValue.begin(), countAndValue.begin());38EXPECT_TRUE(copyIterEmpty.empty());3940FastVector<int, 5> initializerList{1, 2, 3, 4, 5};41EXPECT_EQ(5u, initializerList.size());42EXPECT_EQ(3, initializerList[2]);4344FastVector<int, 5> assignCopy(copyRValue);45EXPECT_EQ(3u, assignCopy.size());4647FastVector<int, 5> assignRValue(std::move(assignCopy));48EXPECT_EQ(3u, assignRValue.size());4950FastVector<int, 5> assignmentInitializerList = {1, 2, 3, 4, 5};51EXPECT_EQ(5u, assignmentInitializerList.size());52EXPECT_EQ(3, assignmentInitializerList[2]);53}5455// Test indexing operations (at, operator[])56TEST(FastVector, Indexing)57{58FastVector<int, 5> vec = {0, 1, 2, 3, 4};59for (int i = 0; i < 5; ++i)60{61EXPECT_EQ(i, vec.at(i));62EXPECT_EQ(vec[i], vec.at(i));63}64}6566// Test the push_back functions67TEST(FastVector, PushBack)68{69FastVector<int, 5> vec;70vec.push_back(1);71EXPECT_EQ(1, vec[0]);72vec.push_back(1);73vec.push_back(1);74vec.push_back(1);75vec.push_back(1);76EXPECT_EQ(5u, vec.size());77}7879// Tests growing the fast vector beyond the fixed storage.80TEST(FastVector, Growth)81{82constexpr size_t kSize = 4;83FastVector<size_t, kSize> vec;8485for (size_t i = 0; i < kSize * 2; ++i)86{87vec.push_back(i);88}8990EXPECT_EQ(kSize * 2, vec.size());9192for (size_t i = kSize * 2; i > 0; --i)93{94ASSERT_EQ(vec.back(), i - 1);95vec.pop_back();96}9798EXPECT_EQ(0u, vec.size());99}100101// Test the pop_back function102TEST(FastVector, PopBack)103{104FastVector<int, 5> vec;105vec.push_back(1);106EXPECT_EQ(1, (int)vec.size());107vec.pop_back();108EXPECT_EQ(0, (int)vec.size());109}110111// Test the back function112TEST(FastVector, Back)113{114FastVector<int, 5> vec;115vec.push_back(1);116vec.push_back(2);117EXPECT_EQ(2, vec.back());118}119120// Test the back function121TEST(FastVector, Front)122{123FastVector<int, 5> vec;124vec.push_back(1);125vec.push_back(2);126EXPECT_EQ(1, vec.front());127}128129// Test the sizing operations130TEST(FastVector, Size)131{132FastVector<int, 5> vec;133EXPECT_TRUE(vec.empty());134EXPECT_EQ(0u, vec.size());135136vec.push_back(1);137EXPECT_FALSE(vec.empty());138EXPECT_EQ(1u, vec.size());139}140141// Test clearing the vector142TEST(FastVector, Clear)143{144FastVector<int, 5> vec = {0, 1, 2, 3, 4};145vec.clear();146EXPECT_TRUE(vec.empty());147}148149// Test clearing the vector larger than the fixed size.150TEST(FastVector, ClearWithLargerThanFixedSize)151{152FastVector<int, 3> vec = {0, 1, 2, 3, 4};153vec.clear();154EXPECT_TRUE(vec.empty());155}156157// Test resizing the vector158TEST(FastVector, Resize)159{160FastVector<int, 5> vec;161vec.resize(5u, 1);162EXPECT_EQ(5u, vec.size());163for (int i : vec)164{165EXPECT_EQ(1, i);166}167168vec.resize(2u);169EXPECT_EQ(2u, vec.size());170for (int i : vec)171{172EXPECT_EQ(1, i);173}174175// Resize to larger than minimum176vec.resize(10u, 2);177EXPECT_EQ(10u, vec.size());178179for (size_t index = 0; index < 2u; ++index)180{181EXPECT_EQ(1, vec[index]);182}183for (size_t index = 2u; index < 10u; ++index)184{185EXPECT_EQ(2, vec[index]);186}187188// Resize back to smaller189vec.resize(2u, 2);190EXPECT_EQ(2u, vec.size());191}192193// Test iterating over the vector194TEST(FastVector, Iteration)195{196FastVector<int, 5> vec = {0, 1, 2, 3};197198int vistedCount = 0;199for (int value : vec)200{201EXPECT_EQ(vistedCount, value);202vistedCount++;203}204EXPECT_EQ(4, vistedCount);205}206207// Tests that equality comparisons work even if reserved size differs.208TEST(FastVector, EqualityWithDifferentReservedSizes)209{210FastVector<int, 3> vec1 = {1, 2, 3, 4, 5};211FastVector<int, 5> vec2 = {1, 2, 3, 4, 5};212EXPECT_EQ(vec1, vec2);213vec2.push_back(6);214EXPECT_NE(vec1, vec2);215}216217// Tests vector operations with a non copyable type.218TEST(FastVector, NonCopyable)219{220struct s : angle::NonCopyable221{222s() : x(0) {}223s(int xin) : x(xin) {}224s(s &&other) : x(other.x) {}225s &operator=(s &&other)226{227x = other.x;228return *this;229}230int x;231};232233FastVector<s, 3> vec;234vec.push_back(3);235EXPECT_EQ(3, vec[0].x);236237FastVector<s, 3> copy = std::move(vec);238EXPECT_EQ(1u, copy.size());239EXPECT_EQ(3, copy[0].x);240}241242// Basic functionality for FastUnorderedMap243TEST(FastUnorderedMap, BasicUsage)244{245FastUnorderedMap<int, bool, 3> testMap;246EXPECT_TRUE(testMap.empty());247EXPECT_EQ(testMap.size(), 0u);248249testMap.insert(5, true);250EXPECT_TRUE(testMap.contains(5));251EXPECT_EQ(testMap.size(), 1u);252253bool value = false;254EXPECT_TRUE(testMap.get(5, &value));255EXPECT_TRUE(value);256EXPECT_FALSE(testMap.get(6, &value));257258EXPECT_FALSE(testMap.empty());259testMap.clear();260EXPECT_TRUE(testMap.empty());261EXPECT_EQ(testMap.size(), 0u);262263for (int i = 0; i < 10; ++i)264{265testMap.insert(i, false);266}267268EXPECT_FALSE(testMap.empty());269EXPECT_EQ(testMap.size(), 10u);270271for (int i = 0; i < 10; ++i)272{273EXPECT_TRUE(testMap.contains(i));274EXPECT_TRUE(testMap.get(i, &value));275EXPECT_FALSE(value);276}277}278279// Basic functionality for FastUnorderedSet280TEST(FastUnorderedSet, BasicUsage)281{282FastUnorderedSet<int, 3> testMap;283EXPECT_TRUE(testMap.empty());284285testMap.insert(5);286EXPECT_TRUE(testMap.contains(5));287EXPECT_FALSE(testMap.contains(6));288EXPECT_FALSE(testMap.empty());289290testMap.clear();291EXPECT_TRUE(testMap.empty());292293for (int i = 0; i < 10; ++i)294{295testMap.insert(i);296}297298for (int i = 0; i < 10; ++i)299{300EXPECT_TRUE(testMap.contains(i));301}302}303304// Basic functionality for FastIntegerSet305TEST(FastIntegerSet, BasicUsage)306{307FastIntegerSet testMap;308EXPECT_TRUE(testMap.empty());309310testMap.insert(5);311EXPECT_TRUE(testMap.contains(5));312EXPECT_FALSE(testMap.contains(6));313EXPECT_FALSE(testMap.empty());314315testMap.clear();316EXPECT_TRUE(testMap.empty());317318for (int i = 0; i < 10; ++i)319{320testMap.insert(i);321}322323for (int i = 0; i < 10; ++i)324{325EXPECT_TRUE(testMap.contains(i));326}327}328329// Basic functionality for FastIntegerMap330TEST(FastIntegerMap, BasicUsage)331{332using KeyValuePair = std::pair<int, std::string>;333std::set<KeyValuePair> entries = {KeyValuePair(17, "testing"), KeyValuePair(63, "fast"),334KeyValuePair(97, "integer"), KeyValuePair(256, "map")};335336FastIntegerMap<std::string> testMap;337EXPECT_TRUE(testMap.empty());338339std::string str;340testMap.insert(entries.begin()->first, entries.begin()->second);341EXPECT_TRUE(testMap.contains(entries.begin()->first));342EXPECT_FALSE(testMap.contains(entries.rbegin()->first));343EXPECT_FALSE(testMap.empty());344EXPECT_EQ(testMap.size(), 1u);345EXPECT_TRUE(testMap.get(entries.begin()->first, &str));346EXPECT_EQ(entries.begin()->second, str);347EXPECT_FALSE(testMap.get(1, &str));348349testMap.clear();350EXPECT_TRUE(testMap.empty());351EXPECT_EQ(testMap.size(), 0u);352353for (KeyValuePair entry : entries)354{355testMap.insert(entry.first, entry.second);356}357EXPECT_EQ(testMap.size(), 4u);358359for (KeyValuePair entry : entries)360{361std::string str;362EXPECT_TRUE(testMap.get(entry.first, &str));363EXPECT_EQ(entry.second, str);364}365366testMap.clear();367EXPECT_TRUE(testMap.empty());368EXPECT_EQ(testMap.size(), 0u);369}370} // namespace angle371372373