зеркало из https://github.com/mozilla/gecko-dev.git
Bug 968520 - Add nsTArray::Assign. r=froydnj
This commit is contained in:
Родитель
e98d080be8
Коммит
30fedff9e1
|
@ -1175,6 +1175,31 @@ public:
|
|||
//
|
||||
// Mutation methods
|
||||
//
|
||||
|
||||
template<class Allocator, typename ActualAlloc = Alloc>
|
||||
typename ActualAlloc::ResultType Assign(
|
||||
const nsTArray_Impl<E, Allocator>& aOther)
|
||||
{
|
||||
return ActualAlloc::ConvertBoolToResultType(
|
||||
!!ReplaceElementsAt<E, ActualAlloc>(0, Length(),
|
||||
aOther.Elements(), aOther.Length()));
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
/* MOZ_WARN_UNUSED_RESULT */
|
||||
bool Assign(const nsTArray_Impl<E, Allocator>& aOther,
|
||||
const mozilla::fallible_t&)
|
||||
{
|
||||
return Assign<Allocator, FallibleAlloc>(aOther);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
void Assign(nsTArray_Impl<E, Allocator>&& aOther)
|
||||
{
|
||||
Clear();
|
||||
SwapElements(aOther);
|
||||
}
|
||||
|
||||
// This method call the destructor on each element of the array, empties it,
|
||||
// but does not shrink the array's capacity.
|
||||
// See also SetLengthAndRetainStorage.
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "nsTArray.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace mozilla;
|
||||
|
||||
namespace TestTArray {
|
||||
|
||||
const nsTArray<int>& DummyArray()
|
||||
{
|
||||
static nsTArray<int> sArray;
|
||||
if (sArray.IsEmpty()) {
|
||||
const int data[] = {4, 1, 2, 8};
|
||||
sArray.AppendElements(data, ArrayLength(data));
|
||||
}
|
||||
return sArray;
|
||||
}
|
||||
|
||||
// This returns an invalid nsTArray with a huge length in order to test that
|
||||
// fallible operations actually fail.
|
||||
#ifdef DEBUG
|
||||
const nsTArray<int>& FakeHugeArray()
|
||||
{
|
||||
static nsTArray<int> sArray;
|
||||
if (sArray.IsEmpty()) {
|
||||
sArray.AppendElement();
|
||||
((nsTArrayHeader*)sArray.DebugGetHeader())->mLength = UINT32_MAX;
|
||||
}
|
||||
return sArray;
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(TArray, assign)
|
||||
{
|
||||
nsTArray<int> array;
|
||||
array.Assign(DummyArray());
|
||||
ASSERT_EQ(DummyArray(), array);
|
||||
|
||||
ASSERT_TRUE(array.Assign(DummyArray(), fallible));
|
||||
ASSERT_EQ(DummyArray(), array);
|
||||
|
||||
#ifdef DEBUG
|
||||
ASSERT_FALSE(array.Assign(FakeHugeArray(), fallible));
|
||||
#endif
|
||||
|
||||
nsTArray<int> array2;
|
||||
array2.Assign(Move(array));
|
||||
ASSERT_TRUE(array.IsEmpty());
|
||||
ASSERT_EQ(DummyArray(), array2);
|
||||
}
|
||||
|
||||
}
|
|
@ -17,6 +17,7 @@ UNIFIED_SOURCES += [
|
|||
'TestStrings.cpp',
|
||||
'TestStringStream.cpp',
|
||||
'TestSynchronization.cpp',
|
||||
'TestTArray.cpp',
|
||||
'TestThreadPool.cpp',
|
||||
'TestThreads.cpp',
|
||||
'TestTimeStamp.cpp',
|
||||
|
|
Загрузка…
Ссылка в новой задаче