62 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/errno_macros.h"10#include "src/time/asctime_r.h"11#include "src/time/time_constants.h"12#include "test/UnitTest/ErrnoCheckingTest.h"13#include "test/UnitTest/Test.h"14#include "test/src/time/TmHelper.h"15 16using LlvmLibcAsctimeR = LIBC_NAMESPACE::testing::ErrnoCheckingTest;17 18static inline char *call_asctime_r(struct tm *tm_data, int year, int month,19 int mday, int hour, int min, int sec,20 int wday, int yday, char *buffer) {21 LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data(22 tm_data, year, month, mday, hour, min, sec, wday, yday);23 return LIBC_NAMESPACE::asctime_r(tm_data, buffer);24}25 26// asctime and asctime_r share the same code and thus didn't repeat all the27// tests from asctime. Added couple of validation tests.28TEST_F(LlvmLibcAsctimeR, Nullptr) {29 char *result;30 result = LIBC_NAMESPACE::asctime_r(nullptr, nullptr);31 ASSERT_ERRNO_EQ(EINVAL);32 ASSERT_STREQ(nullptr, result);33 34 char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];35 result = LIBC_NAMESPACE::asctime_r(nullptr, buffer);36 ASSERT_ERRNO_EQ(EINVAL);37 ASSERT_STREQ(nullptr, result);38 39 struct tm tm_data;40 result = LIBC_NAMESPACE::asctime_r(&tm_data, nullptr);41 ASSERT_ERRNO_EQ(EINVAL);42 ASSERT_STREQ(nullptr, result);43}44 45TEST_F(LlvmLibcAsctimeR, ValidDate) {46 char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];47 struct tm tm_data;48 char *result;49 // 1970-01-01 00:00:00. Test with a valid buffer size.50 result = call_asctime_r(&tm_data,51 1970, // year52 1, // month53 1, // day54 0, // hr55 0, // min56 0, // sec57 4, // wday58 0, // yday59 buffer);60 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);61}62