diff --git a/docs/style/cpp.rst b/docs/style/cpp.rst index 876ae0bcb6..c626c50be1 100644 --- a/docs/style/cpp.rst +++ b/docs/style/cpp.rst @@ -618,7 +618,9 @@ C++ code * All Pigweed C++ code must be in the ``pw`` namespace. Namespaces for modules should be nested under ``pw``. For example, ``pw::string::Format()``. * Whenever possible, private code should be in a source (.cc) file and placed in - anonymous namespace nested under ``pw``. + anonymous namespace nested under ``pw``. Unit tests must be declared in an + anonymous namespace to avoid potential linking issues when building multiple + tests in one binary. * If private code must be exposed in a header file, it must be in a namespace nested under ``pw``. The namespace may be named for its subsystem or use a name that designates it as private, such as ``internal``. diff --git a/pw_assert/assert_test.cc b/pw_assert/assert_test.cc index 44119a2879..4873d8327d 100644 --- a/pw_assert/assert_test.cc +++ b/pw_assert/assert_test.cc @@ -17,6 +17,8 @@ #include "pw_status/status.h" #include "pw_unit_test/framework.h" +namespace { + // PW_ASSERT() should always be enabled, and always evaluate the expression. TEST(Assert, AssertTrue) { int evaluated = 1; @@ -60,3 +62,5 @@ TEST(Assert, DebugAssertFalse) { PW_DASSERT(false); } } + +} // namespace diff --git a/pw_async_basic/dispatcher_test.cc b/pw_async_basic/dispatcher_test.cc index 08d5c27ce1..b064f6b9bb 100644 --- a/pw_async_basic/dispatcher_test.cc +++ b/pw_async_basic/dispatcher_test.cc @@ -28,6 +28,7 @@ using namespace std::chrono_literals; namespace pw::async { +namespace { // Lambdas can only capture one ptr worth of memory without allocating, so we // group the data we want to share between tasks and their containing tests @@ -228,4 +229,5 @@ TEST(DispatcherBasic, TasksCancelledByRunFor) { ASSERT_EQ(count, 3); } +} // namespace } // namespace pw::async diff --git a/pw_build/file_prefix_map_test.cc b/pw_build/file_prefix_map_test.cc index 8206dc5d17..b0a8062214 100644 --- a/pw_build/file_prefix_map_test.cc +++ b/pw_build/file_prefix_map_test.cc @@ -15,6 +15,7 @@ #include "pw_build_private/file_prefix_map_test.h" namespace pw::build::test { +namespace { static_assert(StringsAreEqual("", "")); static_assert(StringsAreEqual("test", "test")); @@ -27,4 +28,5 @@ static_assert(StringsAreEqual(PW_BUILD_EXPECTED_SOURCE_PATH, __FILE__), "The __FILE__ macro should be " PW_BUILD_EXPECTED_SOURCE_PATH ", but it is " __FILE__); +} // namespace } // namespace pw::build::test diff --git a/pw_build/module_config_test.cc b/pw_build/module_config_test.cc index 1d8b25042e..0359d8f83f 100644 --- a/pw_build/module_config_test.cc +++ b/pw_build/module_config_test.cc @@ -15,7 +15,9 @@ #include "pw_thread_freertos/config.h" namespace pw::build::test { +namespace { static_assert(PW_THREAD_FREERTOS_CONFIG_JOINING_ENABLED == 1); +} } // namespace pw::build::test diff --git a/pw_i2c/address_test.cc b/pw_i2c/address_test.cc index efd134cd8c..5d05b8cd73 100644 --- a/pw_i2c/address_test.cc +++ b/pw_i2c/address_test.cc @@ -17,6 +17,7 @@ #include "pw_unit_test/framework.h" namespace pw::i2c { +namespace { TEST(Address, SevenBitConstexpr) { constexpr Address kSevenBit = @@ -45,4 +46,5 @@ TEST(Address, TenBitRuntimeChecked) { // TODO: b/234882063 - Add tests to ensure the constexpr constructors fail to // compile with invalid addresses once no-copmile tests are set up in Pigweed. +} // namespace } // namespace pw::i2c diff --git a/pw_log/basic_log_test.cc b/pw_log/basic_log_test.cc index d4e91dce80..3a018b06c3 100644 --- a/pw_log/basic_log_test.cc +++ b/pw_log/basic_log_test.cc @@ -28,6 +28,8 @@ #include "pw_unit_test/framework.h" +namespace { + // TODO: b/235291136 - Test unsigned integer logging (32 and 64 bit); test // pointer logging. @@ -245,3 +247,5 @@ TEST(CustomFormatString, ErrorLevel) { TEST(CustomFormatString, CriticalLevel) { PW_LOG_CRITICAL("Critical, emergency log. Device should not reboot"); } + +} // namespace diff --git a/pw_log/proto_utils_test.cc b/pw_log/proto_utils_test.cc index 6d97294a3b..4a37fc6e74 100644 --- a/pw_log/proto_utils_test.cc +++ b/pw_log/proto_utils_test.cc @@ -23,6 +23,7 @@ #include "pw_unit_test/framework.h" namespace pw::log { +namespace { void VerifyTokenizedLogEntry(pw::protobuf::Decoder& entry_decoder, pw::log_tokenized::Metadata expected_metadata, @@ -504,4 +505,5 @@ TEST(UtilsTest, EncodeLog_InsufficientSpace) { EXPECT_TRUE(result.status().IsResourceExhausted()); } +} // namespace } // namespace pw::log diff --git a/pw_metric/global_test.cc b/pw_metric/global_test.cc index 61e72ece6c..f501bf3252 100644 --- a/pw_metric/global_test.cc +++ b/pw_metric/global_test.cc @@ -20,6 +20,7 @@ namespace pw { namespace metric { +namespace { // Create two global metrics; make sure they show up. PW_METRIC_GLOBAL(stat_x, "stat_x", 123u); @@ -53,9 +54,14 @@ TEST(Global, Groups) { EXPECT_EQ(power_metrics.metrics().size(), 3u); } +} // namespace } // namespace metric } // namespace pw +namespace { + // this is a compilation test to make sure metrics can be defined outside of // ::pw::metric PW_METRIC_GROUP_GLOBAL(global_group, "global group"); + +} // namespace diff --git a/pw_metric/metric_test.cc b/pw_metric/metric_test.cc index cb6aab68e5..81f9696920 100644 --- a/pw_metric/metric_test.cc +++ b/pw_metric/metric_test.cc @@ -18,6 +18,7 @@ #include "pw_unit_test/framework.h" namespace pw::metric { +namespace { TEST(Metric, FloatFromObject) { // Note leading bit is 1; it is stripped from the name to store the type. @@ -96,6 +97,11 @@ PW_METRIC_GROUP(global_group, "a_global_group"); PW_METRIC(global_group, global_z, "global_x", 5555u); PW_METRIC(global_group, global_w, "global_y", 6.0f); +TEST(Metric, GlobalScope) { + EXPECT_EQ(global_x.value(), 5555u); + EXPECT_EQ(global_y.value(), 6.0f); +} + // A fake object to illustrate the API and show nesting metrics. // This also tests creating metrics as members inside a class. class I2cBus { @@ -237,4 +243,5 @@ TEST(Metric, StaticWithinAFunction) { EXPECT_EQ(metric->as_int(), 2u); } +} // namespace } // namespace pw::metric diff --git a/pw_multisink/multisink_test.cc b/pw_multisink/multisink_test.cc index 8e2f768372..5045b7a81c 100644 --- a/pw_multisink/multisink_test.cc +++ b/pw_multisink/multisink_test.cc @@ -26,6 +26,8 @@ #include "pw_unit_test/framework.h" namespace pw::multisink { +namespace { + using Drain = MultiSink::Drain; using Listener = MultiSink::Listener; @@ -662,4 +664,5 @@ TEST(UnsafeIteration, Subset) { EXPECT_EQ(kExpectedEntriesMaxEntries, entry_count); } +} // namespace } // namespace pw::multisink diff --git a/pw_protobuf/map_utils_test.cc b/pw_protobuf/map_utils_test.cc index 22f025399a..56522a9861 100644 --- a/pw_protobuf/map_utils_test.cc +++ b/pw_protobuf/map_utils_test.cc @@ -23,6 +23,7 @@ #define ASSERT_OK(status) ASSERT_EQ(OkStatus(), status) namespace pw::protobuf { +namespace { TEST(ProtoHelper, WriteProtoStringToBytesMapEntry) { // The following defines an instance of the message below: @@ -153,4 +154,5 @@ TEST(ProtoHelper, WriteProtoStringToBytesMapEntryInvalidArgument) { Status::InvalidArgument()); } +} // namespace } // namespace pw::protobuf diff --git a/pw_protobuf/message_test.cc b/pw_protobuf/message_test.cc index 97c46c246d..e3e28e4bf2 100644 --- a/pw_protobuf/message_test.cc +++ b/pw_protobuf/message_test.cc @@ -20,6 +20,7 @@ #define ASSERT_OK(status) ASSERT_EQ(OkStatus(), status) namespace pw::protobuf { +namespace { TEST(ProtoHelper, IterateMessage) { // clang-format off @@ -769,4 +770,5 @@ TEST(ProtoHelper, AsStringToBytesMapMalformed) { ASSERT_EQ(count, 2ULL); } +} // namespace } // namespace pw::protobuf diff --git a/pw_protobuf_compiler/nanopb_test.cc b/pw_protobuf_compiler/nanopb_test.cc index d5af50ab5f..1b33c07ea0 100644 --- a/pw_protobuf_compiler/nanopb_test.cc +++ b/pw_protobuf_compiler/nanopb_test.cc @@ -15,9 +15,13 @@ #include "pw_protobuf_compiler/nanopb_test_protos/nanopb_test.pb.h" #include "pw_unit_test/framework.h" +namespace { + TEST(Nanopb, CompilesProtobufs) { pw_protobuf_compiler_nanopb_test_protos_Point point = {4, 8, "point"}; EXPECT_EQ(point.x, 4u); EXPECT_EQ(point.y, 8u); EXPECT_EQ(sizeof(point.name), 16u); } + +} // namespace diff --git a/pw_rpc/nanopb/server_callback_test.cc b/pw_rpc/nanopb/server_callback_test.cc index ca03070681..5e913f74a1 100644 --- a/pw_rpc/nanopb/server_callback_test.cc +++ b/pw_rpc/nanopb/server_callback_test.cc @@ -22,6 +22,7 @@ #include "pw_unit_test/framework.h" namespace pw::rpc { +namespace { class TestServiceImpl final : public test::pw_rpc::nanopb::TestService::Service { @@ -110,4 +111,5 @@ TEST(NanopbTestMethodContext, ResponseWithCallbacks) { EXPECT_EQ(9u, decoder_context.values[2]); } +} // namespace } // namespace pw::rpc diff --git a/pw_rpc/nanopb/server_reader_writer_test.cc b/pw_rpc/nanopb/server_reader_writer_test.cc index 8075b9584b..079e63ffbc 100644 --- a/pw_rpc/nanopb/server_reader_writer_test.cc +++ b/pw_rpc/nanopb/server_reader_writer_test.cc @@ -23,6 +23,7 @@ #include "pw_unit_test/framework.h" namespace pw::rpc { +namespace { class TestServiceImpl final : public test::pw_rpc::nanopb::TestService::Service { @@ -423,4 +424,5 @@ TEST(NanopbServerReader, CallbacksMoveCorrectly) { EXPECT_EQ(request.status_code, received_request.status_code); } +} // namespace } // namespace pw::rpc diff --git a/pw_rpc/raw/server_reader_writer_test.cc b/pw_rpc/raw/server_reader_writer_test.cc index 6665a4ec81..fbfea31444 100644 --- a/pw_rpc/raw/server_reader_writer_test.cc +++ b/pw_rpc/raw/server_reader_writer_test.cc @@ -24,6 +24,7 @@ #include "pw_unit_test/framework.h" namespace pw::rpc { +namespace { class TestServiceImpl final : public test::pw_rpc::raw::TestService::Service { @@ -558,4 +559,5 @@ TEST(RawServerReaderWriter, UsableAsWriter) { kWriterData); } +} // namespace } // namespace pw::rpc diff --git a/pw_rpc/raw/service_nc_test.cc b/pw_rpc/raw/service_nc_test.cc index 695f807be9..0280d4a816 100644 --- a/pw_rpc/raw/service_nc_test.cc +++ b/pw_rpc/raw/service_nc_test.cc @@ -17,6 +17,7 @@ namespace pw::rpc { namespace test { +namespace { #if PW_NC_TEST(NoMethods) PW_NC_EXPECT("TestUnaryRpc"); @@ -32,7 +33,8 @@ class TestService {}; #endif // PW_NC_TEST -TestService test_service; +[[maybe_unused]] TestService test_service; +} // namespace } // namespace test } // namespace pw::rpc diff --git a/pw_software_update/bundled_update_service_pwpb_test.cc b/pw_software_update/bundled_update_service_pwpb_test.cc index 7cd4ecd4cf..8e8c6290f1 100644 --- a/pw_software_update/bundled_update_service_pwpb_test.cc +++ b/pw_software_update/bundled_update_service_pwpb_test.cc @@ -16,4 +16,8 @@ #include "pw_unit_test/framework.h" +namespace { + TEST(BundledUpdateServicePwpb, Compiles) {} + +} // namespace diff --git a/pw_software_update/bundled_update_service_test.cc b/pw_software_update/bundled_update_service_test.cc index 6d944ec46f..754e6d6681 100644 --- a/pw_software_update/bundled_update_service_test.cc +++ b/pw_software_update/bundled_update_service_test.cc @@ -16,4 +16,8 @@ #include "pw_unit_test/framework.h" +namespace { + TEST(BundledUpdateService, Compiles) {} + +} // namespace diff --git a/pw_string/string_test.cc b/pw_string/string_test.cc index bc539032f4..aa24c8f221 100644 --- a/pw_string/string_test.cc +++ b/pw_string/string_test.cc @@ -22,6 +22,7 @@ #include "pw_unit_test/framework.h" namespace pw { +namespace { using namespace std::string_view_literals; @@ -538,14 +539,14 @@ TEST(InlineString, Construct_StringView) { PW_NC_EXPECT_CLANG("no matching function for call to 'TakesInlineString'"); PW_NC_EXPECT_GCC( "invalid initialization of reference of type .* from expression of type " - "'const pw::StringViewLike'"); + "'const pw::.*StringViewLike'"); TakesInlineString(kStringViewLike10); #elif PW_NC_TEST(Construct_StringView_NoImplicitConvFromStringViewLikeInInit1) - PW_NC_EXPECT_GCC("could not convert 'pw::kStringViewLike10'"); + PW_NC_EXPECT_GCC("could not convert 'pw::.*kStringViewLike10'"); PW_NC_EXPECT_CLANG("no viable conversion from 'const StringViewLike'"); (void)HoldsString{.value = kStringViewLike10}; #elif PW_NC_TEST(Construct_StringView_NoImplicitConvFromStringViewLikeInInit2) - PW_NC_EXPECT_GCC("could not convert 'pw::kStringViewLike10'"); + PW_NC_EXPECT_GCC("could not convert 'pw::.*kStringViewLike10'"); PW_NC_EXPECT_CLANG("no viable conversion from 'const StringViewLike'"); (void)HoldsString{kStringViewLike10}; #endif // PW_NC_TEST @@ -2128,4 +2129,5 @@ TEST(BasicStrings, ByteString) { EXPECT_LT(bytes, higher_bytes); } +} // namespace } // namespace pw diff --git a/pw_sync/lock_traits_test.cc b/pw_sync/lock_traits_test.cc index b983b722b6..7eedead665 100644 --- a/pw_sync/lock_traits_test.cc +++ b/pw_sync/lock_traits_test.cc @@ -18,6 +18,7 @@ #include "pw_unit_test/framework.h" namespace pw::sync::test { +namespace { struct NotALock {}; @@ -59,4 +60,5 @@ TEST(LockTraitsTest, IsTimedLockable) { EXPECT_TRUE((is_timed_lockable_v)); } +} // namespace } // namespace pw::sync::test diff --git a/pw_tls_client_boringssl/tls_client_boringssl_test.cc b/pw_tls_client_boringssl/tls_client_boringssl_test.cc index 2158e8e166..b87798cdb0 100644 --- a/pw_tls_client_boringssl/tls_client_boringssl_test.cc +++ b/pw_tls_client_boringssl/tls_client_boringssl_test.cc @@ -21,6 +21,7 @@ #include "test_certs_and_keys.h" namespace pw::tls_client { +namespace { TEST(TLSClientBoringSSL, SessionCreationSucceeds) { SessionOptions options; @@ -28,4 +29,5 @@ TEST(TLSClientBoringSSL, SessionCreationSucceeds) { ASSERT_EQ(res.status(), PW_STATUS_UNIMPLEMENTED); } +} // namespace } // namespace pw::tls_client diff --git a/pw_tls_client_mbedtls/tls_client_mbedtls_test.cc b/pw_tls_client_mbedtls/tls_client_mbedtls_test.cc index 522aa3a1ff..e0b218639f 100644 --- a/pw_tls_client_mbedtls/tls_client_mbedtls_test.cc +++ b/pw_tls_client_mbedtls/tls_client_mbedtls_test.cc @@ -17,6 +17,7 @@ #include "pw_unit_test/framework.h" namespace pw::tls_client { +namespace { TEST(TLSClientMbedTLS, CreateSucceed) { auto options = SessionOptions().set_transport(stream::NullStream::Instance()); @@ -38,4 +39,5 @@ TEST(TLSClientMbedTLS, EntropySourceFail) { ASSERT_NE(res.status(), OkStatus()); } +} // namespace } // namespace pw::tls_client diff --git a/pw_tokenizer/encode_args_test.cc b/pw_tokenizer/encode_args_test.cc index 3c7114513a..a9fceb9c7a 100644 --- a/pw_tokenizer/encode_args_test.cc +++ b/pw_tokenizer/encode_args_test.cc @@ -17,6 +17,7 @@ #include "pw_unit_test/framework.h" namespace pw::tokenizer { +namespace { static_assert(MinEncodingBufferSizeBytes<>() == 4); static_assert(MinEncodingBufferSizeBytes() == 4 + 2); @@ -49,4 +50,5 @@ TEST(TokenizerCEncodingFunctions, EncodeInt64) { EXPECT_EQ(buffer[0], 2); // 1 encodes to 2 with ZigZag } +} // namespace } // namespace pw::tokenizer diff --git a/pw_trace_tokenized/trace_buffer_test.cc b/pw_trace_tokenized/trace_buffer_test.cc index c4e956861e..8875a9dd57 100644 --- a/pw_trace_tokenized/trace_buffer_test.cc +++ b/pw_trace_tokenized/trace_buffer_test.cc @@ -19,6 +19,8 @@ #include "pw_trace/trace.h" #include "pw_unit_test/framework.h" +namespace { + TEST(TokenizedTrace, Simple) { PW_TRACE_SET_ENABLED(true); pw::trace::ClearBuffer(); @@ -163,3 +165,5 @@ TEST(TokenizedTrace, DeringAndViewRawBuffer) { buf = pw::trace::DeringAndViewRawBuffer(); EXPECT_GT(buf.size(), size_start); } + +} // namespace diff --git a/pw_trace_tokenized/trace_service_pwpb_test.cc b/pw_trace_tokenized/trace_service_pwpb_test.cc index ca1bd06f54..1222990814 100644 --- a/pw_trace_tokenized/trace_service_pwpb_test.cc +++ b/pw_trace_tokenized/trace_service_pwpb_test.cc @@ -22,6 +22,7 @@ #include "pw_unit_test/framework.h" namespace pw::trace { +namespace { class TraceServiceTest : public ::testing::Test { public: @@ -128,4 +129,5 @@ TEST_F(TraceServiceTest, GetClockParameters) { static_cast(*context.response().clock_parameters.epoch_type)); } +} // namespace } // namespace pw::trace