Blame mfbt/tests/TestMacroForEach.cpp

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#include "mozilla/Assertions.h"
Packit f0b94e
#include "mozilla/MacroForEach.h"
Packit f0b94e
Packit f0b94e
#define HELPER_IDENTITY(x) x
Packit f0b94e
#define HELPER_IDENTITY_PLUS(x) x +
Packit f0b94e
static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (10)) 0 == 10, "");
Packit f0b94e
static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (1, 1, 1)) 0 == 3, "");
Packit f0b94e
static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+),
Packit f0b94e
                                     (), (10)) == 10, "");
Packit f0b94e
static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+),
Packit f0b94e
                                     (), (1, 1, 1)) == 3, "");
Packit f0b94e
Packit f0b94e
#define HELPER_ONE_PLUS(x) HELPER_IDENTITY_PLUS(1)
Packit f0b94e
static_assert(MOZ_FOR_EACH(HELPER_ONE_PLUS, (), ()) 0 == 0, "");
Packit f0b94e
Packit f0b94e
#define HELPER_DEFINE_VAR(x) const int test1_##x = x;
Packit f0b94e
MOZ_FOR_EACH(HELPER_DEFINE_VAR, (), (10, 20))
Packit f0b94e
static_assert(test1_10 == 10 && test1_20 == 20, "");
Packit f0b94e
Packit f0b94e
#define HELPER_DEFINE_VAR2(k, x) const int test2_##x = k + x;
Packit f0b94e
MOZ_FOR_EACH(HELPER_DEFINE_VAR2, (5,), (10, 20))
Packit f0b94e
static_assert(test2_10 == 15 && test2_20 == 25, "");
Packit f0b94e
Packit f0b94e
#define HELPER_DEFINE_PARAM(t, n) t n
Packit f0b94e
constexpr int
Packit f0b94e
test(MOZ_FOR_EACH_SEPARATED(HELPER_DEFINE_PARAM, (,), (int,), (a, b, c)))
Packit f0b94e
{
Packit f0b94e
  return a + b + c;
Packit f0b94e
}
Packit f0b94e
static_assert(test(1, 2, 3) == 6, "");
Packit f0b94e
Packit f0b94e
int
Packit f0b94e
main()
Packit f0b94e
{
Packit f0b94e
#define HELPER_IDENTITY_COMMA(k1, k2, x) k1, k2, x,
Packit f0b94e
  const int a[] = {
Packit f0b94e
    MOZ_FOR_EACH(HELPER_IDENTITY_COMMA, (1, 2,), (10, 20, 30))
Packit f0b94e
  };
Packit f0b94e
  MOZ_RELEASE_ASSERT(a[0] == 1 && a[1] == 2 && a[2] == 10 &&
Packit f0b94e
                     a[3] == 1 && a[4] == 2 && a[5] == 20 &&
Packit f0b94e
                     a[6] == 1 && a[7] == 2 && a[8] == 30,
Packit f0b94e
                     "MOZ_FOR_EACH args enumerated in incorrect order");
Packit f0b94e
  return 0;
Packit f0b94e
}