blob: a43997f4eb72d45132a8f81db68e3d58c8f6aaf0 [file] [log] [blame]
Alexei Frolovc10c8122019-11-01 16:31:19 -07001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolovc10c8122019-11-01 16:31:19 -07006//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolovc10c8122019-11-01 16:31:19 -070014
15#include "pw_preprocessor/concat.h"
16
17#include "pw_preprocessor/util.h"
18#include "pw_unit_test/framework.h"
19
20namespace pw {
21namespace {
22
23TEST(Concat, WithoutMacroExpansions) {
24 static_assert(PW_CONCAT() 9000 PW_CONCAT() == 9000);
25 static_assert(PW_CONCAT(1, 2) == 12);
26 static_assert(PW_CONCAT(1, 2, 3, 4) == 1234);
27 static_assert(PW_CONCAT(1, 2, 3, 4, 5) == 12345);
28 static_assert(PW_CONCAT(1, 2, 3, 4, 5, 6) == 123456);
29 static_assert(PW_CONCAT(1, 2, 3, 4, 5, 6, 7) == 1234567);
30 static_assert(PW_CONCAT(1, 2, 3, 4, 5, 6, 7, 8) == 12345678);
31
32 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, llu) == 0x345678llu);
33 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, llu) == 0x3456789llu);
34 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, llu) == 0x3456789Allu);
35 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, llu) ==
36 0x3456789ABllu);
37 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, C, llu) ==
38 0x3456789ABCllu);
39 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, llu) ==
40 0x3456789ABCDllu);
41 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, llu) ==
42 0x3456789ABCDEllu);
43 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, llu) ==
44 0x3456789ABCDEFllu);
45 static_assert(PW_CONCAT(0x, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, F, llu) ==
46 0x3456789ABCDEFFllu);
47}
48
49// Macros which expand out to test that PW_CONCAT properly expands them.
50#define _PW_TEST_SECTION(x) "section: " x
51#define _PW_TEST_NUMBER 123
52#define _PW_OUTER_MACRO(section_name) \
53 _PW_TEST_SECTION(PW_STRINGIFY(PW_CONCAT(section_name, _PW_TEST_NUMBER)))
54
55#define _PW_WHOSE() my
56#define _PW_WHAT var
57
58TEST(Concat, WithMacroExpansions) {
59 static_assert(PW_CONCAT(_PW_TEST_NUMBER, 5) == 1235);
60
61 int my_var;
62 EXPECT_EQ(&PW_CONCAT(_PW_WHOSE(), _, _PW_WHAT), &my_var);
63
64 EXPECT_STREQ(_PW_OUTER_MACRO(what is up), "section: what is up123");
65}
66
67// Test concatenation up to the maximum supported length.
68TEST(Concat, MaximumConcatenationLength) {
69 constexpr int value31_234567890123456789012345678901 = 1337;
70 constexpr int value32_2345678901234567890123456789012 = 101010;
71
72 // clang-format off
73 static_assert(PW_CONCAT(value31_, 2, 3, 4, 5, 6, 7, 8, 9, 0,
74 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
75 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
76 1) == 1337);
77
78 static_assert(PW_CONCAT(value32_, 2, 3, 4, 5, 6, 7, 8, 9, 0,
79 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
80 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
81 1, 2) == 101010);
82 // clang-format on
83}
84
85} // namespace
86} // namespace pw