|
| 1 | +//===-- Unittests for ctime_s ---------------------------------------------===// |
| 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 "hdr/errno_macros.h" |
| 10 | +#include "src/errno/libc_errno.h" |
| 11 | +#include "src/time/ctime_s.h" |
| 12 | +#include "src/time/time_utils.h" |
| 13 | +#include "test/UnitTest/Test.h" |
| 14 | +#include "test/src/time/TmHelper.h" |
| 15 | + |
| 16 | +using LIBC_NAMESPACE::time_utils::TimeConstants; |
| 17 | + |
| 18 | +TEST(LlvmLibcCtimeS, Nullptr) { |
| 19 | + int result; |
| 20 | + result = LIBC_NAMESPACE::ctime_s(nullptr, 0, nullptr); |
| 21 | + ASSERT_EQ(EINVAL, result); |
| 22 | + |
| 23 | + char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; |
| 24 | + result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), nullptr); |
| 25 | + ASSERT_EQ(EINVAL, result); |
| 26 | + |
| 27 | + time_t t; |
| 28 | + result = LIBC_NAMESPACE::ctime_s(nullptr, 0, &t); |
| 29 | + ASSERT_EQ(EINVAL, result); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(LlvmLibcCtimeS, ValidUnixTimestamp0) { |
| 33 | + char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; |
| 34 | + time_t t; |
| 35 | + int result; |
| 36 | + // 1970-01-01 00:00:00. Test with a valid buffer size. |
| 37 | + t = 0; |
| 38 | + result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t); |
| 39 | + ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buffer); |
| 40 | + ASSERT_EQ(0, result); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(LlvmLibcCtimeS, ValidUnixTimestamp32Int) { |
| 44 | + char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; |
| 45 | + time_t t; |
| 46 | + int result; |
| 47 | + // 2038-01-19 03:14:07. Test with a valid buffer size. |
| 48 | + t = 2147483647; |
| 49 | + result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t); |
| 50 | + ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", buffer); |
| 51 | + ASSERT_EQ(0, result); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(LlvmLibcCtimeS, InvalidArgument) { |
| 55 | + char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; |
| 56 | + time_t t; |
| 57 | + int result; |
| 58 | + t = 2147483648; |
| 59 | + result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t); |
| 60 | + ASSERT_EQ(EINVAL, result); |
| 61 | +} |
0 commit comments