[llvm] [ADT] Use static_assert() rather than assert() where possible (PR #180867)
Ryan Buchner via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 11 17:56:31 PST 2026
================
@@ -1937,4 +1937,33 @@ TEST(STLExtrasTest, AdjacentFind) {
EXPECT_EQ(*std::next(It13), 3);
}
+// Compile-time tests for llvm::is_sorted_constexpr
+static constexpr std::array<int, 0> CEmpty{};
+static_assert(is_sorted_constexpr(CEmpty.begin(), CEmpty.end()),
+ "Empty range should be sorted");
+
+static constexpr std::array<int, 1> CSingle{{42}};
+static_assert(is_sorted_constexpr(CSingle.begin(), CSingle.end()),
+ "Single element range should be sorted");
+static_assert(is_sorted_constexpr(CSingle.begin(), CSingle.end(),
+ std::greater<>()),
+ "Single element range should be sorted with std::greater");
+
+static constexpr std::array<int, 5> CSorted{{1, 2, 2, 3, 5}};
+static_assert(is_sorted_constexpr(CSorted.begin(), CSorted.end()),
+ "Non-descending order with duplicates should be sorted");
+static_assert(is_sorted_constexpr(CSorted.begin(), CSorted.end(),
+ std::less<>()),
+ "Explicit std::less non-descending order should be sorted");
+static_assert(!is_sorted_constexpr(CSorted.begin(), CSorted.end(),
+ std::greater<>()),
+ "Non-descending order should not be sorted by std::greater");
+
+static constexpr std::array<int, 5> CUnsorted{{1, 3, 2, 4, 5}};
+static_assert(!is_sorted_constexpr(CUnsorted.begin(), CUnsorted.end()),
+ "Unsorted range should not be sorted");
+
+static constexpr std::array<int, 5> CDesc{{9, 7, 7, 3, 0}};
+static_assert(is_sorted_constexpr(CDesc.begin(), CDesc.end(), std::greater<>()),
----------------
bababuck wrote:
Added test since the `STLForwardCompat.h` test uses iterators.
https://github.com/llvm/llvm-project/pull/180867
More information about the llvm-commits
mailing list