|
16 | 16 | #include <cmath> |
17 | 17 | #include <cstdint> |
18 | 18 | #include <limits> |
19 | | -#include <random> |
20 | | -#include <utility> |
21 | | -#include <vector> |
22 | 19 |
|
23 | 20 | #include "absl/numeric/int128.h" |
24 | 21 | #include "absl/random/distributions.h" |
25 | 22 | #include "absl/random/random.h" |
26 | 23 | #include "absl/status/status.h" |
27 | 24 | #include "absl/status/statusor.h" |
28 | | -#include "benchmark/benchmark.h" |
29 | 25 | #include "gtest/gtest.h" |
30 | 26 | #include "ortools/base/dump_vars.h" |
31 | | -//#include "ortools/base/fuzztest.h" |
32 | 27 | #include "ortools/base/gmock.h" |
33 | | -#include "ortools/base/mathutil.h" |
34 | 28 | #include "ortools/util/flat_matrix.h" |
35 | 29 |
|
36 | 30 | namespace operations_research { |
37 | 31 | namespace { |
38 | | -//using ::fuzztest::NonNegative; |
39 | 32 | using ::testing::HasSubstr; |
40 | 33 | using ::testing::status::IsOkAndHolds; |
41 | 34 | using ::testing::status::StatusIs; |
@@ -248,74 +241,5 @@ TEST(NChooseKTest, ComparisonAgainstPascalTriangleForK5OrAbove) { |
248 | 241 | } |
249 | 242 | } |
250 | 243 |
|
251 | | -void MatchesLogCombinations(int n, int k) { |
252 | | - if (n < k) { |
253 | | - std::swap(k, n); |
254 | | - } |
255 | | - const auto exact = NChooseK(n, k); |
256 | | - const double log_approx = MathUtil::LogCombinations(n, k); |
257 | | - if (exact.ok()) { |
258 | | - // We accepted to compute the exact value, make sure that it matches the |
259 | | - // approximation. |
260 | | - ASSERT_NEAR(log(exact.value()), log_approx, 0.0001); |
261 | | - } else { |
262 | | - // We declined to compute the exact value, make sure that we had a good |
263 | | - // reason to, i.e. that the result did indeed overflow. |
264 | | - ASSERT_THAT(exact, StatusIs(absl::StatusCode::kInvalidArgument, |
265 | | - HasSubstr("overflows int64"))); |
266 | | - const double approx = exp(log_approx); |
267 | | - ASSERT_GE(std::nextafter(approx, std::numeric_limits<double>::infinity()), |
268 | | - std::numeric_limits<int64_t>::max()) |
269 | | - << "we declined to compute the exact value of NChooseK(" << n << ", " |
270 | | - << k << "), but the log value is " << log_approx |
271 | | - << " (value: " << approx << "), which fits in int64_t"; |
272 | | - } |
273 | | -} |
274 | | -/* |
275 | | -FUZZ_TEST(NChooseKTest, MatchesLogCombinations) |
276 | | - // Ideally we'd test with `uint64_t`, but `LogCombinations` only accepts |
277 | | - // `int`. |
278 | | - .WithDomains(NonNegative<int>(), NonNegative<int>()); |
279 | | -*/ |
280 | | -template <int kMaxN, auto algo> |
281 | | -void BM_NChooseK(benchmark::State& state) { |
282 | | - static constexpr int kNumInputs = 1000; |
283 | | - // Use deterministic random numbers to avoid noise. |
284 | | - std::mt19937 gen(42); |
285 | | - std::uniform_int_distribution<int64_t> random(0, kMaxN); |
286 | | - std::vector<std::pair<int64_t, int64_t>> inputs; |
287 | | - inputs.reserve(kNumInputs); |
288 | | - for (int i = 0; i < kNumInputs; ++i) { |
289 | | - int64_t n = random(gen); |
290 | | - int64_t k = random(gen); |
291 | | - if (n < k) { |
292 | | - std::swap(n, k); |
293 | | - } |
294 | | - inputs.emplace_back(n, k); |
295 | | - } |
296 | | - // Force the one-time, costly static initializations of NChooseK() to happen |
297 | | - // before the benchmark starts. |
298 | | - auto result = NChooseK(62, 31); |
299 | | - benchmark::DoNotOptimize(result); |
300 | | - |
301 | | - // Start the benchmark. |
302 | | - for (auto _ : state) { |
303 | | - for (const auto [n, k] : inputs) { |
304 | | - auto result = algo(n, k); |
305 | | - benchmark::DoNotOptimize(result); |
306 | | - } |
307 | | - } |
308 | | - state.SetItemsProcessed(state.iterations() * kNumInputs); |
309 | | -} |
310 | | -// int32_t domain. |
311 | | -BENCHMARK(BM_NChooseK<30, operations_research::NChooseK>); |
312 | | - |
313 | | -// int{32,64} domain. |
314 | | -BENCHMARK(BM_NChooseK<60, operations_research::NChooseK>); |
315 | | - |
316 | | -// int{32,64,128} domain. |
317 | | -BENCHMARK(BM_NChooseK<100, operations_research::NChooseK>); |
318 | | -BENCHMARK(BM_NChooseK<100, MathUtil::LogCombinations>); |
319 | | - |
320 | 244 | } // namespace |
321 | 245 | } // namespace operations_research |
0 commit comments