Skip to content

Commit

Permalink
Fix nasa#1689, Add time arithmetic functional tests
Browse files Browse the repository at this point in the history
Add tests for
* CFE_TIME_Add
* CFE_TIME_Subtract
* CFE_TIME_Compare
  • Loading branch information
nmullane committed Jul 26, 2021
1 parent 2afdbc1 commit 5c4047c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/cfe_testcase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_cfe_app(cfe_testcase
src/fs_header_test.c
src/sb_pipe_mang_test.c
src/time_current_test.c
src/time_arithmetic_test.c
)

# register the dependency on cfe_assert
Expand Down
1 change: 1 addition & 0 deletions modules/cfe_testcase/src/cfe_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void CFE_TestMain(void)
FSHeaderTestSetup();
SBPipeMangSetup();
TimeCurrentTestSetup();
TimeArithmeticTestSetup();

/*
* Execute the tests
Expand Down
1 change: 1 addition & 0 deletions modules/cfe_testcase/src/cfe_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ void ESMemPoolTestSetup(void);
void FSHeaderTestSetup(void);
void SBPipeMangSetup(void);
void TimeCurrentTestSetup(void);
void TimeArithmeticTestSetup(void);

#endif /* CFE_TEST_H */
119 changes: 119 additions & 0 deletions modules/cfe_testcase/src/time_arithmetic_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*************************************************************************
**
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** File: time_arithmetic_test.c
**
** Purpose:
** Functional test of basic Time Arithmetic APIs
**
** Demonstration of how to register and use the UT assert functions.
**
*************************************************************************/

/*
* Includes
*/

#include "cfe_test.h"

void TestTimeAdd(void)
{
UtPrintf("Testing: CFE_TIME_Add");
CFE_TIME_SysTime_t time1 = { 1000, 0};
CFE_TIME_SysTime_t time2 = { 0, 1000};
CFE_TIME_SysTime_t timeAdded = CFE_TIME_Add(time1, time2);

UtAssert_UINT32_EQ(timeAdded.Seconds, 1000);
UtAssert_UINT32_EQ(timeAdded.Subseconds, 1000);

time1.Seconds = UINT32_MAX;
time1.Subseconds = UINT32_MAX;
time2.Seconds = 0;
time2.Subseconds = 1;
timeAdded = CFE_TIME_Add(time1, time2);
UtAssert_UINT32_EQ(timeAdded.Seconds, 0);
UtAssert_UINT32_EQ(timeAdded.Subseconds, 0);

time1.Seconds = UINT32_MAX;
time1.Subseconds = UINT32_MAX;
time2.Seconds = UINT32_MAX;
time2.Subseconds = UINT32_MAX;
timeAdded = CFE_TIME_Add(time1, time2);
UtAssert_UINT32_EQ(timeAdded.Seconds, UINT32_MAX);
UtAssert_UINT32_EQ(timeAdded.Subseconds, UINT32_MAX - 1);
}

void TestTimeSubtract(void)
{
UtPrintf("Testing: CFE_TIME_Subtract");
CFE_TIME_SysTime_t time1 = { 1000, 1000};
CFE_TIME_SysTime_t time2 = { 999, 999};
CFE_TIME_SysTime_t timeSubtracted = CFE_TIME_Subtract(time1, time2);

UtAssert_UINT32_EQ(timeSubtracted.Seconds, 1);
UtAssert_UINT32_EQ(timeSubtracted.Subseconds, 1);

time1.Seconds = 0;
time1.Subseconds = 0;
time2.Seconds = UINT32_MAX;
time2.Subseconds = UINT32_MAX;
timeSubtracted = CFE_TIME_Subtract(time1, time2);
UtAssert_UINT32_EQ(timeSubtracted.Seconds, 0);
UtAssert_UINT32_EQ(timeSubtracted.Subseconds, 1);

time1.Seconds = 0;
time1.Subseconds = 0;
time2.Seconds = 0;
time2.Subseconds = 1;
timeSubtracted = CFE_TIME_Subtract(time1, time2);
UtAssert_UINT32_EQ(timeSubtracted.Seconds, UINT32_MAX);
UtAssert_UINT32_EQ(timeSubtracted.Subseconds, UINT32_MAX);
}

void TestTimeCompare(void)
{
UtPrintf("Testing: CFE_TIME_Compare");
CFE_TIME_SysTime_t time1 = { 1000, 1000};
CFE_TIME_SysTime_t time2 = { 999, 999};
UtAssert_UINT32_EQ(CFE_TIME_Compare(time1, time2), CFE_TIME_A_GT_B);
UtAssert_UINT32_EQ(CFE_TIME_Compare(time2, time1), CFE_TIME_A_LT_B);

time1.Seconds = 500;
time1.Subseconds = 1;
time2.Seconds = 500;
time2.Subseconds = 1;
UtAssert_UINT32_EQ(CFE_TIME_Compare(time1, time2), CFE_TIME_EQUAL);
UtAssert_UINT32_EQ(CFE_TIME_Compare(time2, time1), CFE_TIME_EQUAL);

// time1 > time2 here due to the roll over handling of the comparison
time1.Seconds = 1;
time1.Subseconds = 1;
time2.Seconds = UINT32_MAX;
time2.Subseconds = UINT32_MAX;
UtAssert_UINT32_EQ(CFE_TIME_Compare(time1, time2), CFE_TIME_A_GT_B);
UtAssert_UINT32_EQ(CFE_TIME_Compare(time2, time1), CFE_TIME_A_LT_B);
}

void TimeArithmeticTestSetup(void)
{
UtTest_Add(TestTimeAdd, NULL, NULL, "Test Time Addition");
UtTest_Add(TestTimeSubtract, NULL, NULL, "Test Time Subtraction");
UtTest_Add(TestTimeCompare, NULL, NULL, "Test Time Comparison");
}

0 comments on commit 5c4047c

Please sign in to comment.