mfbt/tests/TestMacroForEach.cpp

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:a4bfeae37a44
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_PLUS(x) x +
11 static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (10)) 0 == 10, "");
12 static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (1, 1, 1)) 0 == 3, "");
13
14 #define HELPER_DEFINE_VAR(x) const int test1_##x = x;
15 MOZ_FOR_EACH(HELPER_DEFINE_VAR, (), (10, 20))
16 static_assert(test1_10 == 10 && test1_20 == 20, "");
17
18 #define HELPER_DEFINE_VAR2(k, x) const int test2_##x = k + x;
19 MOZ_FOR_EACH(HELPER_DEFINE_VAR2, (5,), (10, 20))
20 static_assert(test2_10 == 15 && test2_20 == 25, "");
21
22 #define HELPER_IDENTITY_COMMA(k1, k2, x) k1, k2, x,
23
24 int main()
25 {
26 const int a[] = { MOZ_FOR_EACH(HELPER_IDENTITY_COMMA, (1, 2,), (10, 20, 30)) };
27 MOZ_RELEASE_ASSERT(a[0] == 1 && a[1] == 2 && a[2] == 10 &&
28 a[3] == 1 && a[4] == 2 && a[5] == 20 &&
29 a[6] == 1 && a[7] == 2 && a[8] == 30,
30 "MOZ_FOR_EACH args enumerated in incorrect order");
31 }

mercurial