| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/Assertions.h" |
| 8 |
#include "mozilla/MacroForEach.h" |
| 9 |
|
| 10 |
#define HELPER_IDENTITY(x) x |
| 11 |
#define HELPER_IDENTITY_PLUS(x) x + |
| 12 |
static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (10)) 0 == 10, ""); |
| 13 |
static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (1, 1, 1)) 0 == 3, ""); |
| 14 |
static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+), (), (10)) == 10, ""); |
| 15 |
static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+), (), (1, 1, 1)) == 3, |
| 16 |
""); |
| 17 |
|
| 18 |
#define HELPER_ONE_PLUS(x) HELPER_IDENTITY_PLUS(1) |
| 19 |
static_assert(MOZ_FOR_EACH(HELPER_ONE_PLUS, (), ()) 0 == 0, ""); |
| 20 |
|
| 21 |
#define HELPER_DEFINE_VAR(x) const int test1_##x = x; |
| 22 |
MOZ_FOR_EACH(HELPER_DEFINE_VAR, (), (10, 20)) |
| 23 |
static_assert(test1_10 == 10 && test1_20 == 20, ""); |
| 24 |
|
| 25 |
#define HELPER_DEFINE_VAR2(k, x) const int test2_##x = k + x; |
| 26 |
MOZ_FOR_EACH(HELPER_DEFINE_VAR2, (5, ), (10, 20)) |
| 27 |
static_assert(test2_10 == 15 && test2_20 == 25, ""); |
| 28 |
|
| 29 |
#define HELPER_DEFINE_PARAM(t, n) t n |
| 30 |
constexpr int test(MOZ_FOR_EACH_SEPARATED(HELPER_DEFINE_PARAM, (, ), (int, ), |
| 31 |
(a, b, c))) { |
| 32 |
return a + b + c; |
| 33 |
} |
| 34 |
static_assert(test(1, 2, 3) == 6, ""); |
| 35 |
|
| 36 |
int main() { |
| 37 |
#define HELPER_IDENTITY_COMMA(k1, k2, x) k1, k2, x, |
| 38 |
const int a[] = {MOZ_FOR_EACH(HELPER_IDENTITY_COMMA, (1, 2, ), (10, 20, 30))}; |
| 39 |
MOZ_RELEASE_ASSERT(a[0] == 1 && a[1] == 2 && a[2] == 10 && a[3] == 1 && |
| 40 |
a[4] == 2 && a[5] == 20 && a[6] == 1 && a[7] == 2 && |
| 41 |
a[8] == 30, |
| 42 |
"MOZ_FOR_EACH args enumerated in incorrect order"); |
| 43 |
return 0; |
| 44 |
} |