1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_preprocessor/util.h"
16
17 #include <cstdint>
18
19 #include "gtest/gtest.h"
20
21 namespace pw {
22 namespace {
23
TEST(Macros,ArraySize)24 TEST(Macros, ArraySize) {
25 uint32_t hello_there[123];
26 static_assert(PW_ARRAY_SIZE(hello_there) == 123);
27
28 char characters[500];
29 static_assert(PW_ARRAY_SIZE(characters) == 500);
30 static_assert(PW_ARRAY_SIZE("2345") == 5);
31
32 struct Object {
33 int a;
34 uint64_t array[7];
35 };
36 Object objects[9];
37
38 static_assert(PW_ARRAY_SIZE(objects) == 9);
39 static_assert(PW_ARRAY_SIZE(Object::array) == 7);
40 static_assert(PW_ARRAY_SIZE(objects[1].array) == 7);
41 }
42
43 #define HELLO hello
44 #define WORLD WORLD_IMPL()
45 #define WORLD_IMPL() WORLD !
46
TEST(Macros,Stringify)47 TEST(Macros, Stringify) {
48 EXPECT_STREQ("", PW_STRINGIFY());
49 EXPECT_STREQ("> _ <", PW_STRINGIFY(> _ <));
50 EXPECT_STREQ("hello WORLD !", PW_STRINGIFY(HELLO WORLD));
51 EXPECT_STREQ("hello, WORLD !", PW_STRINGIFY(HELLO, WORLD));
52 EXPECT_STREQ("a, b, c, hello, WORLD ! 2",
53 PW_STRINGIFY(a, b, c, HELLO, WORLD 2));
54 }
55
56 } // namespace
57 } // namespace pw
58