[libcxx-commits] [libcxx] [libc++] Implement `std::multiset` (PR #128363)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 23 08:37:44 PDT 2025


https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/128363

>From 159aa0c089549a0f86867a969e8bc50583e22039 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Fri, 31 Jan 2025 15:53:09 +0000
Subject: [PATCH 01/10] [libc++] implement std::flat_set

---
 .../flat.set.capacity/empty.verify.cpp        |  20 ++++
 .../copy_assign.addressof.compile.pass.cpp    |  30 ++++++
 .../flat.set.cons/default_noexcept.pass.cpp   |  58 ++++++++++
 .../flat.set.cons/move_assign_clears.pass.cpp | 101 ++++++++++++++++++
 .../move_assign_noexcept.pass.cpp             |  85 +++++++++++++++
 .../flat.set.cons/move_exceptions.pass.cpp    |  58 ++++++++++
 .../flat.set.cons/move_noexcept.pass.cpp      |  94 ++++++++++++++++
 7 files changed, 446 insertions(+)
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.capacity/empty.verify.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp

diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.capacity/empty.verify.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.capacity/empty.verify.cpp
new file mode 100644
index 0000000000000..161fe533eabac
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.capacity/empty.verify.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// [[nodiscard]] bool empty() const noexcept;
+
+#include <flat_set>
+
+void f() {
+  std::flat_set<int> c;
+  c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
new file mode 100644
index 0000000000000..169b469f3bca6
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_set& operator=(const flat_set& s);
+
+// Validate whether the container can be copy-assigned (move-assigned, swapped)
+// with an ADL-hijacking operator&
+
+#include <flat_set>
+#include <utility>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::flat_set<operator_hijacker> so;
+  std::flat_set<operator_hijacker> s;
+  s = so;
+  s = std::move(so);
+  swap(s, so);
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
new file mode 100644
index 0000000000000..b4a3b6de205a3
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_set()
+//    noexcept(
+//        is_nothrow_default_constructible_v<container_type> &&
+//        is_nothrow_default_constructible_v<key_compare>);
+
+// This tests a conforming extension
+
+#include <cassert>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "test_macros.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+
+struct ThrowingCtorComp {
+  ThrowingCtorComp() noexcept(false) {}
+  bool operator()(const auto&, const auto&) const { return false; }
+};
+
+int main(int, char**) {
+#if defined(_LIBCPP_VERSION)
+  {
+    using C = std::flat_set<MoveOnly>;
+    static_assert(std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+  {
+    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+    static_assert(std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+#endif // _LIBCPP_VERSION
+  {
+    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+    static_assert(!std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+  {
+    using C = std::flat_set<MoveOnly, ThrowingCtorComp>;
+    static_assert(!std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
new file mode 100644
index 0000000000000..50817f4be8a81
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_set& operator=(flat_set&&);
+// Preserves the class invariant for the moved-from flat_set.
+
+#include <algorithm>
+#include <cassert>
+#include <compare>
+#include <flat_set>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "../helpers.h"
+#include "test_macros.h"
+
+struct MoveNegates {
+  int value_    = 0;
+  MoveNegates() = default;
+  MoveNegates(int v) : value_(v) {}
+  MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
+  MoveNegates& operator=(MoveNegates&& rhs) {
+    value_     = rhs.value_;
+    rhs.value_ = -rhs.value_;
+    return *this;
+  }
+  ~MoveNegates()                             = default;
+  auto operator<=>(const MoveNegates&) const = default;
+};
+
+struct MoveClears {
+  int value_   = 0;
+  MoveClears() = default;
+  MoveClears(int v) : value_(v) {}
+  MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
+  MoveClears& operator=(MoveClears&& rhs) {
+    value_     = rhs.value_;
+    rhs.value_ = 0;
+    return *this;
+  }
+  ~MoveClears()                             = default;
+  auto operator<=>(const MoveClears&) const = default;
+};
+
+int main(int, char**) {
+  {
+    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
+    using M              = std::flat_set<MoveNegates, std::less<MoveNegates>>;
+    M m                  = M(expected, expected + 8);
+    M m2                 = M(expected, expected + 3);
+
+    m2 = std::move(m);
+
+    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
+    LIBCPP_ASSERT(m.empty());
+    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
+    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
+    m.insert(1);
+    m.insert(2);
+    assert(m.contains(1));
+    assert(m.find(2) != m.end());
+  }
+  {
+    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
+    using M              = std::flat_set<MoveClears, std::less<MoveClears>>;
+    M m                  = M(expected, expected + 8);
+    M m2                 = M(expected, expected + 3);
+
+    m2 = std::move(m);
+
+    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
+    LIBCPP_ASSERT(m.empty());
+    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
+    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
+    m.insert(1);
+    m.insert(2);
+    assert(m.contains(1));
+    assert(m.find(2) != m.end());
+  }
+  {
+    // moved-from object maintains invariant if one of underlying container does not clear after move
+    using M = std::flat_set<int, std::less<>, std::vector<int>>;
+    M m1    = M({1, 2, 3});
+    M m2    = M({1, 2});
+    m2      = std::move(m1);
+    assert(m2.size() == 3);
+    check_invariant(m1);
+    LIBCPP_ASSERT(m1.empty());
+  }
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
new file mode 100644
index 0000000000000..86f3568f0d67a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_set& operator=(flat_set&& c)
+//     noexcept(
+//          is_nothrow_move_assignable<key_container_type>::value &&
+//          is_nothrow_move_assignable<mapped_container_type>::value &&
+//          is_nothrow_copy_assignable<key_compare>::value);
+
+// This tests a conforming extension
+
+#include <flat_set>
+#include <functional>
+#include <memory_resource>
+#include <type_traits>
+#include <vector>
+
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+struct MoveSensitiveComp {
+  MoveSensitiveComp() noexcept(false)                         = default;
+  MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default;
+  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
+  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default;
+  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
+    rhs.is_moved_from_ = true;
+    return *this;
+  }
+  bool operator()(const auto&, const auto&) const { return false; }
+  bool is_moved_from_ = false;
+};
+
+struct MoveThrowsComp {
+  MoveThrowsComp(MoveThrowsComp&&) noexcept(false);
+  MoveThrowsComp(const MoveThrowsComp&) noexcept(true);
+  MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false);
+  MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true);
+  bool operator()(const auto&, const auto&) const;
+};
+
+int main(int, char**) {
+  {
+    using C = std::flat_set<int, int>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_set<int, std::less<int>, std::vector<int, test_allocator<int>>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_set<int, std::less<int>, std::vector<int, other_allocator<int>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    // Test with a comparator that throws on move-assignment.
+    using C = std::flat_set<int, MoveThrowsComp>;
+    LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    // Test with a container that throws on move-assignment.
+    using C = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
new file mode 100644
index 0000000000000..17e4e40387606
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: no-exceptions
+
+// <flat_set>
+
+// flat_set(flat_set&& s);
+// If any member function in [flat.map.defn] exits via an exception, the invariant is restored.
+
+#include <algorithm>
+#include <cassert>
+#include <flat_set>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "../helpers.h"
+#include "test_macros.h"
+
+static int countdown = 0;
+
+struct EvilContainer : std::vector<int> {
+  EvilContainer() = default;
+  EvilContainer(EvilContainer&& rhs) {
+    // Throw on move-construction.
+    if (--countdown == 0) {
+      rhs.insert(rhs.end(), 0);
+      rhs.insert(rhs.end(), 0);
+      throw 42;
+    }
+  }
+};
+
+int main(int, char**) {
+  {
+    using M   = std::flat_set<int, std::less<int>, EvilContainer>;
+    M mo      = {1, 2, 3};
+    countdown = 1;
+    try {
+      M m = std::move(mo);
+      assert(false); // not reached
+    } catch (int x) {
+      assert(x == 42);
+    }
+    // The source flat_set maintains its class invariant.
+    check_invariant(mo);
+    LIBCPP_ASSERT(mo.empty());
+  }
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp
new file mode 100644
index 0000000000000..49d1151fd8a99
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp
@@ -0,0 +1,94 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_set(flat_set&&)
+//        noexcept(is_nothrow_move_constructible<key_container_type>::value &&
+//                 is_nothrow_move_constructible<mapped_container_type>::value &&
+//                 is_nothrow_copy_constructible<key_compare>::value);
+
+// This tests a conforming extension
+
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+
+template <class T>
+struct ThrowingMoveAllocator {
+  using value_type                                    = T;
+  explicit ThrowingMoveAllocator()                    = default;
+  ThrowingMoveAllocator(const ThrowingMoveAllocator&) = default;
+  ThrowingMoveAllocator(ThrowingMoveAllocator&&) noexcept(false) {}
+  T* allocate(std::ptrdiff_t n) { return std::allocator<T>().allocate(n); }
+  void deallocate(T* p, std::ptrdiff_t n) { return std::allocator<T>().deallocate(p, n); }
+  friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default;
+};
+
+struct ThrowingMoveComp {
+  ThrowingMoveComp() = default;
+  ThrowingMoveComp(const ThrowingMoveComp&) noexcept(true) {}
+  ThrowingMoveComp(ThrowingMoveComp&&) noexcept(false) {}
+  bool operator()(const auto&, const auto&) const { return false; }
+};
+
+struct MoveSensitiveComp {
+  MoveSensitiveComp() noexcept(false)                  = default;
+  MoveSensitiveComp(const MoveSensitiveComp&) noexcept = default;
+  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
+  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept(false) = default;
+  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
+    rhs.is_moved_from_ = true;
+    return *this;
+  }
+  bool operator()(const auto&, const auto&) const { return false; }
+  bool is_moved_from_ = false;
+};
+
+int main(int, char**) {
+  {
+    using C = std::flat_set<int>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+  {
+    using C = std::flat_set<int, std::less<int>, std::deque<int, test_allocator<int>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+#if _LIBCPP_VERSION
+  {
+    // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators
+    using C = std::flat_set<int, std::less<int>, std::deque<int, ThrowingMoveAllocator<int>>>;
+    static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>);
+    static_assert(!std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+#endif // _LIBCPP_VERSION
+  {
+    // Comparator fails to be nothrow-move-constructible
+    using C = std::flat_set<int, ThrowingMoveComp>;
+    static_assert(!std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+  return 0;
+}

>From 6d6ae7c45d54f7dcd20f808159e5998baa39c70c Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 2 Feb 2025 13:21:24 +0000
Subject: [PATCH 02/10] review

---
 .../copy_assign.addressof.compile.pass.cpp    |  30 ------
 .../flat.set.cons/default_noexcept.pass.cpp   |  58 ----------
 .../flat.set.cons/move_assign_clears.pass.cpp | 101 ------------------
 .../move_assign_noexcept.pass.cpp             |  85 ---------------
 .../flat.set.cons/move_exceptions.pass.cpp    |  58 ----------
 .../flat.set.cons/move_noexcept.pass.cpp      |  94 ----------------
 .../flat.set.erasure/erase_if.pass.cpp        |  10 ++
 .../flat.set.modifiers/clear.pass.cpp         |   9 ++
 .../flat.set.modifiers/swap_free.pass.cpp     |   2 +
 .../flat.set.operations/contains.pass.cpp     |   7 ++
 .../flat.set.operations/count.pass.cpp        |   7 ++
 .../flat.set.operations/equal_range.pass.cpp  |   7 ++
 .../flat.set.operations/find.pass.cpp         |   7 ++
 .../flat.set.operations/lower_bound.pass.cpp  |   7 ++
 .../flat.set.operations/upper_bound.pass.cpp  |   7 ++
 15 files changed, 63 insertions(+), 426 deletions(-)
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
 delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp

diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
deleted file mode 100644
index 169b469f3bca6..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/copy_assign.addressof.compile.pass.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// <flat_set>
-
-// flat_set& operator=(const flat_set& s);
-
-// Validate whether the container can be copy-assigned (move-assigned, swapped)
-// with an ADL-hijacking operator&
-
-#include <flat_set>
-#include <utility>
-
-#include "test_macros.h"
-#include "operator_hijacker.h"
-
-void test() {
-  std::flat_set<operator_hijacker> so;
-  std::flat_set<operator_hijacker> s;
-  s = so;
-  s = std::move(so);
-  swap(s, so);
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
deleted file mode 100644
index b4a3b6de205a3..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/default_noexcept.pass.cpp
+++ /dev/null
@@ -1,58 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// <flat_set>
-
-// flat_set()
-//    noexcept(
-//        is_nothrow_default_constructible_v<container_type> &&
-//        is_nothrow_default_constructible_v<key_compare>);
-
-// This tests a conforming extension
-
-#include <cassert>
-#include <flat_set>
-#include <functional>
-#include <vector>
-
-#include "test_macros.h"
-#include "MoveOnly.h"
-#include "test_allocator.h"
-
-struct ThrowingCtorComp {
-  ThrowingCtorComp() noexcept(false) {}
-  bool operator()(const auto&, const auto&) const { return false; }
-};
-
-int main(int, char**) {
-#if defined(_LIBCPP_VERSION)
-  {
-    using C = std::flat_set<MoveOnly>;
-    static_assert(std::is_nothrow_default_constructible_v<C>);
-    C c;
-  }
-  {
-    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
-    static_assert(std::is_nothrow_default_constructible_v<C>);
-    C c;
-  }
-#endif // _LIBCPP_VERSION
-  {
-    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
-    static_assert(!std::is_nothrow_default_constructible_v<C>);
-    C c;
-  }
-  {
-    using C = std::flat_set<MoveOnly, ThrowingCtorComp>;
-    static_assert(!std::is_nothrow_default_constructible_v<C>);
-    C c;
-  }
-  return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
deleted file mode 100644
index 50817f4be8a81..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_clears.pass.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// <flat_set>
-
-// flat_set& operator=(flat_set&&);
-// Preserves the class invariant for the moved-from flat_set.
-
-#include <algorithm>
-#include <cassert>
-#include <compare>
-#include <flat_set>
-#include <functional>
-#include <utility>
-#include <vector>
-
-#include "../helpers.h"
-#include "test_macros.h"
-
-struct MoveNegates {
-  int value_    = 0;
-  MoveNegates() = default;
-  MoveNegates(int v) : value_(v) {}
-  MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
-  MoveNegates& operator=(MoveNegates&& rhs) {
-    value_     = rhs.value_;
-    rhs.value_ = -rhs.value_;
-    return *this;
-  }
-  ~MoveNegates()                             = default;
-  auto operator<=>(const MoveNegates&) const = default;
-};
-
-struct MoveClears {
-  int value_   = 0;
-  MoveClears() = default;
-  MoveClears(int v) : value_(v) {}
-  MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
-  MoveClears& operator=(MoveClears&& rhs) {
-    value_     = rhs.value_;
-    rhs.value_ = 0;
-    return *this;
-  }
-  ~MoveClears()                             = default;
-  auto operator<=>(const MoveClears&) const = default;
-};
-
-int main(int, char**) {
-  {
-    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
-    using M              = std::flat_set<MoveNegates, std::less<MoveNegates>>;
-    M m                  = M(expected, expected + 8);
-    M m2                 = M(expected, expected + 3);
-
-    m2 = std::move(m);
-
-    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
-    LIBCPP_ASSERT(m.empty());
-    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
-    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
-    m.insert(1);
-    m.insert(2);
-    assert(m.contains(1));
-    assert(m.find(2) != m.end());
-  }
-  {
-    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
-    using M              = std::flat_set<MoveClears, std::less<MoveClears>>;
-    M m                  = M(expected, expected + 8);
-    M m2                 = M(expected, expected + 3);
-
-    m2 = std::move(m);
-
-    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
-    LIBCPP_ASSERT(m.empty());
-    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
-    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
-    m.insert(1);
-    m.insert(2);
-    assert(m.contains(1));
-    assert(m.find(2) != m.end());
-  }
-  {
-    // moved-from object maintains invariant if one of underlying container does not clear after move
-    using M = std::flat_set<int, std::less<>, std::vector<int>>;
-    M m1    = M({1, 2, 3});
-    M m2    = M({1, 2});
-    m2      = std::move(m1);
-    assert(m2.size() == 3);
-    check_invariant(m1);
-    LIBCPP_ASSERT(m1.empty());
-  }
-  return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
deleted file mode 100644
index 86f3568f0d67a..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign_noexcept.pass.cpp
+++ /dev/null
@@ -1,85 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// <flat_set>
-
-// flat_set& operator=(flat_set&& c)
-//     noexcept(
-//          is_nothrow_move_assignable<key_container_type>::value &&
-//          is_nothrow_move_assignable<mapped_container_type>::value &&
-//          is_nothrow_copy_assignable<key_compare>::value);
-
-// This tests a conforming extension
-
-#include <flat_set>
-#include <functional>
-#include <memory_resource>
-#include <type_traits>
-#include <vector>
-
-#include "MoveOnly.h"
-#include "test_allocator.h"
-#include "test_macros.h"
-
-struct MoveSensitiveComp {
-  MoveSensitiveComp() noexcept(false)                         = default;
-  MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default;
-  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
-  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default;
-  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
-    rhs.is_moved_from_ = true;
-    return *this;
-  }
-  bool operator()(const auto&, const auto&) const { return false; }
-  bool is_moved_from_ = false;
-};
-
-struct MoveThrowsComp {
-  MoveThrowsComp(MoveThrowsComp&&) noexcept(false);
-  MoveThrowsComp(const MoveThrowsComp&) noexcept(true);
-  MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false);
-  MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true);
-  bool operator()(const auto&, const auto&) const;
-};
-
-int main(int, char**) {
-  {
-    using C = std::flat_set<int, int>;
-    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
-    static_assert(!std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    using C = std::flat_set<int, std::less<int>, std::vector<int, test_allocator<int>>>;
-    static_assert(!std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
-    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    using C = std::flat_set<int, std::less<int>, std::vector<int, other_allocator<int>>>;
-    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    // Test with a comparator that throws on move-assignment.
-    using C = std::flat_set<int, MoveThrowsComp>;
-    LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
-  }
-  {
-    // Test with a container that throws on move-assignment.
-    using C = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
-    static_assert(!std::is_nothrow_move_assignable_v<C>);
-  }
-
-  return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
deleted file mode 100644
index 17e4e40387606..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_exceptions.pass.cpp
+++ /dev/null
@@ -1,58 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-// UNSUPPORTED: no-exceptions
-
-// <flat_set>
-
-// flat_set(flat_set&& s);
-// If any member function in [flat.map.defn] exits via an exception, the invariant is restored.
-
-#include <algorithm>
-#include <cassert>
-#include <flat_set>
-#include <functional>
-#include <utility>
-#include <vector>
-
-#include "../helpers.h"
-#include "test_macros.h"
-
-static int countdown = 0;
-
-struct EvilContainer : std::vector<int> {
-  EvilContainer() = default;
-  EvilContainer(EvilContainer&& rhs) {
-    // Throw on move-construction.
-    if (--countdown == 0) {
-      rhs.insert(rhs.end(), 0);
-      rhs.insert(rhs.end(), 0);
-      throw 42;
-    }
-  }
-};
-
-int main(int, char**) {
-  {
-    using M   = std::flat_set<int, std::less<int>, EvilContainer>;
-    M mo      = {1, 2, 3};
-    countdown = 1;
-    try {
-      M m = std::move(mo);
-      assert(false); // not reached
-    } catch (int x) {
-      assert(x == 42);
-    }
-    // The source flat_set maintains its class invariant.
-    check_invariant(mo);
-    LIBCPP_ASSERT(mo.empty());
-  }
-
-  return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp
deleted file mode 100644
index 49d1151fd8a99..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_noexcept.pass.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// <flat_set>
-
-// flat_set(flat_set&&)
-//        noexcept(is_nothrow_move_constructible<key_container_type>::value &&
-//                 is_nothrow_move_constructible<mapped_container_type>::value &&
-//                 is_nothrow_copy_constructible<key_compare>::value);
-
-// This tests a conforming extension
-
-#include <cassert>
-#include <deque>
-#include <flat_set>
-#include <functional>
-#include <memory>
-#include <type_traits>
-#include <vector>
-
-#include "test_macros.h"
-#include "MoveOnly.h"
-#include "test_allocator.h"
-
-template <class T>
-struct ThrowingMoveAllocator {
-  using value_type                                    = T;
-  explicit ThrowingMoveAllocator()                    = default;
-  ThrowingMoveAllocator(const ThrowingMoveAllocator&) = default;
-  ThrowingMoveAllocator(ThrowingMoveAllocator&&) noexcept(false) {}
-  T* allocate(std::ptrdiff_t n) { return std::allocator<T>().allocate(n); }
-  void deallocate(T* p, std::ptrdiff_t n) { return std::allocator<T>().deallocate(p, n); }
-  friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default;
-};
-
-struct ThrowingMoveComp {
-  ThrowingMoveComp() = default;
-  ThrowingMoveComp(const ThrowingMoveComp&) noexcept(true) {}
-  ThrowingMoveComp(ThrowingMoveComp&&) noexcept(false) {}
-  bool operator()(const auto&, const auto&) const { return false; }
-};
-
-struct MoveSensitiveComp {
-  MoveSensitiveComp() noexcept(false)                  = default;
-  MoveSensitiveComp(const MoveSensitiveComp&) noexcept = default;
-  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
-  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept(false) = default;
-  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
-    rhs.is_moved_from_ = true;
-    return *this;
-  }
-  bool operator()(const auto&, const auto&) const { return false; }
-  bool is_moved_from_ = false;
-};
-
-int main(int, char**) {
-  {
-    using C = std::flat_set<int>;
-    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
-    C c;
-    C d = std::move(c);
-  }
-  {
-    using C = std::flat_set<int, std::less<int>, std::deque<int, test_allocator<int>>>;
-    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
-    C c;
-    C d = std::move(c);
-  }
-#if _LIBCPP_VERSION
-  {
-    // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators
-    using C = std::flat_set<int, std::less<int>, std::deque<int, ThrowingMoveAllocator<int>>>;
-    static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>);
-    static_assert(!std::is_nothrow_move_constructible_v<C>);
-    C c;
-    C d = std::move(c);
-  }
-#endif // _LIBCPP_VERSION
-  {
-    // Comparator fails to be nothrow-move-constructible
-    using C = std::flat_set<int, ThrowingMoveComp>;
-    static_assert(!std::is_nothrow_move_constructible_v<C>);
-    C c;
-    C d = std::move(c);
-  }
-  return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if.pass.cpp
index d7d7a98f49d77..d75a5c576abb6 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if.pass.cpp
@@ -94,6 +94,16 @@ void test() {
   test_one<std::flat_set<double>>();
 }
 
+void test() {
+  test_one<std::flat_set<int>>();
+  test_one<std::flat_set<int, std::less<int>, std::vector<int, min_allocator<int>>>>();
+  test_one<std::flat_set<int, std::greater<int>, std::vector<int, test_allocator<int>>>>();
+  test_one<std::flat_set<int, std::less<int>, std::deque<int, min_allocator<int>>>>();
+  test_one<std::flat_set<int, std::greater<int>, std::deque<int, test_allocator<int>>>>();
+  test_one<std::flat_set<long>>();
+  test_one<std::flat_set<double>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/clear.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/clear.pass.cpp
index 5ecdc429b3c58..87f9e1a965d29 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/clear.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/clear.pass.cpp
@@ -67,6 +67,15 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
index 686fe93a593f5..ed13ba1ef3fea 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
@@ -24,6 +24,8 @@
 #include "test_macros.h"
 #include "../helpers.h"
 
+#include "check_assertion.h"
+
 // test noexcept
 
 template <class T>
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/contains.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/contains.pass.cpp
index e0416fc261ffc..5d731cef4f002 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/contains.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/contains.pass.cpp
@@ -73,6 +73,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/count.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/count.pass.cpp
index 51aec21ca212b..0b02f7bacae7d 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/count.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/count.pass.cpp
@@ -73,6 +73,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/equal_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/equal_range.pass.cpp
index 7a8b19273289d..ca5231f441901 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/equal_range.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/equal_range.pass.cpp
@@ -81,6 +81,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/find.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/find.pass.cpp
index 953b8fe638547..4391c2c0ff8a1 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/find.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/find.pass.cpp
@@ -57,6 +57,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/lower_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/lower_bound.pass.cpp
index 32796b7a164f7..eaaa206cf0ada 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/lower_bound.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/lower_bound.pass.cpp
@@ -73,6 +73,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/upper_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/upper_bound.pass.cpp
index 6a967cb2371ba..9574a66dc7400 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/upper_bound.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/upper_bound.pass.cpp
@@ -74,6 +74,13 @@ void test() {
   test_one<std::vector<int, min_allocator<int>>>();
 }
 
+void test() {
+  test_one<std::vector<int>>();
+  test_one<std::deque<int>>();
+  test_one<MinSequenceContainer<int>>();
+  test_one<std::vector<int, min_allocator<int>>>();
+}
+
 int main(int, char**) {
   test();
 

>From 1ed4370f0e6afe909f306d50700c7ba970fb0d30 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 2 Feb 2025 18:45:39 +0000
Subject: [PATCH 03/10] CI

---
 libcxx/include/module.modulemap | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 1809fe5c7f0f7..84b141addd3c2 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1301,6 +1301,7 @@ module std [system] {
       header "__flat_set/flat_set.h"
       export std.vector.vector
       export std.vector.fwd
+      export std.flat_map.sorted_unique
     }
     module ra_iterator                    { header "__flat_set/ra_iterator.h" }
 

>From 7451725165db60fdd6c8291b81477cf32566419d Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 2 Feb 2025 19:14:41 +0000
Subject: [PATCH 04/10] CI again

---
 libcxx/include/module.modulemap | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 84b141addd3c2..1809fe5c7f0f7 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1301,7 +1301,6 @@ module std [system] {
       header "__flat_set/flat_set.h"
       export std.vector.vector
       export std.vector.fwd
-      export std.flat_map.sorted_unique
     }
     module ra_iterator                    { header "__flat_set/ra_iterator.h" }
 

>From 26c350cb759d550e2c7afef7afee089236045c17 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 22 Feb 2025 17:29:12 +0000
Subject: [PATCH 05/10] review

---
 .../flat.set/flat.set.modifiers/swap_free.pass.cpp              | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
index ed13ba1ef3fea..686fe93a593f5 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/swap_free.pass.cpp
@@ -24,8 +24,6 @@
 #include "test_macros.h"
 #include "../helpers.h"
 
-#include "check_assertion.h"
-
 // test noexcept
 
 template <class T>

>From 60192d5307564a1cb86530a161eb09ac3b86e825 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 22 Feb 2025 10:59:31 +0000
Subject: [PATCH 06/10] update

---
 libcxx/docs/FeatureTestMacroTable.rst         |   2 +-
 libcxx/docs/Status/Cxx23Papers.csv            |   2 +-
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__flat_set/flat_multiset.h     | 861 ++++++++++++++++++
 libcxx/include/flat_set                       |  17 +
 libcxx/include/module.modulemap               |   1 +
 libcxx/include/version                        |   2 +-
 libcxx/modules/std/flat_set.inc               |   4 +-
 .../flat_set.version.compile.pass.cpp         |  32 +-
 .../version.version.compile.pass.cpp          |  32 +-
 .../generate_feature_test_macro_components.py |   1 -
 11 files changed, 904 insertions(+), 51 deletions(-)
 create mode 100644 libcxx/include/__flat_set/flat_multiset.h

diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index bbcc76b52f0a9..786b26eaebadc 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -332,7 +332,7 @@ Status
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_flat_map``                                     ``202207L``
     ---------------------------------------------------------- -----------------
-    ``__cpp_lib_flat_set``                                     *unimplemented*
+    ``__cpp_lib_flat_set``                                     ``202207L``
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_format_ranges``                                ``202207L``
     ---------------------------------------------------------- -----------------
diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 4e45debd419ef..fab9900ce71aa 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -54,7 +54,7 @@
 "`P0009R18 <https://wg21.link/P0009R18>`__","mdspan: A Non-Owning Multidimensional Array Reference","2022-07 (Virtual)","|Complete|","18",""
 "`P0429R9 <https://wg21.link/P0429R9>`__","A Standard ``flat_map``","2022-07 (Virtual)","|Complete|","20",""
 "`P1169R4 <https://wg21.link/P1169R4>`__","``static operator()``","2022-07 (Virtual)","|Complete|","16",""
-"`P1222R4 <https://wg21.link/P1222R4>`__","A Standard ``flat_set``","2022-07 (Virtual)","|In progress|","",""
+"`P1222R4 <https://wg21.link/P1222R4>`__","A Standard ``flat_set``","2022-07 (Virtual)","|Complete|","21",""
 "`P1223R5 <https://wg21.link/P1223R5>`__","``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``","2022-07 (Virtual)","|Complete|","19",""
 "`P1467R9 <https://wg21.link/P1467R9>`__","Extended ``floating-point`` types and standard names","2022-07 (Virtual)","","",""
 "`P1642R11 <https://wg21.link/P1642R11>`__","Freestanding ``[utilities]``, ``[ranges]``, and ``[iterators]``","2022-07 (Virtual)","","",""
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index a021b9bb44d67..e70e27bcdcf9f 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -369,6 +369,7 @@ set(files
   __flat_map/sorted_equivalent.h
   __flat_map/sorted_unique.h
   __flat_map/utils.h
+  __flat_set/flat_multiset.h
   __flat_set/flat_set.h
   __flat_set/ra_iterator.h
   __format/buffer.h
diff --git a/libcxx/include/__flat_set/flat_multiset.h b/libcxx/include/__flat_set/flat_multiset.h
new file mode 100644
index 0000000000000..6aa160f0af54b
--- /dev/null
+++ b/libcxx/include/__flat_set/flat_multiset.h
@@ -0,0 +1,861 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FLAT_MAP_FLAT_MULTISET_H
+#define _LIBCPP___FLAT_MAP_FLAT_MULTISET_H
+
+#include <__algorithm/lexicographical_compare_three_way.h>
+#include <__algorithm/min.h>
+#include <__algorithm/ranges_equal.h>
+#include <__algorithm/ranges_equal_range.h>
+#include <__algorithm/ranges_inplace_merge.h>
+#include <__algorithm/ranges_is_sorted.h>
+#include <__algorithm/ranges_lower_bound.h>
+#include <__algorithm/ranges_partition_point.h>
+#include <__algorithm/ranges_sort.h>
+#include <__algorithm/ranges_unique.h>
+#include <__algorithm/ranges_upper_bound.h>
+#include <__algorithm/remove_if.h>
+#include <__assert>
+#include <__compare/synth_three_way.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/swappable.h>
+#include <__config>
+#include <__cstddef/byte.h>
+#include <__cstddef/ptrdiff_t.h>
+#include <__flat_map/key_value_iterator.h>
+#include <__flat_map/sorted_equivalent.h>
+#include <__flat_map/utils.h>
+#include <__functional/invoke.h>
+#include <__functional/is_transparent.h>
+#include <__functional/operations.h>
+#include <__fwd/vector.h>
+#include <__iterator/concepts.h>
+#include <__iterator/distance.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/ranges_iterator_traits.h>
+#include <__iterator/reverse_iterator.h>
+#include <__memory/allocator_traits.h>
+#include <__memory/uses_allocator.h>
+#include <__memory/uses_allocator_construction.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/container_compatible_range.h>
+#include <__ranges/drop_view.h>
+#include <__ranges/from_range.h>
+#include <__ranges/ref_view.h>
+#include <__ranges/size.h>
+#include <__ranges/subrange.h>
+#include <__ranges/zip_view.h>
+#include <__type_traits/conjunction.h>
+#include <__type_traits/container_traits.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_allocator.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/is_same.h>
+#include <__type_traits/maybe_const.h>
+#include <__utility/exception_guard.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
+#include <__utility/scope_guard.h>
+#include <__vector/vector.h>
+#include <initializer_list>
+#include <stdexcept>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key, class _Compare = less<_Key>, class _KeyContainer = vector<_Key>>
+class flat_multiset {
+  template <class, class, class>
+  friend class flat_multiset;
+
+  static_assert(is_same_v<_Key, typename _KeyContainer::value_type>);
+  static_assert(!is_same_v<_KeyContainer, std::vector<bool>>, "vector<bool> is not a sequence container");
+
+public:
+  // types
+  using key_type               = _Key;
+  using value_type             = _Key;
+  using key_compare            = __type_identity_t<_Compare>;
+  using value_compare          = _Compare;
+  using reference              = value_type&;
+  using const_reference        = const value_type&;
+  using size_type              = typename _KeyContainer::size_type;
+  using difference_type        = typename _KeyContainer::difference_type;
+  using iterator               = typename _KeyContainer::const_iterator;
+  using const_iterator         = iterator;
+  using reverse_iterator       = std::reverse_iterator<iterator>;
+  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
+  using container_type         = _KeyContainer;
+
+public:
+  // [flat.multiset.cons], constructors
+  _LIBCPP_HIDE_FROM_ABI flat_multiset() noexcept(is_nothrow_default_constructible_v<_KeyContainer> &&
+                                                 is_nothrow_default_constructible_v<_Compare>)
+      : __keys_(), __compare_() {}
+
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(const flat_multiset&) = default;
+
+  // The copy/move constructors are not specified in the spec, which means they should be defaulted.
+  // However, the move constructor can potentially leave a moved-from object in an inconsistent
+  // state if an exception is thrown.
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(flat_multiset&& __other) noexcept(
+      is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)
+#  if _LIBCPP_HAS_EXCEPTIONS
+      try
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+      : __keys_(std::move(__other.__keys_)), __compare_(std::move(__other.__compare_)) {
+    __other.clear();
+#  if _LIBCPP_HAS_EXCEPTIONS
+  } catch (...) {
+    __other.clear();
+    // gcc does not like the `throw` keyword in a conditionally noexcept function
+    if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)) {
+      throw;
+    }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+  }
+
+  _LIBCPP_HIDE_FROM_ABI explicit flat_multiset(const key_compare& __comp) : __keys_(), __compare_(__comp) {}
+
+  _LIBCPP_HIDE_FROM_ABI explicit flat_multiset(container_type __keys, const key_compare& __comp = key_compare())
+      : __keys_(std::move(__keys)), __compare_(__comp) {
+    ranges::sort(__keys_, __compare_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(sorted_equivalent_t, container_type __keys, const key_compare& __comp = key_compare())
+      : __keys_(std::move(__keys)), __compare_(__comp) {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <class _InputIterator>
+    requires __has_input_iterator_category<_InputIterator>::value
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
+      : __keys_(), __compare_(__comp) {
+    insert(__first, __last);
+  }
+
+  template <class _InputIterator>
+    requires __has_input_iterator_category<_InputIterator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(
+      sorted_equivalent_t, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
+      : __keys_(__first, __last), __compare_(__comp) {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <_ContainerCompatibleRange<value_type> _Range>
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(from_range_t __fr, _Range&& __rg)
+      : flat_multiset(__fr, std::forward<_Range>(__rg), key_compare()) {}
+
+  template <_ContainerCompatibleRange<value_type> _Range>
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(from_range_t, _Range&& __rg, const key_compare& __comp) : flat_multiset(__comp) {
+    insert_range(std::forward<_Range>(__rg));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
+      : flat_multiset(__il.begin(), __il.end(), __comp) {}
+
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(sorted_equivalent_t, initializer_list<value_type> __il, const key_compare& __comp = key_compare())
+      : flat_multiset(sorted_equivalent, __il.begin(), __il.end(), __comp) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI explicit flat_multiset(const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(const key_compare& __comp, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(const container_type& __keys, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_() {
+    ranges::sort(__keys_, __compare_);
+  }
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(const container_type& __keys, const key_compare& __comp, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_(__comp) {
+    ranges::sort(__keys_, __compare_);
+  }
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(sorted_equivalent_t, const container_type& __keys, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_() {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(sorted_equivalent_t, const container_type& __keys, const key_compare& __comp, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_(__comp) {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(const flat_multiset& __other, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __other.__keys_)),
+        __compare_(__other.__compare_) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(flat_multiset&& __other, const _Allocator& __alloc)
+#  if _LIBCPP_HAS_EXCEPTIONS
+      try
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, std::move(__other.__keys_))),
+        __compare_(std::move(__other.__compare_)) {
+    __other.clear();
+#  if _LIBCPP_HAS_EXCEPTIONS
+  } catch (...) {
+    __other.clear();
+    throw;
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+  }
+
+  template <class _InputIterator, class _Allocator>
+    requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(_InputIterator __first, _InputIterator __last, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {
+    insert(__first, __last);
+  }
+
+  template <class _InputIterator, class _Allocator>
+    requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {
+    insert(__first, __last);
+  }
+
+  template <class _InputIterator, class _Allocator>
+    requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(sorted_equivalent_t, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __first, __last)), __compare_() {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <class _InputIterator, class _Allocator>
+    requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(sorted_equivalent_t,
+                _InputIterator __first,
+                _InputIterator __last,
+                const key_compare& __comp,
+                const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc, __first, __last)), __compare_(__comp) {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys_, __compare_), "Key container is not sorted");
+  }
+
+  template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(from_range_t, _Range&& __rg, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {
+    insert_range(std::forward<_Range>(__rg));
+  }
+
+  template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(from_range_t, _Range&& __rg, const key_compare& __comp, const _Allocator& __alloc)
+      : __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {
+    insert_range(std::forward<_Range>(__rg));
+  }
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(initializer_list<value_type> __il, const _Allocator& __alloc)
+      : flat_multiset(__il.begin(), __il.end(), __alloc) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI
+  flat_multiset(initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)
+      : flat_multiset(__il.begin(), __il.end(), __comp, __alloc) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(sorted_equivalent_t, initializer_list<value_type> __il, const _Allocator& __alloc)
+      : flat_multiset(sorted_equivalent, __il.begin(), __il.end(), __alloc) {}
+
+  template <class _Allocator>
+    requires uses_allocator<container_type, _Allocator>::value
+  _LIBCPP_HIDE_FROM_ABI flat_multiset(
+      sorted_equivalent_t, initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)
+      : flat_multiset(sorted_equivalent, __il.begin(), __il.end(), __comp, __alloc) {}
+
+  _LIBCPP_HIDE_FROM_ABI flat_multiset& operator=(initializer_list<value_type> __il) {
+    clear();
+    insert(__il);
+    return *this;
+  }
+
+  // copy/move assignment are not specified in the spec (defaulted)
+  // but move assignment can potentially leave moved from object in an inconsistent
+  // state if an exception is thrown
+  _LIBCPP_HIDE_FROM_ABI flat_multiset& operator=(const flat_multiset&) = default;
+
+  _LIBCPP_HIDE_FROM_ABI flat_multiset& operator=(flat_multiset&& __other) noexcept(
+      is_nothrow_move_assignable_v<_KeyContainer> && is_nothrow_move_assignable_v<_Compare>) {
+    auto __clear_other_guard = std::__make_scope_guard([&]() noexcept { __other.clear() /* noexcept */; });
+    auto __clear_self_guard  = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    __keys_                  = std::move(__other.__keys_);
+    __compare_               = std::move(__other.__compare_);
+    __clear_self_guard.__complete();
+    return *this;
+  }
+
+  // iterators
+  _LIBCPP_HIDE_FROM_ABI iterator begin() noexcept { return __keys_.begin(); }
+  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const noexcept { return __keys_.begin(); }
+  _LIBCPP_HIDE_FROM_ABI iterator end() noexcept { return __keys_.end(); }
+  _LIBCPP_HIDE_FROM_ABI const_iterator end() const noexcept { return __keys_.end(); }
+
+  _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
+  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
+  _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
+  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
+
+  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const noexcept { return begin(); }
+  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const noexcept { return end(); }
+  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
+  _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
+
+  // capacity
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool empty() const noexcept { return __keys_.empty(); }
+  _LIBCPP_HIDE_FROM_ABI size_type size() const noexcept { return __keys_.size(); }
+  _LIBCPP_HIDE_FROM_ABI size_type max_size() const noexcept { return __keys_.max_size(); }
+
+  // [flat.multiset.modifiers], modifiers
+  template <class... _Args>
+    requires is_constructible_v<value_type, _Args...>  
+     _LIBCPP_HIDE_FROM_ABI iterator emplace(_Args&&... __args) {
+    auto __key_it    = ranges::upper_bound(__containers_.keys, __pair.first, __compare_);
+    auto __mapped_it = __corresponding_mapped_it(*this, __key_it);
+
+    return __flat_map_utils::__emplace_exact_pos(
+        *this, std::move(__key_it), std::move(__mapped_it), std::move(__pair.first), std::move(__pair.second));
+  }
+
+  template <class... _Args>
+    requires is_constructible_v<value_type, _Args...>  
+  _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __hint, _Args&&... __args) {
+    std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);
+
+    auto __prev_larger  = __hint != cbegin() && __compare_(__pair.first, (__hint - 1)->first);
+    auto __next_smaller = __hint != cend() && __compare_(__hint->first, __pair.first);
+
+    auto __hint_distance = __hint.__key_iter_ - __containers_.keys.cbegin();
+    auto __key_iter      = __containers_.keys.begin() + __hint_distance;
+    auto __mapped_iter   = __containers_.values.begin() + __hint_distance;
+
+    if (!__prev_larger && !__next_smaller) [[likely]] {
+      // hint correct, just use exact hint iterators
+    } else if (__prev_larger && !__next_smaller) {
+      // the hint position is more to the right than the key should have been.
+      // we want to emplace the element to a position as right as possible
+      // e.g. Insert new element "2" in the following range
+      // 1, 1, 2, 2, 2, 3, 4, 6
+      //                   ^
+      //                   |
+      //                  hint
+      // We want to insert "2" after the last existing "2"
+      __key_iter    = ranges::upper_bound(__containers_.keys.begin(), __key_iter, __pair.first, __compare_);
+      __mapped_iter = __corresponding_mapped_it(*this, __key_iter);
+    } else {
+      _LIBCPP_ASSERT_INTERNAL(!__prev_larger && __next_smaller, "this means that the multiset is not sorted");
+
+      // the hint position is more to the left than the key should have been.
+      // we want to emplace the element to a position as left as possible
+      //  1, 1, 2, 2, 2, 3, 4, 6
+      //  ^
+      //  |
+      // hint
+      // We want to insert "2" before the first existing "2"
+      __key_iter    = ranges::lower_bound(__key_iter, __containers_.keys.end(), __pair.first, __compare_);
+      __mapped_iter = __corresponding_mapped_it(*this, __key_iter);
+    }
+    return __flat_map_utils::__emplace_exact_pos(
+        *this, __key_iter, __mapped_iter, std::move(__pair.first), std::move(__pair.second));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator insert(const value_type& __x) { return emplace(__x); }
+
+  _LIBCPP_HIDE_FROM_ABI iterator insert(value_type&& __x) { return emplace(std::move(__x)); }
+
+  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, const value_type& __x) {
+    return emplace_hint(__hint, __x);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, value_type&& __x) {
+    return emplace_hint(__hint, std::move(__x));
+  }
+
+  template <class _InputIterator>
+    requires __has_input_iterator_category<_InputIterator>::value
+  _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last) {
+    if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {
+      __reserve(__last - __first);
+    }
+    __append_sort_merge</*WasSorted = */ false>(std::move(__first), std::move(__last));
+  }
+
+  template <class _InputIterator>
+    requires __has_input_iterator_category<_InputIterator>::value
+  _LIBCPP_HIDE_FROM_ABI void insert(sorted_equivalent_t, _InputIterator __first, _InputIterator __last) {
+    if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {
+      __reserve(__last - __first);
+    }
+
+    __append_sort_merge</*WasSorted = */ true>(std::move(__first), std::move(__last));
+  }
+
+  template <_ContainerCompatibleRange<value_type> _Range>
+  _LIBCPP_HIDE_FROM_ABI void insert_range(_Range&& __range) {
+    if constexpr (ranges::sized_range<_Range>) {
+      __reserve(ranges::size(__range));
+    }
+
+    __append_sort_merge</*WasSorted = */ false>(ranges::begin(__range), ranges::end(__range));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI void insert(initializer_list<value_type> __il) { insert(__il.begin(), __il.end()); }
+
+  _LIBCPP_HIDE_FROM_ABI void insert(sorted_equivalent_t, initializer_list<value_type> __il) {
+    insert(sorted_equivalent, __il.begin(), __il.end());
+  }
+
+  _LIBCPP_HIDE_FROM_ABI container_type extract() && {
+    auto __guard = std::__make_scope_guard([&]() noexcept { clear() /* noexcept */; });
+    auto __ret   = std::move(__keys_);
+    return __ret;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI void replace(container_type&& __keys) {
+    _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys, __compare_), "Key container is not sorted");
+    auto __guard         = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    __keys_   = std::move(__keys);
+    __guard.__complete();
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __position) {
+    return __erase(__position.__key_iter_, __position.__mapped_iter_);
+  }
+
+  // iterator and const_iterator are the same type
+  // iterator erase(const_iterator __position);
+
+  _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __x) {
+    auto [__first, __last] = equal_range(__x);
+    auto __res             = __last - __first;
+    erase(__first, __last);
+    return __res;
+  }
+
+  template <class _Kp>
+    requires(__is_compare_transparent && !is_convertible_v<_Kp &&, iterator> &&
+             !is_convertible_v<_Kp &&, const_iterator>)
+  _LIBCPP_HIDE_FROM_ABI size_type erase(_Kp&& __x) {
+    auto [__first, __last] = equal_range(__x);
+    auto __res             = __last - __first;
+    erase(__first, __last);
+    return __res;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) {
+    auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    auto __key_it     = __containers_.keys.erase(__first.__key_iter_, __last.__key_iter_);
+    auto __mapped_it  = __containers_.values.erase(__first.__mapped_iter_, __last.__mapped_iter_);
+    __on_failure.__complete();
+    return iterator(std::move(__key_it), std::move(__mapped_it));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI void swap(flat_multiset& __y) noexcept {
+    // warning: The spec has unconditional noexcept, which means that
+    // if any of the following functions throw an exception,
+    // std::terminate will be called
+    ranges::swap(__compare_, __y.__compare_);
+    ranges::swap(__containers_.keys, __y.__containers_.keys);
+    ranges::swap(__containers_.values, __y.__containers_.values);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI void clear() noexcept {
+    __containers_.keys.clear();
+    __containers_.values.clear();
+  }
+
+  // observers
+  _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __compare_; }
+  _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__compare_); }
+
+  _LIBCPP_HIDE_FROM_ABI const key_container_type& keys() const noexcept { return __containers_.keys; }
+  _LIBCPP_HIDE_FROM_ABI const mapped_container_type& values() const noexcept { return __containers_.values; }
+
+  // map operations
+  _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __x) { return __find_impl(*this, __x); }
+
+  _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __x) const { return __find_impl(*this, __x); }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI iterator find(const _Kp& __x) {
+    return __find_impl(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Kp& __x) const {
+    return __find_impl(*this, __x);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __x) const {
+    auto [__first, __last] = equal_range(__x);
+    return __last - __first;
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI size_type count(const _Kp& __x) const {
+    auto [__first, __last] = equal_range(__x);
+    return __last - __first;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __x) const { return find(__x) != end(); }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI bool contains(const _Kp& __x) const {
+    return find(__x) != end();
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __x) { return __lower_bound<iterator>(*this, __x); }
+
+  _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __x) const {
+    return __lower_bound<const_iterator>(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Kp& __x) {
+    return __lower_bound<iterator>(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Kp& __x) const {
+    return __lower_bound<const_iterator>(*this, __x);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __x) { return __upper_bound<iterator>(*this, __x); }
+
+  _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __x) const {
+    return __upper_bound<const_iterator>(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Kp& __x) {
+    return __upper_bound<iterator>(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Kp& __x) const {
+    return __upper_bound<const_iterator>(*this, __x);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __x) {
+    return __equal_range_impl(*this, __x);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __x) const {
+    return __equal_range_impl(*this, __x);
+  }
+
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _Kp& __x) {
+    return __equal_range_impl(*this, __x);
+  }
+  template <class _Kp>
+    requires __is_compare_transparent
+  _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _Kp& __x) const {
+    return __equal_range_impl(*this, __x);
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const flat_multiset& __x, const flat_multiset& __y) {
+    return ranges::equal(__x, __y);
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI auto operator<=>(const flat_multiset& __x, const flat_multiset& __y) {
+    return std::lexicographical_compare_three_way(
+        __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI void swap(flat_multiset& __x, flat_multiset& __y) noexcept { __x.swap(__y); }
+
+private:
+  template <bool _WasSorted, class _InputIterator, class _Sentinel>
+  _LIBCPP_HIDE_FROM_ABI void __append_sort_merge(_InputIterator __first, _Sentinel __last) {
+    auto __on_failure     = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    size_t __num_appended = __flat_map_utils::__append(*this, std::move(__first), std::move(__last));
+    if (__num_appended != 0) {
+      auto __zv                  = ranges::views::zip(__containers_.keys, __containers_.values);
+      auto __append_start_offset = __containers_.keys.size() - __num_appended;
+      auto __end                 = __zv.end();
+      auto __compare_key         = [this](const auto& __p1, const auto& __p2) {
+        return __compare_(std::get<0>(__p1), std::get<0>(__p2));
+      };
+      if constexpr (!_WasSorted) {
+        ranges::sort(__zv.begin() + __append_start_offset, __end, __compare_key);
+      } else {
+        _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(
+            __is_sorted(__containers_.keys | ranges::views::drop(__append_start_offset)),
+            "Key container is not sorted");
+      }
+      ranges::inplace_merge(__zv.begin(), __zv.begin() + __append_start_offset, __end, __compare_key);
+    }
+    __on_failure.__complete();
+  }
+
+  template <class _Self, class _Kp>
+  _LIBCPP_HIDE_FROM_ABI static auto __find_impl(_Self&& __self, const _Kp& __key) {
+    auto __it   = __self.lower_bound(__key);
+    auto __last = __self.end();
+    if (__it == __last || __self.__compare_(__key, __it->first)) {
+      return __last;
+    }
+    return __it;
+  }
+
+  template <class _Self, class _Kp>
+  _LIBCPP_HIDE_FROM_ABI static auto __equal_range_impl(_Self&& __self, const _Kp& __key) {
+    auto [__key_first, __key_last] = ranges::equal_range(__self.__containers_.keys, __key, __self.__compare_);
+
+    using __iterator_type = ranges::iterator_t<decltype(__self)>;
+    return std::make_pair(__iterator_type(__key_first, __corresponding_mapped_it(__self, __key_first)),
+                          __iterator_type(__key_last, __corresponding_mapped_it(__self, __key_last)));
+  }
+
+  template <class _Res, class _Self, class _Kp>
+  _LIBCPP_HIDE_FROM_ABI static _Res __lower_bound(_Self&& __self, _Kp& __x) {
+    auto __key_iter    = ranges::lower_bound(__self.__containers_.keys, __x, __self.__compare_);
+    auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);
+    return _Res(std::move(__key_iter), std::move(__mapped_iter));
+  }
+
+  template <class _Res, class _Self, class _Kp>
+  _LIBCPP_HIDE_FROM_ABI static _Res __upper_bound(_Self&& __self, _Kp& __x) {
+    auto __key_iter    = ranges::upper_bound(__self.__containers_.keys, __x, __self.__compare_);
+    auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);
+    return _Res(std::move(__key_iter), std::move(__mapped_iter));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI void __reserve(size_t __size) {
+    if constexpr (requires { __containers_.keys.reserve(__size); }) {
+      __containers_.keys.reserve(__size);
+    }
+
+    if constexpr (requires { __containers_.values.reserve(__size); }) {
+      __containers_.values.reserve(__size);
+    }
+  }
+
+  template <class _KIter, class _MIter>
+  _LIBCPP_HIDE_FROM_ABI iterator __erase(_KIter __key_iter_to_remove, _MIter __mapped_iter_to_remove) {
+    auto __on_failure  = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    auto __key_iter    = __containers_.keys.erase(__key_iter_to_remove);
+    auto __mapped_iter = __containers_.values.erase(__mapped_iter_to_remove);
+    __on_failure.__complete();
+    return iterator(std::move(__key_iter), std::move(__mapped_iter));
+  }
+
+  template <class _Key2, class _Tp2, class _Compare2, class _KeyContainer2, class _MappedContainer2, class _Predicate>
+  friend typename flat_multiset<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>::size_type
+  erase_if(flat_multiset<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>&, _Predicate);
+
+  friend __flat_map_utils;
+
+  _KeyContainer __keys_;
+  _LIBCPP_NO_UNIQUE_ADDRESS key_compare __compare_;
+
+  struct __key_equiv {
+    _LIBCPP_HIDE_FROM_ABI __key_equiv(key_compare __c) : __comp_(__c) {}
+    _LIBCPP_HIDE_FROM_ABI bool operator()(const_reference __x, const_reference __y) const {
+      return !__comp_(std::get<0>(__x), std::get<0>(__y)) && !__comp_(std::get<0>(__y), std::get<0>(__x));
+    }
+    key_compare __comp_;
+  };
+};
+
+template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+  requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+           !__is_allocator<_MappedContainer>::value &&
+           is_invocable_v<const _Compare&,
+                          const typename _KeyContainer::value_type&,
+                          const typename _KeyContainer::value_type&>)
+flat_multiset(_KeyContainer, _MappedContainer, _Compare = _Compare())
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     _Compare,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Allocator>
+  requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&
+           !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)
+flat_multiset(_KeyContainer, _MappedContainer, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     less<typename _KeyContainer::value_type>,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+  requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+           !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&
+           uses_allocator_v<_MappedContainer, _Allocator> &&
+           is_invocable_v<const _Compare&,
+                          const typename _KeyContainer::value_type&,
+                          const typename _KeyContainer::value_type&>)
+flat_multiset(_KeyContainer, _MappedContainer, _Compare, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     _Compare,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+  requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+           !__is_allocator<_MappedContainer>::value &&
+           is_invocable_v<const _Compare&,
+                          const typename _KeyContainer::value_type&,
+                          const typename _KeyContainer::value_type&>)
+flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     _Compare,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Allocator>
+  requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&
+           !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)
+flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     less<typename _KeyContainer::value_type>,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+  requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+           !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&
+           uses_allocator_v<_MappedContainer, _Allocator> &&
+           is_invocable_v<const _Compare&,
+                          const typename _KeyContainer::value_type&,
+                          const typename _KeyContainer::value_type&>)
+flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type,
+                     typename _MappedContainer::value_type,
+                     _Compare,
+                     _KeyContainer,
+                     _MappedContainer>;
+
+template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+  requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)
+flat_multiset(_InputIterator, _InputIterator, _Compare = _Compare())
+    -> flat_multiset<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+
+template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+  requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)
+flat_multiset(sorted_equivalent_t, _InputIterator, _InputIterator, _Compare = _Compare())
+    -> flat_multiset<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+
+template <ranges::input_range _Range,
+          class _Compare   = less<__range_key_type<_Range>>,
+          class _Allocator = allocator<byte>,
+          class            = __enable_if_t<!__is_allocator<_Compare>::value && __is_allocator<_Allocator>::value>>
+flat_multiset(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) -> flat_multiset<
+    __range_key_type<_Range>,
+    __range_mapped_type<_Range>,
+    _Compare,
+    vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,
+    vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;
+
+template <ranges::input_range _Range, class _Allocator, class = __enable_if_t<__is_allocator<_Allocator>::value>>
+flat_multiset(from_range_t, _Range&&, _Allocator) -> flat_multiset<
+    __range_key_type<_Range>,
+    __range_mapped_type<_Range>,
+    less<__range_key_type<_Range>>,
+    vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,
+    vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>>
+  requires(!__is_allocator<_Compare>::value)
+flat_multiset(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_multiset<_Key, _Tp, _Compare>;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>>
+  requires(!__is_allocator<_Compare>::value)
+flat_multiset(sorted_equivalent_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
+    -> flat_multiset<_Key, _Tp, _Compare>;
+
+template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Allocator>
+struct uses_allocator<flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>, _Allocator>
+    : bool_constant<uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator>> {};
+
+template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Predicate>
+_LIBCPP_HIDE_FROM_ABI typename flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type
+erase_if(flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __flat_multiset, _Predicate __pred) {
+  auto __zv     = ranges::views::zip(__flat_multiset.__containers_.keys, __flat_multiset.__containers_.values);
+  auto __first  = __zv.begin();
+  auto __last   = __zv.end();
+  auto __guard  = std::__make_exception_guard([&] { __flat_multiset.clear(); });
+  auto __it     = std::remove_if(__first, __last, [&](auto&& __zipped) -> bool {
+    using _Ref = typename flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::const_reference;
+    return __pred(_Ref(std::get<0>(__zipped), std::get<1>(__zipped)));
+  });
+  auto __res    = __last - __it;
+  auto __offset = __it - __first;
+
+  const auto __erase_container = [&](auto& __cont) { __cont.erase(__cont.begin() + __offset, __cont.end()); };
+
+  __erase_container(__flat_multiset.__containers_.keys);
+  __erase_container(__flat_multiset.__containers_.values);
+
+  __guard.__complete();
+  return __res;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___FLAT_MAP_FLAT_MULTISET_H
diff --git a/libcxx/include/flat_set b/libcxx/include/flat_set
index d03645fafafdb..5051b27097d2a 100644
--- a/libcxx/include/flat_set
+++ b/libcxx/include/flat_set
@@ -31,6 +31,21 @@ namespace std {
   template<class Key, class Compare, class KeyContainer, class Predicate>
     typename flat_set<Key, Compare, KeyContainer>::size_type
       erase_if(flat_set<Key, Compare, KeyContainer>& c, Predicate pred);
+  
+   // [flat.multiset], class template flat_multiset
+  template<class Key, class Compare = less<Key>, class KeyContainer = vector<Key>>
+    class flat_multiset;
+
+  struct sorted_equivalent_t { explicit sorted_equivalent_t() = default; };
+  inline constexpr sorted_equivalent_t sorted_equivalent{};
+
+  template<class Key, class Compare, class KeyContainer, class Allocator>
+    struct uses_allocator<flat_multiset<Key, Compare, KeyContainer>, Allocator>;
+
+  // [flat.multiset.erasure], erasure for flat_multiset
+  template<class Key, class Compare, class KeyContainer, class Predicate>
+    typename flat_multiset<Key, Compare, KeyContainer>::size_type
+      erase_if(flat_multiset<Key, Compare, KeyContainer>& c, Predicate pred);
 }
 */
 
@@ -41,7 +56,9 @@ namespace std {
 
 #  if _LIBCPP_STD_VER >= 23
 #    include <__flat_map/sorted_unique.h>
+#    include <__flat_map/sorted_equivalent.h>
 #    include <__flat_set/flat_set.h>
+#    include <__flat_set/flat_multiset.h>
 #  endif
 
 // for feature-test macros
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 1809fe5c7f0f7..3009e6c65f14c 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1306,6 +1306,7 @@ module std [system] {
 
     header "flat_set"
     export std.flat_map.sorted_unique
+    export std.flat_map.sorted_equivalent
     export *
   }
 
diff --git a/libcxx/include/version b/libcxx/include/version
index 83ae11dabd2bc..afdb029cebfb1 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -486,7 +486,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 # define __cpp_lib_containers_ranges                    202202L
 # define __cpp_lib_expected                             202211L
 # define __cpp_lib_flat_map                             202207L
-// # define __cpp_lib_flat_set                             202207L
+# define __cpp_lib_flat_set                             202207L
 # define __cpp_lib_format_ranges                        202207L
 // # define __cpp_lib_formatters                           202302L
 # define __cpp_lib_forward_like                         202207L
diff --git a/libcxx/modules/std/flat_set.inc b/libcxx/modules/std/flat_set.inc
index 3f2c6e09a0ebe..51f39b75458b9 100644
--- a/libcxx/modules/std/flat_set.inc
+++ b/libcxx/modules/std/flat_set.inc
@@ -19,13 +19,11 @@ export namespace std {
 
   // [flat.set.erasure], erasure for flat_­set
   using std::erase_if;
-#endif // _LIBCPP_STD_VER >= 23
 
-#if 0
   // [flat.multiset], class template flat_­multiset
   using std::flat_multiset;
 
   using std::sorted_equivalent;
   using std::sorted_equivalent_t;
-#endif
+#endif // _LIBCPP_STD_VER >= 23
 } // namespace std
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/flat_set.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/flat_set.version.compile.pass.cpp
index f9d0b0a6b4e4f..1d96845288b29 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/flat_set.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/flat_set.version.compile.pass.cpp
@@ -48,32 +48,20 @@
 
 #elif TEST_STD_VER == 23
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should be defined in c++23"
-#   endif
-#   if __cpp_lib_flat_set != 202207L
-#     error "__cpp_lib_flat_set should have the value 202207L in c++23"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_flat_set
+#   error "__cpp_lib_flat_set should be defined in c++23"
+# endif
+# if __cpp_lib_flat_set != 202207L
+#   error "__cpp_lib_flat_set should have the value 202207L in c++23"
 # endif
 
 #elif TEST_STD_VER > 23
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should be defined in c++26"
-#   endif
-#   if __cpp_lib_flat_set != 202207L
-#     error "__cpp_lib_flat_set should have the value 202207L in c++26"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_flat_set
+#   error "__cpp_lib_flat_set should be defined in c++26"
+# endif
+# if __cpp_lib_flat_set != 202207L
+#   error "__cpp_lib_flat_set should have the value 202207L in c++26"
 # endif
 
 #endif // TEST_STD_VER > 23
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index e5a657207923b..915e5115ff238 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -5128,17 +5128,11 @@
 #   error "__cpp_lib_flat_map should have the value 202207L in c++23"
 # endif
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should be defined in c++23"
-#   endif
-#   if __cpp_lib_flat_set != 202207L
-#     error "__cpp_lib_flat_set should have the value 202207L in c++23"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_flat_set
+#   error "__cpp_lib_flat_set should be defined in c++23"
+# endif
+# if __cpp_lib_flat_set != 202207L
+#   error "__cpp_lib_flat_set should have the value 202207L in c++23"
 # endif
 
 # if !defined(_LIBCPP_VERSION) || _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT
@@ -6841,17 +6835,11 @@
 #   error "__cpp_lib_flat_map should have the value 202207L in c++26"
 # endif
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should be defined in c++26"
-#   endif
-#   if __cpp_lib_flat_set != 202207L
-#     error "__cpp_lib_flat_set should have the value 202207L in c++26"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_flat_set
-#     error "__cpp_lib_flat_set should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_flat_set
+#   error "__cpp_lib_flat_set should be defined in c++26"
+# endif
+# if __cpp_lib_flat_set != 202207L
+#   error "__cpp_lib_flat_set should have the value 202207L in c++26"
 # endif
 
 # if !defined(_LIBCPP_VERSION) || _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 53252d5e2d673..fef22ee6264d5 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -520,7 +520,6 @@ def add_version_header(tc):
             "name": "__cpp_lib_flat_set",
             "values": {"c++23": 202207},
             "headers": ["flat_set"],
-            "unimplemented": True,
         },
         {
             "name": "__cpp_lib_format",

>From fcbaf8b38511601148b4ac14e20ef2a17cf0519e Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 9 Mar 2025 13:56:27 +0000
Subject: [PATCH 07/10] build

---
 libcxx/include/__flat_set/flat_multiset.h | 361 +++++++++-------------
 1 file changed, 151 insertions(+), 210 deletions(-)

diff --git a/libcxx/include/__flat_set/flat_multiset.h b/libcxx/include/__flat_set/flat_multiset.h
index 6aa160f0af54b..6ad62a75da464 100644
--- a/libcxx/include/__flat_set/flat_multiset.h
+++ b/libcxx/include/__flat_set/flat_multiset.h
@@ -31,7 +31,7 @@
 #include <__cstddef/ptrdiff_t.h>
 #include <__flat_map/key_value_iterator.h>
 #include <__flat_map/sorted_equivalent.h>
-#include <__flat_map/utils.h>
+#include <__flat_set/ra_iterator.h>
 #include <__functional/invoke.h>
 #include <__functional/is_transparent.h>
 #include <__functional/operations.h>
@@ -60,13 +60,13 @@
 #include <__type_traits/is_nothrow_constructible.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/maybe_const.h>
+#include <__utility/as_const.h>
 #include <__utility/exception_guard.h>
 #include <__utility/move.h>
 #include <__utility/pair.h>
 #include <__utility/scope_guard.h>
 #include <__vector/vector.h>
 #include <initializer_list>
-#include <stdexcept>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -97,7 +97,7 @@ class flat_multiset {
   using const_reference        = const value_type&;
   using size_type              = typename _KeyContainer::size_type;
   using difference_type        = typename _KeyContainer::difference_type;
-  using iterator               = typename _KeyContainer::const_iterator;
+  using iterator               = __ra_iterator<flat_multiset, typename _KeyContainer::const_iterator>;
   using const_iterator         = iterator;
   using reverse_iterator       = std::reverse_iterator<iterator>;
   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
@@ -331,10 +331,10 @@ class flat_multiset {
   }
 
   // iterators
-  _LIBCPP_HIDE_FROM_ABI iterator begin() noexcept { return __keys_.begin(); }
-  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const noexcept { return __keys_.begin(); }
-  _LIBCPP_HIDE_FROM_ABI iterator end() noexcept { return __keys_.end(); }
-  _LIBCPP_HIDE_FROM_ABI const_iterator end() const noexcept { return __keys_.end(); }
+  _LIBCPP_HIDE_FROM_ABI iterator begin() noexcept { return iterator(std::as_const(__keys_).begin()); }
+  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const noexcept { return const_iterator(__keys_.begin()); }
+  _LIBCPP_HIDE_FROM_ABI iterator end() noexcept { return iterator(std::as_const(__keys_).end()); }
+  _LIBCPP_HIDE_FROM_ABI const_iterator end() const noexcept { return const_iterator(__keys_.end()); }
 
   _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
   _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
@@ -353,18 +353,24 @@ class flat_multiset {
 
   // [flat.multiset.modifiers], modifiers
   template <class... _Args>
-    requires is_constructible_v<value_type, _Args...>  
-     _LIBCPP_HIDE_FROM_ABI iterator emplace(_Args&&... __args) {
-    auto __key_it    = ranges::upper_bound(__containers_.keys, __pair.first, __compare_);
-    auto __mapped_it = __corresponding_mapped_it(*this, __key_it);
-
-    return __flat_map_utils::__emplace_exact_pos(
-        *this, std::move(__key_it), std::move(__mapped_it), std::move(__pair.first), std::move(__pair.second));
+    requires is_constructible_v<value_type, _Args...>
+  _LIBCPP_HIDE_FROM_ABI iterator emplace(_Args&&... __args) {
+    if constexpr (sizeof...(__args) == 1 && (is_same_v<remove_cvref_t<_Args>, _Key> && ...)) {
+      return __try_emplace(std::forward<_Args>(__args)...);
+    } else {
+      return __try_emplace(_Key(std::forward<_Args>(__args)...));
+    }
   }
 
   template <class... _Args>
-    requires is_constructible_v<value_type, _Args...>  
+    requires is_constructible_v<value_type, _Args...>
   _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __hint, _Args&&... __args) {
+    if constexpr (sizeof...(__args) == 1 && (is_same_v<remove_cvref_t<_Args>, _Key> && ...)) {
+      return __emplace_hint(std::move(__hint), std::forward<_Args>(__args)...);
+    } else {
+      return __emplace_hint(std::move(__hint), _Key(std::forward<_Args>(__args)...));
+    }
+    /*
     std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);
 
     auto __prev_larger  = __hint != cbegin() && __compare_(__pair.first, (__hint - 1)->first);
@@ -402,6 +408,7 @@ class flat_multiset {
     }
     return __flat_map_utils::__emplace_exact_pos(
         *this, __key_iter, __mapped_iter, std::move(__pair.first), std::move(__pair.second));
+        */
   }
 
   _LIBCPP_HIDE_FROM_ABI iterator insert(const value_type& __x) { return emplace(__x); }
@@ -458,13 +465,16 @@ class flat_multiset {
 
   _LIBCPP_HIDE_FROM_ABI void replace(container_type&& __keys) {
     _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(ranges::is_sorted(__keys, __compare_), "Key container is not sorted");
-    auto __guard         = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
-    __keys_   = std::move(__keys);
+    auto __guard = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    __keys_      = std::move(__keys);
     __guard.__complete();
   }
 
   _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __position) {
-    return __erase(__position.__key_iter_, __position.__mapped_iter_);
+    auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    auto __key_iter   = __keys_.erase(__position.__base());
+    __on_failure.__complete();
+    return iterator(__key_iter);
   }
 
   // iterator and const_iterator are the same type
@@ -478,7 +488,7 @@ class flat_multiset {
   }
 
   template <class _Kp>
-    requires(__is_compare_transparent && !is_convertible_v<_Kp &&, iterator> &&
+    requires(__is_transparent_v<_Compare> && !is_convertible_v<_Kp &&, iterator> &&
              !is_convertible_v<_Kp &&, const_iterator>)
   _LIBCPP_HIDE_FROM_ABI size_type erase(_Kp&& __x) {
     auto [__first, __last] = equal_range(__x);
@@ -489,32 +499,25 @@ class flat_multiset {
 
   _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) {
     auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
-    auto __key_it     = __containers_.keys.erase(__first.__key_iter_, __last.__key_iter_);
-    auto __mapped_it  = __containers_.values.erase(__first.__mapped_iter_, __last.__mapped_iter_);
+    auto __key_it     = __keys_.erase(__first.__base(), __last.__base());
     __on_failure.__complete();
-    return iterator(std::move(__key_it), std::move(__mapped_it));
+    return iterator(std::move(__key_it));
   }
 
   _LIBCPP_HIDE_FROM_ABI void swap(flat_multiset& __y) noexcept {
     // warning: The spec has unconditional noexcept, which means that
     // if any of the following functions throw an exception,
     // std::terminate will be called
+    // This is discussed in P2767, which hasn't been voted on yet.
     ranges::swap(__compare_, __y.__compare_);
-    ranges::swap(__containers_.keys, __y.__containers_.keys);
-    ranges::swap(__containers_.values, __y.__containers_.values);
+    ranges::swap(__keys_, __y.__keys_);
   }
 
-  _LIBCPP_HIDE_FROM_ABI void clear() noexcept {
-    __containers_.keys.clear();
-    __containers_.values.clear();
-  }
+  _LIBCPP_HIDE_FROM_ABI void clear() noexcept { __keys_.clear(); }
 
   // observers
   _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __compare_; }
-  _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__compare_); }
-
-  _LIBCPP_HIDE_FROM_ABI const key_container_type& keys() const noexcept { return __containers_.keys; }
-  _LIBCPP_HIDE_FROM_ABI const mapped_container_type& values() const noexcept { return __containers_.values; }
+  _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return __compare_; }
 
   // map operations
   _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __x) { return __find_impl(*this, __x); }
@@ -522,13 +525,13 @@ class flat_multiset {
   _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __x) const { return __find_impl(*this, __x); }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI iterator find(const _Kp& __x) {
     return __find_impl(*this, __x);
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Kp& __x) const {
     return __find_impl(*this, __x);
   }
@@ -539,7 +542,7 @@ class flat_multiset {
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI size_type count(const _Kp& __x) const {
     auto [__first, __last] = equal_range(__x);
     return __last - __first;
@@ -548,45 +551,49 @@ class flat_multiset {
   _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __x) const { return find(__x) != end(); }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI bool contains(const _Kp& __x) const {
     return find(__x) != end();
   }
 
-  _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __x) { return __lower_bound<iterator>(*this, __x); }
+  _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __x) {
+    return iterator(ranges::lower_bound(std::as_const(__keys_), __x, __compare_));
+  }
 
   _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __x) const {
-    return __lower_bound<const_iterator>(*this, __x);
+    return const_iterator(ranges::lower_bound(__keys_, __x, __compare_));
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Kp& __x) {
-    return __lower_bound<iterator>(*this, __x);
+    return iterator(ranges::lower_bound(std::as_const(__keys_), __x, __compare_));
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Kp& __x) const {
-    return __lower_bound<const_iterator>(*this, __x);
+    return const_iterator(ranges::lower_bound(__keys_, __x, __compare_));
   }
 
-  _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __x) { return __upper_bound<iterator>(*this, __x); }
+  _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __x) {
+    return iterator(ranges::upper_bound(std::as_const(__keys_), __x, __compare_));
+  }
 
   _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __x) const {
-    return __upper_bound<const_iterator>(*this, __x);
+    return const_iterator(ranges::upper_bound(__keys_, __x, __compare_));
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Kp& __x) {
-    return __upper_bound<iterator>(*this, __x);
+    return iterator(ranges::upper_bound(std::as_const(__keys_), __x, __compare_));
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Kp& __x) const {
-    return __upper_bound<const_iterator>(*this, __x);
+    return const_iterator(ranges::upper_bound(__keys_, __x, __compare_));
   }
 
   _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __x) {
@@ -598,12 +605,12 @@ class flat_multiset {
   }
 
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _Kp& __x) {
     return __equal_range_impl(*this, __x);
   }
   template <class _Kp>
-    requires __is_compare_transparent
+    requires __is_transparent_v<_Compare>
   _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _Kp& __x) const {
     return __equal_range_impl(*this, __x);
   }
@@ -620,25 +627,40 @@ class flat_multiset {
   friend _LIBCPP_HIDE_FROM_ABI void swap(flat_multiset& __x, flat_multiset& __y) noexcept { __x.swap(__y); }
 
 private:
-  template <bool _WasSorted, class _InputIterator, class _Sentinel>
-  _LIBCPP_HIDE_FROM_ABI void __append_sort_merge(_InputIterator __first, _Sentinel __last) {
-    auto __on_failure     = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
-    size_t __num_appended = __flat_map_utils::__append(*this, std::move(__first), std::move(__last));
-    if (__num_appended != 0) {
-      auto __zv                  = ranges::views::zip(__containers_.keys, __containers_.values);
-      auto __append_start_offset = __containers_.keys.size() - __num_appended;
-      auto __end                 = __zv.end();
-      auto __compare_key         = [this](const auto& __p1, const auto& __p2) {
-        return __compare_(std::get<0>(__p1), std::get<0>(__p2));
-      };
+  // todo: share with flat_set
+  template <class _InputIterator>
+  _LIBCPP_HIDE_FROM_ABI void __append(_InputIterator __first, _InputIterator __last) {
+    __keys_.insert(__keys_.end(), std::move(__first), std::move(__last));
+  }
+
+  template <class _Range>
+  _LIBCPP_HIDE_FROM_ABI void __append(_Range&& __rng) {
+    if constexpr (requires { __keys_.insert_range(__keys_.end(), std::forward<_Range>(__rng)); }) {
+      // C++23 Sequence Container should have insert_range member function
+      // Note that not all Sequence Containers provide append_range.
+      __keys_.insert_range(__keys_.end(), std::forward<_Range>(__rng));
+    } else if constexpr (ranges::common_range<_Range>) {
+      __keys_.insert(__keys_.end(), ranges::begin(__rng), ranges::end(__rng));
+    } else {
+      for (auto&& __x : __rng) {
+        __keys_.insert(__keys_.end(), std::forward<decltype(__x)>(__x));
+      }
+    }
+  }
+
+  template <bool _WasSorted, class... _Args>
+  _LIBCPP_HIDE_FROM_ABI void __append_sort_merge(_Args&&... __args) {
+    auto __on_failure    = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
+    size_type __old_size = size();
+    __append(std::forward<_Args>(__args)...);
+    if (size() != __old_size) {
       if constexpr (!_WasSorted) {
-        ranges::sort(__zv.begin() + __append_start_offset, __end, __compare_key);
+        ranges::sort(__keys_.begin() + __old_size, __keys_.end(), __compare_);
       } else {
-        _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(
-            __is_sorted(__containers_.keys | ranges::views::drop(__append_start_offset)),
-            "Key container is not sorted");
+        _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(__is_sorted_and_unique(__keys_ | ranges::views::drop(__old_size)),
+                                            "Either the key container is not sorted or it contains duplicates");
       }
-      ranges::inplace_merge(__zv.begin(), __zv.begin() + __append_start_offset, __end, __compare_key);
+      ranges::inplace_merge(__keys_.begin(), __keys_.begin() + __old_size, __keys_.end(), __compare_);
     }
     __on_failure.__complete();
   }
@@ -655,51 +677,14 @@ class flat_multiset {
 
   template <class _Self, class _Kp>
   _LIBCPP_HIDE_FROM_ABI static auto __equal_range_impl(_Self&& __self, const _Kp& __key) {
-    auto [__key_first, __key_last] = ranges::equal_range(__self.__containers_.keys, __key, __self.__compare_);
-
-    using __iterator_type = ranges::iterator_t<decltype(__self)>;
-    return std::make_pair(__iterator_type(__key_first, __corresponding_mapped_it(__self, __key_first)),
-                          __iterator_type(__key_last, __corresponding_mapped_it(__self, __key_last)));
+    using __iter                   = _If<is_const_v<__libcpp_remove_reference_t<_Self>>, const_iterator, iterator>;
+    auto [__key_first, __key_last] = ranges::equal_range(__self.__keys_, __key, __self.__compare_);
+    return std::make_pair(__iter(__key_first), __iter(__key_last));
   }
 
-  template <class _Res, class _Self, class _Kp>
-  _LIBCPP_HIDE_FROM_ABI static _Res __lower_bound(_Self&& __self, _Kp& __x) {
-    auto __key_iter    = ranges::lower_bound(__self.__containers_.keys, __x, __self.__compare_);
-    auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);
-    return _Res(std::move(__key_iter), std::move(__mapped_iter));
-  }
-
-  template <class _Res, class _Self, class _Kp>
-  _LIBCPP_HIDE_FROM_ABI static _Res __upper_bound(_Self&& __self, _Kp& __x) {
-    auto __key_iter    = ranges::upper_bound(__self.__containers_.keys, __x, __self.__compare_);
-    auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);
-    return _Res(std::move(__key_iter), std::move(__mapped_iter));
-  }
-
-  _LIBCPP_HIDE_FROM_ABI void __reserve(size_t __size) {
-    if constexpr (requires { __containers_.keys.reserve(__size); }) {
-      __containers_.keys.reserve(__size);
-    }
-
-    if constexpr (requires { __containers_.values.reserve(__size); }) {
-      __containers_.values.reserve(__size);
-    }
-  }
-
-  template <class _KIter, class _MIter>
-  _LIBCPP_HIDE_FROM_ABI iterator __erase(_KIter __key_iter_to_remove, _MIter __mapped_iter_to_remove) {
-    auto __on_failure  = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });
-    auto __key_iter    = __containers_.keys.erase(__key_iter_to_remove);
-    auto __mapped_iter = __containers_.values.erase(__mapped_iter_to_remove);
-    __on_failure.__complete();
-    return iterator(std::move(__key_iter), std::move(__mapped_iter));
-  }
-
-  template <class _Key2, class _Tp2, class _Compare2, class _KeyContainer2, class _MappedContainer2, class _Predicate>
-  friend typename flat_multiset<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>::size_type
-  erase_if(flat_multiset<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>&, _Predicate);
-
-  friend __flat_map_utils;
+  template <class _Key2, class _Compare2, class _KeyContainer2, class _Predicate>
+  friend typename flat_multiset<_Key2, _Compare2, _KeyContainer2>::size_type
+  erase_if(flat_multiset<_Key2, _Compare2, _KeyContainer2>&, _Predicate);
 
   _KeyContainer __keys_;
   _LIBCPP_NO_UNIQUE_ADDRESS key_compare __compare_;
@@ -713,141 +698,97 @@ class flat_multiset {
   };
 };
 
-template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+template <class _KeyContainer, class _Compare = less<typename _KeyContainer::value_type>>
   requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
-           !__is_allocator<_MappedContainer>::value &&
            is_invocable_v<const _Compare&,
                           const typename _KeyContainer::value_type&,
                           const typename _KeyContainer::value_type&>)
-flat_multiset(_KeyContainer, _MappedContainer, _Compare = _Compare())
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     _Compare,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _KeyContainer, class _MappedContainer, class _Allocator>
-  requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&
-           !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)
-flat_multiset(_KeyContainer, _MappedContainer, _Allocator)
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     less<typename _KeyContainer::value_type>,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+flat_multiset(_KeyContainer, _Compare = _Compare())
+    -> flat_multiset<typename _KeyContainer::value_type, _Compare, _KeyContainer>;
+
+template <class _KeyContainer, class _Allocator>
+  requires(uses_allocator_v<_KeyContainer, _Allocator> && !__is_allocator<_KeyContainer>::value)
+flat_multiset(_KeyContainer, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type, less<typename _KeyContainer::value_type>, _KeyContainer>;
+
+template <class _KeyContainer, class _Compare, class _Allocator>
   requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
-           !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&
-           uses_allocator_v<_MappedContainer, _Allocator> &&
+           uses_allocator_v<_KeyContainer, _Allocator> &&
            is_invocable_v<const _Compare&,
                           const typename _KeyContainer::value_type&,
                           const typename _KeyContainer::value_type&>)
-flat_multiset(_KeyContainer, _MappedContainer, _Compare, _Allocator)
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     _Compare,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+flat_multiset(_KeyContainer, _Compare, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type, _Compare, _KeyContainer>;
+
+template <class _KeyContainer, class _Compare = less<typename _KeyContainer::value_type>>
   requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
-           !__is_allocator<_MappedContainer>::value &&
            is_invocable_v<const _Compare&,
                           const typename _KeyContainer::value_type&,
                           const typename _KeyContainer::value_type&>)
-flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     _Compare,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _KeyContainer, class _MappedContainer, class _Allocator>
-  requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&
-           !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)
-flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Allocator)
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     less<typename _KeyContainer::value_type>,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+flat_multiset(sorted_equivalent_t, _KeyContainer, _Compare = _Compare())
+    -> flat_multiset<typename _KeyContainer::value_type, _Compare, _KeyContainer>;
+
+template <class _KeyContainer, class _Allocator>
+  requires(uses_allocator_v<_KeyContainer, _Allocator> && !__is_allocator<_KeyContainer>::value)
+flat_multiset(sorted_equivalent_t, _KeyContainer, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type, less<typename _KeyContainer::value_type>, _KeyContainer>;
+
+template <class _KeyContainer, class _Compare, class _Allocator>
   requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
-           !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&
-           uses_allocator_v<_MappedContainer, _Allocator> &&
+           uses_allocator_v<_KeyContainer, _Allocator> &&
            is_invocable_v<const _Compare&,
                           const typename _KeyContainer::value_type&,
                           const typename _KeyContainer::value_type&>)
-flat_multiset(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)
-    -> flat_multiset<typename _KeyContainer::value_type,
-                     typename _MappedContainer::value_type,
-                     _Compare,
-                     _KeyContainer,
-                     _MappedContainer>;
-
-template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+flat_multiset(sorted_equivalent_t, _KeyContainer, _Compare, _Allocator)
+    -> flat_multiset<typename _KeyContainer::value_type, _Compare, _KeyContainer>;
+
+template <class _InputIterator, class _Compare = less<__iter_value_type<_InputIterator>>>
   requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)
 flat_multiset(_InputIterator, _InputIterator, _Compare = _Compare())
-    -> flat_multiset<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+    -> flat_multiset<__iter_value_type<_InputIterator>, _Compare>;
 
-template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+template <class _InputIterator, class _Compare = less<__iter_value_type<_InputIterator>>>
   requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)
 flat_multiset(sorted_equivalent_t, _InputIterator, _InputIterator, _Compare = _Compare())
-    -> flat_multiset<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+    -> flat_multiset<__iter_value_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
 
 template <ranges::input_range _Range,
-          class _Compare   = less<__range_key_type<_Range>>,
+          class _Compare   = less<__iter_value_type<_Range>>,
           class _Allocator = allocator<byte>,
           class            = __enable_if_t<!__is_allocator<_Compare>::value && __is_allocator<_Allocator>::value>>
 flat_multiset(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) -> flat_multiset<
-    __range_key_type<_Range>,
-    __range_mapped_type<_Range>,
+    ranges::range_value_t<_Range>,
     _Compare,
-    vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,
-    vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;
+    vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>>;
 
 template <ranges::input_range _Range, class _Allocator, class = __enable_if_t<__is_allocator<_Allocator>::value>>
 flat_multiset(from_range_t, _Range&&, _Allocator) -> flat_multiset<
-    __range_key_type<_Range>,
-    __range_mapped_type<_Range>,
-    less<__range_key_type<_Range>>,
-    vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,
-    vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;
+    ranges::range_value_t<_Range>,
+    less<ranges::range_value_t<_Range>>,
+    vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, ranges::range_value_t<_Range>>>>;
 
-template <class _Key, class _Tp, class _Compare = less<_Key>>
+template <class _Key, class _Compare = less<_Key>>
   requires(!__is_allocator<_Compare>::value)
-flat_multiset(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_multiset<_Key, _Tp, _Compare>;
+flat_multiset(initializer_list<_Key>, _Compare = _Compare()) -> flat_multiset<_Key, _Compare>;
 
-template <class _Key, class _Tp, class _Compare = less<_Key>>
+template <class _Key, class _Compare = less<_Key>>
   requires(!__is_allocator<_Compare>::value)
-flat_multiset(sorted_equivalent_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
-    -> flat_multiset<_Key, _Tp, _Compare>;
-
-template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Allocator>
-struct uses_allocator<flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>, _Allocator>
-    : bool_constant<uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator>> {};
-
-template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Predicate>
-_LIBCPP_HIDE_FROM_ABI typename flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type
-erase_if(flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __flat_multiset, _Predicate __pred) {
-  auto __zv     = ranges::views::zip(__flat_multiset.__containers_.keys, __flat_multiset.__containers_.values);
-  auto __first  = __zv.begin();
-  auto __last   = __zv.end();
-  auto __guard  = std::__make_exception_guard([&] { __flat_multiset.clear(); });
-  auto __it     = std::remove_if(__first, __last, [&](auto&& __zipped) -> bool {
-    using _Ref = typename flat_multiset<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::const_reference;
-    return __pred(_Ref(std::get<0>(__zipped), std::get<1>(__zipped)));
-  });
-  auto __res    = __last - __it;
-  auto __offset = __it - __first;
-
-  const auto __erase_container = [&](auto& __cont) { __cont.erase(__cont.begin() + __offset, __cont.end()); };
-
-  __erase_container(__flat_multiset.__containers_.keys);
-  __erase_container(__flat_multiset.__containers_.values);
-
+flat_multiset(sorted_equivalent_t, initializer_list<_Key>, _Compare = _Compare()) -> flat_multiset<_Key, _Compare>;
+
+template <class _Key, class _Compare, class _KeyContainer, class _Allocator>
+struct uses_allocator<flat_multiset<_Key, _Compare, _KeyContainer>, _Allocator>
+    : bool_constant<uses_allocator_v<_KeyContainer, _Allocator> > {};
+
+template <class _Key, class _Compare, class _KeyContainer, class _Predicate>
+_LIBCPP_HIDE_FROM_ABI typename flat_multiset<_Key, _Compare, _KeyContainer>::size_type
+erase_if(flat_multiset<_Key, _Compare, _KeyContainer>& __flat_multiset, _Predicate __pred) {
+  auto __guard = std::__make_exception_guard([&] { __flat_multiset.clear(); });
+  auto __it =
+      std::remove_if(__flat_multiset.__keys_.begin(), __flat_multiset.__keys_.end(), [&](const auto& __e) -> bool {
+        return static_cast<bool>(__pred(__e));
+      });
+  auto __res = __flat_multiset.__keys_.end() - __it;
+  __flat_multiset.__keys_.erase(__it, __flat_multiset.__keys_.end());
   __guard.__complete();
   return __res;
 }

>From 285a1e54af704f05b3fddd84513c74a5f6f7a4d1 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 9 Mar 2025 18:25:12 +0000
Subject: [PATCH 08/10] update

---
 libcxx/include/__flat_set/flat_multiset.h     |  14 +-
 .../flat.multiset.cons/alloc.pass.cpp         |  63 ++++
 .../assign_initializer_list.pass.cpp          |  60 ++++
 .../flat.multiset.cons/compare.pass.cpp       |  85 +++++
 .../flat.multiset.cons/containers.pass.cpp    | 162 +++++++++
 .../flat.multiset.cons/copy.pass.cpp          |  70 ++++
 .../flat.multiset.cons/copy_alloc.pass.cpp    |  65 ++++
 .../flat.multiset.cons/copy_assign.pass.cpp   | 101 ++++++
 .../deduct.compile.pass.cpp                   |  43 +++
 .../flat.multiset.cons/deduct.pass.cpp        | 317 +++++++++++++++++
 .../flat.multiset.cons/deduct_pmr.pass.cpp    |  94 +++++
 .../flat.multiset.cons/default.pass.cpp       |  96 ++++++
 .../flat.multiset.cons/dtor_noexcept.pass.cpp |  61 ++++
 .../initializer_list.pass.cpp                 | 155 +++++++++
 .../flat.multiset.cons/iter_iter.pass.cpp     | 140 ++++++++
 .../flat.multiset.cons/move.pass.cpp          | 188 ++++++++++
 .../flat.multiset.cons/move_alloc.pass.cpp    |  79 +++++
 .../flat.multiset.cons/move_assign.pass.cpp   | 241 +++++++++++++
 .../flat.multiset.cons/pmr.pass.cpp           | 326 ++++++++++++++++++
 .../flat.multiset.cons/range.pass.cpp         | 177 ++++++++++
 .../sorted_container.pass.cpp                 | 147 ++++++++
 .../sorted_initializer_list.pass.cpp          | 154 +++++++++
 .../sorted_iter_iter.pass.cpp                 | 160 +++++++++
 23 files changed, 2994 insertions(+), 4 deletions(-)
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/alloc.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/compare.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/containers.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_alloc.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/default.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/dtor_noexcept.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_assign.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/pmr.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/range.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_container.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_initializer_list.pass.cpp
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_iter_iter.pass.cpp

diff --git a/libcxx/include/__flat_set/flat_multiset.h b/libcxx/include/__flat_set/flat_multiset.h
index 6ad62a75da464..9a95347329368 100644
--- a/libcxx/include/__flat_set/flat_multiset.h
+++ b/libcxx/include/__flat_set/flat_multiset.h
@@ -682,6 +682,12 @@ class flat_multiset {
     return std::make_pair(__iter(__key_first), __iter(__key_last));
   }
 
+  _LIBCPP_HIDE_FROM_ABI void __reserve(size_t __size) {
+    if constexpr (requires { __keys_.reserve(__size); }) {
+      __keys_.reserve(__size);
+    }
+  }
+
   template <class _Key2, class _Compare2, class _KeyContainer2, class _Predicate>
   friend typename flat_multiset<_Key2, _Compare2, _KeyContainer2>::size_type
   erase_if(flat_multiset<_Key2, _Compare2, _KeyContainer2>&, _Predicate);
@@ -750,16 +756,16 @@ flat_multiset(_InputIterator, _InputIterator, _Compare = _Compare())
 template <class _InputIterator, class _Compare = less<__iter_value_type<_InputIterator>>>
   requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)
 flat_multiset(sorted_equivalent_t, _InputIterator, _InputIterator, _Compare = _Compare())
-    -> flat_multiset<__iter_value_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+    -> flat_multiset<__iter_value_type<_InputIterator>, _Compare>;
 
 template <ranges::input_range _Range,
-          class _Compare   = less<__iter_value_type<_Range>>,
-          class _Allocator = allocator<byte>,
+          class _Compare   = less<ranges::range_value_t<_Range>>,
+          class _Allocator = allocator<ranges::range_value_t<_Range>>,
           class            = __enable_if_t<!__is_allocator<_Compare>::value && __is_allocator<_Allocator>::value>>
 flat_multiset(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) -> flat_multiset<
     ranges::range_value_t<_Range>,
     _Compare,
-    vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>>;
+    vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, ranges::range_value_t<_Range>>>>;
 
 template <ranges::input_range _Range, class _Allocator, class = __enable_if_t<__is_allocator<_Allocator>::value>>
 flat_multiset(from_range_t, _Range&&, _Allocator) -> flat_multiset<
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/alloc.pass.cpp
new file mode 100644
index 0000000000000..4fffcb304d20a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/alloc.pass.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// template<class Allocator>
+//   explicit flat_multiset(const Allocator& a);
+
+#include <cassert>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+#include "../../../test_compare.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, const A1&>);
+    static_assert(std::is_constructible_v<M2, const A2&>);
+    static_assert(!std::is_constructible_v<M1, const A2&>);
+    static_assert(!std::is_constructible_v<M2, const A1&>);
+  }
+  {
+    // explicit
+    using M = std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>;
+
+    static_assert(std::is_constructible_v<M, test_allocator<int>>);
+    static_assert(!std::is_convertible_v<test_allocator<int>, M>);
+  }
+  {
+    using A = test_allocator<short>;
+    using M = std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>;
+    M m(A(0, 5));
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    assert(std::move(m).extract().get_allocator().get_id() == 5);
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
new file mode 100644
index 0000000000000..071b0be5d15d7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
@@ -0,0 +1,60 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset& operator=(initializer_list<value_type> il);
+
+#include <algorithm>
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <ranges>
+#include <vector>
+
+#include "MinSequenceContainer.h"
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+template <class KeyContainer>
+void test() {
+  using Key = typename KeyContainer::value_type;
+  using M   = std::flat_multiset<Key, std::less<Key>, KeyContainer>;
+  {
+    M m = {8, 10};
+    assert(m.size() == 2);
+    m              = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};
+    int expected[] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6};
+    assert(std::ranges::equal(m, expected));
+  }
+  {
+    M m = {10, 8};
+    assert(m.size() == 2);
+    m                 = {3};
+    double expected[] = {3};
+    assert(std::ranges::equal(m, expected));
+  }
+}
+
+void test() {
+  test<std::vector<int>>();
+  test<std::vector<double>>();
+  test<std::deque<int>>();
+  test<MinSequenceContainer<int>>();
+  test<std::vector<int, min_allocator<int>>>();
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/compare.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/compare.pass.cpp
new file mode 100644
index 0000000000000..6b68589e6814f
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/compare.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// explicit flat_multiset(const key_compare& comp);
+// template <class Alloc>
+//   flat_multiset(const key_compare& comp, const Alloc& a);
+
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, const C&, const A1&>);
+  }
+  {
+    using C = test_less<int>;
+    auto m  = std::flat_multiset<int, C>(C(3));
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    assert(m.key_comp() == C(3));
+  }
+  {
+    // The one-argument ctor is explicit.
+    using C = test_less<int>;
+    static_assert(std::is_constructible_v<std::flat_multiset<int, C>, C>);
+    static_assert(!std::is_convertible_v<C, std::flat_multiset<int, C>>);
+
+    static_assert(std::is_constructible_v<std::flat_multiset<int>, std::less<int>>);
+    static_assert(!std::is_convertible_v<std::less<int>, std::flat_multiset<int>>);
+  }
+  {
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    auto m   = std::flat_multiset<int, C, std::vector<int, A1>>(C(4), A1(5));
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    assert(m.key_comp() == C(4));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // explicit(false)
+    using C                                           = test_less<int>;
+    using A1                                          = test_allocator<int>;
+    std::flat_multiset<int, C, std::deque<int, A1>> m = {C(4), A1(5)};
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    assert(m.key_comp() == C(4));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/containers.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/containers.pass.cpp
new file mode 100644
index 0000000000000..78eac420a8f22
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/containers.pass.cpp
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// explicit flat_multiset(container_type key_cont, const key_compare& comp = key_compare());
+// template<class Allocator>
+//   flat_multiset(const container_type& key_cont, const Allocator& a);
+// template<class Alloc>
+//   flat_multiset(const container_type& key_cont, const key_compare& comp, const Alloc& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "min_allocator.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+template <class T>
+void conversion_test(T);
+
+template <class T, class... Args>
+concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); };
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, const V1&, const A1&>);
+    static_assert(std::is_constructible_v<M2, const V2&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, const V1&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, const V2&, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, const V1&, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, const V2&, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, const V1&, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, const V2&, const C&, const A1&>);
+  }
+  {
+    // flat_multiset(container_type)
+    using M             = std::flat_multiset<int>;
+    std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    auto m              = M(ks);
+    int expected[]      = {1, 1, 1, 2, 2, 2, 3, 3, 3};
+    assert(std::ranges::equal(m, expected));
+
+    // explicit(false)
+    static_assert(std::is_constructible_v<M, const std::vector<int>&>);
+    static_assert(!ImplicitlyConstructible<M, const std::vector<int>&>);
+
+    m = M(std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert(std::ranges::equal(m, expected));
+  }
+  {
+    // flat_multiset(container_type)
+    // move-only
+    int expected[] = {3, 3, 2, 1};
+    using Ks       = std::deque<MoveOnly, min_allocator<MoveOnly>>;
+    using M        = std::flat_multiset<MoveOnly, std::greater<MoveOnly>, Ks>;
+    Ks ks;
+    ks.push_back(1);
+    ks.push_back(3);
+    ks.push_back(3);
+    ks.push_back(2);
+    auto m = M(std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert(std::ranges::equal(m, expected, std::equal_to<>()));
+  }
+  {
+    // flat_multiset(container_type)
+    // container's allocators are used
+    using A = test_allocator<int>;
+    using M = std::flat_multiset<int, std::less<int>, std::deque<int, A>>;
+    auto ks = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
+    auto m  = M(std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert((m == M{1, 1, 1, 2, 2, 2, 3, 3, 3}));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == A(5));
+  }
+  {
+    // flat_multiset(container_type, key_compare)
+    using C             = test_less<int>;
+    using M             = std::flat_multiset<int, C>;
+    std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    auto m              = M(ks, C(4));
+    assert(std::ranges::equal(m, std::vector<int>{1, 1, 1, 2, 2, 2, 3, 3, 3}));
+    assert(m.key_comp() == C(4));
+
+    // explicit
+    static_assert(std::is_constructible_v<M, const std::vector<int>&, const C&>);
+    static_assert(!ImplicitlyConstructible<M, const std::vector<int>&, const C&>);
+  }
+  {
+    // flat_multiset(container_type , const Allocator&)
+    using A = test_allocator<int>;
+    using M = std::flat_multiset<int, std::less<int>, std::deque<int, A>>;
+    auto ks = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
+    auto m  = M(ks, A(4)); // replaces the allocators
+    assert(!ks.empty());   // it was an lvalue above
+    assert((m == M{1, 1, 1, 2, 2, 2, 3, 3, 3}));
+    auto keys = M(m).extract();
+    assert(keys.get_allocator() == A(4));
+
+    // explicit(false)
+    static_assert(ImplicitlyConstructible<M, const std::deque<int, A>&, const A&>);
+    M m2 = {ks, A(4)};   // implicit ctor
+    assert(!ks.empty()); // it was an lvalue above
+    assert(m2 == m);
+    auto keys2 = std::move(m).extract();
+    assert(keys2.get_allocator() == A(4));
+  }
+  {
+    // flat_multiset(container_type , const Allocator&)
+    using C                = test_less<int>;
+    using A                = test_allocator<int>;
+    using M                = std::flat_multiset<int, C, std::vector<int, A>>;
+    std::vector<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    auto m                 = M(ks, C(4), A(5));
+    assert(std::ranges::equal(m, std::vector<int, A>{1, 1, 1, 2, 2, 2, 3, 3, 3}));
+    assert(m.key_comp() == C(4));
+    auto m_copy = m;
+    auto keys   = std::move(m_copy).extract();
+    assert(keys.get_allocator() == A(5));
+
+    // explicit(false)
+    static_assert(ImplicitlyConstructible<M, const std::vector<int, A>&, const A&>);
+    M m2 = {ks, C(4), A(5)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(4));
+    keys = std::move(m2).extract();
+    assert(keys.get_allocator() == A(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy.pass.cpp
new file mode 100644
index 0000000000000..b4f7220e1bac7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(const flat_multiset& m);
+
+#include <algorithm>
+#include <cassert>
+#include <flat_set>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+void test() {
+  {
+    using C = test_less<int>;
+    std::vector<int, test_allocator<int>> ks({1, 3, 5, 3, 1}, test_allocator<int>(6));
+    const int expected[] = {1, 1, 3, 3, 5};
+    using M              = std::flat_multiset<int, C, decltype(ks)>;
+    auto mo              = M(ks, C(5));
+    auto m               = mo;
+
+    assert(m.key_comp() == C(5));
+    assert(std::ranges::equal(m, expected));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == test_allocator<int>(6));
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert(std::ranges::equal(mo, expected));
+    auto keys2 = std::move(mo).extract();
+    assert(keys2.get_allocator() == test_allocator<int>(6));
+  }
+  {
+    using C              = test_less<int>;
+    using Ks             = std::vector<int, other_allocator<int>>;
+    auto ks              = Ks({1, 3, 5, 3, 1}, other_allocator<int>(6));
+    const int expected[] = {1, 1, 3, 3, 5};
+    using M              = std::flat_multiset<int, C, Ks>;
+    auto mo              = M(Ks(ks, other_allocator<int>(6)), C(5));
+    auto m               = mo;
+
+    assert(m.key_comp() == C(5));
+    assert(std::ranges::equal(m, expected));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == other_allocator<int>(-2));
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert(std::ranges::equal(mo, expected));
+    auto keys2 = std::move(mo).extract();
+    assert(keys2.get_allocator() == other_allocator<int>(6));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_alloc.pass.cpp
new file mode 100644
index 0000000000000..0267c9c0a4f52
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_alloc.pass.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(const flat_multiset&, const allocator_type&);
+
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, const M1&, const A1&>);
+    static_assert(std::is_constructible_v<M2, const M2&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, const M1&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, const M2&, const A1&>);
+  }
+  {
+    using C = test_less<int>;
+    std::vector<int, test_allocator<int>> ks({1, 3, 5, 5}, test_allocator<int>(6));
+    using M = std::flat_multiset<int, C, decltype(ks)>;
+    auto mo = M(ks, C(5));
+    auto m  = M(mo, test_allocator<int>(3));
+
+    assert(m.key_comp() == C(5));
+    assert(std::ranges::equal(m, ks));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == test_allocator<int>(3));
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert(std::ranges::equal(mo, ks));
+    auto keys2 = std::move(mo).extract();
+    assert(keys2.get_allocator() == test_allocator<int>(6));
+  }
+}
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
new file mode 100644
index 0000000000000..35e1945f27b18
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset& operator=(const flat_multiset& m);
+
+#include <algorithm>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "operator_hijacker.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+void test() {
+  {
+    // test_allocator is not propagated
+    using C = test_less<int>;
+    std::vector<int, test_allocator<int>> ks({1, 3, 5, 5}, test_allocator<int>(6));
+    using M = std::flat_multiset<int, C, decltype(ks)>;
+    auto mo = M(ks, C(5));
+    auto m  = M({{3, 4, 5, 4}}, C(3), test_allocator<int>(2));
+    m       = mo;
+
+    assert(m.key_comp() == C(5));
+    assert(std::ranges::equal(m, ks));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == test_allocator<int>(2));
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert(std::ranges::equal(mo, ks));
+    auto keys2 = std::move(mo).extract();
+    assert(keys2.get_allocator() == test_allocator<int>(6));
+  }
+  {
+    // other_allocator is propagated
+    using C              = test_less<int>;
+    using Ks             = std::vector<int, other_allocator<int>>;
+    auto ks              = Ks({1, 3, 5, 3}, other_allocator<int>(6));
+    const int expected[] = {1, 3, 3, 5};
+    using M              = std::flat_multiset<int, C, Ks>;
+    auto mo              = M(Ks(ks, other_allocator<int>(6)), C(5));
+    auto m               = M({3, 4, 5}, C(3), other_allocator<int>(2));
+    m                    = mo;
+
+    assert(m.key_comp() == C(5));
+    assert(std::ranges::equal(m, expected));
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == other_allocator<int>(6));
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert(std::ranges::equal(mo, expected));
+    auto keys2 = std::move(mo).extract();
+    assert(keys2.get_allocator() == other_allocator<int>(6));
+  }
+  {
+    // comparator is copied and invariant is preserved
+    using M = std::flat_multiset<int, std::function<bool(int, int)>>;
+    M mo    = M({1, 2}, std::less<int>());
+    M m     = M({1, 2}, std::greater<int>());
+    assert(m.key_comp()(2, 1) == true);
+    assert(m != mo);
+    m = mo;
+    assert(m.key_comp()(2, 1) == false);
+    assert(m == mo);
+  }
+  {
+    // self-assignment
+    using M = std::flat_multiset<int>;
+    M m     = {{1, 2}};
+    m       = static_cast<const M&>(m);
+    assert((m == M{{1, 2}}));
+  }
+  {
+    // Validate whether the container can be copy-assigned (move-assigned, swapped)
+    // with an ADL-hijacking operator&
+    std::flat_multiset<operator_hijacker> so;
+    std::flat_multiset<operator_hijacker> s;
+    s = so;
+    s = std::move(so);
+    swap(s, so);
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
new file mode 100644
index 0000000000000..90bdad29661dd
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// Test CTAD on cases where deduction should fail.
+
+#include <flat_set>
+#include <functional>
+#include <memory>
+#include <utility>
+#include <vector>
+
+struct NotAnAllocator {
+  friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
+};
+
+template <class... Args>
+concept CanDeductFlatSet = requires { std::flat_multiset(std::declval<Args>()...); };
+
+static_assert(CanDeductFlatSet<std::vector<int>>);
+
+// cannot deduce Key and T from nothing
+static_assert(!CanDeductFlatSet<>);
+
+// cannot deduce Key and T from just (Compare)
+static_assert(!CanDeductFlatSet<std::less<int>>);
+
+// cannot deduce Key and T from just (Compare, Allocator)
+static_assert(!CanDeductFlatSet<std::less<int>, std::allocator<int>>);
+
+// cannot deduce Key and T from just (Allocator)
+static_assert(!CanDeductFlatSet<std::allocator<int>>);
+
+// cannot convert from some arbitrary unrelated type
+static_assert(!CanDeductFlatSet<NotAnAllocator>);
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
new file mode 100644
index 0000000000000..a2eec0be83ec5
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
@@ -0,0 +1,317 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+#include <algorithm>
+#include <cassert>
+#include <climits>
+#include <deque>
+#include <initializer_list>
+#include <list>
+#include <flat_set>
+#include <functional>
+#include <ranges>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "deduction_guides_sfinae_checks.h"
+#include "test_allocator.h"
+
+void test_copy() {
+  {
+    std::flat_multiset<long> source = {1, 2, 2};
+    std::flat_multiset s(source);
+    ASSERT_SAME_TYPE(decltype(s), decltype(source));
+    assert(s == source);
+  }
+  {
+    std::flat_multiset<short, std::greater<short>> source = {1, 2, 2};
+    std::flat_multiset s{source}; // braces instead of parens
+    ASSERT_SAME_TYPE(decltype(s), decltype(source));
+    assert(s == source);
+  }
+  {
+    std::flat_multiset<long, std::greater<long>> source = {1, 2, 2};
+    std::flat_multiset s(source, std::allocator<int>());
+    ASSERT_SAME_TYPE(decltype(s), decltype(source));
+    assert(s == source);
+  }
+}
+void test_containers() {
+  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+  std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
+  int expected[] = {1, 1, 2, 3, INT_MAX};
+  {
+    std::flat_multiset s(ks);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+  {
+    std::flat_multiset s(std::sorted_equivalent, sorted_ks);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+  {
+    std::flat_multiset s(ks, test_allocator<long>(0, 44));
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 44);
+  }
+  {
+    std::flat_multiset s(std::sorted_equivalent, sorted_ks, test_allocator<long>(0, 44));
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 44);
+  }
+}
+
+void test_containers_compare() {
+  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+  std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
+  int expected[] = {INT_MAX, 3, 2, 1, 1};
+  {
+    std::flat_multiset s(ks, std::greater<int>());
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+  {
+    std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>());
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+  {
+    std::flat_multiset s(ks, std::greater<int>(), test_allocator<long>(0, 44));
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 44);
+  }
+  {
+    std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>(), test_allocator<long>(0, 44));
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 44);
+  }
+}
+
+void test_iter_iter() {
+  int arr[]               = {1, 2, 1, INT_MAX, 3};
+  int sorted_arr[]        = {1, 1, 2, 3, INT_MAX};
+  const int arrc[]        = {1, 2, 1, INT_MAX, 3};
+  const int sorted_arrc[] = {1, 1, 2, 3, INT_MAX};
+  {
+    std::flat_multiset m(std::begin(arr), std::end(arr));
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::begin(arrc), std::end(arrc));
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr));
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc));
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset<int> mo;
+    std::flat_multiset m(mo.begin(), mo.end());
+    ASSERT_SAME_TYPE(decltype(m), decltype(mo));
+  }
+  {
+    std::flat_multiset<int> mo;
+    std::flat_multiset m(mo.cbegin(), mo.cend());
+    ASSERT_SAME_TYPE(decltype(m), decltype(mo));
+  }
+  {
+    int source[3] = {1, 2, 3};
+    std::flat_multiset s(source, source + 3);
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int>);
+    assert(s.size() == 3);
+  }
+  {
+    // This does not deduce to flat_multiset(InputIterator, InputIterator)
+    // But deduces to flat_multiset(initializer_list<int*>)
+    int source[3]        = {1, 2, 3};
+    std::flat_multiset s = {source, source + 3};
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int*>);
+    assert(s.size() == 2);
+  }
+  {
+    int source[3] = {1, 2, 3};
+    std::flat_multiset s{
+        std::sorted_equivalent, source, source + 3}; // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator)
+    static_assert(std::is_same_v<decltype(s), std::flat_multiset<int>>);
+    assert(s.size() == 3);
+  }
+}
+
+void test_iter_iter_compare() {
+  int arr[]               = {1, 2, 1, INT_MAX, 3};
+  int sorted_arr[]        = {INT_MAX, 3, 2, 1, 1};
+  const int arrc[]        = {1, 2, 1, INT_MAX, 3};
+  const int sorted_arrc[] = {INT_MAX, 3, 2, 1, 1};
+  using C                 = std::greater<long>;
+  {
+    std::flat_multiset m(std::begin(arr), std::end(arr), C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::begin(arrc), std::end(arrc), C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr), C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc), C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset<int> mo;
+    std::flat_multiset m(mo.begin(), mo.end(), C());
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+  }
+  {
+    std::flat_multiset<int> mo;
+    std::flat_multiset m(mo.cbegin(), mo.cend(), C());
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+  }
+}
+
+void test_initializer_list() {
+  const int sorted_arr[] = {1, 1, 2, 3, INT_MAX};
+  {
+    std::flat_multiset m{1, 2, 1, INT_MAX, 3};
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, {1, 1, 2, 3, INT_MAX});
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset s = {1};
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int>);
+    assert(s.size() == 1);
+  }
+  {
+    using M = std::flat_multiset<int>;
+    M m;
+    std::flat_multiset s{m, m}; // flat_multiset(initializer_list<M>)
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<M>);
+    assert(s.size() == 2);
+  }
+}
+
+void test_initializer_list_compare() {
+  const int sorted_arr[] = {INT_MAX, 3, 2, 1, 1};
+  using C                = std::greater<long>;
+  {
+    std::flat_multiset m({1, 2, 1, INT_MAX, 3}, C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+  {
+    std::flat_multiset m(std::sorted_equivalent, {INT_MAX, 3, 2, 1, 1}, C());
+
+    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    assert(std::ranges::equal(m, sorted_arr));
+  }
+}
+
+void test_from_range() {
+  std::list<int> r     = {1, 2, 1, INT_MAX, 3};
+  const int expected[] = {1, 1, 2, 3, INT_MAX};
+  {
+    std::flat_multiset s(std::from_range, r);
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>>);
+    assert(std::ranges::equal(s, expected));
+  }
+  {
+    std::flat_multiset s(std::from_range, r, test_allocator<long>(0, 42));
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+}
+
+void test_from_range_compare() {
+  std::list<int> r     = {1, 2, 1, INT_MAX, 3};
+  const int expected[] = {INT_MAX, 3, 2, 1, 1};
+  {
+    std::flat_multiset s(std::from_range, r, std::greater<int>());
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>>);
+    assert(std::ranges::equal(s, expected));
+  }
+  {
+    std::flat_multiset s(std::from_range, r, std::greater<int>(), test_allocator<long>(0, 42));
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::vector<int, test_allocator<int>>>);
+    assert(std::ranges::equal(s, expected));
+    assert(std::move(s).extract().get_allocator().get_id() == 42);
+  }
+}
+
+void test() {
+  // Each test function also tests the sorted_equivalent-prefixed and allocator-suffixed overloads.
+  test_copy();
+  test_containers();
+  test_containers_compare();
+  test_iter_iter();
+  test_iter_iter_compare();
+  test_initializer_list();
+  test_initializer_list_compare();
+  test_from_range();
+  test_from_range_compare();
+
+  AssociativeContainerDeductionGuidesSfinaeAway<std::flat_multiset, std::flat_multiset<int>>();
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
new file mode 100644
index 0000000000000..ed3649a7301f7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
@@ -0,0 +1,94 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: availability-pmr-missing
+
+// <flat_set>
+
+#include <algorithm>
+#include <cassert>
+#include <climits>
+#include <deque>
+#include <initializer_list>
+#include <list>
+#include <flat_set>
+#include <functional>
+#include <memory_resource>
+#include <ranges>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "test_allocator.h"
+
+using P  = std::pair<int, long>;
+using PC = std::pair<const int, long>;
+
+void test_containers() {
+  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+  std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
+  const int expected[] = {1, 1, 2, 3, INT_MAX};
+  {
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::monotonic_buffer_resource mr2;
+    std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+    std::flat_multiset s(std::move(pks), &mr2);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
+    assert(std::ranges::equal(s, expected));
+    auto keys = std::move(s).extract();
+    assert(keys.get_allocator().resource() == &mr2);
+  }
+  {
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::monotonic_buffer_resource mr2;
+    std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+    std::flat_multiset s(std::sorted_equivalent, std::move(pks), &mr2);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
+    assert(std::ranges::equal(s, expected));
+    auto keys = std::move(s).extract();
+    assert(keys.get_allocator().resource() == &mr2);
+  }
+}
+
+void test_containers_compare() {
+  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+  std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
+  const int expected[] = {INT_MAX, 3, 2, 1, 1};
+  {
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::monotonic_buffer_resource mr2;
+    std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+    std::flat_multiset s(std::move(pks), std::greater<int>(), &mr2);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
+    assert(std::ranges::equal(s, expected));
+    auto keys = std::move(s).extract();
+    assert(keys.get_allocator().resource() == &mr2);
+  }
+  {
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::monotonic_buffer_resource mr2;
+    std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+    std::flat_multiset s(std::sorted_equivalent, std::move(pks), std::greater<int>(), &mr2);
+
+    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
+    assert(std::ranges::equal(s, expected));
+    auto keys = std::move(s).extract();
+    assert(keys.get_allocator().resource() == &mr2);
+  }
+}
+
+int main(int, char**) {
+  test_containers();
+  test_containers_compare();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/default.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/default.pass.cpp
new file mode 100644
index 0000000000000..16f90322cd31a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/default.pass.cpp
@@ -0,0 +1,96 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset();
+
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <type_traits>
+#include <vector>
+
+#include "min_allocator.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+struct DefaultCtableComp {
+  explicit DefaultCtableComp() { default_constructed_ = true; }
+  bool operator()(int, int) const { return false; }
+  bool default_constructed_ = false;
+};
+
+struct ThrowingCtorComp {
+  ThrowingCtorComp() noexcept(false) {}
+  bool operator()(const auto&, const auto&) const { return false; }
+};
+
+void test() {
+  {
+    std::flat_multiset<int> m;
+    assert(m.empty());
+  }
+  {
+    // explicit(false)
+    std::flat_multiset<int> m = {};
+    assert(m.empty());
+  }
+  {
+    std::flat_multiset<int, DefaultCtableComp, std::deque<int, min_allocator<int>>> m;
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    assert(m.key_comp().default_constructed_);
+  }
+  {
+    using A1 = explicit_allocator<int>;
+    {
+      std::flat_multiset<int, DefaultCtableComp, std::vector<int, A1>> m;
+      assert(m.empty());
+      assert(m.key_comp().default_constructed_);
+    }
+    {
+      A1 a1;
+      std::flat_multiset<int, DefaultCtableComp, std::vector<int, A1>> m(a1);
+      assert(m.empty());
+      assert(m.key_comp().default_constructed_);
+    }
+  }
+#if defined(_LIBCPP_VERSION)
+  {
+    using C = std::flat_multiset<MoveOnly>;
+    static_assert(std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+  {
+    using C = std::flat_multiset<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+    static_assert(std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+#endif // _LIBCPP_VERSION
+  {
+    using C = std::flat_multiset<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+    static_assert(!std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+  {
+    using C = std::flat_multiset<MoveOnly, ThrowingCtorComp>;
+    static_assert(!std::is_nothrow_default_constructible_v<C>);
+    C c;
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/dtor_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/dtor_noexcept.pass.cpp
new file mode 100644
index 0000000000000..f852f2f85572c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/dtor_noexcept.pass.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// ~flat_multiset();
+
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "test_macros.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+
+struct ThrowingDtorComp {
+  bool operator()(const auto&, const auto&) const;
+  ~ThrowingDtorComp() noexcept(false) {}
+};
+
+void test() {
+  {
+    using C = std::flat_multiset<MoveOnly, MoveOnly>;
+    static_assert(std::is_nothrow_destructible_v<C>);
+    C c;
+  }
+  {
+    using V = std::vector<MoveOnly, test_allocator<MoveOnly>>;
+    using C = std::flat_multiset<MoveOnly, std::less<MoveOnly>, V>;
+    static_assert(std::is_nothrow_destructible_v<C>);
+    C c;
+  }
+  {
+    using V = std::deque<MoveOnly, other_allocator<MoveOnly>>;
+    using C = std::flat_multiset<MoveOnly, std::greater<MoveOnly>, V>;
+    static_assert(std::is_nothrow_destructible_v<C>);
+    C c;
+  }
+#if defined(_LIBCPP_VERSION)
+  {
+    using C = std::flat_multiset<MoveOnly, ThrowingDtorComp>;
+    static_assert(!std::is_nothrow_destructible_v<C>);
+    C c;
+  }
+#endif // _LIBCPP_VERSION
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
new file mode 100644
index 0000000000000..7a852443512e6
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
@@ -0,0 +1,155 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(initializer_list<value_type> il, const key_compare& comp = key_compare());
+// template<class Alloc>
+//    flat_multiset(initializer_list<value_type> il, const Alloc& a);
+// template<class Alloc>
+//    flat_multiset(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
+
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+#include "../../../test_compare.h"
+
+struct DefaultCtableComp {
+  explicit DefaultCtableComp() { default_constructed_ = true; }
+  bool operator()(int, int) const { return false; }
+  bool default_constructed_ = false;
+};
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    using IL = std::initializer_list<int>;
+    static_assert(std::is_constructible_v<M1, IL, const A1&>);
+    static_assert(std::is_constructible_v<M2, IL, const A2&>);
+    static_assert(!std::is_constructible_v<M1, IL, const A2&>);
+    static_assert(!std::is_constructible_v<M2, IL, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, IL, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, IL, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, IL, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, IL, const C&, const A1&>);
+  }
+
+  {
+    // initializer_list<value_type> needs to match exactly
+    using M = std::flat_multiset<int>;
+    using C = typename M::key_compare;
+    static_assert(std::is_constructible_v<M, std::initializer_list<int>>);
+    static_assert(std::is_constructible_v<M, std::initializer_list<int>, C>);
+    static_assert(std::is_constructible_v<M, std::initializer_list<int>, C, std::allocator<int>>);
+    static_assert(std::is_constructible_v<M, std::initializer_list<int>, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, std::allocator<int>>);
+  }
+
+  int expected[] = {1, 2, 3, 5};
+  {
+    // flat_multiset(initializer_list<value_type>);
+    using M                       = std::flat_multiset<int>;
+    std::initializer_list<int> il = {5, 2, 2, 3, 1, 3};
+    M m(il);
+    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+  }
+  {
+    // flat_multiset(initializer_list<value_type>);
+    // explicit(false)
+    using M = std::flat_multiset<int>;
+    M m     = {5, 2, 2, 3, 1, 3};
+    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+  }
+  {
+    // flat_multiset(initializer_list<value_type>);
+    using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+    M m     = {5, 2, 2, 3, 1, 3};
+    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+  }
+  {
+    using A = explicit_allocator<int>;
+    {
+      // flat_multiset(initializer_list<value_type>);
+      // different comparator
+      using M = std::flat_multiset<int, DefaultCtableComp, std::vector<int, A>>;
+      M m     = {1, 2, 3};
+      assert(m.size() == 1);
+      LIBCPP_ASSERT(*m.begin() == 1);
+      assert(m.key_comp().default_constructed_);
+    }
+    {
+      // flat_multiset(initializer_list<value_type>, const Allocator&);
+      using M = std::flat_multiset<int, std::greater<int>, std::deque<int, A>>;
+      A a;
+      M m({5, 2, 2, 3, 1, 3}, a);
+      assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+    }
+  }
+  {
+    // flat_multiset(initializer_list<value_type>, const key_compare&);
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C>;
+    auto m  = M({5, 2, 2, 3, 1, 3}, C(10));
+    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+    assert(m.key_comp() == C(10));
+
+    // explicit(false)
+    M m2 = {{5, 2, 2, 1, 3, 3}, C(10)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(10));
+  }
+  {
+    // flat_multiset(initializer_list<value_type>, const key_compare&);
+    // Sorting uses the comparator that was passed in
+    using M = std::flat_multiset<int, std::function<bool(int, int)>, std::deque<int, min_allocator<int>>>;
+    auto m  = M({5, 2, 2, 1, 3, 1}, std::greater<int>());
+    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+    assert(m.key_comp()(2, 1) == true);
+  }
+  {
+    // flat_multiset(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
+    using A = explicit_allocator<int>;
+    using M = std::flat_multiset<int, std::greater<int>, std::deque<int, A>>;
+    A a;
+    M m({5, 2, 2, 3, 1, 3}, {}, a);
+    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
new file mode 100644
index 0000000000000..c4ed12ea1f84e
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
@@ -0,0 +1,140 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// template <class InputIterator>
+//   flat_multiset(InputIterator first, InputIterator last, const key_compare& comp = key_compare());
+// template<class InputIterator, class Allocator>
+//   flat_multiset(InputIterator first, InputIterator last, const Allocator& a);
+// template<class InputIterator, class Allocator>
+//   flat_multiset(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C     = test_less<int>;
+    using A1    = test_allocator<int>;
+    using A2    = other_allocator<int>;
+    using V1    = std::vector<int, A1>;
+    using V2    = std::vector<int, A2>;
+    using M1    = std::flat_multiset<int, C, V1>;
+    using M2    = std::flat_multiset<int, C, V2>;
+    using Iter1 = typename M1::iterator;
+    using Iter2 = typename M2::iterator;
+    static_assert(std::is_constructible_v<M1, Iter1, Iter1, const A1&>);
+    static_assert(std::is_constructible_v<M2, Iter2, Iter2, const A2&>);
+    static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const A2&>);
+    static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, Iter1, Iter1, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, Iter2, Iter2, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const C&, const A1&>);
+  }
+
+  int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+  int expected[] = {1, 2, 3};
+  {
+    // flat_multiset(InputIterator , InputIterator)
+    // cpp17_input_iterator
+    using M = std::flat_multiset<int>;
+    auto m  = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
+    assert(std::ranges::equal(m, expected));
+
+    // explicit(false)
+    M m2 = {cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9)};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator)
+    // greater
+    using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+    auto m  = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
+    assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 2, 1}));
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator)
+    // Test when the operands are of array type (also contiguous iterator type)
+    using M = std::flat_multiset<int, std::greater<int>, std::vector<int, min_allocator<int>>>;
+    auto m  = M(ar, ar);
+    assert(m.empty());
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator, const key_compare&)
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::vector<int>>;
+    auto m  = M(ar, ar + 9, C(3));
+    assert(std::ranges::equal(m, expected));
+    assert(m.key_comp() == C(3));
+
+    // explicit(false)
+    M m2 = {ar, ar + 9, C(3)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(3));
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator, const Allocator&)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::vector<int, A1>>;
+    auto m   = M(ar, ar + 9, A1(5));
+    assert(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator, const Allocator&)
+    // explicit(false)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::vector<int, A1>>;
+    M m      = {ar, ar + 9, A1(5)}; // implicit ctor
+    assert(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator, const key_compare&, const Allocator&)
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, C, std::vector<int, A1>>;
+    auto m   = M(ar, ar + 9, C(3), A1(5));
+    assert(std::ranges::equal(m, expected));
+    assert(m.key_comp() == C(3));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(InputIterator , InputIterator, const key_compare&, const Allocator&)
+    // explicit(false)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::deque<int, A1>>;
+    M m      = {ar, ar + 9, {}, A1(5)}; // implicit ctor
+    assert(std::ranges::equal(m, expected));
+    LIBCPP_ASSERT(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move.pass.cpp
new file mode 100644
index 0000000000000..32387054ab0e9
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move.pass.cpp
@@ -0,0 +1,188 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(flat_multiset&&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "../helpers.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+void test() {
+  {
+    using C = test_less<int>;
+    using A = test_allocator<int>;
+    using M = std::flat_multiset<int, C, std::deque<int, A>>;
+    M mo    = M({1, 2, 3}, C(5), A(7));
+    M m     = std::move(mo);
+    assert((m == M{1, 2, 3}));
+    assert(m.key_comp() == C(5));
+    assert(std::move(m).extract().get_allocator() == A(7));
+
+    assert(mo.empty());
+    assert(mo.key_comp() == C(5));
+    assert(std::move(mo).extract().get_allocator().get_id() == test_alloc_base::moved_value);
+  }
+  {
+    using C = test_less<int>;
+    using A = min_allocator<int>;
+    using M = std::flat_multiset<int, C, std::vector<int, A>>;
+    M mo    = M({1, 2, 3}, C(5), A());
+    M m     = std::move(mo);
+    assert((m == M{1, 2, 3}));
+    assert(m.key_comp() == C(5));
+    assert(std::move(m).extract().get_allocator() == A());
+
+    assert(mo.empty());
+    assert(mo.key_comp() == C(5));
+    assert(std::move(mo).extract().get_allocator() == A());
+  }
+  {
+    // A moved-from flat_multiset maintains its class invariant in the presence of moved-from comparators.
+    using M = std::flat_multiset<int, std::function<bool(int, int)>>;
+    M mo    = M({1, 2, 3}, std::less<int>());
+    M m     = std::move(mo);
+    assert(m.size() == 3);
+    assert(std::is_sorted(m.begin(), m.end(), m.value_comp()));
+    assert(m.key_comp()(1, 2) == true);
+
+    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
+    LIBCPP_ASSERT(m.key_comp()(1, 2) == true);
+    LIBCPP_ASSERT(mo.empty());
+    mo.insert({1, 2, 3}); // insert has no preconditions
+    assert(m == mo);
+  }
+  {
+    // moved-from object maintains invariant if the underlying container does not clear after move
+    using M = std::flat_multiset<int, std::less<>, CopyOnlyVector<int>>;
+    M m1    = M({1, 2, 3});
+    M m2    = std::move(m1);
+    assert(m2.size() == 3);
+    check_invariant(m1);
+    LIBCPP_ASSERT(m1.empty());
+    LIBCPP_ASSERT(m1.size() == 0);
+  }
+}
+
+template <class T>
+struct ThrowingMoveAllocator {
+  using value_type                                    = T;
+  explicit ThrowingMoveAllocator()                    = default;
+  ThrowingMoveAllocator(const ThrowingMoveAllocator&) = default;
+  ThrowingMoveAllocator(ThrowingMoveAllocator&&) noexcept(false) {}
+  T* allocate(std::ptrdiff_t n) { return std::allocator<T>().allocate(n); }
+  void deallocate(T* p, std::ptrdiff_t n) { return std::allocator<T>().deallocate(p, n); }
+  friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default;
+};
+
+struct ThrowingMoveComp {
+  ThrowingMoveComp() = default;
+  ThrowingMoveComp(const ThrowingMoveComp&) noexcept(true) {}
+  ThrowingMoveComp(ThrowingMoveComp&&) noexcept(false) {}
+  bool operator()(const auto&, const auto&) const { return false; }
+};
+
+struct MoveSensitiveComp {
+  MoveSensitiveComp() noexcept(false)                  = default;
+  MoveSensitiveComp(const MoveSensitiveComp&) noexcept = default;
+  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
+  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept(false) = default;
+  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
+    rhs.is_moved_from_ = true;
+    return *this;
+  }
+  bool operator()(const auto&, const auto&) const { return false; }
+  bool is_moved_from_ = false;
+};
+
+void test_move_noexcept() {
+  {
+    using C = std::flat_multiset<int>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+  {
+    using C = std::flat_multiset<int, std::less<int>, std::deque<int, test_allocator<int>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+#if _LIBCPP_VERSION
+  {
+    // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators
+    using C = std::flat_multiset<int, std::less<int>, std::deque<int, ThrowingMoveAllocator<int>>>;
+    static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>);
+    static_assert(!std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+#endif // _LIBCPP_VERSION
+  {
+    // Comparator fails to be nothrow-move-constructible
+    using C = std::flat_multiset<int, ThrowingMoveComp>;
+    static_assert(!std::is_nothrow_move_constructible_v<C>);
+    C c;
+    C d = std::move(c);
+  }
+}
+
+#if !defined(TEST_HAS_NO_EXCEPTIONS)
+static int countdown = 0;
+
+struct EvilContainer : std::vector<int> {
+  EvilContainer() = default;
+  EvilContainer(EvilContainer&& rhs) {
+    // Throw on move-construction.
+    if (--countdown == 0) {
+      rhs.insert(rhs.end(), 0);
+      rhs.insert(rhs.end(), 0);
+      throw 42;
+    }
+  }
+};
+
+void test_move_exception() {
+  {
+    using M   = std::flat_multiset<int, std::less<int>, EvilContainer>;
+    M mo      = {1, 2, 3};
+    countdown = 1;
+    try {
+      M m = std::move(mo);
+      assert(false); // not reached
+    } catch (int x) {
+      assert(x == 42);
+    }
+    // The source flat_multiset maintains its class invariant.
+    check_invariant(mo);
+    LIBCPP_ASSERT(mo.empty());
+  }
+}
+#endif // !defined(TEST_HAS_NO_EXCEPTIONS)
+
+int main(int, char**) {
+  test();
+  test_move_noexcept();
+#if !defined(TEST_HAS_NO_EXCEPTIONS)
+  test_move_exception();
+#endif // !defined(TEST_HAS_NO_EXCEPTIONS)
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
new file mode 100644
index 0000000000000..abb7f39687c9b
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(flat_multiset&&, const allocator_type&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <ranges>
+#include <vector>
+
+#include "../helpers.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, M1&&, const A1&>);
+    static_assert(std::is_constructible_v<M2, M2&&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, M1&&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, M2&&, const A1&>);
+  }
+  {
+    int expected[] = {1, 2, 3};
+    using C        = test_less<int>;
+    using A        = test_allocator<int>;
+    using M        = std::flat_multiset<int, C, std::deque<int, A>>;
+    auto mo        = M(expected, expected + 3, C(5), A(7));
+    auto m         = M(std::move(mo), A(3));
+
+    assert(m.key_comp() == C(5));
+    assert(m.size() == 3);
+    auto keys = std::move(m).extract();
+    assert(keys.get_allocator() == A(3));
+    assert(std::ranges::equal(keys, expected));
+
+    // The original flat_multiset is moved-from.
+    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
+    assert(mo.empty());
+    assert(mo.key_comp() == C(5));
+    assert(std::move(mo).extract().get_allocator() == A(7));
+  }
+  {
+    // moved-from object maintains invariant if one of underlying container does not clear after move
+    using M = std::flat_multiset<int, std::less<>, CopyOnlyVector<int>>;
+    M m1    = M({1, 2, 3});
+    M m2(std::move(m1), std::allocator<int>{});
+    assert(m2.size() == 3);
+    check_invariant(m1);
+    LIBCPP_ASSERT(m1.empty());
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_assign.pass.cpp
new file mode 100644
index 0000000000000..58c3338f9b99e
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_assign.pass.cpp
@@ -0,0 +1,241 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset& operator=(flat_multiset&&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+#include "MoveOnly.h"
+#include "../helpers.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+struct MoveNegates {
+  int value_    = 0;
+  MoveNegates() = default;
+  MoveNegates(int v) : value_(v) {}
+  MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
+  MoveNegates& operator=(MoveNegates&& rhs) {
+    value_     = rhs.value_;
+    rhs.value_ = -rhs.value_;
+    return *this;
+  }
+  ~MoveNegates()                             = default;
+  auto operator<=>(const MoveNegates&) const = default;
+};
+
+struct MoveClears {
+  int value_   = 0;
+  MoveClears() = default;
+  MoveClears(int v) : value_(v) {}
+  MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
+  MoveClears& operator=(MoveClears&& rhs) {
+    value_     = rhs.value_;
+    rhs.value_ = 0;
+    return *this;
+  }
+  ~MoveClears()                             = default;
+  auto operator<=>(const MoveClears&) const = default;
+};
+
+#if !defined(TEST_HAS_NO_EXCEPTIONS)
+struct MoveAssignThrows : std::vector<int> {
+  using std::vector<int>::vector;
+  MoveAssignThrows& operator=(MoveAssignThrows&& other) {
+    push_back(0);
+    push_back(0);
+    other.push_back(0);
+    other.push_back(0);
+    throw 42;
+  }
+};
+#endif // TEST_HAS_NO_EXCEPTIONS
+
+void test_move_assign_clears() {
+  // Preserves the class invariant for the moved-from flat_multiset.
+  {
+    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
+    using M              = std::flat_multiset<MoveNegates, std::less<MoveNegates>>;
+    M m                  = M(expected, expected + 8);
+    M m2                 = M(expected, expected + 3);
+
+    m2 = std::move(m);
+
+    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
+    LIBCPP_ASSERT(m.empty());
+    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
+    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
+    m.insert(1);
+    m.insert(2);
+    assert(m.contains(1));
+    assert(m.find(2) != m.end());
+  }
+  {
+    const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
+    using M              = std::flat_multiset<MoveClears, std::less<MoveClears>>;
+    M m                  = M(expected, expected + 8);
+    M m2                 = M(expected, expected + 3);
+
+    m2 = std::move(m);
+
+    assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
+    LIBCPP_ASSERT(m.empty());
+    assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));                // still sorted
+    assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
+    m.insert(1);
+    m.insert(2);
+    assert(m.contains(1));
+    assert(m.find(2) != m.end());
+  }
+  {
+    // moved-from object maintains invariant if one of underlying container does not clear after move
+    using M = std::flat_multiset<int, std::less<>, std::vector<int>>;
+    M m1    = M({1, 2, 3});
+    M m2    = M({1, 2});
+    m2      = std::move(m1);
+    assert(m2.size() == 3);
+    check_invariant(m1);
+    LIBCPP_ASSERT(m1.empty());
+  }
+#if !defined(TEST_HAS_NO_EXCEPTIONS)
+  {
+    using M = std::flat_multiset<int, std::less<>, MoveAssignThrows>;
+    M m1    = {1, 2, 3};
+    M m2    = {1, 2};
+    try {
+      m2 = std::move(m1);
+      assert(false);
+    } catch (int e) {
+      assert(e == 42);
+    }
+    check_invariant(m1);
+    check_invariant(m2);
+    LIBCPP_ASSERT(m1.empty());
+    LIBCPP_ASSERT(m2.empty());
+  }
+#endif // TEST_HAS_NO_EXCEPTIONS
+}
+
+struct MoveSensitiveComp {
+  MoveSensitiveComp() noexcept(false)                         = default;
+  MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default;
+  MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
+  MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default;
+  MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
+    rhs.is_moved_from_ = true;
+    return *this;
+  }
+  bool operator()(const auto&, const auto&) const { return false; }
+  bool is_moved_from_ = false;
+};
+
+struct MoveThrowsComp {
+  MoveThrowsComp(MoveThrowsComp&&) noexcept(false);
+  MoveThrowsComp(const MoveThrowsComp&) noexcept(true);
+  MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false);
+  MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true);
+  bool operator()(const auto&, const auto&) const;
+};
+
+void test_move_assign_no_except() {
+  // This tests a conforming extension
+
+  {
+    using C = std::flat_multiset<int, int>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_multiset<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_multiset<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    using C = std::flat_multiset<int, std::less<int>, std::vector<int, other_allocator<int>>>;
+    LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    // Test with a comparator that throws on move-assignment.
+    using C = std::flat_multiset<int, MoveThrowsComp>;
+    LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
+  }
+  {
+    // Test with a container that throws on move-assignment.
+    using C = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    static_assert(!std::is_nothrow_move_assignable_v<C>);
+  }
+}
+
+void test() {
+  {
+    using C                           = test_less<int>;
+    using A1                          = test_allocator<int>;
+    using M                           = std::flat_multiset<int, C, std::vector<int, A1>>;
+    M mo                              = M({1, 2, 3}, C(5), A1(7));
+    M m                               = M({}, C(3), A1(7));
+    std::same_as<M&> decltype(auto) r = m = std::move(mo);
+    assert(&r == &m);
+    assert((m == M{1, 2, 3}));
+    assert(m.key_comp() == C(5));
+    auto ks = std::move(m).extract();
+    assert(ks.get_allocator() == A1(7));
+    assert(mo.empty());
+  }
+  {
+    using C                           = test_less<int>;
+    using A1                          = other_allocator<int>;
+    using M                           = std::flat_multiset<int, C, std::deque<int, A1>>;
+    M mo                              = M({4, 5}, C(5), A1(7));
+    M m                               = M({1, 2, 3, 4}, C(3), A1(7));
+    std::same_as<M&> decltype(auto) r = m = std::move(mo);
+    assert(&r == &m);
+    assert((m == M{4, 5}));
+    assert(m.key_comp() == C(5));
+    auto ks = std::move(m).extract();
+    assert(ks.get_allocator() == A1(7));
+    assert(mo.empty());
+  }
+  {
+    using A                           = min_allocator<int>;
+    using M                           = std::flat_multiset<int, std::greater<int>, std::vector<int, A>>;
+    M mo                              = M({5, 4, 3}, A());
+    M m                               = M({4, 3, 2, 1}, A());
+    std::same_as<M&> decltype(auto) r = m = std::move(mo);
+    assert(&r == &m);
+    assert((m == M{5, 4, 3}));
+    auto ks = std::move(m).extract();
+    assert(ks.get_allocator() == A());
+    assert(mo.empty());
+  }
+}
+
+int main(int, char**) {
+  test();
+  test_move_assign_clears();
+  test_move_assign_no_except();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/pmr.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/pmr.pass.cpp
new file mode 100644
index 0000000000000..6dcb491fd1d8c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/pmr.pass.cpp
@@ -0,0 +1,326 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: availability-pmr-missing
+
+// <flat_set>
+
+// Test various constructors with pmr
+
+#include <algorithm>
+#include <cassert>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <memory_resource>
+#include <ranges>
+#include <vector>
+#include <string>
+
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "test_allocator.h"
+#include "../../../test_compare.h"
+
+void test() {
+  {
+    // flat_multiset(const Allocator& a);
+    using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::polymorphic_allocator<int> pa = &mr;
+    auto m1                                 = M(pa);
+    assert(m1.empty());
+    assert(std::move(m1).extract().get_allocator() == pa);
+    auto m2 = M(&mr);
+    assert(m2.empty());
+    assert(std::move(m2).extract().get_allocator() == pa);
+  }
+  {
+    // flat_multiset(const key_compare& comp, const Alloc& a);
+    using M = std::flat_multiset<int, std::function<bool(int, int)>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    vm.emplace_back(std::greater<int>());
+    assert(vm[0] == M{});
+    assert(vm[0].key_comp()(2, 1) == true);
+    assert(vm[0].value_comp()(2, 1) == true);
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
+    //          const Allocator& a);
+    using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    std::pmr::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    assert(ks.get_allocator().resource() != &mr);
+    vm.emplace_back(ks);
+    assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above
+    assert((vm[0] == M{1, 2, 3}));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(const flat_multiset&, const allocator_type&);
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr1;
+    std::pmr::monotonic_buffer_resource mr2;
+    M mo = M({1, 2, 3}, C(5), &mr1);
+    M m  = {mo, &mr2}; // also test the implicitness of this constructor
+
+    assert(m.key_comp() == C(5));
+    auto keys = std::move(m).extract();
+    assert((keys == std::pmr::vector<int>{1, 2, 3}));
+    assert(keys.get_allocator().resource() == &mr2);
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    auto keys2 = std::move(mo).extract();
+    assert((keys2 == std::pmr::vector<int>{1, 2, 3}));
+    assert(keys2.get_allocator().resource() == &mr1);
+  }
+  {
+    // flat_multiset(const flat_multiset&, const allocator_type&);
+    using M = std::flat_multiset<int, std::less<>, std::pmr::vector<int>>;
+    std::pmr::vector<M> vs;
+    M m = {1, 2, 3};
+    vs.push_back(m);
+    assert(vs[0] == m);
+  }
+  {
+    // flat_multiset& operator=(const flat_multiset& m);
+    // pmr allocator is not propagated
+    using M = std::flat_multiset<int, std::less<>, std::pmr::deque<int>>;
+    std::pmr::monotonic_buffer_resource mr1;
+    std::pmr::monotonic_buffer_resource mr2;
+    M mo = M({1, 2, 3}, &mr1);
+    M m  = M({4, 5}, &mr2);
+    m    = mo;
+    assert((m == M{1, 2, 3}));
+    assert(std::move(m).extract().get_allocator().resource() == &mr2);
+
+    // mo is unchanged
+    assert((mo == M{1, 2, 3}));
+    assert(std::move(mo).extract().get_allocator().resource() == &mr1);
+  }
+  {
+    // flat_multiset(const flat_multiset& m);
+    using C = test_less<int>;
+    std::pmr::monotonic_buffer_resource mr;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    auto mo = M({1, 2, 3}, C(5), &mr);
+    auto m  = mo;
+
+    assert(m.key_comp() == C(5));
+    assert((m == M{1, 2, 3}));
+    auto ks = std::move(m).extract();
+    assert(ks.get_allocator().resource() == std::pmr::get_default_resource());
+
+    // mo is unchanged
+    assert(mo.key_comp() == C(5));
+    assert((mo == M{1, 2, 3}));
+    auto kso = std::move(mo).extract();
+    assert(kso.get_allocator().resource() == &mr);
+  }
+  {
+    //  flat_multiset(initializer_list<value_type> il, const Alloc& a);
+    using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
+    vm.emplace_back(il);
+    assert((vm[0] == M{1, 3, 4, 5}));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    //  flat_multiset(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::deque<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
+    vm.emplace_back(il, C(5));
+    assert((vm[0] == M{1, 3, 4, 5}));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+    assert(vm[0].key_comp() == C(5));
+  }
+  {
+    // flat_multiset(InputIterator first, InputIterator last, const Allocator& a);
+    int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    int expected[] = {1, 2, 3};
+    {
+      //  cpp17 iterator
+      using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::vector<M> vm(&mr);
+      vm.emplace_back(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
+      assert(std::ranges::equal(vm[0], expected));
+      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+    }
+    {
+      using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::vector<M> vm(&mr);
+      vm.emplace_back(ar, ar);
+      assert(vm[0].empty());
+      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+    }
+  }
+  {
+    // flat_multiset(flat_multiset&&, const allocator_type&);
+    int expected[] = {1, 2, 3};
+    using C        = test_less<int>;
+    using M        = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr1;
+    std::pmr::monotonic_buffer_resource mr2;
+    M mo = M({1, 3, 1, 2}, C(5), &mr1);
+    M m  = {std::move(mo), &mr2}; // also test the implicitness of this constructor
+
+    assert(m.key_comp() == C(5));
+    assert(m.size() == 3);
+    assert(std::equal(m.begin(), m.end(), expected, expected + 3));
+    assert(std::move(m).extract().get_allocator().resource() == &mr2);
+
+    // The original flat_multiset is moved-from.
+    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
+    assert(mo.key_comp() == C(5));
+    assert(std::move(mo).extract().get_allocator().resource() == &mr1);
+  }
+  {
+    // flat_multiset(flat_multiset&&, const allocator_type&);
+    using M = std::flat_multiset<int, std::less<>, std::pmr::deque<int>>;
+    std::pmr::vector<M> vs;
+    M m = {1, 3, 1, 2};
+    vs.push_back(std::move(m));
+    assert((std::move(vs[0]).extract() == std::pmr::deque<int>{1, 2, 3}));
+  }
+  {
+    // flat_multiset& operator=(flat_multiset&&);
+    using M = std::flat_multiset<std::pmr::string, std::less<>, std::pmr::vector<std::pmr::string>>;
+    std::pmr::monotonic_buffer_resource mr1;
+    std::pmr::monotonic_buffer_resource mr2;
+    M mo =
+        M({"short", "very long string that definitely won't fit in the SSO buffer and therefore becomes empty on move"},
+          &mr1);
+    M m = M({"don't care"}, &mr2);
+    m   = std::move(mo);
+    assert(m.size() == 2);
+    assert(std::is_sorted(m.begin(), m.end(), m.value_comp()));
+    assert(m.begin()->get_allocator().resource() == &mr2);
+
+    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
+    mo.insert("foo");
+    assert(mo.begin()->get_allocator().resource() == &mr1);
+  }
+  {
+    //  flat_multiset(from_range_t, R&&, const Alloc&);
+    int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+    int expected[] = {1, 2, 3};
+    {
+      // input_range
+      using M    = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+      using Iter = cpp20_input_iterator<const int*>;
+      using Sent = sentinel_wrapper<Iter>;
+      using R    = std::ranges::subrange<Iter, Sent>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::vector<M> vm(&mr);
+      vm.emplace_back(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
+      assert(std::ranges::equal(vm[0], expected));
+      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+    }
+    {
+      using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+      using R = std::ranges::subrange<const int*>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::vector<M> vm(&mr);
+      vm.emplace_back(std::from_range, R(ar, ar));
+      assert(vm[0].empty());
+      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+    }
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, const container_type& key_cont, const Alloc& a);
+    using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    std::pmr::vector<int> ks = {1, 2, 4, 10};
+    vm.emplace_back(std::sorted_equivalent, ks);
+    assert(!ks.empty()); // it was an lvalue above
+    assert((vm[0] == M{1, 2, 4, 10}));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, const container_type& key_cont,const Alloc& a);
+    using M = std::flat_multiset<int, std::less<int>, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    std::pmr::vector<int> ks({1, 2, 4, 10}, &mr);
+    vm.emplace_back(std::sorted_equivalent, ks);
+    assert((vm[0] == M{1, 2, 4, 10}));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type> il, const Alloc& a);
+    // cpp_17
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    int ar[] = {1, 2, 4, 5};
+    vm.emplace_back(
+        std::sorted_equivalent, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
+    assert((vm[0] == M{1, 2, 4, 5}));
+    assert(vm[0].key_comp() == C(3));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type> il, const Alloc& a);
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    int ar[1] = {42};
+    vm.emplace_back(std::sorted_equivalent, ar, ar, C(4));
+    assert(vm[0] == M{});
+    assert(vm[0].key_comp() == C(4));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(InputIterator first, InputIterator last, const Alloc& a);
+    // cpp_17
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    int ar[] = {1, 2, 4, 5};
+    vm.emplace_back(
+        std::sorted_equivalent, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
+    assert((vm[0] == M{1, 2, 4, 5}));
+    assert(vm[0].key_comp() == C(3));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+  {
+    // flat_multiset(InputIterator first, InputIterator last, const Alloc& a);
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::pmr::vector<int>>;
+    std::pmr::monotonic_buffer_resource mr;
+    std::pmr::vector<M> vm(&mr);
+    int ar[1] = {42};
+    vm.emplace_back(std::sorted_equivalent, ar, ar, C(4));
+    assert(vm[0] == M{});
+    assert(vm[0].key_comp() == C(4));
+    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/range.pass.cpp
new file mode 100644
index 0000000000000..11c981fdecdd1
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/range.pass.cpp
@@ -0,0 +1,177 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// template<container-compatible-range<value_type> R>
+//     flat_multiset(from_range_t, R&&)
+// template<container-compatible-range<value_type> R>
+//     flat_multiset(from_range_t, R&&, const key_compare&)
+// template<container-compatible-range<value_type> R, class Alloc>
+//      flat_multiset(from_range_t, R&&, const Alloc&);
+// template<container-compatible-range<value_type> R, class Alloc>
+//      flat_multiset(from_range_t, R&&, const key_compare&, const Alloc&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <string>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+// test constraint container-compatible-range
+
+template <class V>
+using RangeOf = std::ranges::subrange<V*>;
+using Set     = std::flat_multiset<int>;
+
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>>);
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>>);
+static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>>);
+
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>>);
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::less<int>>);
+static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>>);
+
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::allocator<int>>);
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::allocator<int>>);
+static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::allocator<int>>);
+
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);
+static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);
+static_assert(
+    !std::
+        is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>, std::allocator<int>>);
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A1&>);
+  }
+
+  int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
+  int expected[] = {1, 2, 3};
+  {
+    // flat_multiset(from_range_t, R&&)
+    // input_range && !common
+    using M    = std::flat_multiset<int>;
+    using Iter = cpp20_input_iterator<const int*>;
+    using Sent = sentinel_wrapper<Iter>;
+    using R    = std::ranges::subrange<Iter, Sent>;
+    auto m     = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
+    assert(std::ranges::equal(m, expected));
+    LIBCPP_ASSERT(std::ranges::equal(m, expected));
+
+    // explicit(false)
+    M m2 = {std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(from_range_t, R&&)
+    // greater
+    using M    = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+    using Iter = cpp20_input_iterator<const int*>;
+    using Sent = sentinel_wrapper<Iter>;
+    using R    = std::ranges::subrange<Iter, Sent>;
+    auto m     = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
+    assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 2, 1}));
+  }
+  {
+    // flat_multiset(from_range_t, R&&)
+    // contiguous range
+    using M = std::flat_multiset<int>;
+    using R = std::ranges::subrange<const int*>;
+    auto m  = M(std::from_range, R(ar, ar + 9));
+    assert(std::ranges::equal(m, expected));
+  }
+  {
+    // flat_multiset(from_range_t, R&&, const key_compare&)
+    using C = test_less<int>;
+    using M = std::flat_multiset<int, C, std::vector<int>>;
+    using R = std::ranges::subrange<const int*>;
+    auto m  = M(std::from_range, R(ar, ar + 9), C(3));
+    assert(std::ranges::equal(m, expected));
+    assert(m.key_comp() == C(3));
+
+    // explicit(false)
+    M m2 = {std::from_range, R(ar, ar + 9), C(3)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(3));
+  }
+  {
+    // flat_multiset(from_range_t, R&&, const Allocator&)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::vector<int, A1>>;
+    using R  = std::ranges::subrange<const int*>;
+    auto m   = M(std::from_range, R(ar, ar + 9), A1(5));
+    assert(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(from_range_t, R&&, const Allocator&)
+    // explicit(false)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::deque<int, A1>>;
+    using R  = std::ranges::subrange<const int*>;
+    M m      = {std::from_range, R(ar, ar + 9), A1(5)}; // implicit ctor
+    assert(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(from_range_t, R&&, const key_compare&, const Allocator&)
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, C, std::vector<int, A1>>;
+    using R  = std::ranges::subrange<const int*>;
+    auto m   = M(std::from_range, R(ar, ar + 9), C(3), A1(5));
+    assert(std::ranges::equal(m, expected));
+    assert(m.key_comp() == C(3));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(from_range_t, R&&, const key_compare&, const Allocator&)
+    // explicit(false)
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, std::less<int>, std::deque<int, A1>>;
+    using R  = std::ranges::subrange<const int*>;
+    M m      = {std::from_range, R(ar, ar + 9), {}, A1(5)}; // implicit ctor
+    assert(std::ranges::equal(m, expected));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_container.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_container.pass.cpp
new file mode 100644
index 0000000000000..43595265884e7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_container.pass.cpp
@@ -0,0 +1,147 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// flat_multiset(sorted_equivalent_t, container_type key_cont, const key_compare& comp = key_compare());
+//
+// template<class Alloc>
+//   flat_multiset(sorted_equivalent_t, const container_type& key_cont, const Alloc& a);
+// template<class Alloc>
+//   flat_multiset(sorted_equivalent_t, const container_type& key_cont,
+//            const key_compare& comp, const Alloc& a);
+
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "min_allocator.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, const V1&, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, const V2&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, const V1&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, const V2&, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, const V1&, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, const V2&, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, const V1&, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, const V2&, const C&, const A1&>);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type)
+    using M             = std::flat_multiset<int>;
+    std::vector<int> ks = {1, 2, 4, 10};
+    auto ks2            = ks;
+
+    auto m = M(std::sorted_equivalent, ks);
+    assert((m == M{1, 2, 4, 10}));
+    m = M(std::sorted_equivalent, std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert((m == M{1, 2, 4, 10}));
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, std::move(ks2)};
+    assert(m == m2);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type)
+    // non-default container, comparator and allocator type
+    using Ks = std::deque<int, min_allocator<int>>;
+    using M  = std::flat_multiset<int, std::greater<int>, Ks>;
+    Ks ks    = {10, 4, 2, 1};
+    auto m   = M(std::sorted_equivalent, ks);
+    assert((m == M{1, 2, 4, 10}));
+    m = M(std::sorted_equivalent, std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert((m == M{1, 2, 4, 10}));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type)
+    // allocator copied into the containers
+    using A = test_allocator<int>;
+    using M = std::flat_multiset<int, std::less<int>, std::deque<int, A>>;
+    auto ks = std::deque<int, A>({1, 2, 4, 10}, A(4));
+    auto m  = M(std::sorted_equivalent, std::move(ks));
+    assert(ks.empty()); // it was moved-from
+    assert((m == M{1, 2, 4, 10}));
+    assert(std::move(m).extract().get_allocator() == A(4));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type ,  key_compare)
+    using C             = test_less<int>;
+    using M             = std::flat_multiset<int, C>;
+    std::vector<int> ks = {1, 2, 4, 10};
+
+    auto m = M(std::sorted_equivalent, ks, C(4));
+    assert((m == M{1, 2, 4, 10}));
+    assert(m.key_comp() == C(4));
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, ks, C(4)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(4));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type , key_compare, const Allocator&)
+    using C                = test_less<int>;
+    using A                = test_allocator<int>;
+    using M                = std::flat_multiset<int, C, std::vector<int, A>>;
+    std::vector<int, A> ks = {1, 2, 4, 10};
+    auto m                 = M(std::sorted_equivalent, ks, C(4), A(5));
+    assert((m == M{1, 2, 4, 10}));
+    assert(m.key_comp() == C(4));
+    assert(M(m).extract().get_allocator() == A(5));
+
+    // explicit(false)
+    M m2 = {ks, C(4), A(5)};
+    assert(m2 == m);
+    assert(m2.key_comp() == C(4));
+    assert(std::move(m2).extract().get_allocator() == A(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, container_type , const Allocator&)
+    using A = test_allocator<int>;
+    using M = std::flat_multiset<int, std::less<int>, std::deque<int, A>>;
+    auto ks = std::deque<int, A>({1, 2, 4, 10}, A(4));
+    auto m  = M(std::sorted_equivalent, ks, A(6)); // replaces the allocators
+    assert(!ks.empty());                       // it was an lvalue above
+    assert((m == M{1, 2, 4, 10}));
+    assert(M(m).extract().get_allocator() == A(6));
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, ks, A(6)};
+    assert(m2 == m);
+    assert(std::move(m2).extract().get_allocator() == A(6));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_initializer_list.pass.cpp
new file mode 100644
index 0000000000000..15adf6214a1f2
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_initializer_list.pass.cpp
@@ -0,0 +1,154 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// template <class InputIterator>
+//   flat_multiset(sorted_equivalent_t s, initializer_list<value_type> il,
+//            const key_compare& comp = key_compare())
+// template<class Alloc>
+//   flat_multiset(sorted_equivalent_t, initializer_list<value_type> il, const Alloc& a);
+// template<class Alloc>
+//   flat_multiset(sorted_equivalent_t, initializer_list<value_type> il,
+//            const key_compare& comp, const Alloc& a);
+
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+template <class T>
+std::initializer_list<T> il = {1, 2, 4, 5};
+
+void test() {
+  const auto il1 = il<int>;
+  const auto il2 = il<short>;
+
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using A2 = other_allocator<int>;
+    using V1 = std::vector<int, A1>;
+    using V2 = std::vector<int, A2>;
+    using M1 = std::flat_multiset<int, C, V1>;
+    using M2 = std::flat_multiset<int, C, V2>;
+    using IL = std::initializer_list<int>;
+
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, IL, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, IL, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, IL, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, IL, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, IL, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, IL, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, IL, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, IL, const C&, const A1&>);
+  }
+  {
+    // initializer_list<value_type> needs to match exactly
+    using M = std::flat_multiset<int>;
+    using C = typename M::key_compare;
+    static_assert(std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<int>>);
+    static_assert(std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<int>, C>);
+    static_assert(std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<int>, C, std::allocator<int>>);
+    static_assert(std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<int>, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>>);
+    static_assert(!std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, C>);
+    static_assert(
+        !std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, C, std::allocator<int>>);
+    static_assert(
+        !std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, std::allocator<int>>);
+    static_assert(!std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>>);
+    static_assert(!std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, C>);
+    static_assert(
+        !std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, C, std::allocator<int>>);
+    static_assert(
+        !std::is_constructible_v<M, std::sorted_equivalent_t, std::initializer_list<const int>, std::allocator<int>>);
+  }
+
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>);
+    using M       = std::flat_multiset<int>;
+    auto m        = M(std::sorted_equivalent, il1);
+    auto expected = M{1, 2, 4, 5};
+    assert(m == expected);
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, il1};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>, const key_compare&);
+    using M = std::flat_multiset<int, std::function<bool(int, int)>>;
+    auto m  = M(std::sorted_equivalent, il1, std::less<int>());
+    assert(m == M({1, 2, 4, 5}, std::less<>()));
+    assert(m.key_comp()(1, 2) == true);
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, il1, std::less<int>()};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>, const key_compare&);
+    // greater
+    using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+    std::initializer_list<int> il4{5, 4, 2, 1};
+    auto m = M(std::sorted_equivalent, il4, std::greater<int>());
+    assert((m == M{5, 4, 2, 1}));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>,  const Allocator&)
+    using A1      = test_allocator<short>;
+    using M       = std::flat_multiset<short, std::less<int>, std::deque<short, A1>>;
+    auto m        = M(std::sorted_equivalent, il2, A1(5));
+    auto expected = M{1, 2, 4, 5};
+    assert(m == expected);
+    assert(M(m).extract().get_allocator() == A1(5));
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, il2, A1(5)};
+    assert(m2 == m);
+    assert(std::move(m2).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>, const key_compare&, const Allocator&);
+    using C  = test_less<int>;
+    using A1 = test_allocator<short>;
+    using M  = std::flat_multiset<short, C, std::vector<short, A1>>;
+    auto m   = M(std::sorted_equivalent, il2, C(3), A1(5));
+    assert((m == M{1, 2, 4, 5}));
+    assert(m.key_comp() == C(3));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, initializer_list<value_type>, const key_compare&, const Allocator&);
+    // explicit(false)
+    using A1 = test_allocator<short>;
+    using M  = std::flat_multiset<short, std::less<int>, std::deque<short, A1>>;
+    M m      = {std::sorted_equivalent, il2, {}, A1(5)}; // implicit ctor
+    assert((m == M{1, 2, 4, 5}));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_iter_iter.pass.cpp
new file mode 100644
index 0000000000000..a73080d5a7869
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/sorted_iter_iter.pass.cpp
@@ -0,0 +1,160 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// template <class InputIterator>
+//   flat_multiset(sorted_equivalent_t, InputIterator first, InputIterator last, const key_compare& comp = key_compare());
+// template<class InputIterator, class Alloc>
+//   flat_multiset(sorted_equivalent_t, InputIterator first, InputIterator last, const Alloc& a);
+// template<class InputIterator, class Allocator>
+//   flat_multiset(sorted_equivalent_t, InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
+
+#include <deque>
+#include <flat_set>
+#include <functional>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+void test() {
+  {
+    // The constructors in this subclause shall not participate in overload
+    // resolution unless uses_allocator_v<container_type, Alloc> is true.
+
+    using C     = test_less<int>;
+    using A1    = test_allocator<int>;
+    using A2    = other_allocator<int>;
+    using V1    = std::vector<int, A1>;
+    using V2    = std::vector<int, A2>;
+    using M1    = std::flat_multiset<int, C, V1>;
+    using M2    = std::flat_multiset<int, C, V2>;
+    using Iter1 = typename M1::iterator;
+    using Iter2 = typename M2::iterator;
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, Iter1, Iter1, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, Iter2, Iter2, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, Iter1, Iter1, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, Iter2, Iter2, const A1&>);
+
+    static_assert(std::is_constructible_v<M1, std::sorted_equivalent_t, Iter1, Iter1, const C&, const A1&>);
+    static_assert(std::is_constructible_v<M2, std::sorted_equivalent_t, Iter2, Iter2, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M1, std::sorted_equivalent_t, Iter1, Iter1, const C&, const A2&>);
+    static_assert(!std::is_constructible_v<M2, std::sorted_equivalent_t, Iter2, Iter2, const C&, const A1&>);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator);
+    // cpp17_input_iterator
+    using M  = std::flat_multiset<int>;
+    int ar[] = {1, 2, 4, 5};
+    auto m   = M(std::sorted_equivalent, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4));
+    auto expected = M{1, 2, 4, 5};
+    assert(m == expected);
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4)};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator);
+    // contiguous iterator
+    using C       = test_less<int>;
+    using M       = std::flat_multiset<int, C, std::vector<int, min_allocator<int>>>;
+    int ar[]      = {1, 2, 4, 5};
+    auto m        = M(std::sorted_equivalent, ar, ar + 4);
+    auto expected = M{1, 2, 4, 5};
+    assert(m == expected);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, const key_compare&);
+    // cpp_17_input_iterator
+    using M  = std::flat_multiset<int, std::function<bool(int, int)>>;
+    int ar[] = {1, 2, 4, 5};
+    auto m   = M(std::sorted_equivalent,
+               cpp17_input_iterator<const int*>(ar),
+               cpp17_input_iterator<const int*>(ar + 4),
+               std::less<int>());
+    assert(m == M({1, 2, 4, 5}, std::less<>()));
+    assert(m.key_comp()(1, 2) == true);
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent,
+            cpp17_input_iterator<const int*>(ar),
+            cpp17_input_iterator<const int*>(ar + 4),
+            std::less<int>()};
+    assert(m2 == m);
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, const key_compare&);
+    // greater
+    using M  = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+    int ar[] = {5, 4, 2, 1};
+    auto m   = M(std::sorted_equivalent,
+               cpp17_input_iterator<const int*>(ar),
+               cpp17_input_iterator<const int*>(ar + 4),
+               std::greater<int>());
+    assert((m == M{5, 4, 2, 1}));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, const key_compare&);
+    // contiguous iterator
+    using C   = test_less<int>;
+    using M   = std::flat_multiset<int, C, std::vector<int, min_allocator<int>>>;
+    int ar[1] = {42};
+    auto m    = M(std::sorted_equivalent, ar, ar, C(5));
+    assert(m.empty());
+    assert(m.key_comp() == C(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator , InputIterator, const Allocator&)
+    using A1      = test_allocator<int>;
+    using M       = std::flat_multiset<int, std::less<int>, std::vector<int, A1>>;
+    int ar[]      = {1, 2, 4, 5};
+    auto m        = M(std::sorted_equivalent, ar, ar + 4, A1(5));
+    auto expected = M{1, 2, 4, 5};
+    assert(m == expected);
+    assert(M(m).extract().get_allocator() == A1(5));
+
+    // explicit(false)
+    M m2 = {std::sorted_equivalent, ar, ar + 4, A1(5)};
+    assert(m2 == m);
+    assert(std::move(m2).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, const key_compare&, const Allocator&);
+    using C  = test_less<int>;
+    using A1 = test_allocator<int>;
+    using M  = std::flat_multiset<int, C, std::deque<int, A1>>;
+    int ar[] = {1, 2, 4, 5};
+    auto m   = M(std::sorted_equivalent, ar, ar + 4, C(3), A1(5));
+    assert((m == M{1, 2, 4, 5}));
+    assert(m.key_comp() == C(3));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+  {
+    // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, const key_compare&, const Allocator&);
+    // explicit(false)
+    using A1 = test_allocator<short>;
+    using M  = std::flat_multiset<short, std::less<int>, std::deque<short, A1>>;
+    int ar[] = {1, 2, 4, 5};
+    M m      = {std::sorted_equivalent, ar, ar + 4, {}, A1(5)}; // implicit ctor
+    assert((m == M{1, 2, 4, 5}));
+    assert(std::move(m).extract().get_allocator() == A1(5));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}

>From 61f4722fc5c15c0b2bbede69d327c88172634227 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 15 Mar 2025 16:23:38 +0000
Subject: [PATCH 09/10] more tests

---
 .../initializer_list.pass.cpp                 | 21 ++++++++++---------
 .../flat.multiset.cons/iter_iter.pass.cpp     |  5 +++--
 .../flat.multiset.cons/move_alloc.pass.cpp    | 12 +++++------
 3 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
index 7a852443512e6..b33994357ca3b 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/initializer_list.pass.cpp
@@ -22,6 +22,7 @@
 #include <functional>
 #include <type_traits>
 #include <vector>
+#include <ranges>
 
 #include "test_macros.h"
 #include "min_allocator.h"
@@ -77,26 +78,26 @@ void test() {
     static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, std::allocator<int>>);
   }
 
-  int expected[] = {1, 2, 3, 5};
+  int expected[] = {1, 2, 2, 3, 3, 5};
   {
     // flat_multiset(initializer_list<value_type>);
     using M                       = std::flat_multiset<int>;
     std::initializer_list<int> il = {5, 2, 2, 3, 1, 3};
     M m(il);
-    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+    assert(std::ranges::equal(m, expected));
   }
   {
     // flat_multiset(initializer_list<value_type>);
     // explicit(false)
     using M = std::flat_multiset<int>;
     M m     = {5, 2, 2, 3, 1, 3};
-    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+    assert(std::ranges::equal(m, expected));
   }
   {
     // flat_multiset(initializer_list<value_type>);
     using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
     M m     = {5, 2, 2, 3, 1, 3};
-    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+    assert(std::ranges::equal(m, expected | std::views::reverse));
   }
   {
     using A = explicit_allocator<int>;
@@ -105,7 +106,7 @@ void test() {
       // different comparator
       using M = std::flat_multiset<int, DefaultCtableComp, std::vector<int, A>>;
       M m     = {1, 2, 3};
-      assert(m.size() == 1);
+      assert(m.size() == 3);
       LIBCPP_ASSERT(*m.begin() == 1);
       assert(m.key_comp().default_constructed_);
     }
@@ -114,7 +115,7 @@ void test() {
       using M = std::flat_multiset<int, std::greater<int>, std::deque<int, A>>;
       A a;
       M m({5, 2, 2, 3, 1, 3}, a);
-      assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+      assert(std::ranges::equal(m, expected | std::views::reverse));
     }
   }
   {
@@ -122,7 +123,7 @@ void test() {
     using C = test_less<int>;
     using M = std::flat_multiset<int, C>;
     auto m  = M({5, 2, 2, 3, 1, 3}, C(10));
-    assert(std::equal(m.begin(), m.end(), expected, expected + 4));
+    assert(std::ranges::equal(m, expected));
     assert(m.key_comp() == C(10));
 
     // explicit(false)
@@ -134,8 +135,8 @@ void test() {
     // flat_multiset(initializer_list<value_type>, const key_compare&);
     // Sorting uses the comparator that was passed in
     using M = std::flat_multiset<int, std::function<bool(int, int)>, std::deque<int, min_allocator<int>>>;
-    auto m  = M({5, 2, 2, 1, 3, 1}, std::greater<int>());
-    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+    auto m  = M({5, 2, 2, 1, 3, 3}, std::greater<int>());
+    assert(std::ranges::equal(m, expected | std::views::reverse));
     assert(m.key_comp()(2, 1) == true);
   }
   {
@@ -144,7 +145,7 @@ void test() {
     using M = std::flat_multiset<int, std::greater<int>, std::deque<int, A>>;
     A a;
     M m({5, 2, 2, 3, 1, 3}, {}, a);
-    assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4));
+    assert(std::ranges::equal(m, expected | std::views::reverse));
   }
 }
 
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
index c4ed12ea1f84e..7a07b5283fe1b 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
@@ -21,6 +21,7 @@
 #include <deque>
 #include <flat_set>
 #include <functional>
+#include <ranges>
 #include <vector>
 
 #include "min_allocator.h"
@@ -55,7 +56,7 @@ void test() {
   }
 
   int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
-  int expected[] = {1, 2, 3};
+  int expected[] = {1, 1,1,2,2,2,3,3,3};
   {
     // flat_multiset(InputIterator , InputIterator)
     // cpp17_input_iterator
@@ -72,7 +73,7 @@ void test() {
     // greater
     using M = std::flat_multiset<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
     auto m  = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
-    assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 2, 1}));
+    assert(std::ranges::equal(m, expected | std::views::reverse));
   }
   {
     // flat_multiset(InputIterator , InputIterator)
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
index abb7f39687c9b..ee8258e5ac846 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/move_alloc.pass.cpp
@@ -42,15 +42,15 @@ void test() {
     static_assert(!std::is_constructible_v<M2, M2&&, const A1&>);
   }
   {
-    int expected[] = {1, 2, 3};
+    int expected[] = {1, 1, 2, 2, 3};
     using C        = test_less<int>;
     using A        = test_allocator<int>;
     using M        = std::flat_multiset<int, C, std::deque<int, A>>;
-    auto mo        = M(expected, expected + 3, C(5), A(7));
+    auto mo        = M(expected, expected + 5, C(5), A(7));
     auto m         = M(std::move(mo), A(3));
 
     assert(m.key_comp() == C(5));
-    assert(m.size() == 3);
+    assert(m.size() == 5);
     auto keys = std::move(m).extract();
     assert(keys.get_allocator() == A(3));
     assert(std::ranges::equal(keys, expected));
@@ -64,10 +64,10 @@ void test() {
   {
     // moved-from object maintains invariant if one of underlying container does not clear after move
     using M = std::flat_multiset<int, std::less<>, CopyOnlyVector<int>>;
-    M m1    = M({1, 2, 3});
+    M m1    = M({1, 2, 2, 1, 3});
     M m2(std::move(m1), std::allocator<int>{});
-    assert(m2.size() == 3);
-    check_invariant(m1);
+    assert(m2.size() == 5);
+    assert(std::ranges::is_sorted(m1));
     LIBCPP_ASSERT(m1.empty());
   }
 }

>From 437a6435b2dde9a7b99ad12d91a5b22311ce7300 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 23 Mar 2025 14:45:49 +0000
Subject: [PATCH 10/10] testing

---
 .../assign_initializer_list.pass.cpp          |   8 +
 .../flat.multiset.cons/copy_assign.pass.cpp   |  11 +-
 .../deduct.compile.pass.cpp                   |  14 +-
 .../flat.multiset.cons/deduct.pass.cpp        | 615 ++++++++++--------
 .../flat.multiset.cons/deduct_pmr.pass.cpp    | 112 ++--
 .../flat.multiset.cons/iter_iter.pass.cpp     |   2 +-
 .../flat.multiset/helpers.h                   | 307 +++++++++
 7 files changed, 748 insertions(+), 321 deletions(-)
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multiset/helpers.h

diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
index 071b0be5d15d7..ae81ab044932d 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/assign_initializer_list.pass.cpp
@@ -43,6 +43,14 @@ void test() {
     double expected[] = {3};
     assert(std::ranges::equal(m, expected));
   }
+  {
+    // was empty
+    M m;
+    assert(m.size() == 0);
+    m              = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};
+    int expected[] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6};
+    assert(std::ranges::equal(m, expected));
+  }
 }
 
 void test() {
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
index 35e1945f27b18..2b6176ac915a7 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/copy_assign.pass.cpp
@@ -80,9 +80,18 @@ void test() {
     // self-assignment
     using M = std::flat_multiset<int>;
     M m     = {{1, 2}};
-    m       = static_cast<const M&>(m);
+    m       = std::as_const(m);
     assert((m == M{{1, 2}}));
   }
+  {
+    // was empty
+    using M = std::flat_multiset<int>;
+    M m;
+    assert(m.size() == 0);
+    m              = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};
+    int expected[] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6};
+    assert(std::ranges::equal(m, expected));
+  }
   {
     // Validate whether the container can be copy-assigned (move-assigned, swapped)
     // with an ADL-hijacking operator&
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
index 90bdad29661dd..f26c90bacdda7 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.compile.pass.cpp
@@ -23,21 +23,21 @@ struct NotAnAllocator {
 };
 
 template <class... Args>
-concept CanDeductFlatSet = requires { std::flat_multiset(std::declval<Args>()...); };
+concept CanDeductFlatMultiSet = requires { std::flat_multiset(std::declval<Args>()...); };
 
-static_assert(CanDeductFlatSet<std::vector<int>>);
+static_assert(CanDeductFlatMultiSet<std::vector<int>>);
 
 // cannot deduce Key and T from nothing
-static_assert(!CanDeductFlatSet<>);
+static_assert(!CanDeductFlatMultiSet<>);
 
 // cannot deduce Key and T from just (Compare)
-static_assert(!CanDeductFlatSet<std::less<int>>);
+static_assert(!CanDeductFlatMultiSet<std::less<int>>);
 
 // cannot deduce Key and T from just (Compare, Allocator)
-static_assert(!CanDeductFlatSet<std::less<int>, std::allocator<int>>);
+static_assert(!CanDeductFlatMultiSet<std::less<int>, std::allocator<int>>);
 
 // cannot deduce Key and T from just (Allocator)
-static_assert(!CanDeductFlatSet<std::allocator<int>>);
+static_assert(!CanDeductFlatMultiSet<std::allocator<int>>);
 
 // cannot convert from some arbitrary unrelated type
-static_assert(!CanDeductFlatSet<NotAnAllocator>);
+static_assert(!CanDeductFlatMultiSet<NotAnAllocator>);
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
index a2eec0be83ec5..7f611776e85c3 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct.pass.cpp
@@ -26,286 +26,379 @@
 #include "deduction_guides_sfinae_checks.h"
 #include "test_allocator.h"
 
-void test_copy() {
+void test() {
   {
+    // Deduction guide generated from
+    // flat_multiset(const flat_multiset&)
     std::flat_multiset<long> source = {1, 2, 2};
     std::flat_multiset s(source);
     ASSERT_SAME_TYPE(decltype(s), decltype(source));
     assert(s == source);
   }
   {
+    // Deduction guide generated from
+    // flat_multiset(const flat_multiset&)
+    // braces instead of parens
     std::flat_multiset<short, std::greater<short>> source = {1, 2, 2};
-    std::flat_multiset s{source}; // braces instead of parens
+    std::flat_multiset s{source};
     ASSERT_SAME_TYPE(decltype(s), decltype(source));
     assert(s == source);
   }
   {
+    // Deduction guide generated from
+    // flat_set(const flat_set&, const Allocator&)
     std::flat_multiset<long, std::greater<long>> source = {1, 2, 2};
     std::flat_multiset s(source, std::allocator<int>());
     ASSERT_SAME_TYPE(decltype(s), decltype(source));
     assert(s == source);
   }
-}
-void test_containers() {
-  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
-  std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
-  int expected[] = {1, 1, 2, 3, INT_MAX};
-  {
-    std::flat_multiset s(ks);
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-  {
-    std::flat_multiset s(std::sorted_equivalent, sorted_ks);
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-  {
-    std::flat_multiset s(ks, test_allocator<long>(0, 44));
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 44);
-  }
-  {
-    std::flat_multiset s(std::sorted_equivalent, sorted_ks, test_allocator<long>(0, 44));
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 44);
-  }
-}
-
-void test_containers_compare() {
-  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
-  std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
-  int expected[] = {INT_MAX, 3, 2, 1, 1};
-  {
-    std::flat_multiset s(ks, std::greater<int>());
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-  {
-    std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>());
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-  {
-    std::flat_multiset s(ks, std::greater<int>(), test_allocator<long>(0, 44));
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 44);
-  }
-  {
-    std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>(), test_allocator<long>(0, 44));
-
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 44);
-  }
-}
-
-void test_iter_iter() {
-  int arr[]               = {1, 2, 1, INT_MAX, 3};
-  int sorted_arr[]        = {1, 1, 2, 3, INT_MAX};
-  const int arrc[]        = {1, 2, 1, INT_MAX, 3};
-  const int sorted_arrc[] = {1, 1, 2, 3, INT_MAX};
-  {
-    std::flat_multiset m(std::begin(arr), std::end(arr));
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::begin(arrc), std::end(arrc));
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr));
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
   {
-    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc));
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset<int> mo;
-    std::flat_multiset m(mo.begin(), mo.end());
-    ASSERT_SAME_TYPE(decltype(m), decltype(mo));
-  }
-  {
-    std::flat_multiset<int> mo;
-    std::flat_multiset m(mo.cbegin(), mo.cend());
-    ASSERT_SAME_TYPE(decltype(m), decltype(mo));
-  }
-  {
-    int source[3] = {1, 2, 3};
-    std::flat_multiset s(source, source + 3);
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int>);
-    assert(s.size() == 3);
+    std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+    std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
+    int expected[] = {1, 1, 2, 3, INT_MAX};
+    {
+      // template<class KeyContainer, class Compare = less<typename KeyContainer::value_type>>
+      // flat_multiset(KeyContainer, Compare = Compare())
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::flat_multiset s(ks);
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
+    {
+      // template<class KeyContainer, class Compare = less<typename KeyContainer::value_type>>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Compare = Compare())
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::flat_multiset s(std::sorted_equivalent, sorted_ks);
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
+    {
+      // template<class KeyContainer, class Allocator>
+      // flat_multiset(KeyContainer, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type,
+      //                    less<typename KeyContainer::value_type>, KeyContainer>;
+      std::flat_multiset s(ks, test_allocator<long>(0, 44));
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 44);
+    }
+    {
+      // template<class KeyContainer, class Allocator>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type,
+      //                    less<typename KeyContainer::value_type>, KeyContainer>;
+      std::flat_multiset s(std::sorted_equivalent, sorted_ks, test_allocator<long>(0, 44));
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 44);
+    }
+  }
+  {
+    std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+    std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
+    int expected[] = {INT_MAX, 3, 2, 1, 1};
+    {
+      // template<class KeyContainer, class Compare = less<typename KeyContainer::value_type>>
+      // flat_multiset(KeyContainer, Compare = Compare())
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::flat_multiset s(ks, std::greater<int>());
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
+    {
+      // template<class KeyContainer, class Compare = less<typename KeyContainer::value_type>>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Compare = Compare())
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+
+      std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>());
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
+    {
+      // template<class KeyContainer, class Compare, class Allocator>
+      // flat_multiset(KeyContainer, Compare, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::flat_multiset s(ks, std::greater<int>(), test_allocator<long>(0, 44));
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 44);
+    }
+    {
+      // template<class KeyContainer, class Compare, class Allocator>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Compare, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::flat_multiset s(std::sorted_equivalent, sorted_ks, std::greater<int>(), test_allocator<long>(0, 44));
+
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, decltype(ks)>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 44);
+    }
+  }
+
+  {
+    int arr[]               = {1, 2, 1, INT_MAX, 3};
+    int sorted_arr[]        = {1, 1, 2, 3, INT_MAX};
+    const int arrc[]        = {1, 2, 1, INT_MAX, 3};
+    const int sorted_arrc[] = {1, 1, 2, 3, INT_MAX};
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      std::flat_multiset m(std::begin(arr), std::end(arr));
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // const
+      std::flat_multiset m(std::begin(arrc), std::end(arrc));
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr));
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // const
+      std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc));
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // flat_multiset iterator
+      std::flat_multiset<int> mo;
+      std::flat_multiset m(mo.begin(), mo.end());
+      ASSERT_SAME_TYPE(decltype(m), decltype(mo));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // flat_multiset const_iterator
+      std::flat_multiset<int> mo;
+      std::flat_multiset m(mo.cbegin(), mo.cend());
+      ASSERT_SAME_TYPE(decltype(m), decltype(mo));
+    }
+    {
+      // This does not deduce to flat_multiset(InputIterator, InputIterator)
+      // But deduces to flat_multiset(initializer_list<int*>)
+      int source[3]        = {1, 2, 3};
+      std::flat_multiset s = {source, source + 3};
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int*>);
+      assert(s.size() == 2);
+    }
+    {
+      // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator)
+      // braces
+      int source[3] = {1, 2, 3};
+      std::flat_multiset s{std::sorted_equivalent, source, source + 3};
+      static_assert(std::is_same_v<decltype(s), std::flat_multiset<int>>);
+      assert(s.size() == 3);
+    }
+  }
+
+  {
+    int arr[]               = {1, 2, 1, INT_MAX, 3};
+    int sorted_arr[]        = {INT_MAX, 3, 2, 1, 1};
+    const int arrc[]        = {1, 2, 1, INT_MAX, 3};
+    const int sorted_arrc[] = {INT_MAX, 3, 2, 1, 1};
+    using C                 = std::greater<long>;
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      std::flat_multiset m(std::begin(arr), std::end(arr), C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // const
+      std::flat_multiset m(std::begin(arrc), std::end(arrc), C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr), C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // const
+      std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc), C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // flat_multiset iterator
+      std::flat_multiset<int> mo;
+      std::flat_multiset m(mo.begin(), mo.end(), C());
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    }
+    {
+      // template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>>
+      // flat_multiset(InputIterator, InputIterator, Compare = Compare())
+      //   -> flat_multiset<iter-value-type<InputIterator>, Compare>;
+      // flat_multiset const_iterator
+      std::flat_multiset<int> mo;
+      std::flat_multiset m(mo.cbegin(), mo.cend(), C());
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+    }
+  }
+  {
+    const int sorted_arr[] = {1, 1, 2, 3, INT_MAX};
+    {
+      // template<class Key, class Compare = less<Key>>
+      // flat_multiset(initializer_list<Key>, Compare = Compare())
+      //   -> flat_multiset<Key, Compare>;
+      std::flat_multiset m{1, 2, 1, INT_MAX, 3};
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class Key, class Compare = less<Key>>
+      // flat_multiset(sorted_equivalent_t, initializer_list<Key>, Compare = Compare())
+      //     -> flat_multiset<Key, Compare>;
+      std::flat_multiset m(std::sorted_equivalent, {1, 1, 2, 3, INT_MAX});
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // One element with brace was treated as initializer_list
+      std::flat_multiset s = {1};
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int>);
+      assert(s.size() == 1);
+    }
+    {
+      // Two elements with brace was treated as initializer_list
+      using M = std::flat_multiset<int>;
+      M m;
+      std::flat_multiset s{m, m}; // flat_multiset(initializer_list<M>)
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<M>);
+      assert(s.size() == 2);
+    }
+  }
+  {
+    const int sorted_arr[] = {INT_MAX, 3, 2, 1, 1};
+    using C                = std::greater<long>;
+    {
+      // template<class Key, class Compare = less<Key>>
+      // flat_multiset(initializer_list<Key>, Compare = Compare())
+      //   -> flat_multiset<Key, Compare>;
+      std::flat_multiset m({1, 2, 1, INT_MAX, 3}, C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+    {
+      // template<class Key, class Compare = less<Key>>
+      // flat_multiset(sorted_equivalent_t, initializer_list<Key>, Compare = Compare())
+      //     -> flat_multiset<Key, Compare>;
+      std::flat_multiset m(std::sorted_equivalent, {INT_MAX, 3, 2, 1, 1}, C());
+
+      ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
+      assert(std::ranges::equal(m, sorted_arr));
+    }
+  }
+  {
+    std::list<int> r     = {1, 2, 1, INT_MAX, 3};
+    const int expected[] = {1, 1, 2, 3, INT_MAX};
+    {
+      // template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
+      //     class Allocator = allocator<ranges::range_value_t<R>>>
+      // flat_multiset(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
+      // -> flat_multiset<ranges::range_value_t<R>, Compare,
+      //                 vector<ranges::range_value_t<R>,
+      //                        alloc-rebind<Allocator, ranges::range_value_t<R>>>>;
+      std::flat_multiset s(std::from_range, r);
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>>);
+      assert(std::ranges::equal(s, expected));
+    }
+    {
+      // template<ranges::input_range R, class Allocator>
+      // flat_multiset(from_range_t, R&&, Allocator)
+      //   -> flat_multiset<ranges::range_value_t<R>, less<ranges::range_value_t<R>>,
+      //                    vector<ranges::range_value_t<R>,
+      //                           alloc-rebind<Allocator, ranges::range_value_t<R>>>>;
+      std::flat_multiset s(std::from_range, r, test_allocator<long>(0, 42));
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
+  }
+
+  {
+    // with comparator
+    std::list<int> r     = {1, 2, 1, INT_MAX, 3};
+    const int expected[] = {INT_MAX, 3, 2, 1, 1};
+    {
+      // template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
+      //     class Allocator = allocator<ranges::range_value_t<R>>>
+      // flat_multiset(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
+      // -> flat_multiset<ranges::range_value_t<R>, Compare,
+      //                 vector<ranges::range_value_t<R>,
+      //                        alloc-rebind<Allocator, ranges::range_value_t<R>>>>;
+      std::flat_multiset s(std::from_range, r, std::greater<int>());
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>>);
+      assert(std::ranges::equal(s, expected));
+    }
+    {
+      // template<ranges::input_range R, class Allocator>
+      // flat_multiset(from_range_t, R&&, Allocator)
+      //   -> flat_multiset<ranges::range_value_t<R>, less<ranges::range_value_t<R>>,
+      //                    vector<ranges::range_value_t<R>,
+      //                           alloc-rebind<Allocator, ranges::range_value_t<R>>>>;
+      std::flat_multiset s(std::from_range, r, std::greater<int>(), test_allocator<long>(0, 42));
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::vector<int, test_allocator<int>>>);
+      assert(std::ranges::equal(s, expected));
+      assert(std::move(s).extract().get_allocator().get_id() == 42);
+    }
   }
-  {
-    // This does not deduce to flat_multiset(InputIterator, InputIterator)
-    // But deduces to flat_multiset(initializer_list<int*>)
-    int source[3]        = {1, 2, 3};
-    std::flat_multiset s = {source, source + 3};
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int*>);
-    assert(s.size() == 2);
-  }
-  {
-    int source[3] = {1, 2, 3};
-    std::flat_multiset s{
-        std::sorted_equivalent, source, source + 3}; // flat_multiset(sorted_equivalent_t, InputIterator, InputIterator)
-    static_assert(std::is_same_v<decltype(s), std::flat_multiset<int>>);
-    assert(s.size() == 3);
-  }
-}
-
-void test_iter_iter_compare() {
-  int arr[]               = {1, 2, 1, INT_MAX, 3};
-  int sorted_arr[]        = {INT_MAX, 3, 2, 1, 1};
-  const int arrc[]        = {1, 2, 1, INT_MAX, 3};
-  const int sorted_arrc[] = {INT_MAX, 3, 2, 1, 1};
-  using C                 = std::greater<long>;
-  {
-    std::flat_multiset m(std::begin(arr), std::end(arr), C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::begin(arrc), std::end(arrc), C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arr), std::end(sorted_arr), C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::sorted_equivalent, std::begin(sorted_arrc), std::end(sorted_arrc), C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset<int> mo;
-    std::flat_multiset m(mo.begin(), mo.end(), C());
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-  }
-  {
-    std::flat_multiset<int> mo;
-    std::flat_multiset m(mo.cbegin(), mo.cend(), C());
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-  }
-}
-
-void test_initializer_list() {
-  const int sorted_arr[] = {1, 1, 2, 3, INT_MAX};
-  {
-    std::flat_multiset m{1, 2, 1, INT_MAX, 3};
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::sorted_equivalent, {1, 1, 2, 3, INT_MAX});
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset s = {1};
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int>);
-    assert(s.size() == 1);
-  }
-  {
-    using M = std::flat_multiset<int>;
-    M m;
-    std::flat_multiset s{m, m}; // flat_multiset(initializer_list<M>)
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<M>);
-    assert(s.size() == 2);
-  }
-}
-
-void test_initializer_list_compare() {
-  const int sorted_arr[] = {INT_MAX, 3, 2, 1, 1};
-  using C                = std::greater<long>;
-  {
-    std::flat_multiset m({1, 2, 1, INT_MAX, 3}, C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-  {
-    std::flat_multiset m(std::sorted_equivalent, {INT_MAX, 3, 2, 1, 1}, C());
-
-    ASSERT_SAME_TYPE(decltype(m), std::flat_multiset<int, C>);
-    assert(std::ranges::equal(m, sorted_arr));
-  }
-}
-
-void test_from_range() {
-  std::list<int> r     = {1, 2, 1, INT_MAX, 3};
-  const int expected[] = {1, 1, 2, 3, INT_MAX};
-  {
-    std::flat_multiset s(std::from_range, r);
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>>);
-    assert(std::ranges::equal(s, expected));
-  }
-  {
-    std::flat_multiset s(std::from_range, r, test_allocator<long>(0, 42));
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::vector<int, test_allocator<int>>>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-}
-
-void test_from_range_compare() {
-  std::list<int> r     = {1, 2, 1, INT_MAX, 3};
-  const int expected[] = {INT_MAX, 3, 2, 1, 1};
-  {
-    std::flat_multiset s(std::from_range, r, std::greater<int>());
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>>);
-    assert(std::ranges::equal(s, expected));
-  }
-  {
-    std::flat_multiset s(std::from_range, r, std::greater<int>(), test_allocator<long>(0, 42));
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::vector<int, test_allocator<int>>>);
-    assert(std::ranges::equal(s, expected));
-    assert(std::move(s).extract().get_allocator().get_id() == 42);
-  }
-}
-
-void test() {
-  // Each test function also tests the sorted_equivalent-prefixed and allocator-suffixed overloads.
-  test_copy();
-  test_containers();
-  test_containers_compare();
-  test_iter_iter();
-  test_iter_iter_compare();
-  test_initializer_list();
-  test_initializer_list_compare();
-  test_from_range();
-  test_from_range_compare();
 
   AssociativeContainerDeductionGuidesSfinaeAway<std::flat_multiset, std::flat_multiset<int>>();
 }
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
index ed3649a7301f7..367dc55e34410 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/deduct_pmr.pass.cpp
@@ -30,65 +30,75 @@
 using P  = std::pair<int, long>;
 using PC = std::pair<const int, long>;
 
-void test_containers() {
-  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
-  std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
-  const int expected[] = {1, 1, 2, 3, INT_MAX};
+int main(int, char**) {
   {
-    std::pmr::monotonic_buffer_resource mr;
-    std::pmr::monotonic_buffer_resource mr2;
-    std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
-    std::flat_multiset s(std::move(pks), &mr2);
+    std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+    std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
+    const int expected[] = {1, 1, 2, 3, INT_MAX};
+    {
+      // template<class KeyContainer, class Allocator>
+      // flat_multiset(KeyContainer, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type,
+      //                    less<typename KeyContainer::value_type>, KeyContainer>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::monotonic_buffer_resource mr2;
+      std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+      std::flat_multiset s(std::move(pks), &mr2);
 
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
-    assert(std::ranges::equal(s, expected));
-    auto keys = std::move(s).extract();
-    assert(keys.get_allocator().resource() == &mr2);
-  }
-  {
-    std::pmr::monotonic_buffer_resource mr;
-    std::pmr::monotonic_buffer_resource mr2;
-    std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
-    std::flat_multiset s(std::sorted_equivalent, std::move(pks), &mr2);
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
+      assert(std::ranges::equal(s, expected));
+      auto keys = std::move(s).extract();
+      assert(keys.get_allocator().resource() == &mr2);
+    }
+    {
+      // template<class KeyContainer, class Allocator>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type,
+      //                    less<typename KeyContainer::value_type>, KeyContainer>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::monotonic_buffer_resource mr2;
+      std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+      std::flat_multiset s(std::sorted_equivalent, std::move(pks), &mr2);
 
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
-    assert(std::ranges::equal(s, expected));
-    auto keys = std::move(s).extract();
-    assert(keys.get_allocator().resource() == &mr2);
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::less<int>, std::pmr::deque<int>>);
+      assert(std::ranges::equal(s, expected));
+      auto keys = std::move(s).extract();
+      assert(keys.get_allocator().resource() == &mr2);
+    }
   }
-}
-
-void test_containers_compare() {
-  std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
-  std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
-  const int expected[] = {INT_MAX, 3, 2, 1, 1};
   {
-    std::pmr::monotonic_buffer_resource mr;
-    std::pmr::monotonic_buffer_resource mr2;
-    std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
-    std::flat_multiset s(std::move(pks), std::greater<int>(), &mr2);
+    std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
+    std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1, 1}, test_allocator<int>(0, 42));
+    const int expected[] = {INT_MAX, 3, 2, 1, 1};
+    {
+      // template<class KeyContainer, class Compare, class Allocator>
+      // flat_multiset(KeyContainer, Compare, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::monotonic_buffer_resource mr2;
+      std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+      std::flat_multiset s(std::move(pks), std::greater<int>(), &mr2);
 
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
-    assert(std::ranges::equal(s, expected));
-    auto keys = std::move(s).extract();
-    assert(keys.get_allocator().resource() == &mr2);
-  }
-  {
-    std::pmr::monotonic_buffer_resource mr;
-    std::pmr::monotonic_buffer_resource mr2;
-    std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
-    std::flat_multiset s(std::sorted_equivalent, std::move(pks), std::greater<int>(), &mr2);
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
+      assert(std::ranges::equal(s, expected));
+      auto keys = std::move(s).extract();
+      assert(keys.get_allocator().resource() == &mr2);
+    }
+    {
+      // template<class KeyContainer, class Compare, class Allocator>
+      // flat_multiset(sorted_equivalent_t, KeyContainer, Compare, Allocator)
+      //   -> flat_multiset<typename KeyContainer::value_type, Compare, KeyContainer>;
+      std::pmr::monotonic_buffer_resource mr;
+      std::pmr::monotonic_buffer_resource mr2;
+      std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+      std::flat_multiset s(std::sorted_equivalent, std::move(pks), std::greater<int>(), &mr2);
 
-    ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
-    assert(std::ranges::equal(s, expected));
-    auto keys = std::move(s).extract();
-    assert(keys.get_allocator().resource() == &mr2);
+      ASSERT_SAME_TYPE(decltype(s), std::flat_multiset<int, std::greater<int>, std::pmr::deque<int>>);
+      assert(std::ranges::equal(s, expected));
+      auto keys = std::move(s).extract();
+      assert(keys.get_allocator().resource() == &mr2);
+    }
   }
-}
-
-int main(int, char**) {
-  test_containers();
-  test_containers_compare();
 
   return 0;
 }
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
index 7a07b5283fe1b..da9aef3dc36cd 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.cons/iter_iter.pass.cpp
@@ -56,7 +56,7 @@ void test() {
   }
 
   int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
-  int expected[] = {1, 1,1,2,2,2,3,3,3};
+  int expected[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
   {
     // flat_multiset(InputIterator , InputIterator)
     // cpp17_input_iterator
diff --git a/libcxx/test/std/containers/container.adaptors/flat.multiset/helpers.h b/libcxx/test/std/containers/container.adaptors/flat.multiset/helpers.h
new file mode 100644
index 0000000000000..2ee8b021337a0
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.multiset/helpers.h
@@ -0,0 +1,307 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_FLAT_SET_HELPERS_H
+#define SUPPORT_FLAT_SET_HELPERS_H
+
+#include <algorithm>
+#include <cassert>
+#include <string>
+#include <vector>
+#include <flat_set>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+template <class... Args>
+void check_invariant(const std::flat_set<Args...>& m) {
+  assert(std::is_sorted(m.begin(), m.end(), m.key_comp()));
+  auto key_equal = [&](const auto& x, const auto& y) {
+    const auto& c = m.key_comp();
+    return !c(x, y) && !c(y, x);
+  };
+  assert(std::adjacent_find(m.begin(), m.end(), key_equal) == m.end());
+}
+
+struct StartsWith {
+  explicit StartsWith(char ch) : lower_(1, ch), upper_(1, ch + 1) {}
+  StartsWith(const StartsWith&)     = delete;
+  void operator=(const StartsWith&) = delete;
+  struct Less {
+    using is_transparent = void;
+    bool operator()(const std::string& a, const std::string& b) const { return a < b; }
+    bool operator()(const StartsWith& a, const std::string& b) const { return a.upper_ <= b; }
+    bool operator()(const std::string& a, const StartsWith& b) const { return a < b.lower_; }
+    bool operator()(const StartsWith&, const StartsWith&) const {
+      assert(false); // should not be called
+      return false;
+    }
+  };
+
+private:
+  std::string lower_;
+  std::string upper_;
+};
+
+template <class T>
+struct CopyOnlyVector : std::vector<T> {
+  using std::vector<T>::vector;
+
+  CopyOnlyVector(const CopyOnlyVector&) = default;
+  CopyOnlyVector(CopyOnlyVector&& other) : CopyOnlyVector(other) {}
+  CopyOnlyVector(CopyOnlyVector&& other, std::vector<T>::allocator_type alloc) : CopyOnlyVector(other, alloc) {}
+
+  CopyOnlyVector& operator=(const CopyOnlyVector&) = default;
+  CopyOnlyVector& operator=(CopyOnlyVector& other) { return this->operator=(other); }
+};
+
+template <class T, bool ConvertibleToT = false>
+struct Transparent {
+  T t;
+
+  operator T() const
+    requires ConvertibleToT
+  {
+    return t;
+  }
+};
+
+template <class T>
+using ConvertibleTransparent = Transparent<T, true>;
+
+template <class T>
+using NonConvertibleTransparent = Transparent<T, false>;
+
+struct TransparentComparator {
+  using is_transparent = void;
+
+  bool* transparent_used  = nullptr;
+  TransparentComparator() = default;
+  TransparentComparator(bool& used) : transparent_used(&used) {}
+
+  template <class T, bool Convertible>
+  bool operator()(const T& t, const Transparent<T, Convertible>& transparent) const {
+    if (transparent_used != nullptr) {
+      *transparent_used = true;
+    }
+    return t < transparent.t;
+  }
+
+  template <class T, bool Convertible>
+  bool operator()(const Transparent<T, Convertible>& transparent, const T& t) const {
+    if (transparent_used != nullptr) {
+      *transparent_used = true;
+    }
+    return transparent.t < t;
+  }
+
+  template <class T>
+  bool operator()(const T& t1, const T& t2) const {
+    return t1 < t2;
+  }
+};
+
+struct NonTransparentComparator {
+  template <class T, bool Convertible>
+  bool operator()(const T&, const Transparent<T, Convertible>&) const;
+
+  template <class T, bool Convertible>
+  bool operator()(const Transparent<T, Convertible>&, const T&) const;
+
+  template <class T>
+  bool operator()(const T&, const T&) const;
+};
+
+struct NoDefaultCtr {
+  NoDefaultCtr() = delete;
+};
+
+#ifndef TEST_HAS_NO_EXCEPTIONS
+template <class T>
+struct EmplaceUnsafeContainer : std::vector<T> {
+  using std::vector<T>::vector;
+
+  template <class... Args>
+  auto emplace(Args&&... args) -> decltype(std::declval<std::vector<T>>().emplace(std::forward<Args>(args)...)) {
+    if (this->size() > 1) {
+      auto it1 = this->begin();
+      auto it2 = it1 + 1;
+      // messing up the container
+      std::iter_swap(it1, it2);
+    }
+
+    throw 42;
+  }
+
+  template <class... Args>
+  auto insert(Args&&... args) -> decltype(std::declval<std::vector<T>>().insert(std::forward<Args>(args)...)) {
+    if (this->size() > 1) {
+      auto it1 = this->begin();
+      auto it2 = it1 + 1;
+      // messing up the container
+      std::iter_swap(it1, it2);
+    }
+
+    throw 42;
+  }
+
+  template <class... Args>
+  auto insert_range(Args&&... args)
+      -> decltype(std::declval<std::vector<T>>().insert_range(std::forward<Args>(args)...)) {
+    if (this->size() > 1) {
+      auto it1 = this->begin();
+      auto it2 = it1 + 1;
+      // messing up the container
+      std::iter_swap(it1, it2);
+    }
+
+    throw 42;
+  }
+};
+
+template <class T>
+struct ThrowOnEraseContainer : std::vector<T> {
+  using std::vector<T>::vector;
+
+  template <class... Args>
+  auto erase(Args&&... args) -> decltype(std::declval<std::vector<T>>().erase(std::forward<Args>(args)...)) {
+    throw 42;
+  }
+};
+
+template <class T>
+struct ThrowOnMoveContainer : std::vector<T> {
+  using std::vector<T>::vector;
+
+  ThrowOnMoveContainer(ThrowOnMoveContainer&&) { throw 42; }
+
+  ThrowOnMoveContainer& operator=(ThrowOnMoveContainer&&) { throw 42; }
+};
+
+#endif
+
+template <class F>
+void test_emplace_exception_guarantee([[maybe_unused]] F&& emplace_function) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+  using C = TransparentComparator;
+  {
+    // Throw on emplace the key, and underlying has strong exception guarantee
+    using KeyContainer = std::vector<int, test_allocator<int>>;
+    using M            = std::flat_set<int, C, KeyContainer>;
+
+    LIBCPP_STATIC_ASSERT(std::__container_traits<KeyContainer>::__emplacement_has_strong_exception_safety_guarantee);
+
+    test_allocator_statistics stats;
+
+    KeyContainer a({1, 2, 3, 4}, test_allocator<int>{&stats});
+    [[maybe_unused]] auto expected_keys = a;
+    M m(std::sorted_unique, std::move(a));
+
+    stats.throw_after = 1;
+    try {
+      emplace_function(m, 0);
+      assert(false);
+    } catch (const std::bad_alloc&) {
+      check_invariant(m);
+      // In libc++, the flat_set is unchanged
+      LIBCPP_ASSERT(m.size() == 4);
+      LIBCPP_ASSERT(std::ranges::equal(m, expected_keys));
+    }
+  }
+  {
+    // Throw on emplace the key, and underlying has no strong exception guarantee
+    using KeyContainer = EmplaceUnsafeContainer<int>;
+    using M            = std::flat_set<int, C, KeyContainer>;
+
+    LIBCPP_STATIC_ASSERT(!std::__container_traits<KeyContainer>::__emplacement_has_strong_exception_safety_guarantee);
+    KeyContainer a = {1, 2, 3, 4};
+    M m(std::sorted_unique, std::move(a));
+    try {
+      emplace_function(m, 0);
+      assert(false);
+    } catch (int) {
+      check_invariant(m);
+      // In libc++, the flat_set is cleared
+      LIBCPP_ASSERT(m.size() == 0);
+    }
+  }
+#endif
+}
+
+template <class F>
+void test_insert_range_exception_guarantee([[maybe_unused]] F&& insert_function) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+  using KeyContainer = EmplaceUnsafeContainer<int>;
+  using M            = std::flat_set<int, std::ranges::less, KeyContainer>;
+  test_allocator_statistics stats;
+  KeyContainer a{1, 2, 3, 4};
+  M m(std::sorted_unique, std::move(a));
+
+  std::vector<int> newValues = {0, 1, 5, 6, 7, 8};
+  stats.throw_after          = 1;
+  try {
+    insert_function(m, newValues);
+    assert(false);
+  } catch (int) {
+    check_invariant(m);
+    // In libc++, we clear if anything goes wrong when inserting a range
+    LIBCPP_ASSERT(m.size() == 0);
+  }
+#endif
+}
+
+template <class F>
+void test_erase_exception_guarantee([[maybe_unused]] F&& erase_function) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+  {
+    // key erase throws
+    using KeyContainer = ThrowOnEraseContainer<int>;
+    using M            = std::flat_set<int, TransparentComparator, KeyContainer>;
+
+    KeyContainer a{1, 2, 3, 4};
+    M m(std::sorted_unique, std::move(a));
+    try {
+      erase_function(m, 3);
+      assert(false);
+    } catch (int) {
+      check_invariant(m);
+      // In libc++, we clear if anything goes wrong when erasing
+      LIBCPP_ASSERT(m.size() == 0);
+    }
+  }
+#endif
+}
+class Moveable {
+  int int_;
+  double double_;
+
+public:
+  Moveable() : int_(0), double_(0) {}
+  Moveable(int i, double d) : int_(i), double_(d) {}
+  Moveable(Moveable&& x) : int_(x.int_), double_(x.double_) {
+    x.int_    = -1;
+    x.double_ = -1;
+  }
+  Moveable& operator=(Moveable&& x) {
+    int_      = x.int_;
+    x.int_    = -1;
+    double_   = x.double_;
+    x.double_ = -1;
+    return *this;
+  }
+
+  Moveable(const Moveable&)            = delete;
+  Moveable& operator=(const Moveable&) = delete;
+  bool operator==(const Moveable& x) const { return int_ == x.int_ && double_ == x.double_; }
+  bool operator<(const Moveable& x) const { return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_); }
+
+  int get() const { return int_; }
+  bool moved() const { return int_ == -1; }
+};
+
+#endif // SUPPORT_FLAT_SET_HELPERS_H



More information about the libcxx-commits mailing list