From 97d4c2d5ec6b0fc75392b6f813d5546e51f94291 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Tue, 30 Jun 2015 11:16:03 -0700 Subject: [PATCH] Bug 968520 - Add nsTArray::Assign. r=froydnj --- xpcom/glue/nsTArray.h | 25 +++++++++++++++ xpcom/tests/gtest/TestTArray.cpp | 55 ++++++++++++++++++++++++++++++++ xpcom/tests/gtest/moz.build | 1 + 3 files changed, 81 insertions(+) create mode 100644 xpcom/tests/gtest/TestTArray.cpp diff --git a/xpcom/glue/nsTArray.h b/xpcom/glue/nsTArray.h index 1e9d0e0e5eb..5d58ae5849a 100644 --- a/xpcom/glue/nsTArray.h +++ b/xpcom/glue/nsTArray.h @@ -1175,6 +1175,31 @@ public: // // Mutation methods // + + template + typename ActualAlloc::ResultType Assign( + const nsTArray_Impl& aOther) + { + return ActualAlloc::ConvertBoolToResultType( + !!ReplaceElementsAt(0, Length(), + aOther.Elements(), aOther.Length())); + } + + template + /* MOZ_WARN_UNUSED_RESULT */ + bool Assign(const nsTArray_Impl& aOther, + const mozilla::fallible_t&) + { + return Assign(aOther); + } + + template + void Assign(nsTArray_Impl&& 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. diff --git a/xpcom/tests/gtest/TestTArray.cpp b/xpcom/tests/gtest/TestTArray.cpp new file mode 100644 index 00000000000..7bcb63a28f9 --- /dev/null +++ b/xpcom/tests/gtest/TestTArray.cpp @@ -0,0 +1,55 @@ +/* -*- 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& DummyArray() +{ + static nsTArray 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. +const nsTArray& FakeHugeArray() +{ + static nsTArray sArray; +#ifdef DEBUG + if (sArray.IsEmpty()) { + sArray.AppendElement(); + ((nsTArrayHeader*)sArray.DebugGetHeader())->mLength = UINT32_MAX; + } +#endif + return sArray; +} + +TEST(TArray, assign) +{ + nsTArray array; + array.Assign(DummyArray()); + ASSERT_EQ(DummyArray(), array); + + ASSERT_TRUE(array.Assign(DummyArray(), fallible)); + ASSERT_EQ(DummyArray(), array); + + ASSERT_FALSE(array.Assign(FakeHugeArray(), fallible)); + + nsTArray array2; + array2.Assign(Move(array)); + ASSERT_TRUE(array.IsEmpty()); + ASSERT_EQ(DummyArray(), array2); +} + +} diff --git a/xpcom/tests/gtest/moz.build b/xpcom/tests/gtest/moz.build index 32bd5d11747..6c47bcee91c 100644 --- a/xpcom/tests/gtest/moz.build +++ b/xpcom/tests/gtest/moz.build @@ -17,6 +17,7 @@ UNIFIED_SOURCES += [ 'TestStrings.cpp', 'TestStringStream.cpp', 'TestSynchronization.cpp', + 'TestTArray.cpp', 'TestThreadPool.cpp', 'TestThreads.cpp', 'TestTimeStamp.cpp',