format tests with clang-format (#1282)

This commit is contained in:
Dominic Hamon 2021-11-10 16:22:31 +00:00 committed by GitHub
parent fcef4fb669
commit c07a498924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 126 additions and 142 deletions

View File

@ -1,10 +1,10 @@
#include "benchmark/benchmark.h"
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <vector> #include <vector>
#include "benchmark/benchmark.h"
class ArgsProductFixture : public ::benchmark::Fixture { class ArgsProductFixture : public ::benchmark::Fixture {
public: public:
ArgsProductFixture() ArgsProductFixture()

View File

@ -96,7 +96,6 @@ void BM_empty_stop_start(benchmark::State& state) {
BENCHMARK(BM_empty_stop_start); BENCHMARK(BM_empty_stop_start);
BENCHMARK(BM_empty_stop_start)->ThreadPerCpu(); BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
void BM_KeepRunning(benchmark::State& state) { void BM_KeepRunning(benchmark::State& state) {
benchmark::IterationCount iter_count = 0; benchmark::IterationCount iter_count = 0;
assert(iter_count == state.iterations()); assert(iter_count == state.iterations());
@ -171,8 +170,10 @@ BENCHMARK(BM_TwoTemplateFunc<double, int>)->Arg(1);
// Ensure that StateIterator provides all the necessary typedefs required to // Ensure that StateIterator provides all the necessary typedefs required to
// instantiate std::iterator_traits. // instantiate std::iterator_traits.
static_assert(std::is_same< static_assert(
typename std::iterator_traits<benchmark::State::StateIterator>::value_type, std::is_same<typename std::iterator_traits<
typename benchmark::State::StateIterator::value_type>::value, ""); benchmark::State::StateIterator>::value_type,
typename benchmark::State::StateIterator::value_type>::value,
"");
BENCHMARK_MAIN(); BENCHMARK_MAIN();

View File

@ -93,8 +93,9 @@ static void BM_SetInsert(benchmark::State& state) {
state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int)); state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int));
} }
// Test many inserts at once to reduce the total iterations needed. Otherwise, the slower, // Test many inserts at once to reduce the total iterations needed. Otherwise,
// non-timed part of each iteration will make the benchmark take forever. // the slower, non-timed part of each iteration will make the benchmark take
// forever.
BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}}); BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}});
template <typename Container, template <typename Container,
@ -214,7 +215,8 @@ BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"),
std::pair<int, double>(42, 3.8)); std::pair<int, double>(42, 3.8));
void BM_non_template_args(benchmark::State& state, int, double) { void BM_non_template_args(benchmark::State& state, int, double) {
while(state.KeepRunning()) {} while (state.KeepRunning()) {
}
} }
BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0); BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);

View File

@ -9,7 +9,6 @@ extern "C" {
extern int ExternInt; extern int ExternInt;
extern int ExternInt2; extern int ExternInt2;
extern int ExternInt3; extern int ExternInt3;
} }
// CHECK-LABEL: test_basic: // CHECK-LABEL: test_basic:

View File

@ -4,6 +4,7 @@
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include "output_test.h" #include "output_test.h"

View File

@ -44,8 +44,7 @@ BENCHMARK_TEMPLATE(BM_template1, long);
BENCHMARK_TEMPLATE1(BM_template1, int); BENCHMARK_TEMPLATE1(BM_template1, int);
template <class T> template <class T>
struct BM_Fixture : public ::benchmark::Fixture { struct BM_Fixture : public ::benchmark::Fixture {};
};
BENCHMARK_TEMPLATE_F(BM_Fixture, BM_template1, long)(benchmark::State& state) { BENCHMARK_TEMPLATE_F(BM_Fixture, BM_template1, long)(benchmark::State& state) {
BM_empty(state); BM_empty(state);

View File

@ -26,7 +26,8 @@ void TestHandler() {
} }
void try_invalid_pause_resume(benchmark::State& state) { void try_invalid_pause_resume(benchmark::State& state) {
#if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && !defined(TEST_HAS_NO_EXCEPTIONS) #if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && \
!defined(TEST_HAS_NO_EXCEPTIONS)
try { try {
state.PauseTiming(); state.PauseTiming();
std::abort(); std::abort();
@ -57,7 +58,6 @@ void BM_diagnostic_test(benchmark::State& state) {
} }
BENCHMARK(BM_diagnostic_test); BENCHMARK(BM_diagnostic_test);
void BM_diagnostic_test_keep_running(benchmark::State& state) { void BM_diagnostic_test_keep_running(benchmark::State& state) {
static bool called_once = false; static bool called_once = false;

View File

@ -23,7 +23,6 @@ struct Large {
int value; int value;
int data[2]; int data[2];
}; };
} }
// CHECK-LABEL: test_with_rvalue: // CHECK-LABEL: test_with_rvalue:
extern "C" void test_with_rvalue() { extern "C" void test_with_rvalue() {
@ -118,8 +117,7 @@ extern "C" int test_div_by_two(int input) {
// CHECK-LABEL: test_inc_integer: // CHECK-LABEL: test_inc_integer:
extern "C" int test_inc_integer() { extern "C" int test_inc_integer() {
int x = 0; int x = 0;
for (int i=0; i < 5; ++i) for (int i = 0; i < 5; ++i) benchmark::DoNotOptimize(++x);
benchmark::DoNotOptimize(++x);
// CHECK: movl $1, [[DEST:.*]] // CHECK: movl $1, [[DEST:.*]]
// CHECK: {{(addl \$1,|incl)}} [[DEST]] // CHECK: {{(addl \$1,|incl)}} [[DEST]]
// CHECK: {{(addl \$1,|incl)}} [[DEST]] // CHECK: {{(addl \$1,|incl)}} [[DEST]]

View File

@ -1,13 +1,13 @@
#include "benchmark/benchmark.h"
#include <cstdint> #include <cstdint>
#include "benchmark/benchmark.h"
namespace { namespace {
#if defined(__GNUC__) #if defined(__GNUC__)
std::uint64_t double_up(const std::uint64_t x) __attribute__((const)); std::uint64_t double_up(const std::uint64_t x) __attribute__((const));
#endif #endif
std::uint64_t double_up(const std::uint64_t x) { return x * 2; } std::uint64_t double_up(const std::uint64_t x) { return x * 2; }
} } // namespace
// Using DoNotOptimize on types like BitRef seem to cause a lot of problems // Using DoNotOptimize on types like BitRef seem to cause a lot of problems
// with the inline assembly on both GCC and Clang. // with the inline assembly on both GCC and Clang.
@ -21,6 +21,7 @@ public:
BitRef b(1, arr[0]); BitRef b(1, arr[0]);
return b; return b;
} }
private: private:
BitRef(int i, unsigned char& b) : index(i), byte(b) {} BitRef(int i, unsigned char& b) : index(i), byte(b) {}
}; };

View File

@ -1,9 +1,9 @@
#include "benchmark/benchmark.h"
#include <cassert> #include <cassert>
#include <memory> #include <memory>
#include "benchmark/benchmark.h"
#define FIXTURE_BECHMARK_NAME MyFixture #define FIXTURE_BECHMARK_NAME MyFixture
class FIXTURE_BECHMARK_NAME : public ::benchmark::Fixture { class FIXTURE_BECHMARK_NAME : public ::benchmark::Fixture {

View File

@ -3,6 +3,7 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include "../src/timers.h" #include "../src/timers.h"
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include "output_test.h" #include "output_test.h"

View File

@ -1,8 +1,8 @@
#include "benchmark/benchmark.h"
#include <cstdlib> #include <cstdlib>
#include <map> #include <map>
#include "benchmark/benchmark.h"
namespace { namespace {
std::map<int, int> ConstructRandomMap(int size) { std::map<int, int> ConstructRandomMap(int size) {

View File

@ -1,10 +1,10 @@
#include "benchmark/benchmark.h"
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <vector> #include <vector>
#include "benchmark/benchmark.h"
class MultipleRangesFixture : public ::benchmark::Fixture { class MultipleRangesFixture : public ::benchmark::Fixture {
public: public:
MultipleRangesFixture() MultipleRangesFixture()

View File

@ -1,7 +1,8 @@
#include "benchmark/benchmark.h"
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include "benchmark/benchmark.h"
#if defined(NDEBUG) #if defined(NDEBUG)
#undef NDEBUG #undef NDEBUG
#endif #endif
@ -65,11 +66,9 @@ void BM_explicit_iteration_count(benchmark::State& state) {
// Test that the requested iteration count is respected. // Test that the requested iteration count is respected.
assert(state.max_iterations == 42); assert(state.max_iterations == 42);
size_t actual_iterations = 0; size_t actual_iterations = 0;
for (auto _ : state) for (auto _ : state) ++actual_iterations;
++actual_iterations;
assert(state.iterations() == state.max_iterations); assert(state.iterations() == state.max_iterations);
assert(state.iterations() == 42); assert(state.iterations() == 42);
} }
BENCHMARK(BM_explicit_iteration_count)->Iterations(42); BENCHMARK(BM_explicit_iteration_count)->Iterations(42);

View File

@ -113,9 +113,7 @@ struct Results {
return NumIterations() * GetTime(kRealTime); return NumIterations() * GetTime(kRealTime);
} }
// get the cpu_time duration of the benchmark in seconds // get the cpu_time duration of the benchmark in seconds
double DurationCPUTime() const { double DurationCPUTime() const { return NumIterations() * GetTime(kCpuTime); }
return NumIterations() * GetTime(kCpuTime);
}
// get the string for a result by name, or nullptr if the name // get the string for a result by name, or nullptr if the name
// is not found // is not found

View File

@ -317,9 +317,7 @@ int Results::NumThreads() const {
return num; return num;
} }
double Results::NumIterations() const { double Results::NumIterations() const { return GetAs<double>("iterations"); }
return GetAs<double>("iterations");
}
double Results::GetTime(BenchmarkTime which) const { double Results::GetTime(BenchmarkTime which) const {
BM_CHECK(which == kCpuTime || which == kRealTime); BM_CHECK(which == kCpuTime || which == kRealTime);
@ -469,8 +467,7 @@ static char RandomHexChar() {
static std::string GetRandomFileName() { static std::string GetRandomFileName() {
std::string model = "test.%%%%%%"; std::string model = "test.%%%%%%";
for (auto& ch : model) { for (auto& ch : model) {
if (ch == '%') if (ch == '%') ch = RandomHexChar();
ch = RandomHexChar();
} }
return model; return model;
} }
@ -487,8 +484,7 @@ static std::string GetTempFileName() {
int retries = 3; int retries = 3;
while (--retries) { while (--retries) {
std::string name = GetRandomFileName(); std::string name = GetRandomFileName();
if (!FileExists(name)) if (!FileExists(name)) return name;
return name;
} }
std::cerr << "Failed to create unique temporary file name" << std::endl; std::cerr << "Failed to create unique temporary file name" << std::endl;
std::abort(); std::abort();

View File

@ -81,15 +81,13 @@ int main(int argc, char** argv) {
TestReporter test_reporter; TestReporter test_reporter;
const char* const spec = "BM_Chosen"; const char* const spec = "BM_Chosen";
const size_t returned_count = const size_t returned_count =
benchmark::RunSpecifiedBenchmarks(&test_reporter, benchmark::RunSpecifiedBenchmarks(&test_reporter, spec);
spec);
assert(returned_count == 1); assert(returned_count == 1);
const std::vector<std::string> matched_functions = const std::vector<std::string> matched_functions =
test_reporter.GetMatchedFunctions(); test_reporter.GetMatchedFunctions();
assert(matched_functions.size() == 1); assert(matched_functions.size() == 1);
if (strcmp(spec, matched_functions.front().c_str()) != 0) { if (strcmp(spec, matched_functions.front().c_str()) != 0) {
std::cerr std::cerr << "Expected benchmark [" << spec << "] to run, but got ["
<< "Expected benchmark [" << spec << "] to run, but got ["
<< matched_functions.front() << "]\n"; << matched_functions.front() << "]\n";
return 2; return 2;
} }

View File

@ -2,8 +2,8 @@
// statistics_test - Unit tests for src/statistics.cc // statistics_test - Unit tests for src/statistics.cc
//===---------------------------------------------------------------------===// //===---------------------------------------------------------------------===//
#include "../src/string_util.h"
#include "../src/internal_macros.h" #include "../src/internal_macros.h"
#include "../src/string_util.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace { namespace {
@ -32,7 +32,8 @@ TEST(StringUtilTest, stoul) {
#elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
{ {
size_t pos = 0; size_t pos = 0;
EXPECT_EQ(0xFFFFFFFFFFFFFFFFul, benchmark::stoul("18446744073709551615", &pos)); EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
benchmark::stoul("18446744073709551615", &pos));
EXPECT_EQ(20ul, pos); EXPECT_EQ(20ul, pos);
} }
#endif #endif
@ -62,18 +63,14 @@ TEST(StringUtilTest, stoul) {
EXPECT_EQ(4ul, pos); EXPECT_EQ(4ul, pos);
} }
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
{ { ASSERT_THROW(benchmark::stoul("this is a test"), std::invalid_argument); }
ASSERT_THROW(benchmark::stoul("this is a test"), std::invalid_argument);
}
#endif #endif
} }
TEST(StringUtilTest, stoi) { TEST(StringUtilTest, stoi){{size_t pos = 0;
{
size_t pos = 0;
EXPECT_EQ(0, benchmark::stoi("0", &pos)); EXPECT_EQ(0, benchmark::stoi("0", &pos));
EXPECT_EQ(1ul, pos); EXPECT_EQ(1ul, pos);
} } // namespace
{ {
size_t pos = 0; size_t pos = 0;
EXPECT_EQ(-17, benchmark::stoi("-17", &pos)); EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
@ -110,15 +107,11 @@ TEST(StringUtilTest, stoi) {
EXPECT_EQ(4ul, pos); EXPECT_EQ(4ul, pos);
} }
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
{ { ASSERT_THROW(benchmark::stoi("this is a test"), std::invalid_argument); }
ASSERT_THROW(benchmark::stoi("this is a test"), std::invalid_argument);
}
#endif #endif
} }
TEST(StringUtilTest, stod) { TEST(StringUtilTest, stod){{size_t pos = 0;
{
size_t pos = 0;
EXPECT_EQ(0.0, benchmark::stod("0", &pos)); EXPECT_EQ(0.0, benchmark::stod("0", &pos));
EXPECT_EQ(1ul, pos); EXPECT_EQ(1ul, pos);
} }
@ -144,9 +137,7 @@ TEST(StringUtilTest, stod) {
EXPECT_EQ(8ul, pos); EXPECT_EQ(8ul, pos);
} }
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
{ { ASSERT_THROW(benchmark::stod("this is a test"), std::invalid_argument); }
ASSERT_THROW(benchmark::stod("this is a test"), std::invalid_argument);
}
#endif #endif
} }

View File

@ -1,9 +1,9 @@
#include "benchmark/benchmark.h"
#include <cassert> #include <cassert>
#include <memory> #include <memory>
#include "benchmark/benchmark.h"
template <typename T> template <typename T>
class MyFixture : public ::benchmark::Fixture { class MyFixture : public ::benchmark::Fixture {
public: public: