Skip to content

Commit

Permalink
Improve UpdateRingBuffer.test.cpp
Browse files Browse the repository at this point in the history
Make it use the real UpdateProgramStatusHistory method by linking
against Sts1CobcSw_Edu.
  • Loading branch information
jeromehue authored and PatrickKa committed Nov 4, 2023
1 parent 3f23bd1 commit 770b44d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
10 changes: 9 additions & 1 deletion Tests/GoldenTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ add_golden_test(

add_golden_test(TESTFILE "HelloDummy.test.cpp" LIB rodos::rodos Sts1CobcSw_Dummy)

add_golden_test(TESTFILE "UpdateRingBuffer.test.cpp" LIB rodos::rodos Sts1CobcSw_Dummy)
add_golden_test(
TESTFILE
"UpdateRingBuffer.test.cpp"
LIB
rodos::rodos
Sts1CobcSw_Edu
Sts1CobcSw_Utility
Sts1CobcSw_Dummy
)

add_golden_test(
TESTFILE
Expand Down
23 changes: 12 additions & 11 deletions Tests/GoldenTests/ExpectedOutputs/UpdateRingBuffer.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Occupied count : 5
Vals[0] = .id(1), .status(notStarted)
Vals[1] = .id(2), .status(notStarted)
Vals[2] = .id(3), .status(notStarted)
Vals[3] = .id(4), .status(notStarted)
Vals[4] = .id(5), .status(notStarted)
Vals[0] = .id(1), .status(running)
Vals[1] = .id(2), .status(notStarted)
Vals[2] = .id(3), .status(running)
Vals[3] = .id(4), .status(notStarted)
Vals[4] = .id(5), .status(notStarted)
Vals[0] = .id(1), .status(programExecutionFailed)
Vals[1] = .id(2), .status(programRunning)
Vals[2] = .id(3), .status(programRunning)
Vals[3] = .id(4), .status(programRunning)
readCnt unchanged : 1
writeCnt unchanged : 0
OccupiedCnt unchanged : 0
Vals[0] = .id(1), .status(programExecutionFailed)
Vals[1] = .id(2), .status(programExecutionSucceeded)
Vals[2] = .id(3), .status(programRunning)
Vals[3] = .id(4), .status(programExecutionFailed)
Vals[4] = .id(5), .status(programExecutionSucceeded)
hw_resetAndReboot() -> exit
117 changes: 57 additions & 60 deletions Tests/GoldenTests/UpdateRingBuffer.test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Sts1CobcSw/Dummy.hpp>
#include <Sts1CobcSw/Edu/ProgramStatusHistory.hpp>

#include <ringbuffer.h>

Expand All @@ -14,93 +15,89 @@ std::uint32_t printfMask = 0;
namespace sts1cobcsw
{

enum Status
{
notStarted,
running,
done
};

struct Entry
{
std::uint32_t id;
Status status;
};

auto StatusToString(Status status) -> std::string
auto StatusToString(edu::ProgramStatus status) -> std::string
{
switch(status)
{
case notStarted:
return {"notStarted"};
case running:
return {"running"};
case done:
return {"done"};
case edu::ProgramStatus::programRunning:
return {"programRunning"};
case edu::ProgramStatus::programExecutionFailed:
return {"programExecutionFailed"};
case edu::ProgramStatus::programExecutionSucceeded:
return {"programExecutionSucceeded"};
default:
break;
}
}

// Defined a global variable for testing purpose
RODOS::RingBuffer<Entry, 10> buffer;

auto UpdateRingBuffer(Status status, std::uint32_t id, Status newStatus) -> void
{
auto isLookedForEntry = [&](Entry & entry)
{ return (entry.id == id and entry.status == status); };

auto count = 0;
for(int i = 0; i < buffer.getLen(); ++i)
{
if(buffer.vals[i].id == id and buffer.vals[i].status == status)
{
count++;
}
}

assert(count == 1 and "More than one program and status history match founds");

for(int i = 0; i < buffer.getLen(); ++i)
{
if(buffer.vals[i].id == id and buffer.vals[i].status == status)
{
buffer.vals[i].status = newStatus;
}
}
}


// Helper function for edu::programStatusHistory
void PrintBuffer()
{
for(int i = 0; i < buffer.occupiedCnt; ++i)
for(int i = 0; i < edu::programStatusHistory.occupiedCnt; ++i)
{
RODOS::PRINTF("Vals[%d] = .id(%d), .status(%s)\n",
i,
buffer.vals[i].id,
StatusToString(buffer.vals[i].status).c_str());
edu::programStatusHistory.vals[i].programId.get(),
StatusToString(edu::programStatusHistory.vals[i].status).c_str());
}
}


class HelloDummy : public RODOS::StaticThread<>
{
void run() override
{
printfMask = 1;
buffer.put(Entry{.id = 1, .status = Status::notStarted});
buffer.put(Entry{.id = 2, .status = Status::notStarted});
buffer.put(Entry{.id = 3, .status = Status::notStarted});
buffer.put(Entry{.id = 4, .status = Status::notStarted});
buffer.put(Entry{.id = 5, .status = Status::notStarted});
edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = static_cast<std::uint16_t>(1),
.queueId = static_cast<std::uint16_t>(1),
.status = edu::ProgramStatus::programExecutionFailed});

edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = static_cast<std::uint16_t>(2),
.queueId = static_cast<std::uint16_t>(1),
.status = edu::ProgramStatus::programRunning});

edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = static_cast<std::uint16_t>(3),
.queueId = static_cast<std::uint16_t>(1),
.status = edu::ProgramStatus::programRunning});

edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = static_cast<std::uint16_t>(4),
.queueId = static_cast<std::uint16_t>(1),
.status = edu::ProgramStatus::programRunning});


auto readCnt = edu::programStatusHistory.readCnt;
auto writeCnt = edu::programStatusHistory.writeCnt;
auto occupiedCnt = edu::programStatusHistory.occupiedCnt;

RODOS::PRINTF("Occupied count : %d\n", buffer.occupiedCnt);

// Print RingBuffer
PrintBuffer();

// Start programs 1 , 3 and 5
UpdateRingBuffer(Status::notStarted, 1, Status::running);
UpdateRingBuffer(Status::notStarted, 3, Status::running);

edu::UpdateProgramStatusHistory(2, 1, edu::ProgramStatus::programExecutionSucceeded);
edu::UpdateProgramStatusHistory(4, 1, edu::ProgramStatus::programExecutionFailed);

edu::programStatusHistory.put(
edu::ProgramStatusHistoryEntry{.programId = static_cast<std::uint16_t>(5),
.queueId = static_cast<std::uint16_t>(1),
.status = edu::ProgramStatus::programRunning});

edu::UpdateProgramStatusHistory(5, 1, edu::ProgramStatus::programExecutionSucceeded);

// 1, we did not read anything
RODOS::PRINTF("readCnt unchanged : %d\n",
static_cast<int>(edu::programStatusHistory.readCnt == readCnt));
// 0, bc we did write
RODOS::PRINTF("writeCnt unchanged : %d\n",
static_cast<int>(edu::programStatusHistory.readCnt == writeCnt));
// 0
RODOS::PRINTF("OccupiedCnt unchanged : %d\n",
static_cast<int>(edu::programStatusHistory.occupiedCnt == occupiedCnt));

// Print RingBuffer
PrintBuffer();
Expand Down

0 comments on commit 770b44d

Please sign in to comment.