From 11fb7202f8d1ef68da1135a92615d872ae5b41f1 Mon Sep 17 00:00:00 2001 From: Botond Ballo Date: Thu, 21 May 2015 21:14:25 -0400 Subject: [PATCH] Bug 1163328 - Tests for mozilla::Tuple. r=froydnj --HG-- extra : source : 9e53f6a2c0d179b303d8dabec517bfcae870c1f8 --- mfbt/tests/TestTuple.cpp | 147 +++++++++++++++++++++++++++++++++++++++ mfbt/tests/moz.build | 1 + 2 files changed, 148 insertions(+) create mode 100644 mfbt/tests/TestTuple.cpp diff --git a/mfbt/tests/TestTuple.cpp b/mfbt/tests/TestTuple.cpp new file mode 100644 index 000000000000..97f1ea280d47 --- /dev/null +++ b/mfbt/tests/TestTuple.cpp @@ -0,0 +1,147 @@ +/* -*- 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/Assertions.h" +#include "mozilla/Move.h" +#include "mozilla/Tuple.h" +#include "mozilla/TypeTraits.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/unused.h" + +#include + +using mozilla::Get; +using mozilla::IsSame; +using mozilla::MakeTuple; +using mozilla::MakeUnique; +using mozilla::Move; +using mozilla::Tuple; +using mozilla::UniquePtr; +using mozilla::unused; + +#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(IsSame::value, \ + "Type mismatch!") + +struct ConvertibleToInt +{ + operator int() { 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{Move(g)}; + CHECK(Get<0>(g) == nullptr); + CHECK(*Get<0>(h) == 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 = Move(f); + CHECK(*Get<0>(e) == 42); + CHECK(Get<0>(f) == 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 bool +TestMakeTuple() +{ + auto tuple = MakeTuple(42, 0.5f, 'c'); + CHECK_TYPE(tuple, Tuple); + CHECK(Get<0>(tuple) == 42); + CHECK(Get<1>(tuple) == 1.0f); + CHECK(Get<2>(tuple) == 'c'); + return true; +} + +int +main() +{ + TestConstruction(); + TestAssignment(); + TestGet(); + TestMakeTuple(); + return 0; +} diff --git a/mfbt/tests/moz.build b/mfbt/tests/moz.build index be862303b5b8..da6e84ead867 100644 --- a/mfbt/tests/moz.build +++ b/mfbt/tests/moz.build @@ -29,6 +29,7 @@ CppUnitTests([ 'TestSHA1', 'TestSplayTree', 'TestTemplateLib', + 'TestTuple', 'TestTypedEnum', 'TestTypeTraits', 'TestUniquePtr',