| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/ArrayUtils.h" |
| 8 |
#include "mozilla/EnumeratedArray.h" |
| 9 |
#include "mozilla/EnumTypeTraits.h" |
| 10 |
|
| 11 |
using mozilla::EnumeratedArray; |
| 12 |
|
| 13 |
enum class AnimalSpecies { Cow, Sheep, Pig }; |
| 14 |
|
| 15 |
template <> |
| 16 |
struct mozilla::MaxContiguousEnumValue<AnimalSpecies> { |
| 17 |
static constexpr AnimalSpecies value = AnimalSpecies::Pig; |
| 18 |
}; |
| 19 |
|
| 20 |
using TestArray = EnumeratedArray<AnimalSpecies, int>; |
| 21 |
|
| 22 |
void TestInitialValueByConstructor() { |
| 23 |
// Style 1 |
| 24 |
TestArray headCount(1, 2, 3); |
| 25 |
MOZ_RELEASE_ASSERT(std::size(headCount) == 3); |
| 26 |
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Cow] == 1); |
| 27 |
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Sheep] == 2); |
| 28 |
MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Pig] == 3); |
| 29 |
// Style 2 |
| 30 |
TestArray headCount2{5, 6, 7}; |
| 31 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 5); |
| 32 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 6); |
| 33 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 7); |
| 34 |
// Style 3 |
| 35 |
TestArray headCount3({8, 9, 10}); |
| 36 |
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Cow] == 8); |
| 37 |
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Sheep] == 9); |
| 38 |
MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Pig] == 10); |
| 39 |
} |
| 40 |
|
| 41 |
void TestAssignment() { |
| 42 |
TestArray headCount{8, 9, 10}; |
| 43 |
TestArray headCount2; |
| 44 |
headCount2 = headCount; |
| 45 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 8); |
| 46 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 9); |
| 47 |
MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 10); |
| 48 |
} |
| 49 |
|
| 50 |
int main() { |
| 51 |
TestInitialValueByConstructor(); |
| 52 |
TestAssignment(); |
| 53 |
return 0; |
| 54 |
} |