Skip to content

Commit

Permalink
feat: added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbertoldo committed Dec 4, 2024
1 parent 4bd87b3 commit 130b4f3
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 96 deletions.
5 changes: 5 additions & 0 deletions src/common/pal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ if(NOT SOURCE_FILES)
endif()

add_library(pal STATIC ${SOURCE_FILES})

if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests/${OS_NAME})
endif()
3 changes: 3 additions & 0 deletions src/common/pal/include/linux/priv_pal_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
#endif

#include <stdio.h>

#define popen popen
#define pclose pclose
3 changes: 3 additions & 0 deletions src/common/pal/include/macos/priv_pal_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
#endif

#include <stdio.h>

#define popen popen
#define pclose pclose
4 changes: 2 additions & 2 deletions src/common/pal/include/windows/priv_pal_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extern "C" {

#include <stdio.h>

FILE* popen(const char* command, const char* mode);
int pclose(FILE* stream);
#define popen _popen
#define pclose _pclose

#ifdef __cplusplus
}
Expand Down
93 changes: 0 additions & 93 deletions src/common/pal/src/windows/pal_process.c

This file was deleted.

10 changes: 9 additions & 1 deletion src/common/pal/src/windows/pal_time.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include <time.h>
#include <errno.h>

struct tm* localtime_r(const time_t* timep, struct tm* result)
struct tm* localtime_r(const time_t* timep, struct tm* result)
{
if (timep == NULL || result == NULL) {
return NULL;
}

errno_t err = localtime_s(result, timep);
if (err != 0) {
return NULL;
Expand All @@ -12,6 +16,10 @@ struct tm* localtime_r(const time_t* timep, struct tm* result)

struct tm* gmtime_r(const time_t* timep, struct tm* result)
{
if (timep == NULL || result == NULL) {
return NULL;
}

errno_t err = gmtime_s(result, timep);
if(err != 0) {
return NULL;
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/common/pal/tests/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
find_package(GTest CONFIG REQUIRED)

add_executable(PalTime_test pal_time_test.cpp)
configure_target(PalTime_test)
target_include_directories(PalTime_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
target_link_libraries(PalTime_test PRIVATE pal GTest::gtest GTest::gtest_main)
add_test(NAME PalTimeTest COMMAND PalTime_test)
58 changes: 58 additions & 0 deletions src/common/pal/tests/windows/pal_time_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <pal.h>
#include <time.h>

class PalTimeTest : public ::testing::Test {
protected:
struct tm result {};
time_t now = time(nullptr);
};

TEST_F(PalTimeTest, LocaltimeRSuccess) {
struct tm expectedResult;
errno_t err = localtime_s(&expectedResult, &now);
ASSERT_EQ(err, 0);

struct tm* ret = localtime_r(&now, &result);
ASSERT_NE(ret, nullptr);
EXPECT_EQ(ret, &result);

EXPECT_EQ(result.tm_year, expectedResult.tm_year);
EXPECT_EQ(result.tm_mon, expectedResult.tm_mon);
EXPECT_EQ(result.tm_mday, expectedResult.tm_mday);
}

TEST_F(PalTimeTest, LocaltimeRNullInput) {
struct tm* ret = localtime_r(nullptr, &result);
EXPECT_EQ(ret, nullptr);
}

TEST_F(PalTimeTest, LocaltimeRNullOutput) {
struct tm* ret = localtime_r(&now, nullptr);
EXPECT_EQ(ret, nullptr);
}

TEST_F(PalTimeTest, GmtimeRSuccess) {
struct tm expectedResult;
errno_t err = gmtime_s(&expectedResult, &now);
ASSERT_EQ(err, 0);

struct tm* ret = gmtime_r(&now, &result);
ASSERT_NE(ret, nullptr);
EXPECT_EQ(ret, &result);

EXPECT_EQ(result.tm_year, expectedResult.tm_year);
EXPECT_EQ(result.tm_mon, expectedResult.tm_mon);
EXPECT_EQ(result.tm_mday, expectedResult.tm_mday);
}

TEST_F(PalTimeTest, GmtimeRNullInput) {
struct tm* ret = gmtime_r(nullptr, &result);
EXPECT_EQ(ret, nullptr);
}

TEST_F(PalTimeTest, GmtimeRNullOutput) {
struct tm* ret = gmtime_r(&now, nullptr);
EXPECT_EQ(ret, nullptr);
}

0 comments on commit 130b4f3

Please sign in to comment.