/* -*- 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 #include #include "mozilla/Assertions.h" #include "mozilla/CompactPair.h" #include "mozilla/Tuple.h" #include "mozilla/UniquePtr.h" #include "mozilla/Unused.h" using mozilla::CompactPair; using mozilla::Get; using mozilla::MakeTuple; using mozilla::MakeUnique; using mozilla::Tie; using mozilla::Tuple; using mozilla::UniquePtr; using mozilla::Unused; using std::pair; #define CHECK(c) \ do { \ bool cond = !!(c); \ MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \ } while (false) // The second argument is the expected type. It's variadic to allow the // type to contain commas. #define CHECK_TYPE(expression, ...) \ static_assert(std::is_same_v, \ "Type mismatch!") struct ConvertibleToInt { operator int() const { return 42; } }; static void TestConstruction() { // Default construction Tuple<> a; Unused << a; Tuple b; Unused << b; // Construction from elements int x = 1, y = 1; Tuple c{x, y}; Tuple d{x, y}; x = 42; y = 42; CHECK(Get<0>(c) == 1); CHECK(Get<1>(c) == 1); CHECK(Get<0>(d) == 42); CHECK(Get<1>(d) == 42); // Construction from objects convertible to the element types Tuple e{1.0, ConvertibleToInt{}}; // Copy construction Tuple x1; Tuple x2{x1}; Tuple f(c); CHECK(Get<0>(f) == 1); CHECK(Get<0>(f) == 1); // Move construction Tuple> g{MakeUnique(42)}; Tuple> h{std::move(g)}; CHECK(Get<0>(g) == nullptr); CHECK(*Get<0>(h) == 42); } static void TestConstructionFromMozPair() { // Construction from elements int x = 1, y = 1; CompactPair a{x, y}; CompactPair b{x, y}; Tuple c(a); Tuple d(b); x = 42; y = 42; CHECK(Get<0>(c) == 1); CHECK(Get<1>(c) == 1); CHECK(Get<0>(d) == 42); CHECK(Get<1>(d) == 42); } static void TestConstructionFromStdPair() { // Construction from elements int x = 1, y = 1; pair a{x, y}; pair b{x, y}; Tuple c(a); Tuple d(b); x = 42; y = 42; CHECK(Get<0>(c) == 1); CHECK(Get<1>(c) == 1); CHECK(Get<0>(d) == 42); CHECK(Get<1>(d) == 42); } static void TestAssignment() { // Copy assignment Tuple a{0}; Tuple b{42}; a = b; CHECK(Get<0>(a) == 42); // Assignment to reference member int i = 0; int j = 42; Tuple c{i}; Tuple d{j}; c = d; CHECK(i == 42); // Move assignment Tuple> e{MakeUnique(0)}; Tuple> f{MakeUnique(42)}; e = std::move(f); CHECK(*Get<0>(e) == 42); CHECK(Get<0>(f) == nullptr); } static void TestAssignmentFromMozPair() { // Copy assignment Tuple a{0, 0}; CompactPair b{42, 42}; a = b; CHECK(Get<0>(a) == 42); CHECK(Get<1>(a) == 42); // Assignment to reference member int i = 0; int j = 0; int k = 42; Tuple c{i, j}; CompactPair d{k, k}; c = d; CHECK(i == 42); CHECK(j == 42); // Move assignment Tuple, UniquePtr> e{MakeUnique(0), MakeUnique(0)}; CompactPair, UniquePtr> f{MakeUnique(42), MakeUnique(42)}; e = std::move(f); CHECK(*Get<0>(e) == 42); CHECK(*Get<1>(e) == 42); CHECK(f.first() == nullptr); CHECK(f.second() == nullptr); } static void TestAssignmentFromStdPair() { // Copy assignment Tuple a{0, 0}; pair b{42, 42}; a = b; CHECK(Get<0>(a) == 42); CHECK(Get<1>(a) == 42); // Assignment to reference member int i = 0; int j = 0; int k = 42; Tuple c{i, j}; pair d{k, k}; c = d; CHECK(i == 42); CHECK(j == 42); // Move assignment. Tuple, UniquePtr> e{MakeUnique(0), MakeUnique(0)}; // XXX: On some platforms std::pair doesn't support move constructor. pair, UniquePtr> f; f.first = MakeUnique(42); f.second = MakeUnique(42); e = std::move(f); CHECK(*Get<0>(e) == 42); CHECK(*Get<1>(e) == 42); CHECK(f.first == nullptr); CHECK(f.second == nullptr); } static void TestGet() { int x = 1; int y = 2; int z = 3; Tuple tuple(x, y, z); // Using Get<>() to read elements CHECK(Get<0>(tuple) == 1); CHECK(Get<1>(tuple) == 2); CHECK(Get<2>(tuple) == 3); // Using Get<>() to write to elements Get<0>(tuple) = 41; CHECK(Get<0>(tuple) == 41); // Writing through reference elements Get<1>(tuple) = 42; CHECK(Get<1>(tuple) == 42); CHECK(y == 42); } static void TestMakeTuple() { auto tuple = MakeTuple(42, 0.5f, 'c'); CHECK_TYPE(tuple, Tuple); CHECK(Get<0>(tuple) == 42); CHECK(Get<1>(tuple) == 0.5f); CHECK(Get<2>(tuple) == 'c'); // Make sure we don't infer the type to be Tuple. int x = 1; auto tuple2 = MakeTuple(x); CHECK_TYPE(tuple2, Tuple); x = 2; CHECK(Get<0>(tuple2) == 1); } static bool TestTieMozPair() { int i; float f; char c; Tuple rhs1(42, 0.5f, 'c'); Tie(i, f, c) = rhs1; CHECK(i == Get<0>(rhs1)); CHECK(f == Get<1>(rhs1)); CHECK(c == Get<2>(rhs1)); // Test conversions Tuple rhs2(ConvertibleToInt(), 0.7f, 'd'); Tie(i, f, c) = rhs2; CHECK(i == Get<0>(rhs2)); CHECK(f == Get<1>(rhs2)); CHECK(c == Get<2>(rhs2)); // Test Pair CompactPair rhs3(42, 1.5f); Tie(i, f) = rhs3; CHECK(i == rhs3.first()); CHECK(f == rhs3.second()); return true; } static bool TestTie() { int i; float f; char c; Tuple rhs1(42, 0.5f, 'c'); Tie(i, f, c) = rhs1; CHECK(i == Get<0>(rhs1)); CHECK(f == Get<1>(rhs1)); CHECK(c == Get<2>(rhs1)); // Test conversions Tuple rhs2(ConvertibleToInt(), 0.7f, 'd'); Tie(i, f, c) = rhs2; CHECK(i == Get<0>(rhs2)); CHECK(f == Get<1>(rhs2)); CHECK(c == Get<2>(rhs2)); // Test Pair pair rhs3(42, 1.5f); Tie(i, f) = rhs3; CHECK(i == rhs3.first); CHECK(f == rhs3.second); return true; } static bool TestTieIgnore() { int i; char c; Tuple rhs1(42, 0.5f, 'c'); Tie(i, mozilla::Ignore, c) = rhs1; CHECK(i == Get<0>(rhs1)); CHECK(c == Get<2>(rhs1)); return true; } template static void CheckForEachCall(const Tuple& aTuple, F&& CallForEach) { constexpr std::size_t tupleSize = sizeof...(Elements); Tuple checkResult; std::size_t i = 0; auto createResult = [&](auto& aElem) { static_assert(tupleSize == 3, "Need to deal with more/less cases in the switch below"); CHECK(i < tupleSize); switch (i) { case 0: Get<0>(checkResult) = aElem; break; case 1: Get<1>(checkResult) = aElem; break; case 2: Get<2>(checkResult) = aElem; break; } ++i; }; CallForEach(aTuple, createResult); CHECK(checkResult == aTuple); } static bool TestForEach() { Tuple tuple = MakeTuple(42, 0.5f, 'c'); CheckForEachCall( tuple, [](auto& aTuple, auto&& aLambda) { aTuple.ForEach(aLambda); }); CheckForEachCall( tuple, [](auto& aTuple, auto&& aLambda) { ForEach(aTuple, aLambda); }); CheckForEachCall(tuple, [](auto& aTuple, auto&& aLambda) { const decltype(aTuple)& constTuple = aTuple; constTuple.ForEach(aLambda); }); CheckForEachCall(tuple, [](auto& aTuple, auto&& aLambda) { const decltype(aTuple)& constTuple = aTuple; ForEach(constTuple, aLambda); }); return true; } int main() { TestConstruction(); TestConstructionFromMozPair(); TestConstructionFromStdPair(); TestAssignment(); TestAssignmentFromMozPair(); TestAssignmentFromStdPair(); TestGet(); TestMakeTuple(); TestTie(); TestTieIgnore(); TestTieMozPair(); TestForEach(); return 0; }