1 //===-- Unittests for asctime_r
2 //--------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "src/time/asctime_r.h"
11 #include "src/time/time_utils.h"
12 #include "test/src/time/TmHelper.h"
13 #include "utils/UnitTest/Test.h"
14
15 using __llvm_libc::time_utils::TimeConstants;
16
call_asctime_r(struct tm * tm_data,int year,int month,int mday,int hour,int min,int sec,int wday,int yday,char * buffer)17 static inline char *call_asctime_r(struct tm *tm_data, int year, int month,
18 int mday, int hour, int min, int sec,
19 int wday, int yday, char *buffer) {
20 __llvm_libc::tmhelper::testing::initialize_tm_data(
21 tm_data, year, month, mday, hour, min, sec, wday, yday);
22 return __llvm_libc::asctime_r(tm_data, buffer);
23 }
24
25 // asctime and asctime_r share the same code and thus didn't repeat all the
26 // tests from asctime. Added couple of validation tests.
TEST(LlvmLibcAsctimeR,Nullptr)27 TEST(LlvmLibcAsctimeR, Nullptr) {
28 char *result;
29 result = __llvm_libc::asctime_r(nullptr, nullptr);
30 ASSERT_EQ(EINVAL, llvmlibc_errno);
31 ASSERT_STREQ(nullptr, result);
32
33 char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
34 result = __llvm_libc::asctime_r(nullptr, buffer);
35 ASSERT_EQ(EINVAL, llvmlibc_errno);
36 ASSERT_STREQ(nullptr, result);
37
38 struct tm tm_data;
39 result = __llvm_libc::asctime_r(&tm_data, nullptr);
40 ASSERT_EQ(EINVAL, llvmlibc_errno);
41 ASSERT_STREQ(nullptr, result);
42 }
43
TEST(LlvmLibcAsctimeR,ValidDate)44 TEST(LlvmLibcAsctimeR, ValidDate) {
45 char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
46 struct tm tm_data;
47 char *result;
48 // 1970-01-01 00:00:00. Test with a valid buffer size.
49 result = call_asctime_r(&tm_data,
50 1970, // year
51 1, // month
52 1, // day
53 0, // hr
54 0, // min
55 0, // sec
56 4, // wday
57 0, // yday
58 buffer);
59 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
60 }
61