[libcxx-commits] [libcxx] 628fcfd - [libc++] Add tests for std::string default constructor and destructor

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Apr 8 03:21:59 PDT 2022


Author: Nikolas Klauser
Date: 2022-04-08T12:21:43+02:00
New Revision: 628fcfd5204cf87a733ede91a648bed4c20eb21a

URL: https://github.com/llvm/llvm-project/commit/628fcfd5204cf87a733ede91a648bed4c20eb21a
DIFF: https://github.com/llvm/llvm-project/commit/628fcfd5204cf87a733ede91a648bed4c20eb21a.diff

LOG: [libc++] Add tests for std::string default constructor and destructor

Reviewed By: ldionne, var-const, #libc, nilayvaish

Spies: nilayvaish, libcxx-commits

Differential Revision: https://reviews.llvm.org/D123129

Added: 
    libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp
    libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp

Modified: 
    

Removed: 
    libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp
    libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp


################################################################################
diff  --git a/libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/default.pass.cpp
new file mode 100644
index 0000000000000..812bc71480100
--- /dev/null
+++ b/libcxx/test/std/strings/basic.string/string.cons/default.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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// basic_string() noexcept(is_nothrow_default_constructible<allocator_type>::value);
+
+#include <cassert>
+#include <string>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+
+#if TEST_STD_VER >= 11
+// Test the noexcept specification, which is a conforming extension
+LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<std::string>::value, "");
+LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<
+                     std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
+LIBCPP_STATIC_ASSERT(!std::is_nothrow_default_constructible<
+                     std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>>>::value, "");
+#endif
+
+bool test() {
+  std::string str;
+  assert(str.empty());
+
+  return true;
+}
+
+int main(int, char**)
+{
+  test();
+#if TEST_STD_VER > 17
+  // static_assert(test());
+#endif
+
+  return 0;
+}

diff  --git a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp
deleted file mode 100644
index d551b597e4475..0000000000000
--- a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp
+++ /dev/null
@@ -1,40 +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
-
-// <string>
-
-// basic_string()
-//        noexcept(is_nothrow_default_constructible<allocator_type>::value);
-
-// This tests a conforming extension
-
-#include <string>
-#include <cassert>
-
-#include "test_macros.h"
-#include "test_allocator.h"
-
-int main(int, char**)
-{
-    {
-        typedef std::string C;
-        static_assert(std::is_nothrow_default_constructible<C>::value, "");
-    }
-    {
-        typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
-        static_assert(std::is_nothrow_default_constructible<C>::value, "");
-    }
-    {
-        typedef std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>> C;
-        static_assert(!std::is_nothrow_default_constructible<C>::value, "");
-    }
-
-  return 0;
-}

diff  --git a/libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp
similarity index 56%
rename from libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp
rename to libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp
index 83d2a929cec7e..1b498811adf4b 100644
--- a/libcxx/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp
@@ -34,22 +34,30 @@ std::string s;
 std::wstring ws;
 #endif
 
+static_assert(std::is_nothrow_destructible<std::string>::value, "");
+static_assert(std::is_nothrow_destructible<
+                std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
+LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<
+                     std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
+
+bool test() {
+  test_allocator_statistics alloc_stats;
+  {
+    std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
+    str2 = "long long string so no SSO";
+    assert(alloc_stats.alloc_count == 1);
+  }
+  assert(alloc_stats.alloc_count == 0);
+
+  return true;
+}
+
 int main(int, char**)
 {
-    {
-        typedef std::string C;
-        static_assert(std::is_nothrow_destructible<C>::value, "");
-    }
-    {
-        typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
-        static_assert(std::is_nothrow_destructible<C>::value, "");
-    }
-#if defined(_LIBCPP_VERSION)
-    {
-        typedef std::basic_string<char, std::char_traits<char>, throwing_alloc<char>> C;
-        static_assert(!std::is_nothrow_destructible<C>::value, "");
-    }
-#endif // _LIBCPP_VERSION
+  test();
+#if TEST_STD_VER > 17
+  // static_assert(test());
+#endif
 
   return 0;
 }


        


More information about the libcxx-commits mailing list