/* -*- 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/. */
// Test Nth with a list of 1 item.
static_assert(std::is_same_v<typename Nth<0, int>::Type, int>, "Nth<0, int>::Type should be int");
// Test Nth with a list of more than 1 item.
static_assert(std::is_same_v<typename Nth<0, int, char>::Type, int>, "Nth<0, int, char>::Type should be int");
static_assert(std::is_same_v<typename Nth<1, int, char>::Type, char>, "Nth<1, int, char>::Type should be char");
using mozilla::detail::SelectVariantType;
// SelectVariantType for zero items (shouldn't happen, but `count` should // still work ok.)
static_assert(SelectVariantType<int, char>::count == 0, "SelectVariantType::count should be 0");
// SelectVariantType for 1 type, for all combinations from/to T, const T, // const T&, T&& // - type to type
static_assert(std::is_same_v<typename SelectVariantType<int, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int>::count == 1, "SelectVariantType::count should be 1");
// - type to const type
static_assert(std::is_same_v<typename SelectVariantType<int, constint>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<int, constint>::count == 1, "SelectVariantType::count should be 1");
// - type to const type&
static_assert(
std::is_same_v<typename SelectVariantType<int, constint&>::Type, constint&>, "SelectVariantType::Type should be const int&");
static_assert(SelectVariantType<int, constint&>::count == 1, "SelectVariantType::count should be 1");
// - type to type&&
static_assert(
std::is_same_v<typename SelectVariantType<int, int&&>::Type, int&&>, "SelectVariantType::Type should be int&&");
static_assert(SelectVariantType<int, int&&>::count == 1, "SelectVariantType::count should be 1");
// - const type to type
static_assert(
std::is_same_v<typename SelectVariantType<constint, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<constint, int>::count == 1, "SelectVariantType::count should be 1");
// - const type to const type
static_assert(
std::is_same_v<typename SelectVariantType<constint, constint>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<constint, constint>::count == 1, "SelectVariantType::count should be 1");
// - const type to const type&
static_assert(
std::is_same_v<typename SelectVariantType<constint, constint&>::Type, constint&>, "SelectVariantType::Type should be const int&");
static_assert(SelectVariantType<constint, constint&>::count == 1, "SelectVariantType::count should be 1");
// - const type to type&&
static_assert(
std::is_same_v<typename SelectVariantType<constint, int&&>::Type, int&&>, "SelectVariantType::Type should be int&&");
static_assert(SelectVariantType<constint, int&&>::count == 1, "SelectVariantType::count should be 1");
// - const type& to type
static_assert(
std::is_same_v<typename SelectVariantType<constint&, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<constint&, int>::count == 1, "SelectVariantType::count should be 1");
// - const type& to const type
static_assert(
std::is_same_v<typename SelectVariantType<constint&, constint>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<constint&, constint>::count == 1, "SelectVariantType::count should be 1");
// - const type& to const type&
static_assert(
std::is_same_v<typename SelectVariantType<constint&, constint&>::Type, constint&>, "SelectVariantType::Type should be const int&");
static_assert(SelectVariantType<constint&, constint&>::count == 1, "SelectVariantType::count should be 1");
// - const type& to type&&
static_assert(
std::is_same_v<typename SelectVariantType<constint&, int&&>::Type, int&&>, "SelectVariantType::Type should be int&&");
static_assert(SelectVariantType<constint&, int&&>::count == 1, "SelectVariantType::count should be 1");
// - type&& to type
static_assert(
std::is_same_v<typename SelectVariantType<int&&, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int&&, int>::count == 1, "SelectVariantType::count should be 1");
// - type&& to const type
static_assert(
std::is_same_v<typename SelectVariantType<int&&, constint>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<int&&, constint>::count == 1, "SelectVariantType::count should be 1");
// - type&& to const type&
static_assert(
std::is_same_v<typename SelectVariantType<int&&, constint&>::Type, constint&>, "SelectVariantType::Type should be const int&");
static_assert(SelectVariantType<int&&, constint&>::count == 1, "SelectVariantType::count should be 1");
// - type&& to type&&
static_assert(
std::is_same_v<typename SelectVariantType<int&&, int&&>::Type, int&&>, "SelectVariantType::Type should be int&&");
static_assert(SelectVariantType<int&&, int&&>::count == 1, "SelectVariantType::count should be 1");
// SelectVariantType for two different types. // (Don't test all combinations, trust that the above tests are sufficient.)
static_assert(
std::is_same_v<typename SelectVariantType<int, int, char>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int, char>::count == 1, "SelectVariantType::count should be 1");
static_assert(
std::is_same_v<typename SelectVariantType<char, int, char>::Type, char>, "SelectVariantType::Type should be char");
static_assert(SelectVariantType<char, int, char>::count == 1, "SelectVariantType::count should be 1");
// SelectVariantType for two identical types.
static_assert(
std::is_same_v<typename SelectVariantType<int, int, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int, int>::count == 2, "SelectVariantType::count should be 2");
// SelectVariantType for two identical types, with others around.
static_assert(
std::is_same_v<typename SelectVariantType<int, char, int, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, char, int, int>::count == 2, "SelectVariantType::count should be 2");
static_assert(
std::is_same_v<typename SelectVariantType<int, int, char, int>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int, char, int>::count == 2, "SelectVariantType::count should be 2");
static_assert(
std::is_same_v<typename SelectVariantType<int, int, int, char>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int, int, char>::count == 2, "SelectVariantType::count should be 2");
static_assert(
std::is_same_v< typename SelectVariantType<int, char, int, char, int, char>::Type, int>, "SelectVariantType::Type should be int");
static_assert(
SelectVariantType<int, char, int, char, int, char>::count == 2, "SelectVariantType::count should be 2");
// SelectVariantType for two identically-selectable types (first one wins!).
static_assert(
std::is_same_v<typename SelectVariantType<int, int, constint>::Type, int>, "SelectVariantType::Type should be int");
static_assert(SelectVariantType<int, int, constint>::count == 2, "SelectVariantType::count should be 2");
static_assert(
std::is_same_v<typename SelectVariantType<int, constint, int>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<int, constint, int>::count == 2, "SelectVariantType::count should be 2");
static_assert(
std::is_same_v<typename SelectVariantType<int, constint, int&&>::Type, constint>, "SelectVariantType::Type should be const int");
static_assert(SelectVariantType<int, constint, int&&>::count == 2, "SelectVariantType::count should be 2");
}
staticvoid testSimple() {
printf("testSimple\n"); using V = Variant<uint32_t, uint64_t>;
// Non-const lvalue.
V v(uint64_t(1));
MOZ_RELEASE_ASSERT(v.is<uint64_t>());
MOZ_RELEASE_ASSERT(!v.is<uint32_t>());
MOZ_RELEASE_ASSERT(v.as<uint64_t>() == 1);
MOZ_RELEASE_ASSERT(v.is<1>());
MOZ_RELEASE_ASSERT(!v.is<0>());
static_assert(std::is_same_v<decltype(v.as<1>()), uint64_t&>, "v.as<1>() should return a uint64_t&");
MOZ_RELEASE_ASSERT(v.as<1>() == 1);
MOZ_RELEASE_ASSERT(cv.is<1>());
MOZ_RELEASE_ASSERT(!cv.is<0>());
static_assert(std::is_same_v<decltype(cv.as<1>()), const uint64_t&>, "cv.as<1>() should return a const uint64_t&");
MOZ_RELEASE_ASSERT(cv.as<1>() == 1);
// Non-const rvalue, using a function to create a temporary. auto MakeV = []() { return V(uint64_t(1)); };
MOZ_RELEASE_ASSERT(MakeV().is<uint64_t>());
MOZ_RELEASE_ASSERT(!MakeV().is<uint32_t>());
MOZ_RELEASE_ASSERT(MakeV().as<uint64_t>() == 1);
MOZ_RELEASE_ASSERT(MakeV().is<1>());
MOZ_RELEASE_ASSERT(!MakeV().is<0>());
static_assert(std::is_same_v<decltype(MakeV().as<1>()), uint64_t&&>, "MakeV().as<1>() should return a uint64_t&&");
MOZ_RELEASE_ASSERT(MakeV().as<1>() == 1);
// Const rvalue, using a function to create a temporary. auto MakeCV = []() -> const V { return V(uint64_t(1)); };
MOZ_RELEASE_ASSERT(MakeCV().is<uint64_t>());
MOZ_RELEASE_ASSERT(!MakeCV().is<uint32_t>());
MOZ_RELEASE_ASSERT(MakeCV().as<uint64_t>() == 1);
MOZ_RELEASE_ASSERT(MakeCV().is<1>());
MOZ_RELEASE_ASSERT(!MakeCV().is<0>());
static_assert(std::is_same_v<decltype(MakeCV().as<1>()), const uint64_t&&>, "MakeCV().as<1>() should return a const uint64_t&&");
MOZ_RELEASE_ASSERT(MakeCV().as<1>() == 1);
}
staticvoid testDuplicate() {
printf("testDuplicate\n");
Variant<uint32_t, uint64_t, uint32_t> v(uint64_t(1));
MOZ_RELEASE_ASSERT(v.is<uint64_t>());
MOZ_RELEASE_ASSERT(v.as<uint64_t>() == 1); // Note: uint32_t is not unique, so `v.is<uint32_t>()` is not allowed.
MOZ_RELEASE_ASSERT(v.is<1>());
MOZ_RELEASE_ASSERT(!v.is<0>());
MOZ_RELEASE_ASSERT(!v.is<2>());
static_assert(std::is_same_v<decltype(v.as<0>()), uint32_t&>, "as<0>() should return a uint64_t");
static_assert(std::is_same_v<decltype(v.as<1>()), uint64_t&>, "as<1>() should return a uint64_t");
static_assert(std::is_same_v<decltype(v.as<2>()), uint32_t&>, "as<2>() should return a uint64_t");
MOZ_RELEASE_ASSERT(v.as<1>() == 1);
MOZ_RELEASE_ASSERT(v.extract<1>() == 1);
}
staticvoid testConstructionWithVariantType() {
Variant<uint32_t, uint64_t, uint32_t> v(mozilla::VariantType<uint64_t>{}, 3);
MOZ_RELEASE_ASSERT(v.is<uint64_t>()); // MOZ_RELEASE_ASSERT(!v.is<uint32_t>()); // uint32_t is not unique!
MOZ_RELEASE_ASSERT(v.as<uint64_t>() == 3);
}
staticvoid testConstructionWithVariantIndex() {
Variant<uint32_t, uint64_t, uint32_t> v(mozilla::VariantIndex<2>{}, 2);
MOZ_RELEASE_ASSERT(!v.is<uint64_t>()); // Note: uint32_t is not unique, so `v.is<uint32_t>()` is not allowed.
// All combinations of possible call operators: // Every line, the parameter integer type changes. // Every 3 lines, the parameter type changes constness. // Every 6 lines, the parameter changes reference l/r-valueness. // Every 12 lines, the member function qualifier changes constness. // After 24 lines, the member function qualifier changes ref l/r-valueness.
CALL(uint8_t&, U8, ParamLREF, &, ThisLREF)
CALL(uint32_t&, U32, ParamLREF, &, ThisLREF)
CALL(uint64_t&, U64, ParamLREF, &, ThisLREF)
// Catch-all, to verify that there is no call with any type other than the // expected ones above. template <typename Other>
Result operator()(const Other&) {
MOZ_RELEASE_ASSERT(false); return RESULT(NA, NA, NA, 0);
}
};
staticvoid testMatching() {
printf("testMatching\n"); using V = Variant<uint8_t, uint32_t, uint64_t>;
// Create a temporary variant by returning a copy of one. auto CopyV = [](const V& aV) { return aV; };
// Create a temporary variant by returning a const copy of one. auto CopyConstV = [](const V& aV) -> const V { return aV; };
// All combinations of possible calls: // Every line, the variant integer type changes. // Every 3 lines, the variant type changes constness. // Every 6 lines, the variant changes reference l/r-valueness. // Every 12 lines, the matcher changes constness. // After 24 lines, the matcher changes ref l/r-valueness.
MOZ_RELEASE_ASSERT(v1.match(desc) == RESULT(U8, ParamLREF, ThisLREF, 1));
MOZ_RELEASE_ASSERT(v2.match(desc) == RESULT(U32, ParamLREF, ThisLREF, 2));
MOZ_RELEASE_ASSERT(v3.match(desc) == RESULT(U64, ParamLREF, ThisLREF, 3));
staticvoid testAddTagToHash() {
printf("testAddToHash\n"); using V = Variant<uint8_t, uint16_t, uint32_t, uint64_t>;
// We don't know what our hash function is, and these are certainly not all // true under all hash functions. But they are probably true under almost any // decent hash function, and our aim is simply to establish that the tag // *does* influence the hash value.
{
mozilla::HashNumber h8 = V(uint8_t(1)).addTagToHash(0);
mozilla::HashNumber h16 = V(uint16_t(1)).addTagToHash(0);
mozilla::HashNumber h32 = V(uint32_t(1)).addTagToHash(0);
mozilla::HashNumber h64 = V(uint64_t(1)).addTagToHash(0);
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 ist noch experimentell.