Use ErrnoCheckingTest harness to clear out / validate errno value before/after the test respectively. Clean up explicit libc_errno.h inclusions which is unnecessary, since no test modifies errno directly.
62 lines
2.3 KiB
C++
62 lines
2.3 KiB
C++
//===-- Unittests for asctime_r -------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "hdr/errno_macros.h"
|
|
#include "src/time/asctime_r.h"
|
|
#include "src/time/time_constants.h"
|
|
#include "test/UnitTest/ErrnoCheckingTest.h"
|
|
#include "test/UnitTest/Test.h"
|
|
#include "test/src/time/TmHelper.h"
|
|
|
|
using LlvmLibcAsctimeR = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
|
|
|
|
static inline char *call_asctime_r(struct tm *tm_data, int year, int month,
|
|
int mday, int hour, int min, int sec,
|
|
int wday, int yday, char *buffer) {
|
|
LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data(
|
|
tm_data, year, month, mday, hour, min, sec, wday, yday);
|
|
return LIBC_NAMESPACE::asctime_r(tm_data, buffer);
|
|
}
|
|
|
|
// asctime and asctime_r share the same code and thus didn't repeat all the
|
|
// tests from asctime. Added couple of validation tests.
|
|
TEST_F(LlvmLibcAsctimeR, Nullptr) {
|
|
char *result;
|
|
result = LIBC_NAMESPACE::asctime_r(nullptr, nullptr);
|
|
ASSERT_ERRNO_EQ(EINVAL);
|
|
ASSERT_STREQ(nullptr, result);
|
|
|
|
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
|
|
result = LIBC_NAMESPACE::asctime_r(nullptr, buffer);
|
|
ASSERT_ERRNO_EQ(EINVAL);
|
|
ASSERT_STREQ(nullptr, result);
|
|
|
|
struct tm tm_data;
|
|
result = LIBC_NAMESPACE::asctime_r(&tm_data, nullptr);
|
|
ASSERT_ERRNO_EQ(EINVAL);
|
|
ASSERT_STREQ(nullptr, result);
|
|
}
|
|
|
|
TEST_F(LlvmLibcAsctimeR, ValidDate) {
|
|
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
|
|
struct tm tm_data;
|
|
char *result;
|
|
// 1970-01-01 00:00:00. Test with a valid buffer size.
|
|
result = call_asctime_r(&tm_data,
|
|
1970, // year
|
|
1, // month
|
|
1, // day
|
|
0, // hr
|
|
0, // min
|
|
0, // sec
|
|
4, // wday
|
|
0, // yday
|
|
buffer);
|
|
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
|
|
}
|