Skip to content

Commit

Permalink
refactor: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zimirza committed Dec 26, 2024
1 parent 40277ee commit 7069d89
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 86 deletions.
2 changes: 1 addition & 1 deletion libc/src/time/mktime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
total_days * TimeConstants::SECONDS_PER_DAY;

// Update the tm structure's year, month, day, etc. from seconds.
if (time_utils::update_from_seconds(seconds, tm_out, false) < 0)
if (time_utils::update_from_seconds(seconds, tm_out) < 0)
return time_utils::out_of_range();

return static_cast<time_t>(seconds);
Expand Down
137 changes: 70 additions & 67 deletions libc/src/time/time_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ char *get_env_var(const char *var_name) {
//
// Compute the number of months from the remaining days. Finally, adjust years
// to be 1900 and months to be from January.
int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) {
int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
// Days in month starting from March in the year 2000.
static const char daysInMonth[] = {31 /* Mar */, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 29};
Expand Down Expand Up @@ -171,66 +171,6 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) {
if (years > INT_MAX || years < INT_MIN)
return time_utils::out_of_range();

int offset;
int dst;

offset = 0;
dst = is_dst(tm);
if (local) {
char *tz_filename = get_env_var("TZ");
if (tz_filename[0] == '\0') {
char localtime[15] = "/etc/localtime";
size_t i = 0;
while (localtime[i] != '\0') {
tz_filename[i] = localtime[i];
i++;
}
} else {
char tmp[64];
char prefix[21] = "/usr/share/zoneinfo/";
size_t i = 0;
while (prefix[i] != '\0') {
tmp[i] = prefix[i];
i++;
}

i = 0;
while (tz_filename[i] != '\0') {
tmp[i + 20] = tz_filename[i];
i++;
}

tz_filename = tmp;
while (tz_filename[i] != '\0') {
if (tz_filename[i] == (char)0xFFFFFFAA) {
tz_filename[i] = '\0';
}
i++;
}
}

acquire_file(tz_filename);

size_t filesize;
filesize = static_cast<size_t>(lseek(fd, 0, SEEK_END));
if (filesize < 0) {
close(fd);
return 0;
}
lseek(fd, 0, 0);

timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize);
if (ptr_tzset == nullptr) {
return time_utils::out_of_range();
}

for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) {
if (dst == ptr_tzset->ttinfo[i].tt_isdst) {
offset = static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_utoff / 3600);
}
}
}

// All the data (years, month and remaining days) was calculated from
// March, 2000. Thus adjust the data to be from January, 1900.
tm->tm_year = static_cast<int>(years + 2000 - TimeConstants::TIME_YEAR_BASE);
Expand All @@ -247,16 +187,79 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) {
tm->tm_sec =
static_cast<int>(remainingSeconds % TimeConstants::SECONDS_PER_MIN);

if (local) {
tm->tm_hour += offset;
tm->tm_isdst = dst;
return 0;
}

struct tm *get_localtime(time_t *t_ptr) {
struct tm *tm;
int offset;
int dst;

tm = nullptr;
offset = 0;
dst = is_dst(tm);
update_from_seconds(*t_ptr, tm);
char *tz_filename = get_env_var("TZ");
if (tz_filename[0] == '\0') {
char localtime[15] = "/etc/localtime";
size_t i = 0;
while (localtime[i] != '\0') {
tz_filename[i] = localtime[i];
i++;
}
} else {
char tmp[64];
char prefix[21] = "/usr/share/zoneinfo/";
size_t i = 0;
while (prefix[i] != '\0') {
tmp[i] = prefix[i];
i++;
}

i = 0;
while (tz_filename[i] != '\0') {
tmp[i + 20] = tz_filename[i];
i++;
}

if (file_usage == 1) {
release_file(fd);
tz_filename = tmp;
while (tz_filename[i] != '\0') {
if (tz_filename[i] == (char)0xFFFFFFAA) {
tz_filename[i] = '\0';
}
i++;
}
}

return 0;
acquire_file(tz_filename);

size_t filesize;
filesize = static_cast<size_t>(lseek(fd, 0, SEEK_END));
if (filesize < 0) {
close(fd);
return nullptr;
}
lseek(fd, 0, 0);

timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize);
if (ptr_tzset == nullptr) {
return nullptr;
}

for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) {
if (dst == ptr_tzset->ttinfo[i].tt_isdst) {
offset = static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_utoff / 3600);
}
}

tm->tm_hour += offset;
tm->tm_isdst = dst;

if (file_usage == 1) {
release_file(fd);
}

return tm;
}

unsigned char is_dst(struct tm *tm) {
Expand Down
8 changes: 4 additions & 4 deletions libc/src/time/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct TimeConstants {

// Update the "tm" structure's year, month, etc. members from seconds.
// "total_seconds" is the number of seconds since January 1st, 1970.
extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local);
extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
extern unsigned char is_dst(struct tm *tm);
extern char *get_env_var(const char *var_name);
extern timezone::tzset *get_tzset(int fd, size_t filesize);
Expand Down Expand Up @@ -153,7 +153,7 @@ LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer,
LIBC_INLINE struct tm *gmtime_internal(const time_t *timer, struct tm *result) {
int64_t seconds = *timer;
// Update the tm structure's year, month, day, etc. from seconds.
if (update_from_seconds(seconds, result, false) < 0) {
if (update_from_seconds(seconds, result) < 0) {
out_of_range();
return nullptr;
}
Expand All @@ -166,7 +166,7 @@ LIBC_INLINE struct tm *localtime(const time_t *t_ptr) {
int64_t time = *t_ptr;

// Update the tm structure's year, month, day, etc. from seconds.
if (update_from_seconds(time, &result, true) < 0) {
if (update_from_seconds(time, &result) < 0) {
out_of_range();
return nullptr;
}
Expand All @@ -179,7 +179,7 @@ LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr,
int64_t t = *t_ptr;

// Update the tm structure's year, month, day, etc. from seconds.
if (update_from_seconds(t, input, true) < 0) {
if (update_from_seconds(t, input) < 0) {
out_of_range();
return nullptr;
}
Expand Down
12 changes: 6 additions & 6 deletions libc/test/src/time/asctime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static inline char *call_asctime(struct tm *tm_data, int year, int month,
TEST(LlvmLibcAsctime, Nullptr) {
char *result;
result = asctime(nullptr);
//ASSERT_ERRNO_EQ(EINVAL);
// ASSERT_ERRNO_EQ(EINVAL);
ASSERT_STREQ(nullptr, result);
}

Expand Down Expand Up @@ -51,7 +51,7 @@ TEST(LlvmLibcAsctime, InvalidWday) {
0, // sec
7, // wday
0); // yday
//ASSERT_ERRNO_EQ(EINVAL);
// ASSERT_ERRNO_EQ(EINVAL);
}

// Months are from January to December. Test passing invalid value in month.
Expand All @@ -68,7 +68,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) {
0, // sec
4, // wday
0); // yday
//ASSERT_ERRNO_EQ(EINVAL);
// ASSERT_ERRNO_EQ(EINVAL);

// Test with month = 13.
call_asctime(&tm_data,
Expand All @@ -80,7 +80,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) {
0, // sec
4, // wday
0); // yday
//ASSERT_ERRNO_EQ(EINVAL);
// ASSERT_ERRNO_EQ(EINVAL);
}

TEST(LlvmLibcAsctime, ValidWeekdays) {
Expand Down Expand Up @@ -208,6 +208,6 @@ TEST(LlvmLibcAsctime, Max64BitYear) {
50, // sec
2, // wday
50); // yday
//ASSERT_ERRNO_EQ(EOVERFLOW);
//ASSERT_STREQ(nullptr, result);
// ASSERT_ERRNO_EQ(EOVERFLOW);
// ASSERT_STREQ(nullptr, result);
}
9 changes: 5 additions & 4 deletions libc/test/src/time/gmtime_r_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/time/gmtime_r.h"
#include "src/time/localtime.h"
#include "src/time/time_utils.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmMatcher.h"
Expand All @@ -15,7 +16,7 @@ using LIBC_NAMESPACE::time_utils::TimeConstants;

// gmtime and gmtime_r share the same code and thus didn't repeat all the tests
// from gmtime. Added couple of validation tests.
/*TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
// Test for maximum value of a signed 32-bit integer.
// Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
time_t seconds = 0x7FFFFFFF;
Expand All @@ -33,9 +34,9 @@ using LIBC_NAMESPACE::time_utils::TimeConstants;
0}),
*tm_data_ptr);
EXPECT_TM_EQ(*tm_data_ptr, tm_data);
}*/
}

/*TEST(LlvmLibcGmTimeR, Max64BitYear) {
TEST(LlvmLibcGmTimeR, Max64BitYear) {
if (sizeof(time_t) == 4)
return;
// Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
Expand All @@ -54,4 +55,4 @@ using LIBC_NAMESPACE::time_utils::TimeConstants;
0}),
*tm_data_ptr);
EXPECT_TM_EQ(*tm_data_ptr, tm_data);
}*/
}
8 changes: 4 additions & 4 deletions libc/test/src/time/localtime_r_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32IntDst) {
ASSERT_EQ(121, input.tm_year);
ASSERT_EQ(6, input.tm_mon);
ASSERT_EQ(25, input.tm_mday);
//ASSERT_EQ(17, input.tm_hour);
// ASSERT_EQ(17, input.tm_hour);
ASSERT_EQ(4, input.tm_min);
ASSERT_EQ(25, input.tm_sec);
ASSERT_EQ(0, input.tm_wday);
ASSERT_EQ(205, input.tm_yday);
//ASSERT_EQ(1, input.tm_isdst);
// ASSERT_EQ(1, input.tm_isdst);

ASSERT_EQ(121, result->tm_year);
ASSERT_EQ(6, result->tm_mon);
ASSERT_EQ(25, result->tm_mday);
//ASSERT_EQ(17, result->tm_hour);
// ASSERT_EQ(17, result->tm_hour);
ASSERT_EQ(4, result->tm_min);
ASSERT_EQ(25, result->tm_sec);
ASSERT_EQ(0, result->tm_wday);
ASSERT_EQ(205, result->tm_yday);
//ASSERT_EQ(1, result->tm_isdst);
// ASSERT_EQ(1, result->tm_isdst);
}

0 comments on commit 7069d89

Please sign in to comment.