218 lines · cpp
1//===-- Unittests for asctime ---------------------------------------------===//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.h"11#include "test/UnitTest/ErrnoCheckingTest.h"12#include "test/UnitTest/Test.h"13#include "test/src/time/TmHelper.h"14 15using LlvmLibcAsctime = LIBC_NAMESPACE::testing::ErrnoCheckingTest;16 17static inline char *call_asctime(struct tm *tm_data, int year, int month,18 int mday, int hour, int min, int sec, int wday,19 int yday) {20 LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data(21 tm_data, year, month, mday, hour, min, sec, wday, yday);22 return LIBC_NAMESPACE::asctime(tm_data);23}24 25TEST_F(LlvmLibcAsctime, Nullptr) {26 char *result;27 result = LIBC_NAMESPACE::asctime(nullptr);28 ASSERT_ERRNO_EQ(EINVAL);29 ASSERT_STREQ(nullptr, result);30}31 32// Weekdays are in the range 0 to 6. Test passing invalid value in wday.33TEST_F(LlvmLibcAsctime, InvalidWday) {34 struct tm tm_data;35 36 // Test with wday = -1.37 call_asctime(&tm_data,38 1970, // year39 1, // month40 1, // day41 0, // hr42 0, // min43 0, // sec44 -1, // wday45 0); // yday46 ASSERT_ERRNO_EQ(EINVAL);47 48 // Test with wday = 7.49 call_asctime(&tm_data,50 1970, // year51 1, // month52 1, // day53 0, // hr54 0, // min55 0, // sec56 7, // wday57 0); // yday58 ASSERT_ERRNO_EQ(EINVAL);59}60 61// Months are from January to December. Test passing invalid value in month.62TEST_F(LlvmLibcAsctime, InvalidMonth) {63 struct tm tm_data;64 65 // Test with month = 0.66 call_asctime(&tm_data,67 1970, // year68 0, // month69 1, // day70 0, // hr71 0, // min72 0, // sec73 4, // wday74 0); // yday75 ASSERT_ERRNO_EQ(EINVAL);76 77 // Test with month = 13.78 call_asctime(&tm_data,79 1970, // year80 13, // month81 1, // day82 0, // hr83 0, // min84 0, // sec85 4, // wday86 0); // yday87 ASSERT_ERRNO_EQ(EINVAL);88}89 90TEST_F(LlvmLibcAsctime, ValidWeekdays) {91 struct tm tm_data;92 char *result;93 // 1970-01-01 00:00:00.94 result = call_asctime(&tm_data,95 1970, // year96 1, // month97 1, // day98 0, // hr99 0, // min100 0, // sec101 4, // wday102 0); // yday103 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);104 105 // 1970-01-03 00:00:00.106 result = call_asctime(&tm_data,107 1970, // year108 1, // month109 3, // day110 0, // hr111 0, // min112 0, // sec113 6, // wday114 0); // yday115 ASSERT_STREQ("Sat Jan 3 00:00:00 1970\n", result);116 117 // 1970-01-04 00:00:00.118 result = call_asctime(&tm_data,119 1970, // year120 1, // month121 4, // day122 0, // hr123 0, // min124 0, // sec125 0, // wday126 0); // yday127 ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n", result);128}129 130TEST_F(LlvmLibcAsctime, ValidMonths) {131 struct tm tm_data;132 char *result;133 // 1970-01-01 00:00:00.134 result = call_asctime(&tm_data,135 1970, // year136 1, // month137 1, // day138 0, // hr139 0, // min140 0, // sec141 4, // wday142 0); // yday143 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);144 145 // 1970-02-01 00:00:00.146 result = call_asctime(&tm_data,147 1970, // year148 2, // month149 1, // day150 0, // hr151 0, // min152 0, // sec153 0, // wday154 0); // yday155 ASSERT_STREQ("Sun Feb 1 00:00:00 1970\n", result);156 157 // 1970-12-31 23:59:59.158 result = call_asctime(&tm_data,159 1970, // year160 12, // month161 31, // day162 23, // hr163 59, // min164 59, // sec165 4, // wday166 0); // yday167 ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result);168}169 170TEST_F(LlvmLibcAsctime, EndOf32BitEpochYear) {171 struct tm tm_data;172 char *result;173 // Test for maximum value of a signed 32-bit integer.174 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.175 result = call_asctime(&tm_data,176 2038, // year177 1, // month178 19, // day179 3, // hr180 14, // min181 7, // sec182 2, // wday183 7); // yday184 ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);185}186 187TEST_F(LlvmLibcAsctime, Max64BitYear) {188 if (sizeof(time_t) == 4)189 return;190 // Mon Jan 1 12:50:50 2170 (200 years from 1970),191 struct tm tm_data;192 char *result;193 result = call_asctime(&tm_data,194 2170, // year195 1, // month196 1, // day197 12, // hr198 50, // min199 50, // sec200 1, // wday201 50); // yday202 ASSERT_STREQ("Mon Jan 1 12:50:50 2170\n", result);203 204 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.205 // This test would cause buffer overflow and thus asctime returns nullptr.206 result = call_asctime(&tm_data,207 2147483647, // year208 1, // month209 1, // day210 12, // hr211 50, // min212 50, // sec213 2, // wday214 50); // yday215 ASSERT_ERRNO_EQ(EOVERFLOW);216 ASSERT_STREQ(nullptr, result);217}218