#elif __cpp_lib_semaphore != 201907L
# error "Feature-test macro for semaphore has wrong value in <semaphore>"
#endif
+
+static_assert(std::is_same_v<std::counting_semaphore<1>,
+ std::binary_semaphore>);
+
+static_assert(! std::is_same_v<std::counting_semaphore<2>,
+ std::binary_semaphore>);
+
+static_assert(! std::is_same_v<std::counting_semaphore<>,
+ std::binary_semaphore>);
+
+// The standard permits max() to be greater than the template argument,
+// but for the current libstdc++ implementation it's always equal to it.
+static_assert(std::binary_semaphore::max() == 1);
+static_assert(std::counting_semaphore<0>::max() == 0);
+static_assert(std::counting_semaphore<2>::max() == 2);
+
+#include <limits.h>
+
+static_assert(std::counting_semaphore<INT_MAX>::max() == INT_MAX);
+static_assert(std::counting_semaphore<INT_MAX-1>::max() == INT_MAX-1);
+static_assert(std::counting_semaphore<PTRDIFF_MAX>::max() == PTRDIFF_MAX);
+static_assert(std::counting_semaphore<PTRDIFF_MAX-3>::max() == PTRDIFF_MAX-3);
--- /dev/null
+// { dg-do compile { target *-*-*linux* } }
+// { dg-require-effective-target c++20 }
+// { dg-require-effective-target hosted }
+
+#include <semaphore>
+#include <limits.h>
+
+// on Linux these specializations all use a futex:
+static_assert(sizeof(std::counting_semaphore<0>) == sizeof(int));
+static_assert(sizeof(std::counting_semaphore<1>) == sizeof(int));
+static_assert(sizeof(std::counting_semaphore<INT_MAX>) == sizeof(int));
+static_assert(sizeof(std::counting_semaphore<>) == sizeof(int));
+
+// This will use a futex iff ptrdiff_t has 32 bits:
+static_assert(sizeof(std::counting_semaphore<PTRDIFF_MAX>) == sizeof(std::ptrdiff_t));
+
+#if PTRDIFF_MAX > INT_MAX
+static_assert(sizeof(std::counting_semaphore<INT_MAX+1LL>) == sizeof(std::ptrdiff_t));
+#endif
--- /dev/null
+// { dg-options "-D_GLIBCXX_ASSERTIONS" }
+// { dg-do run { target c++20 xfail *-*-* } }
+// { dg-require-effective-target hosted }
+// { dg-add-options libatomic }
+
+#include <semaphore>
+
+int main()
+{
+ // Preconditions: desired >= 0 is true, and desired <= max() is true.
+ std::binary_semaphore b(2);
+}