Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fifo Remaining Space function + Test File #595

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Apps/Inc/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct FIFO_STRUCT_NAME {
#define PEEK CONCAT(FIFO_NAME, _peek)
#define POPBACK CONCAT(FIFO_NAME, _popback)
#define RENEW CONCAT(FIFO_NAME, _renew)
#define SPACE CONCAT(FIFO_NAME, _space)

/**
* @brief Initialize a new fifo
Expand Down Expand Up @@ -221,6 +222,24 @@ POPBACK (FIFO_TYPE_NAME *fifo, FIFO_TYPE *elem) {
return false;
}

/**
* @brief Returns how much room is left in the Fifo.
*
* If the type of the fifo is myfifo_t, then this function
* will be called myfifo_space().
*
* @param fifo A pointer to the fifo
* @return uin32_t of empty spaces
*/
static int __attribute__((unused))
SPACE (FIFO_TYPE_NAME *fifo) {
if(!IS_EMPTY(fifo)) {
return FIFO_SIZE - (fifo->put % FIFO_SIZE);
}

return FIFO_SIZE;
}

#undef IS_EMPTY
#undef IS_FULL
#undef FIFO_SIZE
Expand All @@ -232,6 +251,7 @@ POPBACK (FIFO_TYPE_NAME *fifo, FIFO_TYPE *elem) {
#undef PUT
#undef NEW
#undef PEEK
#undef SPACE
#undef POPBACK
#undef CONCAT
#undef _CONCAT
90 changes: 90 additions & 0 deletions Tests/Test_Fifo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */

#include "common.h"
#include "config.h"
#include "Tasks.h"
#include "stdint.h"
#include "stm32f4xx.h"
#include "BSP_UART.h"
#include "BSP_PLL.h"
#include "BSP_Lights.h"

#define FIFO_TYPE int
#define FIFO_SIZE (128)
#define FIFO_NAME Test_Fifo
#include "fifo.h"
static Test_Fifo_t testFifo;

OS_TCB Task1_TCB;
CPU_STK Task1_Stk[DEFAULT_STACK_SIZE];

void test(){
//Check empty Fifo
int out = Test_Fifo_is_empty(&testFifo);
printf("Test 1 expected output: 1, Received: %u", out);

out = Test_Fifo_is_full(&testFifo);
printf("Test 2 expected output: 0, Received: %u", out);

//Test partially filled Fifo
Test_Fifo_put(&testFifo, 1);
Test_Fifo_put(&testFifo, 2);
Test_Fifo_put(&testFifo, 3);

out = Test_Fifo_is_empty(&testFifo);
printf("Test 3 expected output: 0, Received: %u", out);

out = Test_Fifo_is_full(&testFifo);
printf("Test 4 expected output: 0, Received: %u", out);

Test_Fifo_get(&testFifo, &out);
printf("Test 5 expected output: 3, Received: %u", out);

Test_Fifo_peek(&testFifo, &out);
printf("Test 6 expected output: 2, Received: %u", out);

Test_Fifo_popback(&testFifo, &out);
printf("Test 7 expected output: 2, Received: %u", out);

out = Test_Fifo_space(&testFifo);
printf("Test 8 expected output: 127, Received: %u", out);

//Fill the Fifo
for(uint8_t i = 0; i < 127; i++){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe represent 127 as a variable for readability?

Test_Fifo_put(&testFifo, i);
}

out = Test_Fifo_is_full(&testFifo);
printf("Test 9 expected output: 1, Received: %u", out);

out = Test_Fifo_space(&testFifo);
printf("Test 10 expected output: 0, Received: %u", out);
}

// Task for this test
void Task1(void *p_arg){
OS_CPU_SysTickInit(SystemCoreClock / (CPU_INT32U) OSCfg_TickRate_Hz);
BSP_UART_Init(NULL, NULL, UART_USB);
Test_Fifo_new(&testFifo);
test();
}

int main(void){
OS_ERR err;
BSP_PLL_Init();
BSP_Lights_Init();

OSInit(&err);
assertOSError(err);

RTOS_BPS_TaskCreate(&Task1_TCB,
"Task 1",
Task1,
(void *)0,
1,
Task1_Stk,
256);
assertOSError(err);

OSStart(&err);
}