-
Notifications
You must be signed in to change notification settings - Fork 1.8k
change setup and teardown callback type #1934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
db81344
change type of callbacks
EfesX 2809227
fix clang-format
EfesX a4fad8f
back to the original test
EfesX 1f17a90
add test for functors and lambdas
EfesX 57ab5f9
public alias
EfesX 2fe49da
fix clang-format
EfesX 9b5852e
fix clang-format
EfesX 363c33a
back to original test
EfesX a27e122
use universal reference
EfesX 51a4fa0
some upgrades
EfesX 1d0d08c
add test
EfesX 561a8a2
fix linter
EfesX 2cd16a5
fix typo
EfesX a9c15b1
some fixes
EfesX 46350a9
fix linter
EfesX 33faf45
fix test
EfesX 6c63eca
fix linter
EfesX 7d14632
improve test
EfesX a6a05c9
fix test
EfesX e6101c5
Merge branch 'main' into std_func
EfesX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #include "benchmark/benchmark.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using benchmark::BenchmarkReporter; | ||
| using benchmark::callback_function; | ||
| using benchmark::ClearRegisteredBenchmarks; | ||
| using benchmark::RegisterBenchmark; | ||
| using benchmark::RunSpecifiedBenchmarks; | ||
| using benchmark::State; | ||
| using benchmark::internal::Benchmark; | ||
|
|
||
| static int functor_called = 0; | ||
| struct Functor { | ||
| void operator()(const benchmark::State& /*unused*/) { functor_called++; } | ||
| }; | ||
|
|
||
| class NullReporter : public BenchmarkReporter { | ||
| public: | ||
| bool ReportContext(const Context& /*context*/) override { return true; } | ||
| void ReportRuns(const std::vector<Run>& /* report */) override {} | ||
| }; | ||
|
|
||
| class BenchmarkTest : public testing::Test { | ||
| public: | ||
| Benchmark* bm; | ||
| NullReporter null_reporter; | ||
|
|
||
| int setup_calls; | ||
| int teardown_calls; | ||
|
|
||
| void SetUp() override { | ||
| setup_calls = 0; | ||
| teardown_calls = 0; | ||
| functor_called = 0; | ||
|
|
||
| bm = RegisterBenchmark("BM", [](State& st) { | ||
| for (auto _ : st) { | ||
| } | ||
| }); | ||
| bm->Iterations(1); | ||
| } | ||
|
|
||
| void TearDown() override { ClearRegisteredBenchmarks(); } | ||
| }; | ||
|
|
||
| // Test that Setup/Teardown can correctly take a lambda expressions | ||
| TEST_F(BenchmarkTest, LambdaTestCopy) { | ||
| auto setup_lambda = [this](const State&) { setup_calls++; }; | ||
| auto teardown_lambda = [this](const State&) { teardown_calls++; }; | ||
| bm->Setup(setup_lambda); | ||
| bm->Teardown(teardown_lambda); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(setup_calls, 1); | ||
| EXPECT_EQ(teardown_calls, 1); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can correctly take a lambda expressions | ||
| TEST_F(BenchmarkTest, LambdaTestMove) { | ||
| auto setup_lambda = [this](const State&) { setup_calls++; }; | ||
| auto teardown_lambda = [this](const State&) { teardown_calls++; }; | ||
| bm->Setup(std::move(setup_lambda)); | ||
| bm->Teardown(std::move(teardown_lambda)); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(setup_calls, 1); | ||
| EXPECT_EQ(teardown_calls, 1); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can correctly take std::function | ||
| TEST_F(BenchmarkTest, CallbackFunctionCopy) { | ||
| callback_function setup_lambda = [this](const State&) { setup_calls++; }; | ||
| callback_function teardown_lambda = [this](const State&) { | ||
| teardown_calls++; | ||
| }; | ||
| bm->Setup(setup_lambda); | ||
| bm->Teardown(teardown_lambda); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(setup_calls, 1); | ||
| EXPECT_EQ(teardown_calls, 1); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can correctly take std::function | ||
| TEST_F(BenchmarkTest, CallbackFunctionMove) { | ||
| callback_function setup_lambda = [this](const State&) { setup_calls++; }; | ||
| callback_function teardown_lambda = [this](const State&) { | ||
| teardown_calls++; | ||
| }; | ||
| bm->Setup(std::move(setup_lambda)); | ||
| bm->Teardown(std::move(teardown_lambda)); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(setup_calls, 1); | ||
| EXPECT_EQ(teardown_calls, 1); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can correctly take functors | ||
| TEST_F(BenchmarkTest, FunctorCopy) { | ||
| Functor func; | ||
| bm->Setup(func); | ||
| bm->Teardown(func); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(functor_called, 2); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can correctly take functors | ||
| TEST_F(BenchmarkTest, FunctorMove) { | ||
| Functor func1; | ||
| Functor func2; | ||
| bm->Setup(std::move(func1)); | ||
| bm->Teardown(std::move(func2)); | ||
| RunSpecifiedBenchmarks(&null_reporter); | ||
| EXPECT_EQ(functor_called, 2); | ||
| } | ||
|
|
||
| // Test that Setup/Teardown can not take nullptr | ||
| TEST_F(BenchmarkTest, NullptrTest) { | ||
| #if GTEST_HAS_DEATH_TEST | ||
| // Tests only runnable in debug mode (when BM_CHECK is enabled). | ||
| #ifndef NDEBUG | ||
| #ifndef TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS | ||
| EXPECT_DEATH(bm->Setup(nullptr), "setup != nullptr"); | ||
| EXPECT_DEATH(bm->Teardown(nullptr), "teardown != nullptr"); | ||
| #else | ||
| GTEST_SKIP() << "Test skipped because BM_CHECK is disabled"; | ||
| #endif | ||
| #endif | ||
| #endif | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.