mfbt/tests/TestPair.cpp
author Jeff Walden <jwalden@mit.edu>
Mon, 09 Jun 2014 08:49:18 -0700
changeset 212096 f0eb37be6b5f5dbb7dd67d592b16c50f0a3c7dfa
child 217427 1b7d7dcf71e23f6ed68f10a72416f7dc5e58a6c2
permissions -rw-r--r--
Bug 953296 - Introduce an EBO-performing Pair class to mfbt. r=jcranmer

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/Pair.h"

using mozilla::Pair;

// Sizes aren't part of the guaranteed Pair interface, but we want to verify our
// attempts at compactness through EBO are moderately functional, *somewhere*.
#define INSTANTIATE(T1, T2, name, size) \
  Pair<T1, T2> name##_1(T1(0), T2(0)); \
  static_assert(sizeof(name##_1.first()) > 0, \
                "first method should work on Pair<" #T1 ", " #T2 ">"); \
  static_assert(sizeof(name##_1.second()) > 0, \
                "second method should work on Pair<" #T1 ", " #T2 ">"); \
  static_assert(sizeof(name##_1) == (size), \
                "Pair<" #T1 ", " #T2 "> has an unexpected size"); \
  Pair<T2, T1> name##_2(T2(0), T1(0)); \
  static_assert(sizeof(name##_2.first()) > 0, \
                "first method should work on Pair<" #T2 ", " #T1 ">"); \
  static_assert(sizeof(name##_2.second()) > 0, \
                "second method should work on Pair<" #T2 ", " #T1 ">"); \
  static_assert(sizeof(name##_2) == (size), \
                "Pair<" #T2 ", " #T1 "> has an unexpected size");

INSTANTIATE(int, int, prim1, 2 * sizeof(int));
INSTANTIATE(int, long, prim2, 2 * sizeof(long));

struct EmptyClass { EmptyClass(int) {} };
struct NonEmpty { char c; NonEmpty(int) {} };

INSTANTIATE(int, EmptyClass, both1, sizeof(int));
INSTANTIATE(int, NonEmpty, both2, 2 * sizeof(int));
INSTANTIATE(EmptyClass, NonEmpty, both3, 1);

struct A { char dummy; A(int) {} };
struct B : A { B(int i) : A(i) {} };

INSTANTIATE(A, A, class1, 2);
INSTANTIATE(A, B, class2, 2);
INSTANTIATE(A, EmptyClass, class3, 1);

struct OtherEmpty : EmptyClass { OtherEmpty(int i) : EmptyClass(i) {} };

// C++11 requires distinct objects of the same type, within the same "most
// derived object", to have different addresses.  Pair allocates its elements as
// two bases, a base and a member, or two members.  If the two elements have
// non-zero size or are unrelated, no big deal.  But if they're both empty and
// related, something -- possibly both -- must be inflated.  Exactly which are
// inflated depends which PairHelper specialization is used.  We could
// potentially assert something about size for this case, but whatever we could
// assert would be very finicky.  Plus it's two empty classes -- hardly likely.
// So don't bother trying to assert anything about this case.
//INSTANTIATE(EmptyClass, OtherEmpty, class4, ...something finicky...);

int
main()
{
}