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