From c10c81201dba4d6533b1270baec0cd24aca752b9 Mon Sep 17 00:00:00 2001 From: Alexei Frolov Date: Fri, 1 Nov 2019 16:31:19 -0700 Subject: Add preprocessor and unit_test modules This change adds two Pigweed modules: pw_preprocessor and pw_unit_test. The preprocessor module contains header files providing helpful macros for the C preprocessor. The unit test module contains a starter implementation of a unit testing framework for Pigweed. Change-Id: I46e1a4cae1fd8ce36d7840a2e92f8013fb489cde --- pw_preprocessor/util_test.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pw_preprocessor/util_test.cc (limited to 'pw_preprocessor/util_test.cc') diff --git a/pw_preprocessor/util_test.cc b/pw_preprocessor/util_test.cc new file mode 100644 index 000000000..0ce027f53 --- /dev/null +++ b/pw_preprocessor/util_test.cc @@ -0,0 +1,66 @@ +// Copyright 2019 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +#include "pw_preprocessor/util.h" + +#include + +#include "pw_unit_test/framework.h" + +namespace pw { +namespace { + +TEST(Macros, ArraySize) { + uint32_t hello_there[123]; + static_assert(PW_ARRAY_SIZE(hello_there) == 123); + + char characters[500]; + static_assert(PW_ARRAY_SIZE(characters) == 500); + static_assert(PW_ARRAY_SIZE("2345") == 5); + + struct Object { + int a; + uint64_t array[2048]; + }; + Object objects[404]; + + static_assert(PW_ARRAY_SIZE(objects) == 404); + static_assert(PW_ARRAY_SIZE(Object::array) == 2048); + static_assert(PW_ARRAY_SIZE(objects[1].array) == 2048); +} + +TEST(Macros, UnusedVariable) { + int this_is_not_used = 12; + PW_UNUSED(this_is_not_used); + + volatile void* volatile wow; + wow = nullptr; + PW_UNUSED(wow); +} + +#define HELLO hello +#define WORLD WORLD_IMPL() +#define WORLD_IMPL() WORLD ! + +TEST(Macros, Stringify) { + EXPECT_STREQ("", PW_STRINGIFY()); + EXPECT_STREQ("> _ <", PW_STRINGIFY(> _ <)); + EXPECT_STREQ("hello WORLD !", PW_STRINGIFY(HELLO WORLD)); + EXPECT_STREQ("hello, WORLD !", PW_STRINGIFY(HELLO, WORLD)); + EXPECT_STREQ("a, b, c, hello, WORLD ! 2", + PW_STRINGIFY(a, b, c, HELLO, WORLD 2)); +} + +} // namespace +} // namespace pw -- cgit v1.2.3