Bug 565461: Implement nsTArray::operator==. r=bsmedberg

This commit is contained in:
Chris Jones
2010-05-22 14:35:41 -05:00
parent 9af3a14c66
commit f30ea57e16
2 changed files with 35 additions and 0 deletions

View File

@@ -298,6 +298,21 @@ class nsTArray : public nsTArray_base {
return *this; return *this;
} }
// Return true if this array has the same length and the same
// elements as |other|.
bool operator==(const self_type& other) const {
size_type len = Length();
if (len != other.Length())
return false;
// XXX std::equal would be as fast or faster here
for (index_type i = 0; i < len; ++i)
if (!(operator[](i) == other[i]))
return false;
return true;
}
// //
// Accessor methods // Accessor methods
// //

View File

@@ -68,6 +68,9 @@ static PRBool test_basic_array(ElementType *data,
if (ary.Length() != dataLen) { if (ary.Length() != dataLen) {
return PR_FALSE; return PR_FALSE;
} }
if (!(ary == ary)) {
return PR_FALSE;
}
PRUint32 i; PRUint32 i;
for (i = 0; i < ary.Length(); ++i) { for (i = 0; i < ary.Length(); ++i) {
if (ary[i] != data[i]) if (ary[i] != data[i])
@@ -98,6 +101,9 @@ static PRBool test_basic_array(ElementType *data,
if (ary[i] == ary[i - 1]) if (ary[i] == ary[i - 1])
ary.RemoveElementAt(i); ary.RemoveElementAt(i);
} }
if (!(ary == ary)) {
return PR_FALSE;
}
for (i = 0; i < ary.Length(); ++i) { for (i = 0; i < ary.Length(); ++i) {
if (ary.BinaryIndexOf(ary[i]) != i) if (ary.BinaryIndexOf(ary[i]) != i)
return PR_FALSE; return PR_FALSE;
@@ -108,10 +114,14 @@ static PRBool test_basic_array(ElementType *data,
ary.RemoveElement(data[dataLen / 2]); ary.RemoveElement(data[dataLen / 2]);
if (ary.Length() != (oldLen - 1)) if (ary.Length() != (oldLen - 1))
return PR_FALSE; return PR_FALSE;
if (!(ary == ary))
return PR_FALSE;
PRUint32 index = ary.Length() / 2; PRUint32 index = ary.Length() / 2;
if (!ary.InsertElementAt(index, extra)) if (!ary.InsertElementAt(index, extra))
return PR_FALSE; return PR_FALSE;
if (!(ary == ary))
return PR_FALSE;
if (ary[index] != extra) if (ary[index] != extra)
return PR_FALSE; return PR_FALSE;
if (ary.IndexOf(extra) == PR_UINT32_MAX) if (ary.IndexOf(extra) == PR_UINT32_MAX)
@@ -125,6 +135,8 @@ static PRBool test_basic_array(ElementType *data,
return PR_FALSE; return PR_FALSE;
nsTArray<ElementType> copy(ary); nsTArray<ElementType> copy(ary);
if (!(ary == copy))
return PR_FALSE;
for (i = 0; i < copy.Length(); ++i) { for (i = 0; i < copy.Length(); ++i) {
if (ary[i] != copy[i]) if (ary[i] != copy[i])
return PR_FALSE; return PR_FALSE;
@@ -140,11 +152,17 @@ static PRBool test_basic_array(ElementType *data,
ary.Clear(); ary.Clear();
if (!ary.IsEmpty() || ary.Elements() == nsnull) if (!ary.IsEmpty() || ary.Elements() == nsnull)
return PR_FALSE; return PR_FALSE;
if (!(ary == nsTArray<ElementType>()))
return PR_FALSE;
if (ary == copy)
return PR_FALSE;
if (ary.SafeElementAt(0, extra) != extra || if (ary.SafeElementAt(0, extra) != extra ||
ary.SafeElementAt(10, extra) != extra) ary.SafeElementAt(10, extra) != extra)
return PR_FALSE; return PR_FALSE;
ary = copy; ary = copy;
if (!(ary == copy))
return PR_FALSE;
for (i = 0; i < copy.Length(); ++i) { for (i = 0; i < copy.Length(); ++i) {
if (ary[i] != copy[i]) if (ary[i] != copy[i])
return PR_FALSE; return PR_FALSE;
@@ -152,6 +170,8 @@ static PRBool test_basic_array(ElementType *data,
if (!ary.InsertElementsAt(0, copy)) if (!ary.InsertElementsAt(0, copy))
return PR_FALSE; return PR_FALSE;
if (ary == copy)
return PR_FALSE;
ary.RemoveElementsAt(0, copy.Length()); ary.RemoveElementsAt(0, copy.Length());
for (i = 0; i < copy.Length(); ++i) { for (i = 0; i < copy.Length(); ++i) {
if (ary[i] != copy[i]) if (ary[i] != copy[i])