xref: /llvm-project/libc/test/src/time/ctime_r_test.cpp (revision f9c2377fb68e5051b3061186c507f7b87db2a8b2)
1000a3f0aSЗишан Мирза //===-- Unittests for ctime_r ---------------------------------------------===//
2000a3f0aSЗишан Мирза //
3000a3f0aSЗишан Мирза // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4000a3f0aSЗишан Мирза // See https://llvm.org/LICENSE.txt for license information.
5000a3f0aSЗишан Мирза // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6000a3f0aSЗишан Мирза //
7000a3f0aSЗишан Мирза //===----------------------------------------------------------------------===//
8000a3f0aSЗишан Мирза 
9000a3f0aSЗишан Мирза #include "src/errno/libc_errno.h"
10000a3f0aSЗишан Мирза #include "src/time/ctime_r.h"
11*f9c2377fSMichael Jones #include "src/time/time_constants.h"
12000a3f0aSЗишан Мирза #include "test/UnitTest/Test.h"
13000a3f0aSЗишан Мирза #include "test/src/time/TmHelper.h"
14000a3f0aSЗишан Мирза 
15000a3f0aSЗишан Мирза TEST(LlvmLibcCtimeR, Nullptr) {
16000a3f0aSЗишан Мирза   char *result;
17000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr);
18000a3f0aSЗишан Мирза   ASSERT_STREQ(nullptr, result);
19000a3f0aSЗишан Мирза 
20*f9c2377fSMichael Jones   char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
21000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(nullptr, buffer);
22000a3f0aSЗишан Мирза   ASSERT_STREQ(nullptr, result);
23000a3f0aSЗишан Мирза 
24000a3f0aSЗишан Мирза   time_t t;
25000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(&t, nullptr);
26000a3f0aSЗишан Мирза   ASSERT_STREQ(nullptr, result);
27000a3f0aSЗишан Мирза }
28000a3f0aSЗишан Мирза 
29000a3f0aSЗишан Мирза TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) {
30*f9c2377fSMichael Jones   char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
31000a3f0aSЗишан Мирза   time_t t;
32000a3f0aSЗишан Мирза   char *result;
33000a3f0aSЗишан Мирза   // 1970-01-01 00:00:00. Test with a valid buffer size.
34000a3f0aSЗишан Мирза   t = 0;
35000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(&t, buffer);
36000a3f0aSЗишан Мирза   ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", result);
37000a3f0aSЗишан Мирза }
38000a3f0aSЗишан Мирза 
39000a3f0aSЗишан Мирза TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
40*f9c2377fSMichael Jones   char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
41000a3f0aSЗишан Мирза   time_t t;
42000a3f0aSЗишан Мирза   char *result;
43000a3f0aSЗишан Мирза   // 2038-01-19 03:14:07. Test with a valid buffer size.
44000a3f0aSЗишан Мирза   t = 2147483647;
45000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(&t, buffer);
46000a3f0aSЗишан Мирза   ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
47000a3f0aSЗишан Мирза }
48000a3f0aSЗишан Мирза 
49000a3f0aSЗишан Мирза TEST(LlvmLibcCtimeR, InvalidArgument) {
50*f9c2377fSMichael Jones   char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
51000a3f0aSЗишан Мирза   time_t t;
52000a3f0aSЗишан Мирза   char *result;
53000a3f0aSЗишан Мирза   t = 2147483648;
54000a3f0aSЗишан Мирза   result = LIBC_NAMESPACE::ctime_r(&t, buffer);
55000a3f0aSЗишан Мирза   ASSERT_STREQ(nullptr, result);
56000a3f0aSЗишан Мирза }
57