tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestEnumeratedArray.cpp (1808B)


      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/EnumeratedArray.h"
      8 #include "mozilla/EnumTypeTraits.h"
      9 
     10 using mozilla::EnumeratedArray;
     11 
     12 enum class AnimalSpecies { Cow, Sheep, Pig };
     13 
     14 template <>
     15 struct mozilla::MaxContiguousEnumValue<AnimalSpecies> {
     16  static constexpr AnimalSpecies value = AnimalSpecies::Pig;
     17 };
     18 
     19 using TestArray = EnumeratedArray<AnimalSpecies, int>;
     20 
     21 void TestInitialValueByConstructor() {
     22  // Style 1
     23  TestArray headCount(1, 2, 3);
     24  MOZ_RELEASE_ASSERT(std::size(headCount) == 3);
     25  MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Cow] == 1);
     26  MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Sheep] == 2);
     27  MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Pig] == 3);
     28  // Style 2
     29  TestArray headCount2{5, 6, 7};
     30  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 5);
     31  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 6);
     32  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 7);
     33  // Style 3
     34  TestArray headCount3({8, 9, 10});
     35  MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Cow] == 8);
     36  MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Sheep] == 9);
     37  MOZ_RELEASE_ASSERT(headCount3[AnimalSpecies::Pig] == 10);
     38 }
     39 
     40 void TestAssignment() {
     41  TestArray headCount{8, 9, 10};
     42  TestArray headCount2;
     43  headCount2 = headCount;
     44  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Cow] == 8);
     45  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Sheep] == 9);
     46  MOZ_RELEASE_ASSERT(headCount2[AnimalSpecies::Pig] == 10);
     47 }
     48 
     49 int main() {
     50  TestInitialValueByConstructor();
     51  TestAssignment();
     52  return 0;
     53 }