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_constants.h" 12 #include "test/UnitTest/Test.h" 13 #include "test/src/time/TmHelper.h" 14 15 static inline char *call_asctime_r(struct tm *tm_data, int year, int month, 16 int mday, int hour, int min, int sec, 17 int wday, int yday, char *buffer) { 18 LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data( 19 tm_data, year, month, mday, hour, min, sec, wday, yday); 20 return LIBC_NAMESPACE::asctime_r(tm_data, buffer); 21 } 22 23 // asctime and asctime_r share the same code and thus didn't repeat all the 24 // tests from asctime. Added couple of validation tests. 25 TEST(LlvmLibcAsctimeR, Nullptr) { 26 char *result; 27 result = LIBC_NAMESPACE::asctime_r(nullptr, nullptr); 28 ASSERT_ERRNO_EQ(EINVAL); 29 ASSERT_STREQ(nullptr, result); 30 31 char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; 32 result = LIBC_NAMESPACE::asctime_r(nullptr, buffer); 33 ASSERT_ERRNO_EQ(EINVAL); 34 ASSERT_STREQ(nullptr, result); 35 36 struct tm tm_data; 37 result = LIBC_NAMESPACE::asctime_r(&tm_data, nullptr); 38 ASSERT_ERRNO_EQ(EINVAL); 39 ASSERT_STREQ(nullptr, result); 40 } 41 42 TEST(LlvmLibcAsctimeR, ValidDate) { 43 char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; 44 struct tm tm_data; 45 char *result; 46 // 1970-01-01 00:00:00. Test with a valid buffer size. 47 result = call_asctime_r(&tm_data, 48 1970, // year 49 1, // month 50 1, // day 51 0, // hr 52 0, // min 53 0, // sec 54 4, // wday 55 0, // yday 56 buffer); 57 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result); 58 } 59