2005-11-23 22:44:16 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2015-09-16 01:30:44 +03:00
|
|
|
#include "mozilla/unused.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-05-09 05:03:35 +04:00
|
|
|
#include <iostream>
|
2005-11-23 22:44:16 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2011-09-22 19:22:20 +04:00
|
|
|
#include "nsStringAPI.h"
|
2005-11-23 22:44:16 +03:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-08-18 20:45:38 +04:00
|
|
|
namespace TestTArray {
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
// Define this so we can use test_basic_array in test_comptr_array
|
|
|
|
template <class T>
|
|
|
|
inline bool operator<(const nsCOMPtr<T>& lhs, const nsCOMPtr<T>& rhs) {
|
|
|
|
return lhs.get() < rhs.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
|
|
|
template <class ElementType>
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_basic_array(ElementType *data,
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t dataLen,
|
|
|
|
const ElementType& extra) {
|
2005-11-23 22:44:16 +03:00
|
|
|
nsTArray<ElementType> ary;
|
|
|
|
ary.AppendElements(data, dataLen);
|
2006-10-18 01:40:07 +04:00
|
|
|
if (ary.Length() != dataLen) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
}
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == ary)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
}
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t i;
|
2005-11-23 22:44:16 +03:00
|
|
|
for (i = 0; i < ary.Length(); ++i) {
|
|
|
|
if (ary[i] != data[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2006-10-18 01:40:07 +04:00
|
|
|
for (i = 0; i < ary.Length(); ++i) {
|
|
|
|
if (ary.SafeElementAt(i, extra) != data[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
}
|
|
|
|
if (ary.SafeElementAt(ary.Length(), extra) != extra ||
|
|
|
|
ary.SafeElementAt(ary.Length() * 10, extra) != extra)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
// ensure sort results in ascending order
|
|
|
|
ary.Sort();
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t j = 0, k = ary.IndexOfFirstElementGt(extra);
|
2013-02-13 19:11:53 +04:00
|
|
|
if (k != 0 && ary[k-1] == extra)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-02-20 18:26:12 +03:00
|
|
|
for (i = 0; i < ary.Length(); ++i) {
|
2013-02-13 19:11:53 +04:00
|
|
|
k = ary.IndexOfFirstElementGt(ary[i]);
|
|
|
|
if (k == 0 || ary[k-1] != ary[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-02-20 18:26:12 +03:00
|
|
|
if (k < j)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-02-20 18:26:12 +03:00
|
|
|
j = k;
|
|
|
|
}
|
2008-02-21 12:43:15 +03:00
|
|
|
for (i = ary.Length(); --i; ) {
|
|
|
|
if (ary[i] < ary[i - 1])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-02-21 12:43:15 +03:00
|
|
|
if (ary[i] == ary[i - 1])
|
|
|
|
ary.RemoveElementAt(i);
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == ary)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
}
|
2008-02-21 12:43:15 +03:00
|
|
|
for (i = 0; i < ary.Length(); ++i) {
|
|
|
|
if (ary.BinaryIndexOf(ary[i]) != i)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-02-21 12:43:15 +03:00
|
|
|
}
|
2010-04-02 20:34:31 +04:00
|
|
|
if (ary.BinaryIndexOf(extra) != ary.NoIndex)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t oldLen = ary.Length();
|
2005-11-23 22:44:16 +03:00
|
|
|
ary.RemoveElement(data[dataLen / 2]);
|
|
|
|
if (ary.Length() != (oldLen - 1))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == ary))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t index = ary.Length() / 2;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (!ary.InsertElementAt(index, extra))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == ary))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (ary[index] != extra)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-05-09 05:03:35 +04:00
|
|
|
if (ary.IndexOf(extra) == ary.NoIndex)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-05-09 05:03:35 +04:00
|
|
|
if (ary.LastIndexOf(extra) == ary.NoIndex)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
// ensure proper searching
|
|
|
|
if (ary.IndexOf(extra) > ary.LastIndexOf(extra))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (ary.IndexOf(extra, index) != ary.LastIndexOf(extra, index))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
|
|
|
|
nsTArray<ElementType> copy(ary);
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == copy))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
for (i = 0; i < copy.Length(); ++i) {
|
|
|
|
if (ary[i] != copy[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
if (!ary.AppendElements(copy))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-06-02 13:26:12 +04:00
|
|
|
size_t cap = ary.Capacity();
|
2005-11-23 22:44:16 +03:00
|
|
|
ary.RemoveElementsAt(copy.Length(), copy.Length());
|
|
|
|
ary.Compact();
|
2014-06-02 13:26:12 +04:00
|
|
|
if (ary.Capacity() == cap)
|
|
|
|
return false;
|
|
|
|
|
2012-10-05 02:23:41 +04:00
|
|
|
ary.Clear();
|
2014-05-09 05:03:35 +04:00
|
|
|
if (ary.IndexOf(extra) != ary.NoIndex)
|
2012-10-05 02:23:41 +04:00
|
|
|
return false;
|
2014-05-09 05:03:35 +04:00
|
|
|
if (ary.LastIndexOf(extra) != ary.NoIndex)
|
2012-10-05 02:23:41 +04:00
|
|
|
return false;
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
ary.Clear();
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!ary.IsEmpty() || ary.Elements() == nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == nsTArray<ElementType>()))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (ary == copy)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
if (ary.SafeElementAt(0, extra) != extra ||
|
|
|
|
ary.SafeElementAt(10, extra) != extra)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
|
|
|
|
ary = copy;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (!(ary == copy))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
for (i = 0; i < copy.Length(); ++i) {
|
|
|
|
if (ary[i] != copy[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2006-02-08 04:23:26 +03:00
|
|
|
if (!ary.InsertElementsAt(0, copy))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 23:35:41 +04:00
|
|
|
if (ary == copy)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-08 04:23:26 +03:00
|
|
|
ary.RemoveElementsAt(0, copy.Length());
|
|
|
|
for (i = 0; i < copy.Length(); ++i) {
|
|
|
|
if (ary[i] != copy[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-08 04:23:26 +03:00
|
|
|
}
|
|
|
|
|
2005-11-28 23:27:42 +03:00
|
|
|
// These shouldn't crash!
|
|
|
|
nsTArray<ElementType> empty;
|
2007-07-08 11:08:04 +04:00
|
|
|
ary.AppendElements(reinterpret_cast<ElementType *>(0), 0);
|
2005-11-28 23:27:42 +03:00
|
|
|
ary.AppendElements(empty);
|
|
|
|
|
2006-01-30 20:16:50 +03:00
|
|
|
// See bug 324981
|
|
|
|
ary.RemoveElement(extra);
|
|
|
|
ary.RemoveElement(extra);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_int_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
int data[] = {4,6,8,2,4,1,5,7,3};
|
2011-10-11 09:50:08 +04:00
|
|
|
return test_basic_array(data, ArrayLength(data), int(14));
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_int64_array() {
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t data[] = {4,6,8,2,4,1,5,7,3};
|
|
|
|
return test_basic_array(data, ArrayLength(data), int64_t(14));
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_char_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
char data[] = {4,6,8,2,4,1,5,7,3};
|
2011-10-11 09:50:08 +04:00
|
|
|
return test_basic_array(data, ArrayLength(data), char(14));
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_uint32_array() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t data[] = {4,6,8,2,4,1,5,7,3};
|
|
|
|
return test_basic_array(data, ArrayLength(data), uint32_t(14));
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
|
|
|
class Object {
|
|
|
|
public:
|
|
|
|
Object() : mNum(0) {
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
Object(const char *str, uint32_t num) : mStr(str), mNum(num) {
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
Object(const Object& other) : mStr(other.mStr), mNum(other.mNum) {
|
|
|
|
}
|
|
|
|
~Object() {}
|
|
|
|
|
|
|
|
Object& operator=(const Object& other) {
|
|
|
|
mStr = other.mStr;
|
|
|
|
mNum = other.mNum;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator==(const Object& other) const {
|
2005-11-23 22:44:16 +03:00
|
|
|
return mStr == other.mStr && mNum == other.mNum;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator<(const Object& other) const {
|
2005-11-23 22:44:16 +03:00
|
|
|
// sort based on mStr only
|
2011-09-22 19:22:20 +04:00
|
|
|
return mStr.Compare(other.mStr) < 0;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *Str() const { return mStr.get(); }
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Num() const { return mNum; }
|
2005-11-23 22:44:16 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mStr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mNum;
|
2005-11-23 22:44:16 +03:00
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_object_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
nsTArray<Object> objArray;
|
|
|
|
const char kdata[] = "hello world";
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t i;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kdata); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
char x[] = {kdata[i],'\0'};
|
|
|
|
if (!objArray.AppendElement(Object(x, i)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kdata); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
if (objArray[i].Str()[0] != kdata[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (objArray[i].Num() != i)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
objArray.Sort();
|
|
|
|
const char ksorted[] = "\0 dehllloorw";
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kdata)-1; ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
if (objArray[i].Str()[0] != ksorted[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
|
2014-03-15 21:55:00 +04:00
|
|
|
class Countable {
|
|
|
|
static uint32_t sCount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Countable()
|
|
|
|
{
|
|
|
|
sCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Countable(const Countable& aOther)
|
|
|
|
{
|
|
|
|
sCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t Count() { return sCount; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Moveable {
|
|
|
|
static uint32_t sCount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Moveable()
|
|
|
|
{
|
|
|
|
sCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Moveable(const Moveable& aOther)
|
|
|
|
{
|
|
|
|
sCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Moveable(Moveable&& aOther)
|
|
|
|
{
|
|
|
|
// Do not increment sCount
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t Count() { return sCount; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ uint32_t Countable::sCount = 0;
|
|
|
|
/* static */ uint32_t Moveable::sCount = 0;
|
|
|
|
|
2015-04-21 01:42:56 +03:00
|
|
|
static nsTArray<int> returns_by_value() {
|
|
|
|
nsTArray<int> result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_return_by_value() {
|
|
|
|
nsTArray<int> result = returns_by_value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-15 21:55:00 +04:00
|
|
|
static bool test_move_array() {
|
|
|
|
nsTArray<Countable> countableArray;
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
if (!countableArray.AppendElement(Countable()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Countable::Count() != 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const nsTArray<Countable>& constRefCountableArray = countableArray;
|
|
|
|
|
|
|
|
if (Countable::Count() != 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Countable> copyCountableArray(constRefCountableArray);
|
|
|
|
|
|
|
|
if (Countable::Count() != 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Countable>&& moveRefCountableArray = Move(countableArray);
|
|
|
|
moveRefCountableArray.Length(); // Make compilers happy.
|
|
|
|
|
|
|
|
if (Countable::Count() != 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Countable> movedCountableArray(Move(countableArray));
|
|
|
|
|
|
|
|
if (Countable::Count() != 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Test ctor
|
|
|
|
FallibleTArray<Countable> differentAllocatorCountableArray(Move(copyCountableArray));
|
|
|
|
// operator=
|
|
|
|
copyCountableArray = Move(differentAllocatorCountableArray);
|
|
|
|
differentAllocatorCountableArray = Move(copyCountableArray);
|
|
|
|
// And the other ctor
|
|
|
|
nsTArray<Countable> copyCountableArray2(Move(differentAllocatorCountableArray));
|
|
|
|
// with auto
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<Countable, 3> autoCountableArray(Move(copyCountableArray2));
|
2014-03-15 21:55:00 +04:00
|
|
|
// operator=
|
|
|
|
copyCountableArray2 = Move(autoCountableArray);
|
|
|
|
// Mix with FallibleTArray
|
|
|
|
FallibleTArray<Countable> differentAllocatorCountableArray2(Move(copyCountableArray2));
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<Countable, 4> autoCountableArray2(Move(differentAllocatorCountableArray2));
|
2014-03-15 21:55:00 +04:00
|
|
|
differentAllocatorCountableArray2 = Move(autoCountableArray2);
|
|
|
|
|
|
|
|
if (Countable::Count() != 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Moveable> moveableArray;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
if (!moveableArray.AppendElement(Moveable()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 4)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const nsTArray<Moveable>& constRefMoveableArray = moveableArray;
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 4)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Moveable> copyMoveableArray(constRefMoveableArray);
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 8)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Moveable>&& moveRefMoveableArray = Move(moveableArray);
|
|
|
|
moveRefMoveableArray.Length(); // Make compilers happy.
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 8)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
nsTArray<Moveable> movedMoveableArray(Move(moveableArray));
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 8)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Test ctor
|
|
|
|
FallibleTArray<Moveable> differentAllocatorMoveableArray(Move(copyMoveableArray));
|
|
|
|
// operator=
|
|
|
|
copyMoveableArray = Move(differentAllocatorMoveableArray);
|
|
|
|
differentAllocatorMoveableArray = Move(copyMoveableArray);
|
|
|
|
// And the other ctor
|
|
|
|
nsTArray<Moveable> copyMoveableArray2(Move(differentAllocatorMoveableArray));
|
|
|
|
// with auto
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<Moveable, 3> autoMoveableArray(Move(copyMoveableArray2));
|
2014-03-15 21:55:00 +04:00
|
|
|
// operator=
|
|
|
|
copyMoveableArray2 = Move(autoMoveableArray);
|
|
|
|
// Mix with FallibleTArray
|
|
|
|
FallibleTArray<Moveable> differentAllocatorMoveableArray2(Move(copyMoveableArray2));
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<Moveable, 4> autoMoveableArray2(Move(differentAllocatorMoveableArray2));
|
2014-03-15 21:55:00 +04:00
|
|
|
differentAllocatorMoveableArray2 = Move(autoMoveableArray2);
|
|
|
|
|
2014-03-15 21:56:00 +04:00
|
|
|
if (Moveable::Count() != 8)
|
2014-03-15 21:55:00 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
// nsTArray<nsAutoPtr<T>> is not supported
|
|
|
|
#if 0
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_autoptr_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
nsTArray< nsAutoPtr<Object> > objArray;
|
|
|
|
const char kdata[] = "hello world";
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(kdata); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
char x[] = {kdata[i],'\0'};
|
|
|
|
nsAutoPtr<Object> obj(new Object(x,i));
|
|
|
|
if (!objArray.AppendElement(obj)) // XXX does not call copy-constructor for nsAutoPtr!!!
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (obj.get() == nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
obj.forget(); // the array now owns the reference
|
|
|
|
}
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(kdata); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
if (objArray[i]->Str()[0] != kdata[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (objArray[i]->Num() != i)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_string_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
nsTArray<nsCString> strArray;
|
|
|
|
const char kdata[] = "hello world";
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t i;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kdata); ++i) {
|
2011-09-22 19:22:20 +04:00
|
|
|
nsCString str;
|
|
|
|
str.Assign(kdata[i]);
|
|
|
|
if (!strArray.AppendElement(str))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kdata); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
if (strArray[i].CharAt(0) != kdata[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2005-12-13 00:28:29 +03:00
|
|
|
|
2006-02-08 04:23:26 +03:00
|
|
|
const char kextra[] = "foo bar";
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t oldLen = strArray.Length();
|
2006-02-08 04:23:26 +03:00
|
|
|
if (!strArray.AppendElement(kextra))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-08 04:23:26 +03:00
|
|
|
strArray.RemoveElement(kextra);
|
|
|
|
if (oldLen != strArray.Length())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-02-08 04:23:26 +03:00
|
|
|
|
2005-12-13 00:28:29 +03:00
|
|
|
if (strArray.IndexOf("e") != 1)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-12-13 00:28:29 +03:00
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
strArray.Sort();
|
|
|
|
const char ksorted[] = "\0 dehllloorw";
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = ArrayLength(kdata); i--; ) {
|
2005-11-23 22:44:16 +03:00
|
|
|
if (strArray[i].CharAt(0) != ksorted[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-02-21 12:43:15 +03:00
|
|
|
if (i > 0 && strArray[i] == strArray[i - 1])
|
|
|
|
strArray.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
for (i = 0; i < strArray.Length(); ++i) {
|
|
|
|
if (strArray.BinaryIndexOf(strArray[i]) != i)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
2010-04-02 20:34:31 +04:00
|
|
|
if (strArray.BinaryIndexOf(EmptyCString()) != strArray.NoIndex)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2014-01-30 22:26:54 +04:00
|
|
|
nsCString rawArray[MOZ_ARRAY_LENGTH(kdata) - 1];
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(rawArray); ++i)
|
2005-11-23 22:44:16 +03:00
|
|
|
rawArray[i].Assign(kdata + i); // substrings of kdata
|
2011-10-11 09:50:08 +04:00
|
|
|
return test_basic_array(rawArray, ArrayLength(rawArray),
|
2005-11-23 22:44:16 +03:00
|
|
|
nsCString("foopy"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2005-12-13 00:28:29 +03:00
|
|
|
typedef nsCOMPtr<nsIFile> FilePointer;
|
|
|
|
|
|
|
|
class nsFileNameComparator {
|
|
|
|
public:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const FilePointer &a, const char *b) const {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name;
|
2005-12-13 00:28:29 +03:00
|
|
|
a->GetNativeLeafName(name);
|
|
|
|
return name.Equals(b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_comptr_array() {
|
2005-11-23 22:44:16 +03:00
|
|
|
FilePointer tmpDir;
|
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpDir));
|
|
|
|
if (!tmpDir)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
const char *kNames[] = {
|
|
|
|
"foo.txt", "bar.html", "baz.gif"
|
|
|
|
};
|
|
|
|
nsTArray<FilePointer> fileArray;
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t i;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(kNames); ++i) {
|
2005-11-23 22:44:16 +03:00
|
|
|
FilePointer f;
|
|
|
|
tmpDir->Clone(getter_AddRefs(f));
|
|
|
|
if (!f)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
if (NS_FAILED(f->AppendNative(nsDependentCString(kNames[i]))))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-23 22:44:16 +03:00
|
|
|
fileArray.AppendElement(f);
|
|
|
|
}
|
2005-12-13 00:28:29 +03:00
|
|
|
|
2005-12-13 21:27:04 +03:00
|
|
|
if (fileArray.IndexOf(kNames[1], 0, nsFileNameComparator()) != 1)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-12-13 00:28:29 +03:00
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
// It's unclear what 'operator<' means for nsCOMPtr, but whatever...
|
|
|
|
return test_basic_array(fileArray.Elements(), fileArray.Length(),
|
|
|
|
tmpDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2005-12-13 00:28:29 +03:00
|
|
|
class RefcountedObject {
|
|
|
|
public:
|
|
|
|
RefcountedObject() : rc(0) {}
|
|
|
|
void AddRef() {
|
|
|
|
++rc;
|
|
|
|
}
|
|
|
|
void Release() {
|
|
|
|
if (--rc == 0)
|
|
|
|
delete this;
|
|
|
|
}
|
2006-02-04 04:21:54 +03:00
|
|
|
~RefcountedObject() {}
|
2006-02-08 04:23:26 +03:00
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t rc;
|
2005-12-13 00:28:29 +03:00
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_refptr_array() {
|
|
|
|
bool rv = true;
|
2005-12-13 00:28:29 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray< RefPtr<RefcountedObject> > objArray;
|
2005-12-13 00:28:29 +03:00
|
|
|
|
|
|
|
RefcountedObject *a = new RefcountedObject(); a->AddRef();
|
|
|
|
RefcountedObject *b = new RefcountedObject(); b->AddRef();
|
|
|
|
RefcountedObject *c = new RefcountedObject(); c->AddRef();
|
|
|
|
|
|
|
|
objArray.AppendElement(a);
|
|
|
|
objArray.AppendElement(b);
|
|
|
|
objArray.AppendElement(c);
|
|
|
|
|
|
|
|
if (objArray.IndexOf(b) != 1)
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = false;
|
2005-12-13 00:28:29 +03:00
|
|
|
|
|
|
|
a->Release();
|
|
|
|
b->Release();
|
|
|
|
c->Release();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_ptrarray() {
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<uint32_t*> ary;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (ary.SafeElementAt(0) != nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (ary.SafeElementAt(1000) != nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t a = 10;
|
2006-10-18 01:40:07 +04:00
|
|
|
ary.AppendElement(&a);
|
|
|
|
if (*ary[0] != a)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
if (*ary.SafeElementAt(0) != a)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<const uint32_t*> cary;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (cary.SafeElementAt(0) != nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (cary.SafeElementAt(1000) != nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t b = 14;
|
2006-10-18 01:40:07 +04:00
|
|
|
cary.AppendElement(&a);
|
|
|
|
cary.AppendElement(&b);
|
|
|
|
if (*cary[0] != a || *cary[1] != b)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
if (*cary.SafeElementAt(0) != a || *cary.SafeElementAt(1) != b)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-10-18 01:40:07 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-10-18 01:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2010-05-13 16:19:50 +04:00
|
|
|
// This test relies too heavily on the existence of DebugGetHeader to be
|
2006-11-03 03:52:40 +03:00
|
|
|
// useful in non-debug builds.
|
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_autoarray() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t data[] = {4,6,8,2,4,1,5,7,3};
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<uint32_t, MOZ_ARRAY_LENGTH(data)> array;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
|
|
|
void* hdr = array.DebugGetHeader();
|
2012-08-22 19:56:38 +04:00
|
|
|
if (hdr == nsTArray<uint32_t>().DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2016-02-02 18:36:30 +03:00
|
|
|
if (hdr == AutoTArray<uint32_t, MOZ_ARRAY_LENGTH(data)>().DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2006-11-04 08:14:55 +03:00
|
|
|
array.AppendElement(1u);
|
2006-11-02 22:33:10 +03:00
|
|
|
if (hdr != array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2006-11-04 08:14:55 +03:00
|
|
|
array.RemoveElement(1u);
|
2011-10-11 09:50:08 +04:00
|
|
|
array.AppendElements(data, ArrayLength(data));
|
2006-11-02 22:33:10 +03:00
|
|
|
if (hdr != array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2006-11-04 08:14:55 +03:00
|
|
|
array.AppendElement(2u);
|
2006-11-02 22:33:10 +03:00
|
|
|
if (hdr == array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
|
|
|
array.Clear();
|
|
|
|
array.Compact();
|
|
|
|
if (hdr != array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-10-11 09:50:08 +04:00
|
|
|
array.AppendElements(data, ArrayLength(data));
|
2006-11-02 22:33:10 +03:00
|
|
|
if (hdr != array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<uint32_t> array2;
|
2006-11-02 22:33:10 +03:00
|
|
|
void* emptyHdr = array2.DebugGetHeader();
|
|
|
|
array.SwapElements(array2);
|
|
|
|
if (emptyHdr == array.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
if (hdr == array2.DebugGetHeader())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t i;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(data); ++i) {
|
2006-11-02 22:33:10 +03:00
|
|
|
if (array2[i] != data[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
}
|
|
|
|
if (!array.IsEmpty())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
|
|
|
array.Compact();
|
2011-10-11 09:50:08 +04:00
|
|
|
array.AppendElements(data, ArrayLength(data));
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t data3[] = {5, 7, 11};
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<uint32_t, MOZ_ARRAY_LENGTH(data3)> array3;
|
2011-10-11 09:50:08 +04:00
|
|
|
array3.AppendElements(data3, ArrayLength(data3));
|
2006-11-02 22:33:10 +03:00
|
|
|
array.SwapElements(array3);
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(data); ++i) {
|
2006-11-02 22:33:10 +03:00
|
|
|
if (array3[i] != data[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
}
|
2011-10-11 09:50:08 +04:00
|
|
|
for (i = 0; i < ArrayLength(data3); ++i) {
|
2006-11-02 22:33:10 +03:00
|
|
|
if (array[i] != data3[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-11-02 22:33:10 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-11-02 22:33:10 +03:00
|
|
|
}
|
2006-11-03 03:52:40 +03:00
|
|
|
#endif
|
2006-11-02 22:33:10 +03:00
|
|
|
|
|
|
|
//----
|
|
|
|
|
2009-04-11 05:08:08 +04:00
|
|
|
// IndexOf used to potentially scan beyond the end of the array. Test for
|
|
|
|
// this incorrect behavior by adding a value (5), removing it, then seeing
|
|
|
|
// if IndexOf finds it.
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_indexof() {
|
2009-04-11 05:08:08 +04:00
|
|
|
nsTArray<int> array;
|
|
|
|
array.AppendElement(0);
|
|
|
|
// add and remove the 5
|
|
|
|
array.AppendElement(5);
|
|
|
|
array.RemoveElementAt(1);
|
|
|
|
// we should not find the 5!
|
2010-04-02 20:34:31 +04:00
|
|
|
return array.IndexOf(5, 1) == array.NoIndex;
|
2009-04-11 05:08:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2010-05-05 21:21:23 +04:00
|
|
|
template <class Array>
|
2014-05-09 05:03:35 +04:00
|
|
|
static bool is_heap(const Array& ary, size_t len) {
|
|
|
|
size_t index = 1;
|
2010-05-05 21:21:23 +04:00
|
|
|
while (index < len) {
|
|
|
|
if (ary[index] > ary[(index - 1) >> 1])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-05 21:21:23 +04:00
|
|
|
index++;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-05-05 21:21:23 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_heap() {
|
2010-05-05 21:21:23 +04:00
|
|
|
const int data[] = {4,6,8,2,4,1,5,7,3};
|
|
|
|
nsTArray<int> ary;
|
2011-10-11 09:50:08 +04:00
|
|
|
ary.AppendElements(data, ArrayLength(data));
|
2010-05-05 21:21:23 +04:00
|
|
|
// make a heap and make sure it's a heap
|
|
|
|
ary.MakeHeap();
|
2011-10-11 09:50:08 +04:00
|
|
|
if (!is_heap(ary, ArrayLength(data)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-05 21:21:23 +04:00
|
|
|
// pop the root and make sure it's still a heap
|
|
|
|
int root = ary[0];
|
|
|
|
ary.PopHeap();
|
2011-10-11 09:50:08 +04:00
|
|
|
if (!is_heap(ary, ArrayLength(data) - 1))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-05 21:21:23 +04:00
|
|
|
// push the previously poped value back on and make sure it's still a heap
|
|
|
|
ary.PushHeap(root);
|
2011-10-11 09:50:08 +04:00
|
|
|
if (!is_heap(ary, ArrayLength(data)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-05 21:21:23 +04:00
|
|
|
// make sure the heap looks like what we expect
|
|
|
|
const int expected_data[] = {8,7,5,6,4,1,4,2,3};
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t index;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (index = 0; index < ArrayLength(data); index++)
|
2010-05-05 21:21:23 +04:00
|
|
|
if (ary[index] != expected_data[index])
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
|
|
|
return true;
|
2010-05-05 21:21:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----
|
|
|
|
|
2011-09-22 19:22:20 +04:00
|
|
|
// An array |arr| is using its auto buffer if |&arr < arr.Elements()| and
|
|
|
|
// |arr.Elements() - &arr| is small.
|
|
|
|
|
|
|
|
#define IS_USING_AUTO(arr) \
|
|
|
|
((uintptr_t) &(arr) < (uintptr_t) arr.Elements() && \
|
2012-08-09 11:09:31 +04:00
|
|
|
((ptrdiff_t)arr.Elements() - (ptrdiff_t)&arr) <= 16)
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
#define CHECK_IS_USING_AUTO(arr) \
|
|
|
|
do { \
|
|
|
|
if (!(IS_USING_AUTO(arr))) { \
|
|
|
|
printf("%s:%d CHECK_IS_USING_AUTO(%s) failed.\n", \
|
|
|
|
__FILE__, __LINE__, #arr); \
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; \
|
2011-09-22 19:22:20 +04:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define CHECK_NOT_USING_AUTO(arr) \
|
|
|
|
do { \
|
|
|
|
if (IS_USING_AUTO(arr)) { \
|
|
|
|
printf("%s:%d CHECK_NOT_USING_AUTO(%s) failed.\n", \
|
|
|
|
__FILE__, __LINE__, #arr); \
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; \
|
2011-09-22 19:22:20 +04:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define CHECK_USES_SHARED_EMPTY_HDR(arr) \
|
|
|
|
do { \
|
|
|
|
nsTArray<int> _empty; \
|
|
|
|
if (_empty.Elements() != arr.Elements()) { \
|
|
|
|
printf("%s:%d CHECK_USES_EMPTY_HDR(%s) failed.\n", \
|
|
|
|
__FILE__, __LINE__, #arr); \
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; \
|
2011-09-22 19:22:20 +04:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define CHECK_EQ_INT(actual, expected) \
|
|
|
|
do { \
|
|
|
|
if ((actual) != (expected)) { \
|
2014-05-09 05:03:35 +04:00
|
|
|
std::cout << __FILE__ << ":" << __LINE__ << " CHECK_EQ_INT(" \
|
|
|
|
<< #actual << "=" << (actual) << ", " \
|
|
|
|
<< #expected << "=" << (expected) << ") failed." \
|
|
|
|
<< std::endl; \
|
|
|
|
return false; \
|
2011-09-22 19:22:20 +04:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define CHECK_ARRAY(arr, data) \
|
|
|
|
do { \
|
2014-05-09 05:03:35 +04:00
|
|
|
CHECK_EQ_INT((arr).Length(), (size_t)ArrayLength(data)); \
|
|
|
|
for (size_t _i = 0; _i < ArrayLength(data); _i++) { \
|
2011-09-22 19:22:20 +04:00
|
|
|
CHECK_EQ_INT((arr)[_i], (data)[_i]); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool test_swap() {
|
2011-09-22 19:22:20 +04:00
|
|
|
// Test nsTArray::SwapElements. Unfortunately there are many cases.
|
|
|
|
int data1[] = {8, 6, 7, 5};
|
|
|
|
int data2[] = {3, 0, 9};
|
|
|
|
|
|
|
|
// Swap two auto arrays.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 8> a;
|
|
|
|
AutoTArray<int, 6> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
CHECK_ARRAY(a, data2);
|
|
|
|
CHECK_ARRAY(b, data1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap two auto arrays -- one whose data lives on the heap, the other whose
|
|
|
|
// data lives on the stack -- which each fits into the other's auto storage.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 3> a;
|
|
|
|
AutoTArray<int, 3> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
2011-09-22 19:22:20 +04:00
|
|
|
a.RemoveElementAt(3);
|
2011-10-11 09:50:08 +04:00
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
// Here and elsewhere, we assert that if we start with an auto array
|
|
|
|
// capable of storing N elements, we store N+1 elements into the array, and
|
|
|
|
// then we remove one element, that array is still not using its auto
|
|
|
|
// buffer.
|
|
|
|
//
|
|
|
|
// This isn't at all required by the TArray API. It would be fine if, when
|
|
|
|
// we shrink back to N elements, the TArray frees its heap storage and goes
|
|
|
|
// back to using its stack storage. But we assert here as a check that the
|
|
|
|
// test does what we expect. If the TArray implementation changes, just
|
|
|
|
// change the failing assertions.
|
|
|
|
CHECK_NOT_USING_AUTO(a);
|
|
|
|
|
|
|
|
// This check had better not change, though.
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
CHECK_ARRAY(a, data2);
|
|
|
|
int expectedB[] = {8, 6, 7};
|
|
|
|
CHECK_ARRAY(b, expectedB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap two auto arrays which are using heap storage such that one fits into
|
|
|
|
// the other's auto storage, but the other needs to stay on the heap.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 3> a;
|
|
|
|
AutoTArray<int, 2> b;
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
2011-09-22 19:22:20 +04:00
|
|
|
a.RemoveElementAt(3);
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
b.RemoveElementAt(2);
|
|
|
|
|
|
|
|
CHECK_NOT_USING_AUTO(a);
|
|
|
|
CHECK_NOT_USING_AUTO(b);
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_NOT_USING_AUTO(b);
|
|
|
|
|
|
|
|
int expected1[] = {3, 0};
|
|
|
|
int expected2[] = {8, 6, 7};
|
|
|
|
|
|
|
|
CHECK_ARRAY(a, expected1);
|
|
|
|
CHECK_ARRAY(b, expected2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap two arrays, neither of which fits into the other's auto-storage.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 1> a;
|
|
|
|
AutoTArray<int, 3> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_ARRAY(a, data2);
|
|
|
|
CHECK_ARRAY(b, data1);
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Swap an empty nsTArray with a non-empty AutoTArray.
|
2011-09-22 19:22:20 +04:00
|
|
|
{
|
|
|
|
nsTArray<int> a;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 3> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_ARRAY(a, data2);
|
|
|
|
CHECK_EQ_INT(b.Length(), 0);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap two big auto arrays.
|
|
|
|
{
|
2012-10-20 00:26:50 +04:00
|
|
|
const unsigned size = 8192;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<unsigned, size> a;
|
|
|
|
AutoTArray<unsigned, size> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2012-10-20 00:26:50 +04:00
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2011-09-22 19:22:20 +04:00
|
|
|
a.AppendElement(i);
|
|
|
|
b.AppendElement(i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
|
|
|
|
CHECK_EQ_INT(a.Length(), size);
|
|
|
|
CHECK_EQ_INT(b.Length(), size);
|
|
|
|
|
2012-10-20 00:26:50 +04:00
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2011-09-22 19:22:20 +04:00
|
|
|
CHECK_EQ_INT(a[i], i + 1);
|
|
|
|
CHECK_EQ_INT(b[i], i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap two arrays and make sure that their capacities don't increase
|
|
|
|
// unnecessarily.
|
|
|
|
{
|
|
|
|
nsTArray<int> a;
|
|
|
|
nsTArray<int> b;
|
2011-10-11 09:50:08 +04:00
|
|
|
b.AppendElements(data2, ArrayLength(data2));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
CHECK_EQ_INT(a.Capacity(), 0);
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t bCapacity = b.Capacity();
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
// Make sure that we didn't increase the capacity of either array.
|
|
|
|
CHECK_ARRAY(a, data2);
|
|
|
|
CHECK_EQ_INT(b.Length(), 0);
|
|
|
|
CHECK_EQ_INT(b.Capacity(), 0);
|
|
|
|
CHECK_EQ_INT(a.Capacity(), bCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap an auto array with a TArray, then clear the auto array and make sure
|
|
|
|
// it doesn't forget the fact that it has an auto buffer.
|
|
|
|
{
|
|
|
|
nsTArray<int> a;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 3> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_EQ_INT(a.Length(), 0);
|
|
|
|
CHECK_ARRAY(b, data1);
|
|
|
|
|
|
|
|
b.Clear();
|
|
|
|
|
|
|
|
CHECK_USES_SHARED_EMPTY_HDR(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same thing as the previous test, but with more auto arrays.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 16> a;
|
|
|
|
AutoTArray<int, 3> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_EQ_INT(a.Length(), 0);
|
|
|
|
CHECK_ARRAY(b, data1);
|
|
|
|
|
|
|
|
b.Clear();
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_IS_USING_AUTO(b);
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Swap an empty nsTArray and an empty AutoTArray.
|
2011-09-22 19:22:20 +04:00
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 8> a;
|
2011-09-22 19:22:20 +04:00
|
|
|
nsTArray<int> b;
|
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_NOT_USING_AUTO(b);
|
|
|
|
CHECK_EQ_INT(a.Length(), 0);
|
|
|
|
CHECK_EQ_INT(b.Length(), 0);
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Swap empty auto array with non-empty AutoTArray using malloc'ed storage.
|
2011-09-22 19:22:20 +04:00
|
|
|
// I promise, all these tests have a point.
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 2> a;
|
|
|
|
AutoTArray<int, 1> b;
|
2011-09-22 19:22:20 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
a.AppendElements(data1, ArrayLength(data1));
|
2011-09-22 19:22:20 +04:00
|
|
|
|
|
|
|
a.SwapElements(b);
|
|
|
|
|
|
|
|
CHECK_IS_USING_AUTO(a);
|
|
|
|
CHECK_NOT_USING_AUTO(b);
|
|
|
|
CHECK_ARRAY(b, data1);
|
|
|
|
CHECK_EQ_INT(a.Length(), 0);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-09-22 19:22:20 +04:00
|
|
|
}
|
|
|
|
|
2012-01-26 21:51:34 +04:00
|
|
|
static bool test_fallible()
|
|
|
|
{
|
|
|
|
// Test that FallibleTArray works properly; that is, it never OOMs, but
|
|
|
|
// instead eventually returns false.
|
|
|
|
//
|
|
|
|
// This test is only meaningful on 32-bit systems. On a 64-bit system, we
|
|
|
|
// might never OOM.
|
|
|
|
if (sizeof(void*) > 4) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-29 21:48:08 +04:00
|
|
|
// Allocate a bunch of 128MB arrays. Larger allocations will fail on some
|
|
|
|
// platforms without actually hitting OOM.
|
2012-01-26 21:51:34 +04:00
|
|
|
//
|
2013-11-29 21:48:08 +04:00
|
|
|
// 36 * 128MB > 4GB, so we should definitely OOM by the 36th array.
|
|
|
|
const unsigned numArrays = 36;
|
2012-01-26 21:51:34 +04:00
|
|
|
FallibleTArray<char> arrays[numArrays];
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < numArrays; i++) {
|
2015-09-03 13:30:12 +03:00
|
|
|
// SetCapacity allocates the requested capacity + a header, and we want to
|
|
|
|
// avoid allocating more than 128MB overall because of the size padding it
|
|
|
|
// will cause, which depends on allocator behavior, so use 128MB - an
|
|
|
|
// arbitrary size larger than the array header, so that chances are good
|
|
|
|
// that allocations will always be 128MB.
|
|
|
|
bool success = arrays[i].SetCapacity(128 * 1024 * 1024 - 1024, fallible);
|
2012-01-26 21:51:34 +04:00
|
|
|
if (!success) {
|
|
|
|
// We got our OOM. Check that it didn't come too early.
|
2013-11-29 21:48:08 +04:00
|
|
|
if (i < 8) {
|
2014-05-09 05:03:35 +04:00
|
|
|
printf("test_fallible: Got OOM on iteration %d. Too early!\n", int(i));
|
2012-01-26 21:51:34 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No OOM? That's...weird.
|
|
|
|
printf("test_fallible: Didn't OOM or crash? nsTArray::SetCapacity "
|
|
|
|
"must be lying.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-19 05:16:05 +04:00
|
|
|
static bool test_conversion_operator() {
|
|
|
|
FallibleTArray<int> f;
|
|
|
|
const FallibleTArray<int> fconst;
|
|
|
|
|
|
|
|
InfallibleTArray<int> i;
|
|
|
|
const InfallibleTArray<int> iconst;
|
|
|
|
|
|
|
|
nsTArray<int> t;
|
|
|
|
const nsTArray<int> tconst;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, 8> tauto;
|
|
|
|
const AutoTArray<int, 8> tautoconst;
|
2012-12-19 05:16:05 +04:00
|
|
|
|
|
|
|
#define CHECK_ARRAY_CAST(type) \
|
|
|
|
do { \
|
|
|
|
const type<int>& z1 = f; \
|
|
|
|
if ((void*)&z1 != (void*)&f) return false; \
|
|
|
|
const type<int>& z2 = fconst; \
|
|
|
|
if ((void*)&z2 != (void*)&fconst) return false; \
|
|
|
|
const type<int>& z5 = i; \
|
|
|
|
if ((void*)&z5 != (void*)&i) return false; \
|
|
|
|
const type<int>& z6 = iconst; \
|
|
|
|
if ((void*)&z6 != (void*)&iconst) return false; \
|
|
|
|
const type<int>& z9 = t; \
|
|
|
|
if ((void*)&z9 != (void*)&t) return false; \
|
|
|
|
const type<int>& z10 = tconst; \
|
|
|
|
if ((void*)&z10 != (void*)&tconst) return false; \
|
|
|
|
const type<int>& z11 = tauto; \
|
|
|
|
if ((void*)&z11 != (void*)&tauto) return false; \
|
|
|
|
const type<int>& z12 = tautoconst; \
|
|
|
|
if ((void*)&z12 != (void*)&tautoconst) return false; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CHECK_ARRAY_CAST(FallibleTArray);
|
|
|
|
CHECK_ARRAY_CAST(InfallibleTArray);
|
|
|
|
CHECK_ARRAY_CAST(nsTArray);
|
|
|
|
|
|
|
|
#undef CHECK_ARRAY_CAST
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-08 06:05:02 +04:00
|
|
|
template<class T>
|
|
|
|
struct BufAccessor : public T
|
|
|
|
{
|
|
|
|
void* GetHdr() { return T::mHdr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool test_SetLengthAndRetainStorage_no_ctor() {
|
|
|
|
// 1050 because sizeof(int)*1050 is more than a page typically.
|
|
|
|
const int N = 1050;
|
|
|
|
FallibleTArray<int> f;
|
|
|
|
|
|
|
|
InfallibleTArray<int> i;
|
|
|
|
|
|
|
|
nsTArray<int> t;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int, N> tauto;
|
2013-09-08 06:05:02 +04:00
|
|
|
|
|
|
|
#define LPAREN (
|
|
|
|
#define RPAREN )
|
|
|
|
#define FOR_EACH(pre, post) \
|
|
|
|
do { \
|
|
|
|
pre f post; \
|
|
|
|
pre i post; \
|
|
|
|
pre t post; \
|
|
|
|
pre tauto post; \
|
|
|
|
} while (0)
|
2015-09-16 01:30:44 +03:00
|
|
|
|
2013-09-08 06:05:02 +04:00
|
|
|
// Setup test arrays.
|
2015-11-02 08:53:26 +03:00
|
|
|
FOR_EACH(; Unused << , .SetLength(N, fallible));
|
2013-09-08 06:05:02 +04:00
|
|
|
for (int n = 0; n < N; ++n) {
|
|
|
|
FOR_EACH(;, [n] = n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* initial_Hdrs[] = {
|
|
|
|
static_cast<BufAccessor<FallibleTArray<int> >&>(f).GetHdr(),
|
|
|
|
static_cast<BufAccessor<InfallibleTArray<int> >&>(i).GetHdr(),
|
|
|
|
static_cast<BufAccessor<nsTArray<int> >&>(t).GetHdr(),
|
2016-02-02 18:36:30 +03:00
|
|
|
static_cast<BufAccessor<AutoTArray<int, N> >&>(tauto).GetHdr(),
|
2013-09-08 06:05:02 +04:00
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
// SetLengthAndRetainStorage(n), should NOT overwrite memory when T hasn't
|
|
|
|
// a default constructor.
|
|
|
|
FOR_EACH(;, .SetLengthAndRetainStorage(8));
|
|
|
|
FOR_EACH(;, .SetLengthAndRetainStorage(12));
|
|
|
|
for (int n = 0; n < 12; ++n) {
|
|
|
|
FOR_EACH(if LPAREN, [n] != n RPAREN return false);
|
|
|
|
}
|
|
|
|
FOR_EACH(;, .SetLengthAndRetainStorage(0));
|
|
|
|
FOR_EACH(;, .SetLengthAndRetainStorage(N));
|
|
|
|
for (int n = 0; n < N; ++n) {
|
|
|
|
FOR_EACH(if LPAREN, [n] != n RPAREN return false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* current_Hdrs[] = {
|
|
|
|
static_cast<BufAccessor<FallibleTArray<int> >&>(f).GetHdr(),
|
|
|
|
static_cast<BufAccessor<InfallibleTArray<int> >&>(i).GetHdr(),
|
|
|
|
static_cast<BufAccessor<nsTArray<int> >&>(t).GetHdr(),
|
2016-02-02 18:36:30 +03:00
|
|
|
static_cast<BufAccessor<AutoTArray<int, N> >&>(tauto).GetHdr(),
|
2013-09-08 06:05:02 +04:00
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
// SetLengthAndRetainStorage(n) should NOT have reallocated the internal
|
|
|
|
// memory.
|
|
|
|
if (sizeof(initial_Hdrs) != sizeof(current_Hdrs)) return false;
|
|
|
|
for (size_t n = 0; n < sizeof(current_Hdrs) / sizeof(current_Hdrs[0]); ++n) {
|
|
|
|
if (current_Hdrs[n] != initial_Hdrs[n]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef FOR_EACH
|
|
|
|
#undef LPAREN
|
|
|
|
#undef RPAREN
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-22 19:22:20 +04:00
|
|
|
//----
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
typedef bool (*TestFunc)();
|
2005-11-23 22:44:16 +03:00
|
|
|
#define DECL_TEST(name) { #name, name }
|
|
|
|
|
|
|
|
static const struct Test {
|
|
|
|
const char* name;
|
|
|
|
TestFunc func;
|
|
|
|
} tests[] = {
|
|
|
|
DECL_TEST(test_int_array),
|
|
|
|
DECL_TEST(test_int64_array),
|
|
|
|
DECL_TEST(test_char_array),
|
|
|
|
DECL_TEST(test_uint32_array),
|
|
|
|
DECL_TEST(test_object_array),
|
2015-04-21 01:42:56 +03:00
|
|
|
DECL_TEST(test_return_by_value),
|
2014-03-15 21:55:00 +04:00
|
|
|
DECL_TEST(test_move_array),
|
2005-11-23 22:44:16 +03:00
|
|
|
DECL_TEST(test_string_array),
|
|
|
|
DECL_TEST(test_comptr_array),
|
2005-12-13 00:28:29 +03:00
|
|
|
DECL_TEST(test_refptr_array),
|
2006-10-18 01:40:07 +04:00
|
|
|
DECL_TEST(test_ptrarray),
|
2006-11-03 03:52:40 +03:00
|
|
|
#ifdef DEBUG
|
2006-11-02 22:33:10 +03:00
|
|
|
DECL_TEST(test_autoarray),
|
2006-11-03 03:52:40 +03:00
|
|
|
#endif
|
2009-04-11 05:08:08 +04:00
|
|
|
DECL_TEST(test_indexof),
|
2010-05-05 21:21:23 +04:00
|
|
|
DECL_TEST(test_heap),
|
2011-09-22 19:22:20 +04:00
|
|
|
DECL_TEST(test_swap),
|
2012-01-26 21:51:34 +04:00
|
|
|
DECL_TEST(test_fallible),
|
2012-12-19 05:16:05 +04:00
|
|
|
DECL_TEST(test_conversion_operator),
|
2013-09-08 06:05:02 +04:00
|
|
|
DECL_TEST(test_SetLengthAndRetainStorage_no_ctor),
|
2012-07-30 18:20:58 +04:00
|
|
|
{ nullptr, nullptr }
|
2005-11-23 22:44:16 +03:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace TestTArray
|
2008-08-18 20:45:38 +04:00
|
|
|
|
|
|
|
using namespace TestTArray;
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int count = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
count = atoi(argv[1]);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
|
2005-11-23 22:44:16 +03:00
|
|
|
return -1;
|
|
|
|
|
2011-09-22 19:22:20 +04:00
|
|
|
bool success = true;
|
2005-11-23 22:44:16 +03:00
|
|
|
while (count--) {
|
2012-07-30 18:20:58 +04:00
|
|
|
for (const Test* t = tests; t->name != nullptr; ++t) {
|
2011-09-22 19:22:20 +04:00
|
|
|
bool test_result = t->func();
|
|
|
|
printf("%25s : %s\n", t->name, test_result ? "SUCCESS" : "FAILURE");
|
|
|
|
if (!test_result)
|
|
|
|
success = false;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2011-09-22 19:22:20 +04:00
|
|
|
return success ? 0 : -1;
|
2005-11-23 22:44:16 +03:00
|
|
|
}
|