Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/mfbt/tests/   (Firefox Browser Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 4 kB image not shown  

Quelle  TestHashTable.cpp

  Sprache: C
 

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */


#include "mozilla/HashTable.h"
#include "mozilla/PairHash.h"

#include <utility>

void TestMoveConstructor() {
  using namespace mozilla;

  HashMap<intint> map;
  MOZ_RELEASE_ASSERT(map.putNew(332));
  MOZ_RELEASE_ASSERT(map.putNew(442));
  MOZ_RELEASE_ASSERT(map.count() == 2);
  MOZ_RELEASE_ASSERT(!map.empty());
  MOZ_RELEASE_ASSERT(!map.lookup(2));
  MOZ_RELEASE_ASSERT(map.lookup(3)->value() == 32);
  MOZ_RELEASE_ASSERT(map.lookup(4)->value() == 42);

  HashMap<intint> moved = std::move(map);
  MOZ_RELEASE_ASSERT(moved.count() == 2);
  MOZ_RELEASE_ASSERT(!moved.empty());
  MOZ_RELEASE_ASSERT(!moved.lookup(2));
  MOZ_RELEASE_ASSERT(moved.lookup(3)->value() == 32);
  MOZ_RELEASE_ASSERT(moved.lookup(4)->value() == 42);

  MOZ_RELEASE_ASSERT(map.empty());
  MOZ_RELEASE_ASSERT(!map.count());
}

void CheckSwapMap1(const mozilla::HashMap<intint>& map1) {
  MOZ_RELEASE_ASSERT(map1.count() == 2);
  MOZ_RELEASE_ASSERT(!map1.empty());
  MOZ_RELEASE_ASSERT(!map1.lookup(3));
  MOZ_RELEASE_ASSERT(!map1.lookup(4));
  MOZ_RELEASE_ASSERT(map1.lookup(1)->value() == 10);
  MOZ_RELEASE_ASSERT(map1.lookup(2)->value() == 20);
}

void CheckSwapMap2(const mozilla::HashMap<intint>& map2) {
  MOZ_RELEASE_ASSERT(map2.count() == 2);
  MOZ_RELEASE_ASSERT(!map2.empty());
  MOZ_RELEASE_ASSERT(!map2.lookup(1));
  MOZ_RELEASE_ASSERT(!map2.lookup(2));
  MOZ_RELEASE_ASSERT(map2.lookup(3)->value() == 30);
  MOZ_RELEASE_ASSERT(map2.lookup(4)->value() == 40);
}

void TestSwap() {
  using namespace mozilla;

  HashMap<intint> map1;
  MOZ_RELEASE_ASSERT(map1.putNew(110));
  MOZ_RELEASE_ASSERT(map1.putNew(220));
  CheckSwapMap1(map1);

  HashMap<intint> map2;
  MOZ_RELEASE_ASSERT(map2.putNew(330));
  MOZ_RELEASE_ASSERT(map2.putNew(440));
  CheckSwapMap2(map2);

  map1.swap(map2);
  CheckSwapMap2(map1);
  CheckSwapMap1(map2);
}

enum SimpleEnum { SIMPLE_1, SIMPLE_2 };

enum class ClassEnum : int {
  CLASS_ENUM_1,
  CLASS_ENUM_2,
};

void TestEnumHash() {
  using namespace mozilla;

  HashMap<SimpleEnum, int> map;
  MOZ_RELEASE_ASSERT(map.put(SIMPLE_1, 1));
  MOZ_RELEASE_ASSERT(map.put(SIMPLE_2, 2));

  MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_1)->value() == 1);
  MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_2)->value() == 2);

  HashMap<ClassEnum, int> map2;
  MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_1, 1));
  MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_2, 2));

  MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_1)->value() == 1);
  MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_2)->value() == 2);
}

void TestHashPair() {
  using namespace mozilla;

  // Test with std::pair
  {
    HashMap<std::pair<intbool>, int, PairHasher<intbool>> map;
    std::pair<intbool> key1 = std::make_pair(1true);
    MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
    MOZ_RELEASE_ASSERT(map.has(key1));
    std::pair<intbool> key2 = std::make_pair(1false);
    MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
    std::pair<intbool> key3 = std::make_pair(2false);
    MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
    MOZ_RELEASE_ASSERT(map.has(key3));

    MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
  }
  // Test wtih compact pair
  {
    HashMap<mozilla::CompactPair<intbool>, int, CompactPairHasher<intbool>>
        map;
    mozilla::CompactPair<intbool> key1 = mozilla::MakeCompactPair(1true);
    MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
    MOZ_RELEASE_ASSERT(map.has(key1));
    mozilla::CompactPair<intbool> key2 = mozilla::MakeCompactPair(1false);
    MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
    mozilla::CompactPair<intbool> key3 = mozilla::MakeCompactPair(2false);
    MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
    MOZ_RELEASE_ASSERT(map.has(key3));

    MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
  }
}

int main() {
  TestMoveConstructor();
  TestEnumHash();
  TestHashPair();
  return 0;
}

Messung V0.5 in Prozent
C=85 H=96 G=90

¤ Dauer der Verarbeitung: 0.13 Sekunden  (vorverarbeitet am  2026-06-05) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.