Skip to content

Commit

Permalink
Fix nasa#429, check time conversions in coverage test
Browse files Browse the repository at this point in the history
Add test cases to coverage test to validate all basic
OS_time_t access operations and conversions.
  • Loading branch information
jphickey committed Jan 4, 2021
1 parent ccbaca0 commit 779d3e3
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,82 @@ void Test_OS_SetLocalTime(void)
UtAssert_True(actual == expected, "OS_SetLocalTime() (%ld) == OS_INVALID_POINTER", (long)actual);
}

void Test_OS_TimeAccessConversions(void)
{
/*
* Test cases for the various time access and conversion functions:
*
* int64 OS_TimeGetTotalSeconds(OS_time_t tm)
* int64 OS_TimeGetTotalMilliseconds(OS_time_t tm)
* int64 OS_TimeGetTotalMicroseconds(OS_time_t tm)
* int64 OS_TimeGetTotalNanoseconds(OS_time_t tm)
*
* uint32 OS_TimeGetSubsecondsPart(OS_time_t tm)
* uint32 OS_TimeGetMillisecondsPart(OS_time_t tm)
* uint32 OS_TimeGetMicrosecondsPart(OS_time_t tm)
* uint32 OS_TimeGetNanosecondsPart(OS_time_t tm)
*
* OS_time_t OS_TimeAssembleFromNanoseconds(int64 seconds, uint32 nanoseconds)
* OS_time_t OS_TimeAssembleFromSubseconds(int64 seconds, uint32 subseconds)
* OS_time_t OS_TimeAdd(OS_time_t time1, OS_time_t time2)
* OS_time_t OS_TimeSubtract(OS_time_t time1, OS_time_t time2)
*/
OS_time_t t1;
OS_time_t t2;
OS_time_t t3;
OS_time_t t4;

/* To base-2 32-bit fixed point: 0.234567890 s * 0x100000000 ~= 0x3c0ca428 */
t1 = OS_TimeAssembleFromNanoseconds(1,234567890);

/* From base-2 32-bit fixed point: 0x87654321 / 0x100000000 ~= 0.528888888 s */
t2 = OS_TimeAssembleFromSubseconds(2,0x87654321);

/* These functions only return the total (whole + fraction) in the requested units */
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t1), 1);
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t2), 2);

UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t1), 1234);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t2), 2528);

UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t1), 1234567);
UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t2), 2528888);

/* Note: Nanoseconds/Subseconds may not be exact due to limitations of OS_time_t resolution */
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t1), 1234567000);
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t2), 2528888000);

/* These functions only return the fractional part, not the whole part */
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t1), 0x3c0c953a);
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t2), 0x87653438);

UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t1), 234);
UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t2), 528);

UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t1), 234567);
UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t2), 528888);

UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t1), 234567000);
UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t2), 528888000);

/* Simple Add/Subtract */
t3 = OS_TimeAdd(t1, t2);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t3), 3763);
t4 = OS_TimeSubtract(t3, t2);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t4), 1234);

/* Add/Subtract that will require carry */
t1 = OS_TimeAssembleFromNanoseconds(3,777777777);
t2 = OS_TimeAssembleFromNanoseconds(4,888888888);

t3 = OS_TimeAdd(t1, t2);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t3), 8666);
t4 = OS_TimeSubtract(t3, t2);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t4), 3777);
}



/* Osapi_Test_Setup
*
* Purpose:
Expand All @@ -86,4 +162,5 @@ void UtTest_Setup(void)
{
ADD_TEST(OS_GetLocalTime);
ADD_TEST(OS_SetLocalTime);
ADD_TEST(OS_TimeAccessConversions);
}

0 comments on commit 779d3e3

Please sign in to comment.