[libc-commits] [libc] [libc] Add `ctime_s` (PR #110676)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Wed Nov 20 11:30:17 PST 2024


================
@@ -0,0 +1,66 @@
+//===-- Unittests for ctime_s ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/errno/libc_errno.h"
+#include "src/time/ctime_s.h"
+#include "src/time/time_utils.h"
+#include "test/UnitTest/Test.h"
+#include "test/src/time/TmHelper.h"
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+TEST(LlvmLibcCtimeS, Nullptr) {
+  int result = LIBC_NAMESPACE::ctime_s(nullptr, 0, nullptr);
+  ASSERT_EQ(EINVAL, result);
+
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), nullptr);
+  ASSERT_EQ(EINVAL, result);
+  ASSERT_EQ('\0', buffer[0]);
----------------
nickdesaulniers wrote:

This test fails for me:
```
$ ninja libc_time_unittests
...
[ RUN      ] LlvmLibcCtimeS.Nullptr
/android0/llvm-project/libc/test/src/time/ctime_s_test.cpp:25: FAILURE
      Expected: '\0'
      Which is: 0
To be equal to: buffer[0]
      Which is: 74
```
Because `nullptr` is not a constraint violation, you can just remove this assert.
```suggestion
```


https://github.com/llvm/llvm-project/pull/110676


More information about the libc-commits mailing list