Skip to content

Commit

Permalink
fix(tests): prevent clang from optimizing new away
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Jan 2, 2024
1 parent b1bc8e7 commit 1292061
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/logging_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023, Google Inc.
// Copyright (c) 2024, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -355,10 +355,19 @@ struct NewHook {
~NewHook() { g_new_hook = nullptr; }
};

namespace {
int* allocInt() { return new int; }
} // namespace

TEST(DeathNoAllocNewHook, logging) {
// tests that NewHook used below works
NewHook new_hook;
ASSERT_DEATH({ new int; }, "unexpected new");
// Avoid unused warnings under MinGW
//
// NOTE MSVC produces warning C4551 here if we do not take the address of the
// function explicitly.
(void)&allocInt;
ASSERT_DEATH({ allocInt(); }, "unexpected new");
}

void TestRawLogging() {
Expand Down

0 comments on commit 1292061

Please sign in to comment.