Bug 1546697 - EnumeratedArray should have a copy assignment operator. r=froydnj
I'll use this in a following patch. Differential Revision: https://phabricator.services.mozilla.com/D28679
This commit is contained in:
@@ -80,6 +80,13 @@ class EnumeratedArray {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EnumeratedArray& operator=(const EnumeratedArray& aOther) {
|
||||||
|
for (size_t i = 0; i < kSize; i++) {
|
||||||
|
mArray[i] = aOther.mArray[i];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
typedef typename ArrayType::iterator iterator;
|
typedef typename ArrayType::iterator iterator;
|
||||||
typedef typename ArrayType::const_iterator const_iterator;
|
typedef typename ArrayType::const_iterator const_iterator;
|
||||||
typedef typename ArrayType::reverse_iterator reverse_iterator;
|
typedef typename ArrayType::reverse_iterator reverse_iterator;
|
||||||
|
|||||||
@@ -6,29 +6,41 @@
|
|||||||
|
|
||||||
#include "mozilla/EnumeratedArray.h"
|
#include "mozilla/EnumeratedArray.h"
|
||||||
|
|
||||||
|
using mozilla::EnumeratedArray;
|
||||||
|
|
||||||
enum class AnimalSpecies { Cow, Sheep, Pig, Count };
|
enum class AnimalSpecies { Cow, Sheep, Pig, Count };
|
||||||
|
|
||||||
|
using TestArray = EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int>;
|
||||||
|
|
||||||
void TestInitialValueByConstructor() {
|
void TestInitialValueByConstructor() {
|
||||||
using namespace mozilla;
|
|
||||||
// Style 1
|
// Style 1
|
||||||
EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount(1, 2, 3);
|
TestArray headCount(1, 2, 3);
|
||||||
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Cow] == 1);
|
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Cow] == 1);
|
||||||
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Sheep] == 2);
|
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Sheep] == 2);
|
||||||
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Pig] == 3);
|
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Pig] == 3);
|
||||||
// Style 2
|
// Style 2
|
||||||
EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount2{5, 6, 7};
|
TestArray headCount2{5, 6, 7};
|
||||||
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 5);
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 5);
|
||||||
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 6);
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 6);
|
||||||
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 7);
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 7);
|
||||||
// Style 3
|
// Style 3
|
||||||
EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount3(
|
TestArray headCount3({8, 9, 10});
|
||||||
{8, 9, 10});
|
|
||||||
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Cow] == 8);
|
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Cow] == 8);
|
||||||
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Sheep] == 9);
|
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Sheep] == 9);
|
||||||
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Pig] == 10);
|
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Pig] == 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestAssignment() {
|
||||||
|
TestArray headCount{8, 9, 10};
|
||||||
|
TestArray headCount2;
|
||||||
|
headCount2 = headCount;
|
||||||
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 8);
|
||||||
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 9);
|
||||||
|
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 10);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
TestInitialValueByConstructor();
|
TestInitialValueByConstructor();
|
||||||
|
TestAssignment();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user