Bug 1325771 - mfbt:tests: Handle targets with less strict alignment in TestPair. r?Waldo
Previously, the tests assumed that the alignment of int and long equals
their size. This commit fixes the tests for targets like m68k that have
sizeof(int) == 4 and alignof(int) == 2. A static helper function sizemax
was introduced as the offset of the second element in Pair<int,long>
might be either determined by its alignment requirement or the size of
the preceding int element and we use the helper function to pick the
larger of the two values.
--- a/mfbt/tests/TestPair.cpp
+++ b/mfbt/tests/TestPair.cpp
@@ -24,24 +24,29 @@ using mozilla::Pair;
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");
+static constexpr size_t sizemax(size_t a, size_t b)
+{
+ return (a > b) ? a : b;
+}
+
INSTANTIATE(int, int, prim1, 2 * sizeof(int));
-INSTANTIATE(int, long, prim2, 2 * sizeof(long));
+INSTANTIATE(int, long, prim2, sizeof(long) + sizemax(sizeof(int), alignof(long)));
struct EmptyClass { explicit EmptyClass(int) {} };
struct NonEmpty { char mC; explicit NonEmpty(int) {} };
INSTANTIATE(int, EmptyClass, both1, sizeof(int));
-INSTANTIATE(int, NonEmpty, both2, 2 * sizeof(int));
+INSTANTIATE(int, NonEmpty, both2, sizeof(int) + alignof(int));
INSTANTIATE(EmptyClass, NonEmpty, both3, 1);
struct A { char dummy; explicit A(int) {} };
struct B : A { explicit B(int aI) : A(aI) {} };
INSTANTIATE(A, A, class1, 2);
INSTANTIATE(A, B, class2, 2);
INSTANTIATE(A, EmptyClass, class3, 1);