[libc-commits] [libc] Reland "[libc] Implement cpp::unique_ptr utility" (PR #233800)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Tue Oct 20 06:49:20 PDT 2026
================
@@ -0,0 +1,244 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for UniquePtr.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/unique_ptr.h"
+#include "src/__support/CPP/utility.h"
+#include "src/__support/alloc-checker.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::AllocChecker;
+using LIBC_NAMESPACE::cpp::unique_ptr;
+
+struct DestructTracker {
+ int *destruct_count = nullptr;
+ DestructTracker() = default;
+ DestructTracker(int *count) : destruct_count(count) {}
+ ~DestructTracker() {
+ if (destruct_count)
+ (*destruct_count)++;
+ }
+};
+
+// Test basic construction, ownership, and destruction.
+TEST(LlvmLibcUniquePtrTest, Basic) {
+ int destruct_count = 0;
+ {
+ AllocChecker ac;
+ unique_ptr<DestructTracker> ptr(new (ac) DestructTracker(&destruct_count));
+ ASSERT_TRUE(static_cast<bool>(ac));
+ ASSERT_TRUE(static_cast<bool>(ptr));
+ ASSERT_EQ(destruct_count, 0);
+ }
+ ASSERT_EQ(destruct_count, 1);
+}
+
+// Test nullptr construction and behavior.
+TEST(LlvmLibcUniquePtrTest, Nullptr) {
+ unique_ptr<int> ptr(nullptr);
+ ASSERT_FALSE(static_cast<bool>(ptr));
+ ASSERT_EQ(ptr.get(), static_cast<int *>(nullptr));
----------------
labath wrote:
Done.
https://github.com/llvm/llvm-project/pull/233800
More information about the libc-commits
mailing list