[libcxx-commits] [libcxx] [libc++] Add input validation for set_intersection() in debug mode. (PR #101508)

Iuri Chaer via libcxx-commits libcxx-commits at lists.llvm.org
Sat Nov 9 08:51:12 PST 2024


================
@@ -43,33 +43,31 @@
 
 #include "test_iterators.h"
 
-namespace {
-
-// __debug_less will perform an additional comparison in an assertion
-static constexpr unsigned std_less_comparison_count_multiplier() noexcept {
-#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
-  return 2;
+// Debug mode provides no complexity guarantees, testing them would be a waste of effort.
----------------
ichaer wrote:

> I'm leaning towards making the debug mode guarantee big-O complexity

I like that. It's tricky to automate validation, but it sounds like the right thing to do. I suppose we could either keep that part only in the benchmark tests, making use of the asymptotic complexity calculation feature it has, or come up with a framework for testing both strict number of steps and asymptotic complexity, and we'd do the curve fitting ourselves... maybe something like:
```
template <typename T>
struct ComplexityTestCaseInput {
    T input;
    size_t N;
};

template <class OutputStepsType=size_t>
struct ComplexityTestCaseExpectation {
    static_assert(std::is_convertible_v<OutputStepsType, size_t>,
        "OutputStepsType must provide N for O(N) when cast to size_t");
    std::function<bool(const OutputStepsType&)> isNumStepsGood;
    AsymptoticComplexityFunc standardGuarantee;
    std::optional<AsymptoticComplexityFunc> nonDebugExpectation;
};

template <class InputIter, class InputSent, class OutputStepsType>
bool meetsComplexityRequirements(
    InputIter firstInputIt,
    InputSent inputSentinel,
    std::function<OutputStepsType(decltype(*std::declval<InputIter>())))> test,
    ComplexityTestCaseExpectation expectedComplexity);
```

lol, sorry, got carried away ;). feels a bit clunky too... needs more time to brew.

https://github.com/llvm/llvm-project/pull/101508


More information about the libcxx-commits mailing list