1//===-- Unittests for asctime_r -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/errno/libc_errno.h"
10#include "src/time/asctime_r.h"
11#include "src/time/time_utils.h"
12#include "test/UnitTest/Test.h"
13#include "test/src/time/TmHelper.h"
14
15using LIBC_NAMESPACE::time_utils::TimeConstants;
16
17static 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 LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data(
21 tm_data, year, month, mday, hour, min, sec, wday, yday);
22 return LIBC_NAMESPACE::asctime_r(timeptr: 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.
27TEST(LlvmLibcAsctimeR, Nullptr) {
28 char *result;
29 result = LIBC_NAMESPACE::asctime_r(timeptr: nullptr, buffer: nullptr);
30 ASSERT_ERRNO_EQ(EINVAL);
31 ASSERT_STREQ(nullptr, result);
32
33 char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
34 result = LIBC_NAMESPACE::asctime_r(timeptr: nullptr, buffer);
35 ASSERT_ERRNO_EQ(EINVAL);
36 ASSERT_STREQ(nullptr, result);
37
38 struct tm tm_data;
39 result = LIBC_NAMESPACE::asctime_r(timeptr: &tm_data, buffer: nullptr);
40 ASSERT_ERRNO_EQ(EINVAL);
41 ASSERT_STREQ(nullptr, result);
42}
43
44TEST(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: &tm_data,
50 year: 1970, // year
51 month: 1, // month
52 mday: 1, // day
53 hour: 0, // hr
54 min: 0, // min
55 sec: 0, // sec
56 wday: 4, // wday
57 yday: 0, // yday
58 buffer);
59 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
60}
61

source code of libc/test/src/time/asctime_r_test.cpp