[RISCV] Add shrinkwrap test cases showing gaps in current impl
[llvm-project.git] / third-party / benchmark / test / benchmark_min_time_flag_time_test.cc
blob04a82eb95bf9dc91b3d2a4bd189b611325050db6
1 #include <cassert>
2 #include <climits>
3 #include <cmath>
4 #include <cstdlib>
5 #include <cstring>
6 #include <iostream>
7 #include <string>
8 #include <vector>
10 #include "benchmark/benchmark.h"
12 // Tests that we can specify the min time with
13 // --benchmark_min_time=<NUM> (no suffix needed) OR
14 // --benchmark_min_time=<NUM>s
15 namespace {
17 // This is from benchmark.h
18 typedef int64_t IterationCount;
20 class TestReporter : public benchmark::ConsoleReporter {
21 public:
22 virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE {
23 return ConsoleReporter::ReportContext(context);
26 virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
27 assert(report.size() == 1);
28 ConsoleReporter::ReportRuns(report);
31 virtual void ReportRunsConfig(double min_time, bool /* has_explicit_iters */,
32 IterationCount /* iters */) BENCHMARK_OVERRIDE {
33 min_times_.push_back(min_time);
36 TestReporter() {}
38 virtual ~TestReporter() {}
40 const std::vector<double>& GetMinTimes() const { return min_times_; }
42 private:
43 std::vector<double> min_times_;
46 bool AlmostEqual(double a, double b) {
47 return std::fabs(a - b) < std::numeric_limits<double>::epsilon();
50 void DoTestHelper(int* argc, const char** argv, double expected) {
51 benchmark::Initialize(argc, const_cast<char**>(argv));
53 TestReporter test_reporter;
54 const size_t returned_count =
55 benchmark::RunSpecifiedBenchmarks(&test_reporter, "BM_MyBench");
56 assert(returned_count == 1);
58 // Check the min_time
59 const std::vector<double>& min_times = test_reporter.GetMinTimes();
60 assert(!min_times.empty() && AlmostEqual(min_times[0], expected));
63 } // end namespace
65 static void BM_MyBench(benchmark::State& state) {
66 for (auto s : state) {
69 BENCHMARK(BM_MyBench);
71 int main(int argc, char** argv) {
72 // Make a fake argv and append the new --benchmark_min_time=<foo> to it.
73 int fake_argc = argc + 1;
74 const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
76 for (int i = 0; i < argc; ++i) fake_argv[i] = argv[i];
78 const char* no_suffix = "--benchmark_min_time=4";
79 const char* with_suffix = "--benchmark_min_time=4.0s";
80 double expected = 4.0;
82 fake_argv[argc] = no_suffix;
83 DoTestHelper(&fake_argc, fake_argv, expected);
85 fake_argv[argc] = with_suffix;
86 DoTestHelper(&fake_argc, fake_argv, expected);
88 delete[] fake_argv;
89 return 0;