Skip to content

Commit

Permalink
add death tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oontvoo committed Jan 24, 2023
1 parent f0c1285 commit 4a22d68
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/benchmark_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ void RunInThread(const BenchmarkInstance* b, IterationCount iters,
manager->NotifyThreadComplete();
}

double ComputeMinTime(const benchmark::internal::BenchmarkInstance& b,
const BenchTimeType& iters_or_time) {
if (!IsZero(b.min_time())) return b.min_time();
// If the flag was used to specify number of iters, then return the default
// min_time.
if (iters_or_time.tag == BenchTimeType::ITERS) return kDefaultMinTime;

return iters_or_time.time;
}

IterationCount ComputeIters(const benchmark::internal::BenchmarkInstance& b,
const BenchTimeType& iters_or_time) {
if (b.iterations() != 0) return b.iterations();

// We've already concluded that this flag is currently used to pass
// iters but do a check here again anyway.
BM_CHECK(iters_or_time.tag == BenchTimeType::ITERS);
return iters_or_time.iters;
}

} // end namespace

BenchTimeType ParseBenchMinTime(const std::string& value) {
BenchTimeType ret;

Expand Down Expand Up @@ -197,28 +219,6 @@ BenchTimeType ParseBenchMinTime(const std::string& value) {
return ret;
}

double ComputeMinTime(const benchmark::internal::BenchmarkInstance& b,
const BenchTimeType& iters_or_time) {
if (!IsZero(b.min_time())) return b.min_time();
// If the flag was used to specify number of iters, then return the default
// min_time.
if (iters_or_time.tag == BenchTimeType::ITERS) return kDefaultMinTime;

return iters_or_time.time;
}

IterationCount ComputeIters(const benchmark::internal::BenchmarkInstance& b,
const BenchTimeType& iters_or_time) {
if (b.iterations() != 0) return b.iterations();

// We've already concluded that this flag is currently used to pass
// iters but do a check here again anyway.
BM_CHECK(iters_or_time.tag == BenchTimeType::ITERS);
return iters_or_time.iters;
}

} // end namespace

BenchmarkRunner::BenchmarkRunner(
const benchmark::internal::BenchmarkInstance& b_,
BenchmarkReporter::PerFamilyRunReports* reports_for_family_)
Expand Down
2 changes: 2 additions & 0 deletions src/benchmark_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct BenchTimeType {
};
};

BenchTimeType ParseBenchMinTime(const std::string& value);

class BenchmarkRunner {
public:
BenchmarkRunner(const benchmark::internal::BenchmarkInstance& b_,
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
add_gtest(string_util_gtest)
add_gtest(perf_counters_gtest)
add_gtest(time_unit_gtest)
add_gtest(min_time_parse_gtest)
endif(BENCHMARK_ENABLE_GTEST_TESTS)

###############################################################################
Expand Down
20 changes: 20 additions & 0 deletions test/min_time_parse_gtest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../src/benchmark_runner.h"
#include "gtest/gtest.h"

namespace {

TEST(ParseMinTimeTest, InvalidInput) {
ASSERT_DEATH({ benchmark::internal::ParseBenchMinTime("abc"); },
"Malformed seconds value passed to --benchmark_min_time: `abc`");

ASSERT_DEATH(
{ benchmark::internal::ParseBenchMinTime("123ms"); },
"Malformed seconds value passed to --benchmark_min_time: `123ms`");

ASSERT_DEATH({ benchmark::internal::ParseBenchMinTime("1z"); },
"Malformed seconds value passed to --benchmark_min_time: `1z`");

ASSERT_DEATH({ benchmark::internal::ParseBenchMinTime("1hs"); },
"Malformed seconds value passed to --benchmark_min_time: `1hs`");
}
} // namespace

0 comments on commit 4a22d68

Please sign in to comment.