From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- mfbt/tests/TestHashTable.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 mfbt/tests/TestHashTable.cpp (limited to 'mfbt/tests/TestHashTable.cpp') diff --git a/mfbt/tests/TestHashTable.cpp b/mfbt/tests/TestHashTable.cpp new file mode 100644 index 0000000000..c648184040 --- /dev/null +++ b/mfbt/tests/TestHashTable.cpp @@ -0,0 +1,103 @@ +/* -*- 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 + +void TestMoveConstructor() { + using namespace mozilla; + + HashMap map; + MOZ_RELEASE_ASSERT(map.putNew(3, 32)); + MOZ_RELEASE_ASSERT(map.putNew(4, 42)); + 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 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()); +} + +enum SimpleEnum { SIMPLE_1, SIMPLE_2 }; + +enum class ClassEnum : int { + CLASS_ENUM_1, + CLASS_ENUM_2, +}; + +void TestEnumHash() { + using namespace mozilla; + + HashMap 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 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, int, PairHasher> map; + std::pair key1 = std::make_pair(1, true); + MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); + MOZ_RELEASE_ASSERT(map.has(key1)); + std::pair key2 = std::make_pair(1, false); + MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); + std::pair key3 = std::make_pair(2, false); + 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, int, CompactPairHasher> + map; + mozilla::CompactPair key1 = mozilla::MakeCompactPair(1, true); + MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); + MOZ_RELEASE_ASSERT(map.has(key1)); + mozilla::CompactPair key2 = mozilla::MakeCompactPair(1, false); + MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); + mozilla::CompactPair key3 = mozilla::MakeCompactPair(2, false); + 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; +} -- cgit v1.2.3