Repository: carbon-language/carbon-lang
File: testing/file_test/file_test_base.cpp (C++)

Code before:
    // We expect to have leaked memory if one or more of our tests crashed.
    std::abort();
  }

  // Calculate the total test time.
  auto all_elapsed_ms = all_timer.elapsed_ms();
  auto total_elapsed_ms = std::chrono::milliseconds(0);
  for (auto& test : tests_) {
    total_elapsed_ms += test.elapsed_ms;
  }

  llvm::errs() << "\nRan " << run_count << " tests in "
               << all_elapsed_ms.count() << " ms wall time, "
               << total_elapsed_ms.count() << " ms across threads\n";

  // When there are multiple tests, give additional timing details, particularly
  // slowest tests.
  auto print_slowest_tests = absl::GetFlag(FLAGS_print_slowest_tests);
  if (run_count > 1 && print_slowest_tests != 0) {
    // Sort in a copy so that `FileTestCase` pointers to the original tests
    // remain stable.
    llvm::SmallVector<const FileTestInfo*> sorted_tests;
    sorted_tests.reserve(tests_.size());
    for (auto& test : tests_) {
      sorted_tests.push_back(&test);
    }
    llvm::sort(sorted_tests,
               [](const FileTestInfo* lhs, const FileTestInfo* rhs) {
                 return lhs->elapsed_ms > rhs->elapsed_ms;
               });

    llvm::errs() << "  Slowest tests:\n";
    int count = print_slowest_tests > 0 ? print_slowest_tests : run_count;
    for (const auto* test : llvm::ArrayRef(sorted_tests).take_front(count)) {
      std::chrono::milliseconds run_ms(0);
      if (test->test_result && test->test_result->ok()) {
        run_ms = test->test_result.value()->run_elapsed_ms;
      }
      llvm::errs() << "  - " << test->test_name << ": "
                   << test->elapsed_ms.count() << " ms, " << run_ms.count()
                   << " ms in Run\n";
    }
  }
}

// Implements main() within the Carbon::Testing namespace for convenience.
static auto Main(int argc, char** argv) -> ErrorOr<int> {
  // Default to brief because we expect lots of tests, and `FileTestBase`
  // provides some summaries. Note `--test_arg=--gtest_brief=0` works to restore
  // output.
  absl::SetFlag(&FLAGS_gtest_brief, 1);

Review comment (suggestion):
How about
```suggestion
    sorted_tests.append_range(llvm::map_range(tests_, [](const auto& test) { return &test; }));
```

Code after:
    // We expect to have leaked memory if one or more of our tests crashed.
    std::abort();
  }

  // Calculate the total test time.
  auto all_elapsed_ms = all_timer.elapsed_ms();
  auto total_elapsed_ms = std::chrono::milliseconds(0);
  for (auto& test : tests_) {
    total_elapsed_ms += test.elapsed_ms;
  }

  llvm::errs() << "\nRan " << run_count << " tests in "
               << all_elapsed_ms.count() << " ms wall time, "
               << total_elapsed_ms.count() << " ms across threads\n";

  // When there are multiple tests, give additional timing details, particularly
  // slowest tests.
  auto print_slowest_tests = absl::GetFlag(FLAGS_print_slowest_tests);
  if (run_count > 1 && print_slowest_tests != 0) {
    // Sort in a copy so that `FileTestCase` pointers to the original tests
    // remain stable.
    llvm::SmallVector<const FileTestInfo*> sorted_tests(
        llvm::make_pointer_range(tests_));
    llvm::sort(sorted_tests,
               [](const FileTestInfo* lhs, const FileTestInfo* rhs) {
                 return lhs->elapsed_ms > rhs->elapsed_ms;
               });

    llvm::errs() << "  Slowest tests:\n";
    int count = print_slowest_tests > 0 ? print_slowest_tests : run_count;
    for (const auto* test : llvm::ArrayRef(sorted_tests).take_front(count)) {
      std::chrono::milliseconds run_ms(0);
      if (test->test_result && test->test_result->ok()) {
        run_ms = test->test_result.value()->run_elapsed_ms;
      }
      llvm::errs() << "  - " << test->test_name << ": "
                   << test->elapsed_ms.count() << " ms, " << run_ms.count()
                   << " ms in Run\n";
    }
  }
}

// Implements main() within the Carbon::Testing namespace for convenience.
static auto Main(int argc, char** argv) -> ErrorOr<int> {
  // Default to brief because we expect lots of tests, and `FileTestBase`
  // provides some summaries. Note `--test_arg=--gtest_brief=0` works to restore
  // output.
  absl::SetFlag(&FLAGS_gtest_brief, 1);

  Carbon::InitLLVM init_llvm(argc, argv);
  testing::InitGoogleTest(&argc, argv);